deploy_evm_entropy_contracts.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. ENTROPY_DEFAULT_KEEPER,
  8. ENTROPY_DEFAULT_PROVIDER,
  9. EvmEntropyContract,
  10. getDefaultDeploymentConfig,
  11. toDeploymentType,
  12. toPrivateKey,
  13. } from "../src";
  14. import {
  15. COMMON_DEPLOY_OPTIONS,
  16. deployIfNotCached,
  17. getWeb3Contract,
  18. getOrDeployWormholeContract,
  19. BaseDeployConfig,
  20. topupAccountsIfNecessary,
  21. DefaultAddresses,
  22. } from "./common";
  23. interface DeploymentConfig extends BaseDeployConfig {
  24. type: DeploymentType;
  25. saveContract: boolean;
  26. }
  27. const CACHE_FILE = ".cache-deploy-evm-entropy-contracts";
  28. const parser = yargs(hideBin(process.argv))
  29. .scriptName("deploy_evm_entropy_contracts.ts")
  30. .usage(
  31. "Usage: $0 --std-output-dir <path/to/std-output-dir/> --private-key <private-key> --chain <chain> --wormhole-addr <wormhole-addr>"
  32. )
  33. .options({
  34. ...COMMON_DEPLOY_OPTIONS,
  35. chain: {
  36. type: "string",
  37. demandOption: true,
  38. desc: "Chain to upload the contract on. Can be one of the evm chains available in the store",
  39. },
  40. });
  41. async function deployExecutorContracts(
  42. chain: EvmChain,
  43. config: DeploymentConfig,
  44. wormholeAddr: string
  45. ): Promise<string> {
  46. const executorImplAddr = await deployIfNotCached(
  47. CACHE_FILE,
  48. chain,
  49. config,
  50. "ExecutorUpgradable",
  51. []
  52. );
  53. // Craft the init data for the proxy contract
  54. const { governanceDataSource } = getDefaultDeploymentConfig(config.type);
  55. const executorImplContract = getWeb3Contract(
  56. config.jsonOutputDir,
  57. "ExecutorUpgradable",
  58. executorImplAddr
  59. );
  60. const executorInitData = executorImplContract.methods
  61. .initialize(
  62. wormholeAddr,
  63. 0, // lastExecutedSequence,
  64. chain.getWormholeChainId(),
  65. governanceDataSource.emitterChain,
  66. `0x${governanceDataSource.emitterAddress}`
  67. )
  68. .encodeABI();
  69. return await deployIfNotCached(CACHE_FILE, chain, config, "ERC1967Proxy", [
  70. executorImplAddr,
  71. executorInitData,
  72. ]);
  73. }
  74. async function deployEntropyContracts(
  75. chain: EvmChain,
  76. config: DeploymentConfig,
  77. executorAddr: string
  78. ): Promise<string> {
  79. const entropyImplAddr = await deployIfNotCached(
  80. CACHE_FILE,
  81. chain,
  82. config,
  83. "EntropyUpgradable",
  84. []
  85. );
  86. const entropyImplContract = getWeb3Contract(
  87. config.jsonOutputDir,
  88. "EntropyUpgradable",
  89. entropyImplAddr
  90. );
  91. const entropyInitData = entropyImplContract.methods
  92. .initialize(
  93. executorAddr, // owner
  94. executorAddr, // admin
  95. 1, // pythFeeInWei
  96. chain.isMainnet()
  97. ? ENTROPY_DEFAULT_PROVIDER.mainnet
  98. : ENTROPY_DEFAULT_PROVIDER.testnet,
  99. true // prefillRequestStorage
  100. )
  101. .encodeABI();
  102. return await deployIfNotCached(
  103. CACHE_FILE,
  104. chain,
  105. config,
  106. "ERC1967Proxy",
  107. [entropyImplAddr, entropyInitData],
  108. // NOTE: we are deploying a ERC1967Proxy when deploying executor
  109. // we need to provide a different cache key. As the `artifactname`
  110. // is same in both case which means the cache key will be same
  111. `${chain.getId()}-ERC1967Proxy-ENTROPY`
  112. );
  113. }
  114. async function topupEntropyAccountsIfNecessary(
  115. chain: EvmChain,
  116. deploymentConfig: DeploymentConfig
  117. ) {
  118. const accounts: Array<[string, DefaultAddresses]> = [
  119. ["keeper", ENTROPY_DEFAULT_KEEPER],
  120. ["provider", ENTROPY_DEFAULT_PROVIDER],
  121. ];
  122. await topupAccountsIfNecessary(chain, deploymentConfig, accounts);
  123. }
  124. async function main() {
  125. const argv = await parser.argv;
  126. const chain = DefaultStore.getChainOrThrow(argv.chain, EvmChain);
  127. const deploymentConfig: DeploymentConfig = {
  128. type: toDeploymentType(argv.deploymentType),
  129. gasMultiplier: argv.gasMultiplier,
  130. gasPriceMultiplier: argv.gasPriceMultiplier,
  131. privateKey: toPrivateKey(argv.privateKey),
  132. jsonOutputDir: argv.stdOutputDir,
  133. saveContract: argv.saveContract,
  134. };
  135. const wormholeContract = await getOrDeployWormholeContract(
  136. chain,
  137. deploymentConfig,
  138. CACHE_FILE
  139. );
  140. await topupEntropyAccountsIfNecessary(chain, deploymentConfig);
  141. console.log(
  142. `Deployment config: ${JSON.stringify(deploymentConfig, null, 2)}\n`
  143. );
  144. console.log(`Deploying entropy contracts on ${chain.getId()}...`);
  145. const executorAddr = await deployExecutorContracts(
  146. chain,
  147. deploymentConfig,
  148. wormholeContract.address
  149. );
  150. const entropyAddr = await deployEntropyContracts(
  151. chain,
  152. deploymentConfig,
  153. executorAddr
  154. );
  155. if (deploymentConfig.saveContract) {
  156. console.log("Saving the contract in the store...");
  157. const contract = new EvmEntropyContract(chain, entropyAddr);
  158. DefaultStore.entropy_contracts[contract.getId()] = contract;
  159. DefaultStore.saveAllContracts();
  160. }
  161. console.log(
  162. `✅ Deployed entropy contracts on ${chain.getId()} at ${entropyAddr}\n\n`
  163. );
  164. }
  165. main();