Skip to main content

How to use real-time data in TON contracts

Pyth is a pull oracle. Pyth price feeds on TON are managed through the main TON Pyth smart contract, enabling seamless interaction with on-chain data. In TON, these interactions are facilitated by specific functions within the Pyth TON contract. This contract acts as an interface to Pyth price feeds, handling the retrieval and updating of price data.

Install the Pyth SDK

Install the Pyth TON SDK and other necessary dependencies using npm or yarn:

Write code to interact with oracle

Off-chain data fetch and update

The following code snippet demonstrates how to fetch price updates, interact with the Pyth contract on TON, and update price feeds: The following example uses the testnet contract. For mainnet usage, change the PYTH_CONTRACT_ADDRESS_TESTNET to PYTH_CONTRACT_ADDRESS_MAINNET accordingly.

On-chain data verification

TODO This code snippet does the following:
  1. Initializes a TonClient and creates a PythContract instance.
  2. Retrieves the current guardian set index and BTC price from the TON contract.
  3. Fetches the latest price updates from Hermes.
  4. Prepares the update data and calculates the update fee.
  5. Updates the price feeds on the TON contract.

Additional resources

You may find these additional resources helpful for developing your TON application: