update_pricefeed.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { PriceServiceConnection } from "@pythnetwork/price-service-client";
  2. import yargs from "yargs";
  3. import { hideBin } from "yargs/helpers";
  4. import { toPrivateKey } from "../src/core/base";
  5. import { DefaultStore } from "../src/node/utils/store";
  6. const parser = yargs(hideBin(process.argv))
  7. .usage(
  8. "Usage: $0 --contract <contract_id> --feed-id <feed-id> --private-key <private-key>",
  9. )
  10. .options({
  11. contract: {
  12. type: "string",
  13. demandOption: true,
  14. desc: "Contract to update price feeds for (e.g mumbai_0xff1a0f4744e8582DF1aE09D5611b887B6a12925C)",
  15. },
  16. "feed-id": {
  17. type: "array",
  18. demandOption: true,
  19. desc: "Price feed ids to update without the leading 0x (e.g f9c0172ba10dfa4d19088d94f5bf61d3b54d5bd7483a322a982e1373ee8ea31b). Can be provided multiple times for multiple feed updates",
  20. },
  21. "private-key": {
  22. type: "string",
  23. demandOption: true,
  24. desc: "Private key to use to sign transaction",
  25. },
  26. endpoint: {
  27. type: "string",
  28. desc: "Hermes endpoint to use, defaults to https://hermes.pyth.network",
  29. },
  30. });
  31. async function main() {
  32. const argv = await parser.argv;
  33. const contract = DefaultStore.contracts[argv.contract];
  34. if (!contract) {
  35. throw new Error(
  36. `Contract ${argv.contract} not found. Contracts found: ${Object.keys(
  37. DefaultStore.contracts,
  38. )}`,
  39. );
  40. }
  41. const priceService = new PriceServiceConnection(
  42. argv.endpoint || "https://hermes.pyth.network",
  43. );
  44. const vaas = await priceService.getLatestVaas(argv["feed-id"] as string[]);
  45. const privateKey = toPrivateKey(argv["private-key"]);
  46. console.log(
  47. await contract.executeUpdatePriceFeed(
  48. privateKey,
  49. vaas.map((vaa) => Buffer.from(vaa, "base64")),
  50. ),
  51. );
  52. }
  53. main();