deploy_evm_entropy_contracts.ts 4.0 KB

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