-
Couldn't load subscription status.
- Fork 303
feat(target_chains/ton): add ton js sdk #2044
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| module.exports = { | ||
| root: true, | ||
| parser: "@typescript-eslint/parser", | ||
| plugins: ["@typescript-eslint"], | ||
| extends: ["eslint:recommended", "plugin:@typescript-eslint/recommended"], | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| lib |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| # Pyth Network TON SDK | ||
|
|
||
| This SDK provides a JavaScript interface for interacting with the Pyth Network on the TON blockchain. | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| npm install @pythnetwork/pyth-ton-js | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| Here's a basic example of how to use the SDK: | ||
|
|
||
| ```bash | ||
| npm install @pythnetwork/pyth-ton-js @pythnetwork/hermes-client @ton/core @ton/ton @ton/crypto | ||
| ``` | ||
|
|
||
| ```typescript | ||
| 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() { | ||
| const client = new TonClient({ | ||
| endpoint: "https://testnet.toncenter.com/api/v2/jsonRPC", | ||
| apiKey: "your-api-key-here", // Optional, but note that without api-key you need to send requests once per second | ||
| }); | ||
|
|
||
| const contractAddress = Address.parse(PYTH_CONTRACT_ADDRESS_TESTNET); | ||
| const contract = client.open(PythContract.createFromAddress(contractAddress)); | ||
|
|
||
| const guardianSetIndex = await contract.getCurrentGuardianSetIndex(); | ||
| console.log("Guardian Set Index:", guardianSetIndex); | ||
|
|
||
| const price = await contract.getPriceUnsafe(BTC_PRICE_FEED_ID); | ||
| console.log("BTC Price from TON contract:", price); | ||
|
|
||
| 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); | ||
|
|
||
| const updateData = Buffer.from(latestPriceUpdates.binary.data[0], "hex"); | ||
| console.log("Update data:", updateData); | ||
|
|
||
| const updateFee = await contract.getUpdateFee(updateData); | ||
| console.log("Update fee:", updateFee); | ||
|
|
||
| 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 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."); | ||
|
|
||
| const updatedPrice = await contract.getPriceUnsafe(BTC_PRICE_FEED_ID); | ||
| console.log("Updated BTC Price from TON contract:", updatedPrice); | ||
| } | ||
|
|
||
| main().catch(console.error); | ||
| ``` | ||
|
|
||
| ## API Reference | ||
|
|
||
| ### `PythContract` | ||
|
|
||
| The main class for interacting with the Pyth contract on TON. | ||
|
|
||
| #### Methods: | ||
|
|
||
| - `getCurrentGuardianSetIndex(provider: ContractProvider): Promise<number>` | ||
| - `getPriceUnsafe(provider: ContractProvider, priceFeedId: string): Promise<PriceInfo>` | ||
| - `getPriceNoOlderThan(provider: ContractProvider, timePeriod: number, priceFeedId: string): Promise<PriceInfo>` | ||
| - `getEmaPriceUnsafe(provider: ContractProvider, priceFeedId: string): Promise<PriceInfo>` | ||
| - `getEmaPriceNoOlderThan(provider: ContractProvider, timePeriod: number, priceFeedId: string): Promise<PriceInfo>` | ||
| - `getUpdateFee(provider: ContractProvider, vm: Buffer): Promise<number>` | ||
| - `getSingleUpdateFee(provider: ContractProvider): Promise<number>` | ||
| - `sendUpdatePriceFeeds(provider: ContractProvider, via: Sender, updateData: Buffer, updateFee: bigint): Promise<void>` | ||
|
|
||
| ### Constants | ||
|
|
||
| - `PYTH_CONTRACT_ADDRESS_TESTNET`: The address of the Pyth contract on the TON testnet. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| { | ||
| "name": "@pythnetwork/pyth-ton-js", | ||
| "version": "0.1.0", | ||
| "description": "Pyth Network TON Utilities", | ||
| "homepage": "https://pyth.network", | ||
| "author": { | ||
| "name": "Pyth Data Association" | ||
| }, | ||
| "main": "lib/index.js", | ||
| "types": "lib/index.d.ts", | ||
| "files": [ | ||
| "lib/**/*" | ||
| ], | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/pyth-network/pyth-crosschain", | ||
| "directory": "target_chains/ton/sdk/js" | ||
| }, | ||
| "publishConfig": { | ||
| "access": "public" | ||
| }, | ||
| "scripts": { | ||
| "test": "jest --passWithNoTests", | ||
| "build": "tsc", | ||
| "format": "prettier --write \"src/**/*.ts\"", | ||
| "lint": "eslint src/", | ||
| "prepublishOnly": "pnpm run build && pnpm test && pnpm run lint", | ||
| "preversion": "pnpm run lint", | ||
| "version": "pnpm run format && git add -A src" | ||
| }, | ||
| "keywords": [ | ||
| "pyth", | ||
| "oracle" | ||
| ], | ||
| "license": "Apache-2.0", | ||
| "devDependencies": { | ||
| "@ton/core": "^0.59.0", | ||
| "@ton/ton": "^15.1.0", | ||
| "@ton/crypto": "^3.3.0", | ||
| "@types/node": "^18.11.18", | ||
| "@typescript-eslint/eslint-plugin": "^5.21.0", | ||
| "@typescript-eslint/parser": "^5.21.0", | ||
| "eslint": "^8.14.0", | ||
| "jest": "^29.4.1", | ||
| "prettier": "^2.6.2", | ||
| "ts-jest": "^29.0.5", | ||
| "typescript": "^4.6.3", | ||
| "ts-node": "^10.9.2" | ||
| }, | ||
| "dependencies": {} | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.