deploy_evm_pricefeed_contracts.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import yargs from "yargs";
  2. import { hideBin } from "yargs/helpers";
  3. import { EvmChain } from "../src/chains";
  4. import { DefaultStore } from "../src/store";
  5. import {
  6. DeploymentType,
  7. EvmPriceFeedContract,
  8. getDefaultDeploymentConfig,
  9. toDeploymentType,
  10. toPrivateKey,
  11. } from "../src";
  12. import {
  13. COMMON_DEPLOY_OPTIONS,
  14. deployIfNotCached,
  15. getWeb3Contract,
  16. getOrDeployWormholeContract,
  17. BaseDeployConfig,
  18. } from "./common";
  19. interface DeploymentConfig extends BaseDeployConfig {
  20. type: DeploymentType;
  21. validTimePeriodSeconds: number;
  22. singleUpdateFeeInWei: number;
  23. saveContract: boolean;
  24. }
  25. const CACHE_FILE = ".cache-deploy-evm";
  26. const parser = yargs(hideBin(process.argv))
  27. .scriptName("deploy_evm_pricefeed_contracts.ts")
  28. .usage(
  29. "Usage: $0 --std-output-dir <path/to/std-output-dir/> --private-key <private-key> --chain <chain0> --chain <chain1>",
  30. )
  31. .options({
  32. ...COMMON_DEPLOY_OPTIONS,
  33. "valid-time-period-seconds": {
  34. type: "number",
  35. demandOption: false,
  36. default: 60,
  37. desc: "Valid time period in seconds for the price feed staleness",
  38. },
  39. "single-update-fee-in-wei": {
  40. type: "number",
  41. demandOption: false,
  42. default: 1,
  43. desc: "Single update fee in wei for the price feed",
  44. },
  45. });
  46. async function deployPriceFeedContracts(
  47. chain: EvmChain,
  48. config: DeploymentConfig,
  49. wormholeAddr: string,
  50. ): Promise<string> {
  51. const pythImplAddr = await deployIfNotCached(
  52. CACHE_FILE,
  53. chain,
  54. config,
  55. "PythUpgradable",
  56. [],
  57. );
  58. // Craft the init data for the proxy contract
  59. const { dataSources, governanceDataSource } = getDefaultDeploymentConfig(
  60. config.type,
  61. );
  62. const pythImplContract = getWeb3Contract(
  63. config.jsonOutputDir,
  64. "PythUpgradable",
  65. pythImplAddr,
  66. );
  67. const pythInitData = pythImplContract.methods
  68. .initialize(
  69. wormholeAddr,
  70. dataSources.map((ds) => ds.emitterChain),
  71. dataSources.map((ds) => "0x" + ds.emitterAddress),
  72. governanceDataSource.emitterChain,
  73. "0x" + governanceDataSource.emitterAddress,
  74. 0, // governanceInitialSequence
  75. config.validTimePeriodSeconds,
  76. config.singleUpdateFeeInWei,
  77. )
  78. .encodeABI();
  79. return await deployIfNotCached(CACHE_FILE, chain, config, "ERC1967Proxy", [
  80. pythImplAddr,
  81. pythInitData,
  82. ]);
  83. }
  84. async function main() {
  85. const argv = await parser.argv;
  86. const deploymentConfig: DeploymentConfig = {
  87. type: toDeploymentType(argv.deploymentType),
  88. validTimePeriodSeconds: argv.validTimePeriodSeconds,
  89. singleUpdateFeeInWei: argv.singleUpdateFeeInWei,
  90. gasMultiplier: argv.gasMultiplier,
  91. gasPriceMultiplier: argv.gasPriceMultiplier,
  92. privateKey: toPrivateKey(argv.privateKey),
  93. jsonOutputDir: argv.stdOutputDir,
  94. saveContract: argv.saveContract,
  95. };
  96. console.log(
  97. `Deployment config: ${JSON.stringify(deploymentConfig, null, 2)}\n`,
  98. );
  99. const chainNames = argv.chain;
  100. for (const chainName of chainNames) {
  101. const chain = DefaultStore.getChainOrThrow(chainName, EvmChain);
  102. console.log(`Deploying price feed contracts on ${chain.getId()}...`);
  103. const wormholeContract = await getOrDeployWormholeContract(
  104. chain,
  105. deploymentConfig,
  106. CACHE_FILE,
  107. );
  108. const priceFeedAddr = await deployPriceFeedContracts(
  109. chain,
  110. deploymentConfig,
  111. wormholeContract.address,
  112. );
  113. if (deploymentConfig.saveContract) {
  114. console.log("Saving the contract in the store...");
  115. const contract = new EvmPriceFeedContract(chain, priceFeedAddr);
  116. DefaultStore.contracts[contract.getId()] = contract;
  117. DefaultStore.saveAllContracts();
  118. }
  119. console.log(
  120. `✅ Deployed price feed contracts on ${chain.getId()} at ${priceFeedAddr}\n\n`,
  121. );
  122. }
  123. }
  124. main();