update_pricefeed.ts 2.0 KB

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