Integrate Pyth Lazer as a Consumer on EVM chains
This guide is intended to serve users who wants to consume prices from the Pyth Lazer on EVM chains.
Integrating with Pyth Lazer in smart contracts as a consumer is a three-step process:
- Use Pyth Lazer SDK in EVM smart contracts to parse the price updates.
- Subscribe to Pyth Lazer websocket to receive price updates on backend or frontend.
- Include the price updates into the smart contract transactions.
Use Pyth Lazer SDK in smart contracts
Pyth Lazer provides a Solidity SDK (opens in a new tab), 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
(opens in a new tab) 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 Pyth Lazer price udpate:
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()
(opens in a new tab) function and passed to the verifyUpdate
call. This fee is used to cover the cost of verifying the price update.
This SDK provides parsePayloadHeader
(opens in a new tab) 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 Lazer to receive Price Updates
Pyth Lazer provides a websocket endpoint to receive price updates. Moreover, Pyth Lazer also provides a Typescript SDK (opens in a new tab) to subscribe to the websocket endpoint.
Consult How to fetch price updates from Pyth Lazer 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 integrating Pyth Lazer into your EVM smart contracts.
Price Feed IDs
Pyth Lazer 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 (opens in a new tab) is a simple example contract that parses and consumes price updates from Pyth Lazer.
pyth-lazer-example-js (opens in a new tab) is a simple example for subscribing to the Pyth Lazer websocket.