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:- TON Mainnet:
EQBU6k8HH6yX4Jf3d18swWbnYr31D3PJI7PgjXT-flsKHqql
- TON Testnet:
EQB4ZnrI5qsP_IUJgVJNwEGKLzZWsQOFhiaqDbD7pTt_f9oU
PYTH_CONTRACT_ADDRESS_TESTNET
to PYTH_CONTRACT_ADDRESS_MAINNET
accordingly.
On-chain data verification
TODO This code snippet does the following:- Initializes a
TonClient
and creates aPythContract
instance. - Retrieves the current guardian set index and BTC price from the TON contract.
- Fetches the latest price updates from Hermes.
- Prepares the update data and calculates the update fee.
- Updates the price feeds on the TON contract.