Skip to content

Commit 5a945e2

Browse files
authored
chore: benchmarks example (#2121)
* go * comments
1 parent 65cef16 commit 5a945e2

File tree

6 files changed

+121
-24
lines changed

6 files changed

+121
-24
lines changed

pnpm-lock.yaml

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

target_chains/solana/sdk/js/pyth_solana_receiver/examples/post_price_update.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Connection, Keypair, PublicKey } from "@solana/web3.js";
2-
import { PriceServiceConnection } from "@pythnetwork/price-service-client";
32
import { InstructionWithEphemeralSigners, PythSolanaReceiver } from "../";
43
import { Wallet } from "@coral-xyz/anchor";
54
import fs from "fs";
65
import os from "os";
6+
import { HermesClient } from "@pythnetwork/hermes-client";
77

88
// Get price feed ids from https://pyth.network/developers/price-feed-ids#pyth-evm-stable
99
const SOL_PRICE_FEED_ID =
@@ -67,15 +67,17 @@ async function main() {
6767

6868
// Fetch price update data from Hermes
6969
async function getPriceUpdateData() {
70-
const priceServiceConnection = new PriceServiceConnection(
70+
const priceServiceConnection = new HermesClient(
7171
"https://hermes.pyth.network/",
72-
{ priceFeedRequestConfig: { binary: true } }
72+
{}
7373
);
7474

75-
return await priceServiceConnection.getLatestVaas([
76-
SOL_PRICE_FEED_ID,
77-
ETH_PRICE_FEED_ID,
78-
]);
75+
const response = await priceServiceConnection.getLatestPriceUpdates(
76+
[SOL_PRICE_FEED_ID, ETH_PRICE_FEED_ID],
77+
{ encoding: "base64" }
78+
);
79+
80+
return response.binary.data;
7981
}
8082

8183
// Load a solana keypair from an id.json file
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import { Connection, Keypair, PublicKey } from "@solana/web3.js";
2+
import { HermesClient, PriceUpdate } from "@pythnetwork/hermes-client";
3+
import { InstructionWithEphemeralSigners, PythSolanaReceiver } from "../";
4+
import { Wallet } from "@coral-xyz/anchor";
5+
import fs from "fs";
6+
import os from "os";
7+
8+
// Get price feed ids from https://pyth.network/developers/price-feed-ids#pyth-evm-stable
9+
const SOL_PRICE_FEED_ID =
10+
"0xef0d8b6fda2ceba41da15d4095d1da392a0d2f8ed0c6c7bc0f4cfac8c280b56d";
11+
12+
let keypairFile = "";
13+
if (process.env["SOLANA_KEYPAIR"]) {
14+
keypairFile = process.env["SOLANA_KEYPAIR"];
15+
} else {
16+
keypairFile = `${os.homedir()}/.config/solana/id.json`;
17+
}
18+
19+
async function main() {
20+
const connection = new Connection("https://api.devnet.solana.com");
21+
const keypair = await loadKeypairFromFile(keypairFile);
22+
console.log(
23+
`Sending transactions from account: ${keypair.publicKey.toBase58()}`
24+
);
25+
const wallet = new Wallet(keypair);
26+
const pythSolanaReceiver = new PythSolanaReceiver({ connection, wallet });
27+
28+
// Get the price update from hermes
29+
const priceUpdateData = await getPriceUpdateDataFromOneDayAgo();
30+
console.log(`Posting price update: ${priceUpdateData}`);
31+
32+
// If closeUpdateAccounts = true, the builder will automatically generate instructions to close the ephemeral price update accounts
33+
// at the end of the transaction. Closing the accounts will reclaim their rent.
34+
// The example is using closeUpdateAccounts = false so you can easily look up the price update account in an explorer.
35+
const transactionBuilder = pythSolanaReceiver.newTransactionBuilder({
36+
closeUpdateAccounts: false,
37+
});
38+
// Post the price updates to ephemeral accounts, one per price feed.
39+
await transactionBuilder.addPostPriceUpdates(priceUpdateData);
40+
console.log(
41+
"The SOL/USD price update will get posted to:",
42+
transactionBuilder.getPriceUpdateAccount(SOL_PRICE_FEED_ID).toBase58()
43+
);
44+
45+
await transactionBuilder.addPriceConsumerInstructions(
46+
async (
47+
getPriceUpdateAccount: (priceFeedId: string) => PublicKey
48+
): Promise<InstructionWithEphemeralSigners[]> => {
49+
// You can generate instructions here that use the price updates posted above.
50+
// getPriceUpdateAccount(<price feed id>) will give you the account you need.
51+
// These accounts will be packed into transactions by the builder.
52+
return [];
53+
}
54+
);
55+
56+
// Send the instructions in the builder in 1 or more transactions.
57+
// The builder will pack the instructions into transactions automatically.
58+
await pythSolanaReceiver.provider.sendAll(
59+
await transactionBuilder.buildVersionedTransactions({
60+
computeUnitPriceMicroLamports: 100000,
61+
}),
62+
{ preflightCommitment: "processed" }
63+
);
64+
}
65+
66+
// Fetch price update data from Hermes
67+
async function getPriceUpdateDataFromOneDayAgo(): Promise<string[]> {
68+
const hermesClient = new HermesClient("https://hermes.pyth.network/", {});
69+
70+
const oneDayAgo = Math.floor(Date.now() / 1000) - 86400;
71+
const response = await hermesClient.getPriceUpdatesAtTimestamp(
72+
oneDayAgo,
73+
[SOL_PRICE_FEED_ID],
74+
{ encoding: "base64" }
75+
);
76+
return response.binary.data;
77+
}
78+
79+
// Load a solana keypair from an id.json file
80+
async function loadKeypairFromFile(filePath: string): Promise<Keypair> {
81+
try {
82+
const keypairData = JSON.parse(
83+
await fs.promises.readFile(filePath, "utf8")
84+
);
85+
return Keypair.fromSecretKey(Uint8Array.from(keypairData));
86+
} catch (error) {
87+
throw new Error(`Error loading keypair from file: ${error}`);
88+
}
89+
}
90+
91+
main();

target_chains/solana/sdk/js/pyth_solana_receiver/examples/post_price_update_instructions.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Connection, Keypair } from "@solana/web3.js";
2-
import { PriceServiceConnection } from "@pythnetwork/price-service-client";
32
import { InstructionWithEphemeralSigners, PythSolanaReceiver } from "../";
43
import { Wallet } from "@coral-xyz/anchor";
54
import fs from "fs";
65
import os from "os";
6+
import { HermesClient } from "@pythnetwork/hermes-client";
77

88
// Get price feed ids from https://pyth.network/developers/price-feed-ids#pyth-evm-stable
99
const SOL_PRICE_FEED_ID =
@@ -58,15 +58,17 @@ async function main() {
5858

5959
// Fetch price update data from Hermes
6060
async function getPriceUpdateData() {
61-
const priceServiceConnection = new PriceServiceConnection(
61+
const priceServiceConnection = new HermesClient(
6262
"https://hermes.pyth.network/",
63-
{ priceFeedRequestConfig: { binary: true } }
63+
{}
6464
);
6565

66-
return await priceServiceConnection.getLatestVaas([
67-
SOL_PRICE_FEED_ID,
68-
ETH_PRICE_FEED_ID,
69-
]);
66+
const response = await priceServiceConnection.getLatestPriceUpdates(
67+
[SOL_PRICE_FEED_ID, ETH_PRICE_FEED_ID],
68+
{ encoding: "base64" }
69+
);
70+
71+
return response.binary.data;
7072
}
7173

7274
// Load a solana keypair from an id.json file

target_chains/solana/sdk/js/pyth_solana_receiver/examples/update_price_feed.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Connection, Keypair, PublicKey } from "@solana/web3.js";
2-
import { PriceServiceConnection } from "@pythnetwork/price-service-client";
32
import { InstructionWithEphemeralSigners, PythSolanaReceiver } from "../";
43
import { Wallet } from "@coral-xyz/anchor";
54
import fs from "fs";
65
import os from "os";
6+
import { HermesClient } from "@pythnetwork/hermes-client";
77

88
// Get price feed ids from https://pyth.network/developers/price-feed-ids#pyth-evm-stable
99
const SOL_PRICE_FEED_ID =
@@ -67,15 +67,17 @@ async function main() {
6767

6868
// Fetch price update data from Hermes
6969
async function getPriceUpdateData() {
70-
const priceServiceConnection = new PriceServiceConnection(
70+
const priceServiceConnection = new HermesClient(
7171
"https://hermes.pyth.network/",
72-
{ priceFeedRequestConfig: { binary: true } }
72+
{}
7373
);
7474

75-
return await priceServiceConnection.getLatestVaas([
76-
SOL_PRICE_FEED_ID,
77-
ETH_PRICE_FEED_ID,
78-
]);
75+
const response = await priceServiceConnection.getLatestPriceUpdates(
76+
[SOL_PRICE_FEED_ID, ETH_PRICE_FEED_ID],
77+
{ encoding: "base64" }
78+
);
79+
80+
return response.binary.data;
7981
}
8082

8183
// Load a solana keypair from an id.json file

target_chains/solana/sdk/js/pyth_solana_receiver/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
],
3131
"license": "Apache-2.0",
3232
"devDependencies": {
33-
"@pythnetwork/price-service-client": "workspace:*",
33+
"@pythnetwork/hermes-client": "workspace:*",
3434
"@types/jest": "^29.4.0",
3535
"@typescript-eslint/eslint-plugin": "^5.20.0",
3636
"@typescript-eslint/parser": "^5.20.0",

0 commit comments

Comments
 (0)