Skip to content

feat: add ton documentation #457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Starknet Guide

pages/price-feeds/use-real-time-data/starknet.mdx

pages/price-feeds/use-real-time-data/ton.mdx
1 change: 1 addition & 0 deletions pages/price-feeds/contract-addresses.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ The contracts are split by ecosystem into several different documents:
- [Sui](contract-addresses/sui)
- [CosmWasm](contract-addresses/cosmwasm)
- [NEAR](contract-addresses/near)
- [TON](contract-addresses/ton)

Please see the relevant ecosystem document to find the Pyth contract address on your blockchain of choice.
3 changes: 2 additions & 1 deletion pages/price-feeds/contract-addresses/_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"sui": "Sui",
"cosmwasm": "CosmWasm",
"near": "NEAR",
"pythnet": "Pythnet"
"pythnet": "Pythnet",
"ton": "TON"
}
9 changes: 9 additions & 0 deletions pages/price-feeds/contract-addresses/ton.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import CopyAddress from "../../../components/CopyAddress";

# Price Feed Contract Addresses on TON

Pyth is currently deployed on TON Testnet.

| Network | Contract address |
| ----------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| TON Testnet | <CopyAddress address={`EQDwGkJmcj7MMmWAHmhldnY-lAKI6hcTQ2tAEcapmwCnztQU`} url="https://testnet.tonscan.org/address/EQDwGkJmcj7MMmWAHmhldnY-lAKI6hcTQ2tAEcapmwCnztQU" /> |
1 change: 1 addition & 0 deletions pages/price-feeds/use-real-time-data/_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"fuel": "in Fuel Contracts",
"aptos": "in Aptos Contracts",
"sui": "in Sui Contracts",
"ton": "in TON Contracts",
"cosmwasm": "in CosmWasm Contracts",
"near": "in Near Contracts",
"off-chain": "in Off-Chain Applications"
Expand Down
108 changes: 108 additions & 0 deletions pages/price-feeds/use-real-time-data/ton.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
---
description: Consume Pyth Network prices in TON applications
---

import { Tabs } from "nextra/components";

# How to Use Real-Time Data in TON Contracts

This guide explains how to use real-time Pyth data in TON applications.

## Install the Pyth SDK

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

<Tabs items={["npm", "yarn"]}>
<Tabs.Tab>
```sh copy
npm install @pythnetwork/pyth-ton-js @pythnetwork/hermes-client
@ton/core @ton/ton @ton/crypto
```
</Tabs.Tab>
<Tabs.Tab>
```sh copy
yarn add @pythnetwork/pyth-ton-js @pythnetwork/hermes-client
@ton/core @ton/ton @ton/crypto
```
</Tabs.Tab>
</Tabs>

## Write Client Code

The following code snippet demonstrates how to fetch price updates, interact with the Pyth contract on TON, and update price feeds:

```typescript copy
import { TonClient, Address, WalletContractV4 } from "@ton/ton";
import { toNano } from "@ton/core";
import { mnemonicToPrivateKey } from "@ton/crypto";
import { HermesClient } from "@pythnetwork/hermes-client";
import {
PythContract,
PYTH_CONTRACT_ADDRESS_TESTNET,
} from "@pythnetwork/pyth-ton-js";
const BTC_PRICE_FEED_ID =
"0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43";
async function main() {
// Initialize TonClient
const client = new TonClient({
endpoint: "https://testnet.toncenter.com/api/v2/jsonRPC",
apiKey: "your-api-key-here", // Optional
});
// Create PythContract instance
const contractAddress = Address.parse(PYTH_CONTRACT_ADDRESS_TESTNET);
const contract = client.open(PythContract.createFromAddress(contractAddress));
// Get current guardian set index
const guardianSetIndex = await contract.getCurrentGuardianSetIndex();
console.log("Guardian Set Index:", guardianSetIndex);
// Get BTC price from TON contract
const price = await contract.getPriceUnsafe(BTC_PRICE_FEED_ID);
console.log("BTC Price from TON contract:", price);
// Fetch latest price updates from Hermes
const hermesEndpoint = "https://hermes.pyth.network";
const hermesClient = new HermesClient(hermesEndpoint);
const priceIds = [BTC_PRICE_FEED_ID];
const latestPriceUpdates = await hermesClient.getLatestPriceUpdates(
priceIds,
{ encoding: "hex" }
);
console.log("Hermes BTC price:", latestPriceUpdates.parsed?.[0].price);
// Prepare update data
const updateData = Buffer.from(latestPriceUpdates.binary.data[0], "hex");
console.log("Update data:", updateData);
// Get update fee
const updateFee = await contract.getUpdateFee(updateData);
console.log("Update fee:", updateFee);
// Update price feeds
const mnemonic = "your mnemonic here";
const key = await mnemonicToPrivateKey(mnemonic.split(" "));
const wallet = WalletContractV4.create({
publicKey: key.publicKey,
workchain: 0,
});
const provider = client.open(wallet);
await contract.sendUpdatePriceFeeds(
provider.sender(key.secretKey),
updateData,
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)
);
console.log("Price feeds updated successfully.");
}
main().catch(console.error);
```

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:

- [TON Documentation](https://ton.org/docs/)
- [Pyth Price Feed IDs](https://pyth.network/developers/price-feed-ids)
- [Pyth TON Contract](https://github.com/pyth-network/pyth-crosschain/tree/main/target_chains/ton/contracts)
- [Pyth TON SDK](https://github.com/pyth-network/pyth-crosschain/tree/main/target_chains/ton/sdk)
Loading