description |
---|
Use historical Pyth price data in your application |
Pyth benchmarks allows users to query a historical archive of prices from Pythnet Price Feeds. For example, applications can retrieve the price of BTC/USD as of last Friday at 8:00 AM UTC. This price can be used for contract settlement or any other application that requires historical price data. Benchmarks data is signed and verifiable on-chain with the same trust assumptions as Pythnet Price Feeds.
Users can access benchmark prices in several different ways, depending on the use case:
The benchmarks page of the pyth.network website allows users to search the archive of benchmark prices. It also provides default options for common settlement dates and times. Users can select a time and see all of the Pyth prices that were active at that time. Technically, each displayed price corresponds to the Pyth price update published just before the selected time; this ensures that the displayed price is the one a user would have seen if they observed the Pyth price at that time.
On-chain contracts can consume benchmark prices using the same on-demand model as Pythnet Price Feeds. Integrators can follow these three steps:
- Use the price service endpoint
/api/get_vaa
to retrieve a signed price update for the desired price feed and time. This endpoint will return a signed price update, specifically the price update immediately after the queried time. This step can be done in either a web frontend or a backend process, depending on who is expected to initiate the transaction. - Pass this price update to your on-chain contract as part of the transaction that needs the benchmark data.
- In your on-chain contract, pass the price update to the
parsePriceFeedUpdates
function on the Pyth contract. This function will check the validity of the update and return the price of the requested asset (if valid). The price update will only be valid if the signatures are correct and the update falls within a user-configured time interval; integrators should set this time interval such that it matches their desired time.
Note that this process is similar to the standard process for consuming on-demand updates.
The main differences are that users call the get_vaa
endpoint instead of latest_vaas
, and the on-chain contract calls parsePriceFeedUpdates
instead of updatePriceFeeds
.
The pyth-crosschain repo has example code for an integration along the lines above for EVM chains.