Developer Hub

Consume Data on EVM Chains

Consume and verify Pyth Pro price updates in EVM smart contracts

This guide is intended to serve users who wants to consume prices from the Pyth Pro on EVM chains.

Integrating with Pyth Pro in smart contracts as a consumer is a three-step process:

  1. Use Pyth Lazer SDK in EVM smart contracts to parse the price updates.
  2. Subscribe to Pyth Pro websocket to receive price updates on backend or frontend.
  3. Include the price updates into the smart contract transactions.

Use Pyth Lazer SDK in smart contracts

Pyth Pro provides a Solidity SDK, which allows consumers to parse the price updates.

forge install pyth-network/pyth-crosschain

Add the following to requirements.txt file:

pyth-lazer-sdk/=lib/pyth-network/pyth-crosschain/lazer/contracts/evm

Once the SDK is installed, one can import the sdk into smart contracts:

import { PythLazer } from "pyth-lazer-sdk/PythLazer.sol";
import { PythLazerLib } from "pyth-lazer-sdk/PythLazerLib.sol";

After importing the SDK, initialize the PythLazer contract and set up state varables to store prices and timestamps:

contract ExampleConsumer {
  // Example state.
  PythLazer pythLazer;
  uint64 public price;
  uint64 public timestamp;

  //...

  constructor(address pythLazerAddress) {
    pythLazer = PythLazer(pythLazerAddress);
  }
}

Add an argument of type bytes calldata to the method which will receive the price update:

function updatePrice(bytes calldata priceUpdate) public payable {
  uint256 verification_fee = pythLazer.verification_fee();
  (bytes calldata payload, ) = verifyUpdate{ value: verification_fee }(update);
  //...
}

The verifyUpdate function will verify the price update and return the payload and the verification fee. This call takes a fee which can be queried from verification_fee() function and passed to the verifyUpdate call. This fee is used to cover the cost of verifying the price update.

This SDK provides parsePayloadHeader method to retrieve the values from the payload header.

(uint64 _timestamp, Channel channel, uint8 feedsLen, uint16 pos) = parsePayloadHeader(payload);

This method returns:

  • _timestamp: The timestamp of the price update.
  • channel: The channel of the price update.
  • feedsLen: The number of feeds in the price update.
  • pos: The cursor position of the payload.

One can iterate over all the feeds and properties present within the price update, modifying the state variables as necessary.

Here is an example of how to iterate over the feeds and properties:

for (uint8 i = 0; i < feedsLen; i++) {
    uint32 feedId;
    uint8 num_properties;
    (feedId, num_properties, pos) = parseFeedHeader(payload, pos);
    for (uint8 j = 0; j < num_properties; j++) {
        PriceFeedProperty property;
        (property, pos) = parseFeedProperty(payload, pos);
        if (property == PriceFeedProperty.Price) {
            uint64 _price;
            (_price, pos) = parseFeedValueUint64(payload, pos);
            if (feedId == 2 && _timestamp > timestamp) {
                price = _price;
                timestamp = _timestamp;
            }
        } else if (property == PriceFeedProperty.BestBidPrice) {
            uint64 _price;
            (_price, pos) = parseFeedValueUint64(payload, pos);
        } else if (property == PriceFeedProperty.BestAskPrice) {
            uint64 _price;
            (_price, pos) = parseFeedValueUint64(payload, pos);
        } else {
            revert("unknown property");
        }
    }
}

Make sure to pass the pos variable to every parsing call and assign the returned pos value to the same variable. Failure to do so will cause incorrect parsing results.

When calling these parse functions, you must not skip price feeds or properties. Every parsing function will modify your pos variable, so skipping a call of parseFeedHeader, parseFeedProperty, or parseFeedValueUint64 will lead to incorrect parsing results. Keep in mind that you can customize the set of price feeds and properties when requesting price updates via WebSocket. This will be explained in the next step.

Subscribe to Pyth Pro to receive Price Updates

Pyth Pro provides a websocket endpoint to receive price updates. Moreover, Pyth Pro also provides a Typescript SDK to subscribe to the websocket endpoint.

Consult How to subscribe to prices for a complete step-by-step guide.

Include the price updates into smart contract transactions

Now that you have the price updates, and your smart contract is able to parse the price updates, you can include the price updates into the smart contract transactions by passing the price updates received from the previous step as an argument to the updatePrice method of your smart contract.

Additional Resources

You may find these additional resources helpful for consuming prices from Pyth Pro into your EVM smart contracts.

Price Feed IDs

Pyth Pro supports a wide range of price feeds. Consult the Price Feed IDs page for a complete list of supported price feeds.

Examples

Pyth-lazer-example-evm is a simple example contract that parses and consumes price updates from Pyth Pro.

pyth-lazer-example-js is a simple example for subscribing to the Pyth Pro websocket.