Pārlūkot izejas kodu

chore: benchmarks example (#2121)

* go

* comments
guibescos 1 gadu atpakaļ
vecāks
revīzija
5a945e2f06

+ 2 - 2
pnpm-lock.yaml

@@ -2395,9 +2395,9 @@ importers:
         specifier: 1.92.3
         version: 1.92.3(bufferutil@4.0.8)(encoding@0.1.13)(utf-8-validate@5.0.10)
     devDependencies:
-      '@pythnetwork/price-service-client':
+      '@pythnetwork/hermes-client':
         specifier: workspace:*
-        version: link:../../../../../price_service/client/js
+        version: link:../../../../../apps/hermes/client/js
       '@types/jest':
         specifier: ^29.4.0
         version: 29.4.0

+ 9 - 7
target_chains/solana/sdk/js/pyth_solana_receiver/examples/post_price_update.ts

@@ -1,9 +1,9 @@
 import { Connection, Keypair, PublicKey } from "@solana/web3.js";
-import { PriceServiceConnection } from "@pythnetwork/price-service-client";
 import { InstructionWithEphemeralSigners, PythSolanaReceiver } from "../";
 import { Wallet } from "@coral-xyz/anchor";
 import fs from "fs";
 import os from "os";
+import { HermesClient } from "@pythnetwork/hermes-client";
 
 // Get price feed ids from https://pyth.network/developers/price-feed-ids#pyth-evm-stable
 const SOL_PRICE_FEED_ID =
@@ -67,15 +67,17 @@ async function main() {
 
 // Fetch price update data from Hermes
 async function getPriceUpdateData() {
-  const priceServiceConnection = new PriceServiceConnection(
+  const priceServiceConnection = new HermesClient(
     "https://hermes.pyth.network/",
-    { priceFeedRequestConfig: { binary: true } }
+    {}
   );
 
-  return await priceServiceConnection.getLatestVaas([
-    SOL_PRICE_FEED_ID,
-    ETH_PRICE_FEED_ID,
-  ]);
+  const response = await priceServiceConnection.getLatestPriceUpdates(
+    [SOL_PRICE_FEED_ID, ETH_PRICE_FEED_ID],
+    { encoding: "base64" }
+  );
+
+  return response.binary.data;
 }
 
 // Load a solana keypair from an id.json file

+ 91 - 0
target_chains/solana/sdk/js/pyth_solana_receiver/examples/post_price_update_from_benchmarks.ts

@@ -0,0 +1,91 @@
+import { Connection, Keypair, PublicKey } from "@solana/web3.js";
+import { HermesClient, PriceUpdate } from "@pythnetwork/hermes-client";
+import { InstructionWithEphemeralSigners, PythSolanaReceiver } from "../";
+import { Wallet } from "@coral-xyz/anchor";
+import fs from "fs";
+import os from "os";
+
+// Get price feed ids from https://pyth.network/developers/price-feed-ids#pyth-evm-stable
+const SOL_PRICE_FEED_ID =
+  "0xef0d8b6fda2ceba41da15d4095d1da392a0d2f8ed0c6c7bc0f4cfac8c280b56d";
+
+let keypairFile = "";
+if (process.env["SOLANA_KEYPAIR"]) {
+  keypairFile = process.env["SOLANA_KEYPAIR"];
+} else {
+  keypairFile = `${os.homedir()}/.config/solana/id.json`;
+}
+
+async function main() {
+  const connection = new Connection("https://api.devnet.solana.com");
+  const keypair = await loadKeypairFromFile(keypairFile);
+  console.log(
+    `Sending transactions from account: ${keypair.publicKey.toBase58()}`
+  );
+  const wallet = new Wallet(keypair);
+  const pythSolanaReceiver = new PythSolanaReceiver({ connection, wallet });
+
+  // Get the price update from hermes
+  const priceUpdateData = await getPriceUpdateDataFromOneDayAgo();
+  console.log(`Posting price update: ${priceUpdateData}`);
+
+  // If closeUpdateAccounts = true, the builder will automatically generate instructions to close the ephemeral price update accounts
+  // at the end of the transaction. Closing the accounts will reclaim their rent.
+  // The example is using closeUpdateAccounts = false so you can easily look up the price update account in an explorer.
+  const transactionBuilder = pythSolanaReceiver.newTransactionBuilder({
+    closeUpdateAccounts: false,
+  });
+  // Post the price updates to ephemeral accounts, one per price feed.
+  await transactionBuilder.addPostPriceUpdates(priceUpdateData);
+  console.log(
+    "The SOL/USD price update will get posted to:",
+    transactionBuilder.getPriceUpdateAccount(SOL_PRICE_FEED_ID).toBase58()
+  );
+
+  await transactionBuilder.addPriceConsumerInstructions(
+    async (
+      getPriceUpdateAccount: (priceFeedId: string) => PublicKey
+    ): Promise<InstructionWithEphemeralSigners[]> => {
+      // You can generate instructions here that use the price updates posted above.
+      // getPriceUpdateAccount(<price feed id>) will give you the account you need.
+      // These accounts will be packed into transactions by the builder.
+      return [];
+    }
+  );
+
+  // Send the instructions in the builder in 1 or more transactions.
+  // The builder will pack the instructions into transactions automatically.
+  await pythSolanaReceiver.provider.sendAll(
+    await transactionBuilder.buildVersionedTransactions({
+      computeUnitPriceMicroLamports: 100000,
+    }),
+    { preflightCommitment: "processed" }
+  );
+}
+
+// Fetch price update data from Hermes
+async function getPriceUpdateDataFromOneDayAgo(): Promise<string[]> {
+  const hermesClient = new HermesClient("https://hermes.pyth.network/", {});
+
+  const oneDayAgo = Math.floor(Date.now() / 1000) - 86400;
+  const response = await hermesClient.getPriceUpdatesAtTimestamp(
+    oneDayAgo,
+    [SOL_PRICE_FEED_ID],
+    { encoding: "base64" }
+  );
+  return response.binary.data;
+}
+
+// Load a solana keypair from an id.json file
+async function loadKeypairFromFile(filePath: string): Promise<Keypair> {
+  try {
+    const keypairData = JSON.parse(
+      await fs.promises.readFile(filePath, "utf8")
+    );
+    return Keypair.fromSecretKey(Uint8Array.from(keypairData));
+  } catch (error) {
+    throw new Error(`Error loading keypair from file: ${error}`);
+  }
+}
+
+main();

+ 9 - 7
target_chains/solana/sdk/js/pyth_solana_receiver/examples/post_price_update_instructions.ts

@@ -1,9 +1,9 @@
 import { Connection, Keypair } from "@solana/web3.js";
-import { PriceServiceConnection } from "@pythnetwork/price-service-client";
 import { InstructionWithEphemeralSigners, PythSolanaReceiver } from "../";
 import { Wallet } from "@coral-xyz/anchor";
 import fs from "fs";
 import os from "os";
+import { HermesClient } from "@pythnetwork/hermes-client";
 
 // Get price feed ids from https://pyth.network/developers/price-feed-ids#pyth-evm-stable
 const SOL_PRICE_FEED_ID =
@@ -58,15 +58,17 @@ async function main() {
 
 // Fetch price update data from Hermes
 async function getPriceUpdateData() {
-  const priceServiceConnection = new PriceServiceConnection(
+  const priceServiceConnection = new HermesClient(
     "https://hermes.pyth.network/",
-    { priceFeedRequestConfig: { binary: true } }
+    {}
   );
 
-  return await priceServiceConnection.getLatestVaas([
-    SOL_PRICE_FEED_ID,
-    ETH_PRICE_FEED_ID,
-  ]);
+  const response = await priceServiceConnection.getLatestPriceUpdates(
+    [SOL_PRICE_FEED_ID, ETH_PRICE_FEED_ID],
+    { encoding: "base64" }
+  );
+
+  return response.binary.data;
 }
 
 // Load a solana keypair from an id.json file

+ 9 - 7
target_chains/solana/sdk/js/pyth_solana_receiver/examples/update_price_feed.ts

@@ -1,9 +1,9 @@
 import { Connection, Keypair, PublicKey } from "@solana/web3.js";
-import { PriceServiceConnection } from "@pythnetwork/price-service-client";
 import { InstructionWithEphemeralSigners, PythSolanaReceiver } from "../";
 import { Wallet } from "@coral-xyz/anchor";
 import fs from "fs";
 import os from "os";
+import { HermesClient } from "@pythnetwork/hermes-client";
 
 // Get price feed ids from https://pyth.network/developers/price-feed-ids#pyth-evm-stable
 const SOL_PRICE_FEED_ID =
@@ -67,15 +67,17 @@ async function main() {
 
 // Fetch price update data from Hermes
 async function getPriceUpdateData() {
-  const priceServiceConnection = new PriceServiceConnection(
+  const priceServiceConnection = new HermesClient(
     "https://hermes.pyth.network/",
-    { priceFeedRequestConfig: { binary: true } }
+    {}
   );
 
-  return await priceServiceConnection.getLatestVaas([
-    SOL_PRICE_FEED_ID,
-    ETH_PRICE_FEED_ID,
-  ]);
+  const response = await priceServiceConnection.getLatestPriceUpdates(
+    [SOL_PRICE_FEED_ID, ETH_PRICE_FEED_ID],
+    { encoding: "base64" }
+  );
+
+  return response.binary.data;
 }
 
 // Load a solana keypair from an id.json file

+ 1 - 1
target_chains/solana/sdk/js/pyth_solana_receiver/package.json

@@ -30,7 +30,7 @@
   ],
   "license": "Apache-2.0",
   "devDependencies": {
-    "@pythnetwork/price-service-client": "workspace:*",
+    "@pythnetwork/hermes-client": "workspace:*",
     "@types/jest": "^29.4.0",
     "@typescript-eslint/eslint-plugin": "^5.20.0",
     "@typescript-eslint/parser": "^5.20.0",