Skip to content

Commit deb8046

Browse files
authored
add an example for benchmarks (#647)
1 parent 66783e5 commit deb8046

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

target_chains/ethereum/sdk/js/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"build": "tsc",
2222
"example-client": "npm run build && node lib/examples/EvmPriceServiceClient.js",
2323
"example-relay": "npm run build && node lib/examples/EvmRelay.js",
24+
"example-benchmark": "npm run build && node lib/examples/EvmBenchmark.js",
2425
"format": "prettier --write \"src/**/*.ts\"",
2526
"lint": "eslint src/",
2627
"prepublishOnly": "npm run build && npm test && npm run lint",
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import Web3 from "web3";
2+
import yargs from "yargs";
3+
import { hideBin } from "yargs/helpers";
4+
5+
import { EvmPriceServiceConnection } from "../index";
6+
import HDWalletProvider from "@truffle/hdwallet-provider";
7+
import PythInterfaceAbi from "@pythnetwork/pyth-sdk-solidity/abis/IPyth.json";
8+
9+
const argv = yargs(hideBin(process.argv))
10+
.option("network", {
11+
description: "RPC of the network to relay on.",
12+
type: "string",
13+
required: true,
14+
})
15+
.option("endpoint", {
16+
description:
17+
"Endpoint URL for the price service. e.g: https://endpoint/example",
18+
type: "string",
19+
required: true,
20+
})
21+
.option("pyth-contract", {
22+
description: "Pyth contract address.",
23+
type: "string",
24+
required: true,
25+
})
26+
.option("price-id", {
27+
description:
28+
"Price feed id (in hex) to fetch" + " e.g: 0xf9c0172ba10dfa4d19088d...",
29+
type: "string",
30+
required: true,
31+
})
32+
.option("timestamp", {
33+
description: "Timestamp of the prices to fetch" + " e.g., 2022-", // TODO
34+
type: "string",
35+
required: true,
36+
})
37+
.option("mnemonic", {
38+
description: "Mnemonic (private key) for sender",
39+
type: "string",
40+
required: true,
41+
})
42+
.help()
43+
.alias("help", "h")
44+
.parserConfiguration({
45+
"parse-numbers": false,
46+
})
47+
.parseSync();
48+
49+
const network = argv.network;
50+
const pythContractAddr = argv.pythContract;
51+
52+
const connection = new EvmPriceServiceConnection(argv.endpoint);
53+
54+
async function run() {
55+
const provider = new HDWalletProvider({
56+
mnemonic: {
57+
phrase: argv.mnemonic,
58+
},
59+
providerOrUrl: network,
60+
});
61+
62+
// @ts-ignore
63+
const web3 = new Web3(provider);
64+
const priceId = argv.priceId as string;
65+
// The unix timestamp in seconds
66+
const unixTimestamp = Date.parse(argv.timestamp) / 1000;
67+
68+
console.log(`Querying unix timestamp: ${unixTimestamp}`);
69+
70+
const [priceFeedUpdateVaa, updateTimestamp] = await connection.getVaa(
71+
priceId,
72+
unixTimestamp
73+
);
74+
console.log(`Next pyth update was at: ${updateTimestamp}`);
75+
console.log(priceFeedUpdateVaa);
76+
77+
const priceFeedUpdate =
78+
"0x" + Buffer.from(priceFeedUpdateVaa, "base64").toString("hex");
79+
80+
const pythContract = new web3.eth.Contract(
81+
PythInterfaceAbi as any,
82+
pythContractAddr,
83+
{
84+
from: provider.getAddress(0),
85+
}
86+
);
87+
88+
const updateFee = await pythContract.methods
89+
.getUpdateFee([priceFeedUpdate])
90+
.call();
91+
console.log(`Update fee: ${updateFee}`);
92+
93+
// In real use cases, you would pass the update to your contract, then call parsePriceFeedUpdates within your contract.
94+
// When invoked on-chain, this function will return a PriceFeed struct containing the data in the price update
95+
// (such as the current price).
96+
await pythContract.methods
97+
.parsePriceFeedUpdates(
98+
[priceFeedUpdate],
99+
[priceId],
100+
// parsePriceFeedUpdates will reject any price update outside of the time window provided in the following
101+
// two arguments. Integrators can use this to specify the timestamp of the update they are expecting.
102+
unixTimestamp,
103+
unixTimestamp + 5
104+
)
105+
.send({ value: updateFee })
106+
.on("transactionHash", (hash: string) => {
107+
console.log(`Tx hash: ${hash}`);
108+
})
109+
.on("error", (err: any, receipt: any) => {
110+
console.error(receipt);
111+
throw err;
112+
});
113+
114+
provider.engine.stop();
115+
}
116+
117+
run();

0 commit comments

Comments
 (0)