Skip to main content

Pyth

TL;DR

  • First call pyth.updatePriceFeeds
  • Next call pyth.getPrice
  • Find price feed addresses on Devnet and Mainnet

Introduction

Pyth is an open-source real-time on-chain market data feed. To use Pyth prices, you must call the function updatePriceFeeds , which submits the price update data to the Pyth contract in your target chain.

Your contract interacts with the Pyth contract to request a data refresh. This interaction also provides an opportunity to validate that the updates you received are authentic.

Next, your contract should query the Pyth Contract that holds this updated data for the token prices you require. The price feed IDs are available for Neon EVM:

How to integrate with the Pyth contract

Because Pyth is an on-demand oracle, you must first retrieve the price feeds before calling the token's price based on the price feed ID for the token symbol/s you are interested in.

Let's examine an extract of a swap function (for the full contract, see Pyth's example):

SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

function swap(
bool isBuy,
uint size,
bytes[] calldata pythUpdateData
) external payable {
// Ensure the contract on Neon EVM has the current data.
uint updateFee = pyth.getUpdateFee(pythUpdateData);
pyth.updatePriceFeeds{value: updateFee}(pythUpdateData);
// Retrieve the token price for the tokens you need.
PythStructs.Price memory currentBasePrice = pyth.getPrice(
baseTokenPriceId
);
PythStructs.Price memory currentQuotePrice = pyth.getPrice(
quoteTokenPriceId
);

Line 7 refreshes the data held by the Pyth contract, and line 13-15 retrieves the first of the token prices required for the swap logic.

Considerations for using Pyth on Neon EVM

Pyth maintains the contracts on Neon EVM, with the repo maintained by Pyth on GitHub at pyth-network/pyth-neon.

It's strongly recommended that you follow the consumer best practices when consuming Pyth data.

note

While Pyth provides a sane default for the staleness threshold and a fallback process if feed data is stale, users may configure this functionality.

What next?

To learn more about Pyth's architecture, see their video: How to use Pyth's on-demand model.

Was this page helpful?