Skip to content

Commit ad0c635

Browse files
committed
add ton
1 parent bc12c87 commit ad0c635

File tree

5 files changed

+117
-1
lines changed

5 files changed

+117
-1
lines changed

pages/price-feeds/contract-addresses.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ The contracts are split by ecosystem into several different documents:
1111
- [Sui](contract-addresses/sui)
1212
- [CosmWasm](contract-addresses/cosmwasm)
1313
- [NEAR](contract-addresses/near)
14+
- [TON](contract-addresses/ton)
1415

1516
Please see the relevant ecosystem document to find the Pyth contract address on your blockchain of choice.

pages/price-feeds/contract-addresses/_meta.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
"sui": "Sui",
88
"cosmwasm": "CosmWasm",
99
"near": "NEAR",
10-
"pythnet": "Pythnet"
10+
"pythnet": "Pythnet",
11+
"ton": "TON"
1112
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import CopyAddress from "../../../components/CopyAddress";
2+
3+
# Price Feed Contract Addresses on TON
4+
5+
Pyth is currently deployed on TON Testnet.
6+
7+
| Network | Contract address |
8+
| ----------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
9+
| TON Testnet | <CopyAddress address={`EQDwGkJmcj7MMmWAHmhldnY-lAKI6hcTQ2tAEcapmwCnztQU`} url="https://testnet.tonscan.org/address/EQDwGkJmcj7MMmWAHmhldnY-lAKI6hcTQ2tAEcapmwCnztQU" /> |

pages/price-feeds/use-real-time-data/_meta.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"fuel": "in Fuel Contracts",
66
"aptos": "in Aptos Contracts",
77
"sui": "in Sui Contracts",
8+
"ton": "in TON Contracts",
89
"cosmwasm": "in CosmWasm Contracts",
910
"near": "in Near Contracts",
1011
"off-chain": "in Off-Chain Applications"
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
```bash copy npm install @pythnetwork/pyth-ton-js @pythnetwork/hermes-client
18+
@ton/core @ton/ton @ton/crypto ```
19+
</Tabs.Tab>
20+
<Tabs.Tab>
21+
```bash copy yarn add @pythnetwork/pyth-ton-js @pythnetwork/hermes-client
22+
@ton/core @ton/ton @ton/crypto ```
23+
</Tabs.Tab>
24+
</Tabs>
25+
26+
## Write Client Code
27+
28+
The following code snippet demonstrates how to fetch price updates, interact with the Pyth contract on TON, and update price feeds:
29+
30+
```typescript copy
31+
import { TonClient, Address, WalletContractV4 } from "@ton/ton";
32+
import { toNano } from "@ton/core";
33+
import { mnemonicToPrivateKey } from "@ton/crypto";
34+
import { HermesClient } from "@pythnetwork/hermes-client";
35+
import {
36+
PythContract,
37+
PYTH_CONTRACT_ADDRESS_TESTNET,
38+
} from "@pythnetwork/pyth-ton-js";
39+
const BTC_PRICE_FEED_ID =
40+
"0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43";
41+
async function main() {
42+
// Initialize TonClient
43+
const client = new TonClient({
44+
endpoint: "https://testnet.toncenter.com/api/v2/jsonRPC",
45+
apiKey: "your-api-key-here", // Optional
46+
});
47+
// Create PythContract instance
48+
const contractAddress = Address.parse(PYTH_CONTRACT_ADDRESS_TESTNET);
49+
const contract = client.open(PythContract.createFromAddress(contractAddress));
50+
// Get current guardian set index
51+
const guardianSetIndex = await contract.getCurrentGuardianSetIndex();
52+
console.log("Guardian Set Index:", guardianSetIndex);
53+
// Get BTC price from TON contract
54+
const price = await contract.getPriceUnsafe(BTC_PRICE_FEED_ID);
55+
console.log("BTC Price from TON contract:", price);
56+
// Fetch latest price updates from Hermes
57+
const hermesEndpoint = "https://hermes.pyth.network";
58+
const hermesClient = new HermesClient(hermesEndpoint);
59+
const priceIds = [BTC_PRICE_FEED_ID];
60+
const latestPriceUpdates = await hermesClient.getLatestPriceUpdates(
61+
priceIds,
62+
{ encoding: "hex" }
63+
);
64+
console.log("Hermes BTC price:", latestPriceUpdates.parsed?.[0].price);
65+
// Prepare update data
66+
const updateData = Buffer.from(latestPriceUpdates.binary.data[0], "hex");
67+
console.log("Update data:", updateData);
68+
// Get update fee
69+
const updateFee = await contract.getUpdateFee(updateData);
70+
console.log("Update fee:", updateFee);
71+
// Update price feeds
72+
const mnemonic = "your mnemonic here";
73+
const key = await mnemonicToPrivateKey(mnemonic.split(" "));
74+
const wallet = WalletContractV4.create({
75+
publicKey: key.publicKey,
76+
workchain: 0,
77+
});
78+
const provider = client.open(wallet);
79+
await contract.sendUpdatePriceFeeds(
80+
provider.sender(key.secretKey),
81+
updateData,
82+
toNano(updateFee)
83+
);
84+
console.log("Price feeds updated successfully.");
85+
}
86+
main().catch(console.error);
87+
```
88+
89+
This code snippet does the following:
90+
91+
1. Initializes a `TonClient` and creates a `PythContract` instance.
92+
2. Retrieves the current guardian set index and BTC price from the TON contract.
93+
3. Fetches the latest price updates from Hermes.
94+
4. Prepares the update data and calculates the update fee.
95+
5. Updates the price feeds on the TON contract.
96+
97+
## Additional Resources
98+
99+
You may find these additional resources helpful for developing your TON application:
100+
101+
- [TON Documentation](https://ton.org/docs/)
102+
- [Pyth Price Feed IDs](https://pyth.network/developers/price-feed-ids)
103+
- [Pyth TON Contract](https://github.com/pyth-network/pyth-crosschain/tree/main/target_chains/ton/contracts)
104+
- [Pyth TON SDK](https://github.com/pyth-network/pyth-crosschain/tree/main/target_chains/ton/sdk)

0 commit comments

Comments
 (0)