|
| 1 | +import yargs from "yargs"; |
| 2 | +import { hideBin } from "yargs/helpers"; |
| 3 | +import { EvmChain } from "../src/chains"; |
| 4 | +import { findWormholeContract } from "./common"; |
| 5 | +import { DefaultStore } from "../src"; |
| 6 | +import { EvmPriceFeedContract, getDefaultDeploymentConfig } from "../src"; |
| 7 | +import { getWeb3Contract } from "./common"; |
| 8 | + |
| 9 | +interface Config { |
| 10 | + validTimePeriodSeconds: number; |
| 11 | + singleUpdateFeeInWei: number; |
| 12 | + stdOutput: string; |
| 13 | +} |
| 14 | + |
| 15 | +// It only works for stable contracts |
| 16 | +const parser = yargs(hideBin(process.argv)) |
| 17 | + .scriptName("get_pyth_evm_pricefeed_details.ts") |
| 18 | + .usage("Usage: $0 --chain <chain-name>") |
| 19 | + .options({ |
| 20 | + contract: { |
| 21 | + type: "string", |
| 22 | + demandOption: true, |
| 23 | + desc: "Contract to get Pyth price feed details for", |
| 24 | + }, |
| 25 | + validTimePeriodSeconds: { |
| 26 | + type: "number", |
| 27 | + demandOption: false, |
| 28 | + default: 60, |
| 29 | + desc: "Valid time period in seconds for the price feed", |
| 30 | + }, |
| 31 | + singleUpdateFeeInWei: { |
| 32 | + type: "number", |
| 33 | + demandOption: false, |
| 34 | + default: 1, |
| 35 | + desc: "Single update fee in wei for the price feed", |
| 36 | + }, |
| 37 | + stdOutput: { |
| 38 | + type: "string", |
| 39 | + demandOption: true, |
| 40 | + default: "../target_chains/ethereum/contracts/build/contracts/", |
| 41 | + desc: "Path to the standard JSON output of the pyth contract (build artifact)", |
| 42 | + }, |
| 43 | + }); |
| 44 | + |
| 45 | +async function getPythPriceFeedDetails( |
| 46 | + contract: EvmPriceFeedContract, |
| 47 | + config: Config |
| 48 | +) { |
| 49 | + // Get wormhole contract from store |
| 50 | + const wormholeContract = findWormholeContract( |
| 51 | + contract.getChain() as EvmChain |
| 52 | + ); |
| 53 | + if (!wormholeContract) { |
| 54 | + throw new Error(`Wormhole contract not found for contract ${contract}`); |
| 55 | + } |
| 56 | + |
| 57 | + // Deploy PythUpgradable contract |
| 58 | + const pythImplAddr = await contract.getImplementationAddress(); |
| 59 | + |
| 60 | + // Get contract instance |
| 61 | + const pythImplContract = getWeb3Contract( |
| 62 | + config.stdOutput, |
| 63 | + "PythUpgradable", |
| 64 | + pythImplAddr |
| 65 | + ); |
| 66 | + |
| 67 | + // Get data sources |
| 68 | + const { dataSources, governanceDataSource } = |
| 69 | + getDefaultDeploymentConfig("stable"); |
| 70 | + |
| 71 | + // Create init data |
| 72 | + const pythInitData = pythImplContract.methods |
| 73 | + .initialize( |
| 74 | + wormholeContract.address, |
| 75 | + dataSources.map((ds) => ds.emitterChain), |
| 76 | + dataSources.map((ds) => "0x" + ds.emitterAddress), |
| 77 | + governanceDataSource.emitterChain, |
| 78 | + "0x" + governanceDataSource.emitterAddress, |
| 79 | + 0, // governanceInitialSequence |
| 80 | + config.validTimePeriodSeconds, |
| 81 | + config.singleUpdateFeeInWei |
| 82 | + ) |
| 83 | + .encodeABI(); |
| 84 | + |
| 85 | + return { |
| 86 | + pythImplAddr, |
| 87 | + pythInitData, |
| 88 | + }; |
| 89 | +} |
| 90 | + |
| 91 | +async function main() { |
| 92 | + const argv = await parser.argv; |
| 93 | + const contract = DefaultStore.contracts[argv.contract]; |
| 94 | + const validTimePeriodSeconds = argv.validTimePeriodSeconds; |
| 95 | + const singleUpdateFeeInWei = argv.singleUpdateFeeInWei; |
| 96 | + const stdOutput = argv.stdOutput; |
| 97 | + const config: Config = { |
| 98 | + validTimePeriodSeconds, |
| 99 | + singleUpdateFeeInWei, |
| 100 | + stdOutput, |
| 101 | + }; |
| 102 | + |
| 103 | + console.log(`Getting Pyth price feed details for contract ${contract}...`); |
| 104 | + |
| 105 | + const details = await getPythPriceFeedDetails( |
| 106 | + contract as EvmPriceFeedContract, |
| 107 | + config |
| 108 | + ); |
| 109 | + |
| 110 | + console.log( |
| 111 | + `\nPyth Price Feed Implementation Address: ${details.pythImplAddr}` |
| 112 | + ); |
| 113 | + console.log(`Pyth Init Data: ${details.pythInitData}`); |
| 114 | +} |
| 115 | + |
| 116 | +main().catch((error) => { |
| 117 | + console.error(error); |
| 118 | + process.exit(1); |
| 119 | +}); |
0 commit comments