@pythnetwork/pyth-ton-js 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +13 -0
- package/README.md +106 -0
- package/lib/index.d.ts +46 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +135 -0
- package/package.json +51 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Copyright 2024 Pyth Data Association.
|
|
2
|
+
|
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License.
|
|
5
|
+
You may obtain a copy of the License at
|
|
6
|
+
|
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
|
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
See the License for the specific language governing permissions and
|
|
13
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
# Pyth Network TON SDK
|
|
2
|
+
|
|
3
|
+
This SDK provides a JavaScript interface for interacting with the Pyth Network on the TON blockchain.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @pythnetwork/pyth-ton-js
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
Here's a basic example of how to use the SDK:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @pythnetwork/pyth-ton-js @pythnetwork/hermes-client @ton/core @ton/ton @ton/crypto
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
import { TonClient, Address, WalletContractV4 } from "@ton/ton";
|
|
21
|
+
import { toNano } from "@ton/core";
|
|
22
|
+
import { mnemonicToPrivateKey } from "@ton/crypto";
|
|
23
|
+
import { HermesClient } from "@pythnetwork/hermes-client";
|
|
24
|
+
import {
|
|
25
|
+
PythContract,
|
|
26
|
+
PYTH_CONTRACT_ADDRESS_TESTNET,
|
|
27
|
+
} from "@pythnetwork/pyth-ton-js";
|
|
28
|
+
|
|
29
|
+
const BTC_PRICE_FEED_ID =
|
|
30
|
+
"0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43";
|
|
31
|
+
|
|
32
|
+
async function main() {
|
|
33
|
+
const client = new TonClient({
|
|
34
|
+
endpoint: "https://testnet.toncenter.com/api/v2/jsonRPC",
|
|
35
|
+
apiKey: "your-api-key-here", // Optional, but note that without api-key you need to send requests once per second
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
const contractAddress = Address.parse(PYTH_CONTRACT_ADDRESS_TESTNET);
|
|
39
|
+
const contract = client.open(PythContract.createFromAddress(contractAddress));
|
|
40
|
+
|
|
41
|
+
const guardianSetIndex = await contract.getCurrentGuardianSetIndex();
|
|
42
|
+
console.log("Guardian Set Index:", guardianSetIndex);
|
|
43
|
+
|
|
44
|
+
const price = await contract.getPriceUnsafe(BTC_PRICE_FEED_ID);
|
|
45
|
+
console.log("BTC Price from TON contract:", price);
|
|
46
|
+
|
|
47
|
+
const hermesEndpoint = "https://hermes.pyth.network";
|
|
48
|
+
const hermesClient = new HermesClient(hermesEndpoint);
|
|
49
|
+
|
|
50
|
+
const priceIds = [BTC_PRICE_FEED_ID];
|
|
51
|
+
const latestPriceUpdates = await hermesClient.getLatestPriceUpdates(
|
|
52
|
+
priceIds,
|
|
53
|
+
{
|
|
54
|
+
encoding: "hex",
|
|
55
|
+
}
|
|
56
|
+
);
|
|
57
|
+
console.log("Hermes BTC price:", latestPriceUpdates.parsed?.[0].price);
|
|
58
|
+
|
|
59
|
+
const updateData = Buffer.from(latestPriceUpdates.binary.data[0], "hex");
|
|
60
|
+
console.log("Update data:", updateData);
|
|
61
|
+
|
|
62
|
+
const updateFee = await contract.getUpdateFee(updateData);
|
|
63
|
+
console.log("Update fee:", updateFee);
|
|
64
|
+
|
|
65
|
+
const mnemonic = "your mnemonic here";
|
|
66
|
+
const key = await mnemonicToPrivateKey(mnemonic.split(" "));
|
|
67
|
+
const wallet = WalletContractV4.create({
|
|
68
|
+
publicKey: key.publicKey,
|
|
69
|
+
workchain: 0,
|
|
70
|
+
});
|
|
71
|
+
const provider = client.open(wallet);
|
|
72
|
+
|
|
73
|
+
await contract.sendUpdatePriceFeeds(
|
|
74
|
+
provider.sender(key.secretKey),
|
|
75
|
+
updateData,
|
|
76
|
+
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)
|
|
77
|
+
);
|
|
78
|
+
console.log("Price feeds updated successfully.");
|
|
79
|
+
|
|
80
|
+
const updatedPrice = await contract.getPriceUnsafe(BTC_PRICE_FEED_ID);
|
|
81
|
+
console.log("Updated BTC Price from TON contract:", updatedPrice);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
main().catch(console.error);
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## API Reference
|
|
88
|
+
|
|
89
|
+
### `PythContract`
|
|
90
|
+
|
|
91
|
+
The main class for interacting with the Pyth contract on TON.
|
|
92
|
+
|
|
93
|
+
#### Methods:
|
|
94
|
+
|
|
95
|
+
- `getCurrentGuardianSetIndex(provider: ContractProvider): Promise<number>`
|
|
96
|
+
- `getPriceUnsafe(provider: ContractProvider, priceFeedId: string): Promise<PriceInfo>`
|
|
97
|
+
- `getPriceNoOlderThan(provider: ContractProvider, timePeriod: number, priceFeedId: string): Promise<PriceInfo>`
|
|
98
|
+
- `getEmaPriceUnsafe(provider: ContractProvider, priceFeedId: string): Promise<PriceInfo>`
|
|
99
|
+
- `getEmaPriceNoOlderThan(provider: ContractProvider, timePeriod: number, priceFeedId: string): Promise<PriceInfo>`
|
|
100
|
+
- `getUpdateFee(provider: ContractProvider, vm: Buffer): Promise<number>`
|
|
101
|
+
- `getSingleUpdateFee(provider: ContractProvider): Promise<number>`
|
|
102
|
+
- `sendUpdatePriceFeeds(provider: ContractProvider, via: Sender, updateData: Buffer, updateFee: bigint): Promise<void>`
|
|
103
|
+
|
|
104
|
+
### Constants
|
|
105
|
+
|
|
106
|
+
- `PYTH_CONTRACT_ADDRESS_TESTNET`: The address of the Pyth contract on the TON testnet.
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Address, Cell, Contract, Sender } from "@ton/core";
|
|
3
|
+
import { ContractProvider } from "@ton/ton";
|
|
4
|
+
export declare const PYTH_CONTRACT_ADDRESS_TESTNET = "EQDwGkJmcj7MMmWAHmhldnY-lAKI6hcTQ2tAEcapmwCnztQU";
|
|
5
|
+
export declare class PythContract implements Contract {
|
|
6
|
+
readonly address: Address;
|
|
7
|
+
readonly init?: {
|
|
8
|
+
code: Cell;
|
|
9
|
+
data: Cell;
|
|
10
|
+
} | undefined;
|
|
11
|
+
constructor(address: Address, init?: {
|
|
12
|
+
code: Cell;
|
|
13
|
+
data: Cell;
|
|
14
|
+
} | undefined);
|
|
15
|
+
static createFromAddress(address: Address): PythContract;
|
|
16
|
+
getCurrentGuardianSetIndex(provider: ContractProvider): Promise<number>;
|
|
17
|
+
sendUpdatePriceFeeds(provider: ContractProvider, via: Sender, updateData: Buffer, updateFee: bigint): Promise<void>;
|
|
18
|
+
getPriceUnsafe(provider: ContractProvider, priceFeedId: string): Promise<{
|
|
19
|
+
price: number;
|
|
20
|
+
conf: number;
|
|
21
|
+
expo: number;
|
|
22
|
+
publishTime: number;
|
|
23
|
+
}>;
|
|
24
|
+
getPriceNoOlderThan(provider: ContractProvider, timePeriod: number, priceFeedId: string): Promise<{
|
|
25
|
+
price: number;
|
|
26
|
+
conf: number;
|
|
27
|
+
expo: number;
|
|
28
|
+
publishTime: number;
|
|
29
|
+
}>;
|
|
30
|
+
getEmaPriceUnsafe(provider: ContractProvider, priceFeedId: string): Promise<{
|
|
31
|
+
price: number;
|
|
32
|
+
conf: number;
|
|
33
|
+
expo: number;
|
|
34
|
+
publishTime: number;
|
|
35
|
+
}>;
|
|
36
|
+
getEmaPriceNoOlderThan(provider: ContractProvider, timePeriod: number, priceFeedId: string): Promise<{
|
|
37
|
+
price: number;
|
|
38
|
+
conf: number;
|
|
39
|
+
expo: number;
|
|
40
|
+
publishTime: number;
|
|
41
|
+
}>;
|
|
42
|
+
getUpdateFee(provider: ContractProvider, vm: Buffer): Promise<number>;
|
|
43
|
+
getSingleUpdateFee(provider: ContractProvider): Promise<number>;
|
|
44
|
+
}
|
|
45
|
+
export declare function createCellChain(buffer: Buffer): Cell;
|
|
46
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,OAAO,EACL,OAAO,EAEP,IAAI,EACJ,QAAQ,EACR,MAAM,EAEP,MAAM,WAAW,CAAC;AACnB,OAAO,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAE5C,eAAO,MAAM,6BAA6B,qDACU,CAAC;AAErD,qBAAa,YAAa,YAAW,QAAQ;IAEzC,QAAQ,CAAC,OAAO,EAAE,OAAO;IACzB,QAAQ,CAAC,IAAI,CAAC;cAAU,IAAI;cAAQ,IAAI;;gBAD/B,OAAO,EAAE,OAAO,EAChB,IAAI,CAAC;cAAU,IAAI;cAAQ,IAAI;iBAAE;IAG5C,MAAM,CAAC,iBAAiB,CAAC,OAAO,EAAE,OAAO;IAInC,0BAA0B,CAAC,QAAQ,EAAE,gBAAgB;IAMrD,oBAAoB,CACxB,QAAQ,EAAE,gBAAgB,EAC1B,GAAG,EAAE,MAAM,EACX,UAAU,EAAE,MAAM,EAClB,SAAS,EAAE,MAAM;IAcb,cAAc,CAAC,QAAQ,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM;;;;;;IAkB9D,mBAAmB,CACvB,QAAQ,EAAE,gBAAgB,EAC1B,UAAU,EAAE,MAAM,EAClB,WAAW,EAAE,MAAM;;;;;;IAoBf,iBAAiB,CAAC,QAAQ,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM;;;;;;IAkBjE,sBAAsB,CAC1B,QAAQ,EAAE,gBAAgB,EAC1B,UAAU,EAAE,MAAM,EAClB,WAAW,EAAE,MAAM;;;;;;IAoBf,YAAY,CAAC,QAAQ,EAAE,gBAAgB,EAAE,EAAE,EAAE,MAAM;IAQnD,kBAAkB,CAAC,QAAQ,EAAE,gBAAgB;CAKpD;AAED,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAsBpD"}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createCellChain = exports.PythContract = exports.PYTH_CONTRACT_ADDRESS_TESTNET = void 0;
|
|
4
|
+
const core_1 = require("@ton/core");
|
|
5
|
+
exports.PYTH_CONTRACT_ADDRESS_TESTNET = "EQDwGkJmcj7MMmWAHmhldnY-lAKI6hcTQ2tAEcapmwCnztQU";
|
|
6
|
+
class PythContract {
|
|
7
|
+
address;
|
|
8
|
+
init;
|
|
9
|
+
constructor(address, init) {
|
|
10
|
+
this.address = address;
|
|
11
|
+
this.init = init;
|
|
12
|
+
}
|
|
13
|
+
static createFromAddress(address) {
|
|
14
|
+
return new PythContract(address);
|
|
15
|
+
}
|
|
16
|
+
async getCurrentGuardianSetIndex(provider) {
|
|
17
|
+
const result = await provider.get("get_current_guardian_set_index", []);
|
|
18
|
+
return result.stack.readNumber();
|
|
19
|
+
}
|
|
20
|
+
async sendUpdatePriceFeeds(provider, via, updateData, updateFee) {
|
|
21
|
+
const messageBody = (0, core_1.beginCell)()
|
|
22
|
+
.storeUint(2, 32) // OP_UPDATE_PRICE_FEEDS
|
|
23
|
+
.storeRef(createCellChain(updateData))
|
|
24
|
+
.endCell();
|
|
25
|
+
await provider.internal(via, {
|
|
26
|
+
value: updateFee,
|
|
27
|
+
sendMode: core_1.SendMode.PAY_GAS_SEPARATELY,
|
|
28
|
+
body: messageBody,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
async getPriceUnsafe(provider, priceFeedId) {
|
|
32
|
+
const result = await provider.get("get_price_unsafe", [
|
|
33
|
+
{ type: "int", value: BigInt(priceFeedId) },
|
|
34
|
+
]);
|
|
35
|
+
const price = result.stack.readNumber();
|
|
36
|
+
const conf = result.stack.readNumber();
|
|
37
|
+
const expo = result.stack.readNumber();
|
|
38
|
+
const publishTime = result.stack.readNumber();
|
|
39
|
+
return {
|
|
40
|
+
price,
|
|
41
|
+
conf,
|
|
42
|
+
expo,
|
|
43
|
+
publishTime,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
async getPriceNoOlderThan(provider, timePeriod, priceFeedId) {
|
|
47
|
+
const result = await provider.get("get_price_no_older_than", [
|
|
48
|
+
{ type: "int", value: BigInt(timePeriod) },
|
|
49
|
+
{ type: "int", value: BigInt(priceFeedId) },
|
|
50
|
+
]);
|
|
51
|
+
const price = result.stack.readNumber();
|
|
52
|
+
const conf = result.stack.readNumber();
|
|
53
|
+
const expo = result.stack.readNumber();
|
|
54
|
+
const publishTime = result.stack.readNumber();
|
|
55
|
+
return {
|
|
56
|
+
price,
|
|
57
|
+
conf,
|
|
58
|
+
expo,
|
|
59
|
+
publishTime,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
async getEmaPriceUnsafe(provider, priceFeedId) {
|
|
63
|
+
const result = await provider.get("get_ema_price_unsafe", [
|
|
64
|
+
{ type: "int", value: BigInt(priceFeedId) },
|
|
65
|
+
]);
|
|
66
|
+
const price = result.stack.readNumber();
|
|
67
|
+
const conf = result.stack.readNumber();
|
|
68
|
+
const expo = result.stack.readNumber();
|
|
69
|
+
const publishTime = result.stack.readNumber();
|
|
70
|
+
return {
|
|
71
|
+
price,
|
|
72
|
+
conf,
|
|
73
|
+
expo,
|
|
74
|
+
publishTime,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
async getEmaPriceNoOlderThan(provider, timePeriod, priceFeedId) {
|
|
78
|
+
const result = await provider.get("get_ema_price_no_older_than", [
|
|
79
|
+
{ type: "int", value: BigInt(timePeriod) },
|
|
80
|
+
{ type: "int", value: BigInt(priceFeedId) },
|
|
81
|
+
]);
|
|
82
|
+
const price = result.stack.readNumber();
|
|
83
|
+
const conf = result.stack.readNumber();
|
|
84
|
+
const expo = result.stack.readNumber();
|
|
85
|
+
const publishTime = result.stack.readNumber();
|
|
86
|
+
return {
|
|
87
|
+
price,
|
|
88
|
+
conf,
|
|
89
|
+
expo,
|
|
90
|
+
publishTime,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
async getUpdateFee(provider, vm) {
|
|
94
|
+
const result = await provider.get("get_update_fee", [
|
|
95
|
+
{ type: "slice", cell: createCellChain(vm) },
|
|
96
|
+
]);
|
|
97
|
+
return result.stack.readNumber();
|
|
98
|
+
}
|
|
99
|
+
async getSingleUpdateFee(provider) {
|
|
100
|
+
const result = await provider.get("get_single_update_fee", []);
|
|
101
|
+
return result.stack.readNumber();
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
exports.PythContract = PythContract;
|
|
105
|
+
function createCellChain(buffer) {
|
|
106
|
+
const chunks = bufferToChunks(buffer, 127);
|
|
107
|
+
let lastCell = null;
|
|
108
|
+
// Iterate through chunks in reverse order
|
|
109
|
+
for (let i = chunks.length - 1; i >= 0; i--) {
|
|
110
|
+
const chunk = chunks[i];
|
|
111
|
+
const cellBuilder = (0, core_1.beginCell)();
|
|
112
|
+
const buffer = Buffer.from(chunk);
|
|
113
|
+
cellBuilder.storeBuffer(buffer);
|
|
114
|
+
if (lastCell) {
|
|
115
|
+
cellBuilder.storeRef(lastCell);
|
|
116
|
+
}
|
|
117
|
+
lastCell = cellBuilder.endCell();
|
|
118
|
+
}
|
|
119
|
+
// lastCell will be the root cell of our chain
|
|
120
|
+
if (!lastCell) {
|
|
121
|
+
throw new Error("Failed to create cell chain");
|
|
122
|
+
}
|
|
123
|
+
return lastCell;
|
|
124
|
+
}
|
|
125
|
+
exports.createCellChain = createCellChain;
|
|
126
|
+
function bufferToChunks(buff, chunkSizeBytes = 127) {
|
|
127
|
+
const chunks = [];
|
|
128
|
+
const uint8Array = new Uint8Array(buff.buffer, buff.byteOffset, buff.byteLength);
|
|
129
|
+
for (let offset = 0; offset < uint8Array.length; offset += chunkSizeBytes) {
|
|
130
|
+
const remainingBytes = Math.min(chunkSizeBytes, uint8Array.length - offset);
|
|
131
|
+
const chunk = uint8Array.subarray(offset, offset + remainingBytes);
|
|
132
|
+
chunks.push(chunk);
|
|
133
|
+
}
|
|
134
|
+
return chunks;
|
|
135
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pythnetwork/pyth-ton-js",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Pyth Network TON Utilities",
|
|
5
|
+
"homepage": "https://pyth.network",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "Pyth Data Association"
|
|
8
|
+
},
|
|
9
|
+
"main": "lib/index.js",
|
|
10
|
+
"types": "lib/index.d.ts",
|
|
11
|
+
"files": [
|
|
12
|
+
"lib/**/*"
|
|
13
|
+
],
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "https://github.com/pyth-network/pyth-crosschain",
|
|
17
|
+
"directory": "target_chains/ton/sdk/js"
|
|
18
|
+
},
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"test": "jest --passWithNoTests",
|
|
24
|
+
"build": "tsc",
|
|
25
|
+
"format": "prettier --write \"src/**/*.ts\"",
|
|
26
|
+
"lint": "eslint src/",
|
|
27
|
+
"prepublishOnly": "pnpm run build && pnpm test && pnpm run lint",
|
|
28
|
+
"preversion": "pnpm run lint",
|
|
29
|
+
"version": "pnpm run format && git add -A src"
|
|
30
|
+
},
|
|
31
|
+
"keywords": [
|
|
32
|
+
"pyth",
|
|
33
|
+
"oracle"
|
|
34
|
+
],
|
|
35
|
+
"license": "Apache-2.0",
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@ton/core": "^0.59.0",
|
|
38
|
+
"@ton/crypto": "^3.3.0",
|
|
39
|
+
"@ton/ton": "^15.1.0",
|
|
40
|
+
"@types/node": "^18.11.18",
|
|
41
|
+
"@typescript-eslint/eslint-plugin": "^5.21.0",
|
|
42
|
+
"@typescript-eslint/parser": "^5.21.0",
|
|
43
|
+
"eslint": "^8.14.0",
|
|
44
|
+
"jest": "^29.4.1",
|
|
45
|
+
"prettier": "^2.6.2",
|
|
46
|
+
"ts-jest": "^29.0.5",
|
|
47
|
+
"ts-node": "^10.9.2",
|
|
48
|
+
"typescript": "^4.6.3"
|
|
49
|
+
},
|
|
50
|
+
"gitHead": "29e5c9bc62a0234819540fccfb509165cd89b6a2"
|
|
51
|
+
}
|