import { Callout, Tabs, Steps } from "nextra/components";
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.
Pyth Lazer provides a Solidity SDK, which allows consumers to parse the price updates.
forge install pyth-network/pyth-crosschain
Add the following to requirements.txt{:js}
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{:solidity}
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 }(priceUpdate);
//...
}
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(){:solidity}
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");
}
}
}
Pyth Lazer provides a websocket endpoint to receive price updates. Moreover, Pyth Lazer also provides a Typescript SDK to subscribe to the websocket endpoint.
Consult How to fetch price updates from Pyth Lazer for a complete step-by-step guide.
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.
You may find these additional resources helpful for integrating Pyth Lazer into your EVM smart contracts.
Pyth Lazer supports a wide range of price feeds. Consult the Price Feed IDs page for a complete list of supported price feeds.
Pyth-lazer-example-evm is a simple example contract that parses and consumes price updates from Pyth Lazer.
pyth-lazer-example-js is a simple example for subscribing to the Pyth Lazer websocket.