|
| 1 | +--- |
| 2 | +description: Consume Pyth Network prices in TON applications |
| 3 | +--- |
| 4 | + |
| 5 | +import { Tabs } from "nextra/components"; |
| 6 | + |
| 7 | +# How to Use Real-Time Data in TON Contracts |
| 8 | + |
| 9 | +This guide explains how to use real-time Pyth data in TON applications. |
| 10 | + |
| 11 | +## Install the Pyth SDK |
| 12 | + |
| 13 | +Install the Pyth TON SDK and other necessary dependencies using npm: |
| 14 | + |
| 15 | +<Tabs items={["npm", "yarn"]}> |
| 16 | + <Tabs.Tab> |
| 17 | + ```sh copy |
| 18 | + npm install @pythnetwork/pyth-ton-js @pythnetwork/hermes-client |
| 19 | + @ton/core @ton/ton @ton/crypto |
| 20 | + ``` |
| 21 | + </Tabs.Tab> |
| 22 | + <Tabs.Tab> |
| 23 | + ```sh copy |
| 24 | + yarn add @pythnetwork/pyth-ton-js @pythnetwork/hermes-client |
| 25 | + @ton/core @ton/ton @ton/crypto |
| 26 | + ``` |
| 27 | + </Tabs.Tab> |
| 28 | +</Tabs> |
| 29 | + |
| 30 | +## Write Client Code |
| 31 | + |
| 32 | +The following code snippet demonstrates how to fetch price updates, interact with the Pyth contract on TON, and update price feeds: |
| 33 | + |
| 34 | +```typescript copy |
| 35 | +import { TonClient, Address, WalletContractV4 } from "@ton/ton"; |
| 36 | +import { toNano } from "@ton/core"; |
| 37 | +import { mnemonicToPrivateKey } from "@ton/crypto"; |
| 38 | +import { HermesClient } from "@pythnetwork/hermes-client"; |
| 39 | +import { |
| 40 | + PythContract, |
| 41 | + PYTH_CONTRACT_ADDRESS_TESTNET, |
| 42 | +} from "@pythnetwork/pyth-ton-js"; |
| 43 | +const BTC_PRICE_FEED_ID = |
| 44 | + "0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43"; |
| 45 | +async function main() { |
| 46 | + // Initialize TonClient |
| 47 | + const client = new TonClient({ |
| 48 | + endpoint: "https://testnet.toncenter.com/api/v2/jsonRPC", |
| 49 | + apiKey: "your-api-key-here", // Optional |
| 50 | + }); |
| 51 | + // Create PythContract instance |
| 52 | + const contractAddress = Address.parse(PYTH_CONTRACT_ADDRESS_TESTNET); |
| 53 | + const contract = client.open(PythContract.createFromAddress(contractAddress)); |
| 54 | + // Get current guardian set index |
| 55 | + const guardianSetIndex = await contract.getCurrentGuardianSetIndex(); |
| 56 | + console.log("Guardian Set Index:", guardianSetIndex); |
| 57 | + // Get BTC price from TON contract |
| 58 | + const price = await contract.getPriceUnsafe(BTC_PRICE_FEED_ID); |
| 59 | + console.log("BTC Price from TON contract:", price); |
| 60 | + // Fetch latest price updates from Hermes |
| 61 | + const hermesEndpoint = "https://hermes.pyth.network"; |
| 62 | + const hermesClient = new HermesClient(hermesEndpoint); |
| 63 | + const priceIds = [BTC_PRICE_FEED_ID]; |
| 64 | + const latestPriceUpdates = await hermesClient.getLatestPriceUpdates( |
| 65 | + priceIds, |
| 66 | + { encoding: "hex" } |
| 67 | + ); |
| 68 | + console.log("Hermes BTC price:", latestPriceUpdates.parsed?.[0].price); |
| 69 | + // Prepare update data |
| 70 | + const updateData = Buffer.from(latestPriceUpdates.binary.data[0], "hex"); |
| 71 | + console.log("Update data:", updateData); |
| 72 | + // Get update fee |
| 73 | + const updateFee = await contract.getUpdateFee(updateData); |
| 74 | + console.log("Update fee:", updateFee); |
| 75 | + // Update price feeds |
| 76 | + const mnemonic = "your mnemonic here"; |
| 77 | + const key = await mnemonicToPrivateKey(mnemonic.split(" ")); |
| 78 | + const wallet = WalletContractV4.create({ |
| 79 | + publicKey: key.publicKey, |
| 80 | + workchain: 0, |
| 81 | + }); |
| 82 | + const provider = client.open(wallet); |
| 83 | + await contract.sendUpdatePriceFeeds( |
| 84 | + provider.sender(key.secretKey), |
| 85 | + updateData, |
| 86 | + 156000000n + BigInt(updateFee) // 156000000 = 390000 (estimated gas used for the transaction, this is defined in (contracts/common/gas.fc)[https://github.com/pyth-network/pyth-crosschain/tree/main/target_chains/ton/contracts/common/gas.fc] as UPDATE_PRICE_FEEDS_GAS) * 400 (current settings in basechain are as follows: 1 unit of gas costs 400 nanotons) |
| 87 | + ); |
| 88 | + console.log("Price feeds updated successfully."); |
| 89 | +} |
| 90 | +main().catch(console.error); |
| 91 | +``` |
| 92 | + |
| 93 | +This code snippet does the following: |
| 94 | + |
| 95 | +1. Initializes a `TonClient` and creates a `PythContract` instance. |
| 96 | +2. Retrieves the current guardian set index and BTC price from the TON contract. |
| 97 | +3. Fetches the latest price updates from Hermes. |
| 98 | +4. Prepares the update data and calculates the update fee. |
| 99 | +5. Updates the price feeds on the TON contract. |
| 100 | + |
| 101 | +## Additional Resources |
| 102 | + |
| 103 | +You may find these additional resources helpful for developing your TON application: |
| 104 | + |
| 105 | +- [TON Documentation](https://ton.org/docs/) |
| 106 | +- [Pyth Price Feed IDs](https://pyth.network/developers/price-feed-ids) |
| 107 | +- [Pyth TON Contract](https://github.com/pyth-network/pyth-crosschain/tree/main/target_chains/ton/contracts) |
| 108 | +- [Pyth TON SDK](https://github.com/pyth-network/pyth-crosschain/tree/main/target_chains/ton/sdk) |
0 commit comments