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