update_all_pricefeeds.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import yargs from "yargs";
  2. import { hideBin } from "yargs/helpers";
  3. import { HermesClient, PriceFeedMetadata } from "@pythnetwork/hermes-client";
  4. import { DefaultStore, toPrivateKey } from "../src";
  5. const parser = yargs(hideBin(process.argv))
  6. .usage("Update the set of price feeds in a network. Usage: $0")
  7. .options({
  8. "private-key": {
  9. type: "string",
  10. demandOption: true,
  11. desc: "Private key to sign the transactions with",
  12. },
  13. contract: {
  14. type: "string",
  15. demandOption: true,
  16. desc: "Contract to update price feeds for (e.g mumbai_0xff1a0f4744e8582DF1aE09D5611b887B6a12925C)",
  17. },
  18. endpoint: {
  19. type: "string",
  20. desc: "Hermes endpoint to use, defaults to https://hermes.pyth.network",
  21. },
  22. encoding: {
  23. type: "string",
  24. desc: "Encoding to use for the price feeds (hex or base64), defaults to hex",
  25. choices: ["hex", "base64"],
  26. default: "hex",
  27. },
  28. "chunk-size": {
  29. type: "number",
  30. desc: "Chunk size to use for the price feeds, defaults to 150",
  31. default: 150,
  32. },
  33. });
  34. // This script is intended to update all pricefeeds after we deploy pyth pricefeeds contract.
  35. // It will fetch all pricefeeds from hermes and update the pricefeeds contract with the new pricefeeds.
  36. async function main() {
  37. const argv = await parser.argv;
  38. let priceFeedsMetadata: PriceFeedMetadata[] = [];
  39. const client = new HermesClient(
  40. argv.endpoint || "https://hermes.pyth.network",
  41. );
  42. const contract = DefaultStore.contracts[argv.contract];
  43. const privateKey = toPrivateKey(argv["private-key"]);
  44. const encoding = argv.encoding || "hex";
  45. priceFeedsMetadata = await client.getPriceFeeds();
  46. const priceFeedIds = priceFeedsMetadata.map((feed) => feed.id);
  47. console.log(`Fetched ${priceFeedIds.length} price feed IDs`);
  48. // We can adjust the chunk size based on the chain. Don't exceed 150 for now.
  49. // TODO: Add a check for the chain's block gas limit and adjust the chunk size accordingly.
  50. const chunkSize = argv.chunkSize;
  51. for (let i = 0; i < priceFeedIds.length; i += chunkSize) {
  52. console.log(
  53. `Processing chunk ${i / chunkSize + 1} of ${Math.ceil(
  54. priceFeedIds.length / chunkSize,
  55. )}`,
  56. );
  57. const chunk = priceFeedIds.slice(i, i + chunkSize);
  58. console.log(`length: ${chunk.length}`);
  59. const updates = await client.getLatestPriceUpdates(chunk, {
  60. parsed: false,
  61. });
  62. console.log(
  63. await contract.executeUpdatePriceFeed(
  64. privateKey,
  65. updates.binary.data.map((update) =>
  66. encoding === "hex"
  67. ? Buffer.from(update, "hex")
  68. : Buffer.from(update, "base64"),
  69. ),
  70. ),
  71. );
  72. }
  73. }
  74. main();