Skip to content

Commit 5012551

Browse files
authored
feat: add ton documentation (#457)
* add ton * fix ton formatting issues * update docs
1 parent d377e14 commit 5012551

File tree

6 files changed

+123
-1
lines changed

6 files changed

+123
-1
lines changed

.prettierignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
# Starknet Guide
22

33
pages/price-feeds/use-real-time-data/starknet.mdx
4+
5+
pages/price-feeds/use-real-time-data/ton.mdx

pages/price-feeds/contract-addresses.mdx

+1
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

+2-1
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
}
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

+1
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"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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

Comments
 (0)