batchDeployReceivers.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * This script deploys the receiver contracts on all the chains and creates a governance proposal to update the
  3. * wormhole addresses to the deployed receiver contracts.
  4. */
  5. import yargs from "yargs";
  6. import { hideBin } from "yargs/helpers";
  7. import {
  8. DefaultStore,
  9. EvmChain,
  10. loadHotWallet,
  11. WormholeEvmContract,
  12. } from "contract_manager";
  13. import Web3 from "web3";
  14. import { CHAINS } from "xc_admin_common";
  15. import * as fs from "fs";
  16. const { getDefaultConfig } = require("./contractManagerConfig");
  17. const parser = yargs(hideBin(process.argv))
  18. .usage(
  19. "Usage: $0 --contracts <path-to-contract-json-folder> --network <contract_id> --private-key <private-key> --ops-wallet <ops-wallet>"
  20. )
  21. .options({
  22. contract: {
  23. type: "string",
  24. demandOption: true,
  25. desc: "Path to the contract json file containing abi and bytecode",
  26. },
  27. network: {
  28. type: "string",
  29. demandOption: true,
  30. choices: ["testnet", "mainnet"],
  31. desc: "The network to deploy the contract on",
  32. },
  33. "private-key": {
  34. type: "string",
  35. demandOption: true,
  36. desc: "Private key to sign the transactions. Hex format, without 0x prefix.",
  37. },
  38. "ops-wallet": {
  39. type: "string",
  40. demandOption: true,
  41. desc: "Path to operations wallet json file",
  42. },
  43. });
  44. async function memoize(key: string, fn: () => Promise<any>) {
  45. const path = `./cache/${key}.json`;
  46. if (fs.existsSync(path)) {
  47. return JSON.parse(fs.readFileSync(path).toString());
  48. }
  49. const result = await fn();
  50. fs.writeFileSync(path, JSON.stringify(result));
  51. return result;
  52. }
  53. async function main() {
  54. const argv = await parser.argv;
  55. const privateKey = argv["private-key"];
  56. const network = argv["network"];
  57. const setupInfo = require(argv["contract"] + "/ReceiverSetup.json");
  58. const implementationInfo = require(argv["contract"] +
  59. "/ReceiverImplementation.json");
  60. const receiverInfo = require(argv["contract"] + "/WormholeReceiver.json");
  61. const payloads: Buffer[] = [];
  62. for (const chain of Object.values(DefaultStore.chains)) {
  63. if (
  64. chain instanceof EvmChain &&
  65. chain.isMainnet() === (network === "mainnet")
  66. ) {
  67. if (chain.wormholeChainName === "zksync") continue; // deploy zksync receiver separately
  68. const {
  69. wormholeGovernanceChainId,
  70. wormholeGovernanceContract,
  71. wormholeInitialSigners,
  72. } = getDefaultConfig(chain.getId());
  73. console.log(chain.getId());
  74. const address = await memoize(chain.getId(), async () => {
  75. const setupAddress = await chain.deploy(
  76. privateKey,
  77. setupInfo.abi,
  78. setupInfo.bytecode,
  79. []
  80. );
  81. console.log("setupAddress", setupAddress);
  82. const implementationAddress = await chain.deploy(
  83. privateKey,
  84. implementationInfo.abi,
  85. implementationInfo.bytecode,
  86. []
  87. );
  88. console.log("implementationAddress", implementationAddress);
  89. const web3 = new Web3();
  90. const setup = new web3.eth.Contract(setupInfo.abi, setupAddress);
  91. const initData = setup.methods
  92. .setup(
  93. implementationAddress,
  94. wormholeInitialSigners,
  95. CHAINS[chain.wormholeChainName],
  96. wormholeGovernanceChainId,
  97. wormholeGovernanceContract
  98. )
  99. .encodeABI();
  100. // deploy proxy
  101. const receiverAddress = await chain.deploy(
  102. privateKey,
  103. receiverInfo.abi,
  104. receiverInfo.bytecode,
  105. [setupAddress, initData]
  106. );
  107. const contract = new WormholeEvmContract(chain, receiverAddress);
  108. console.log("receiverAddress", receiverAddress);
  109. await contract.syncMainnetGuardianSets(privateKey);
  110. console.log("synced");
  111. return contract.address;
  112. });
  113. const payload = chain.generateGovernanceSetWormholeAddressPayload(
  114. address.replace("0x", "")
  115. );
  116. payloads.push(payload);
  117. }
  118. }
  119. let vaultName;
  120. if (network === "mainnet") {
  121. vaultName = "mainnet-beta_FVQyHcooAtThJ83XFrNnv74BcinbRH3bRmfFamAHBfuj";
  122. } else {
  123. vaultName = "devnet_6baWtW1zTUVMSJHJQVxDUXWzqrQeYBr6mu31j3bTKwY3";
  124. }
  125. const vault = DefaultStore.vaults[vaultName];
  126. vault.connect(await loadHotWallet(argv["ops-wallet"]));
  127. await vault.proposeWormholeMessage(payloads);
  128. }
  129. main();