batchDeployReceivers.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. EvmWormholeContract,
  12. } from "@pythnetwork/contract-manager";
  13. import Web3 from "web3";
  14. import { CHAINS } from "@pythnetwork/xc-admin-common";
  15. import * as fs from "fs";
  16. import { getDefaultConfig } from "../../target_chains/ethereum/contracts/scripts/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(
  45. key: string,
  46. fn: () => Promise<string>,
  47. ): Promise<string> {
  48. const cacheDir = "../../target_chains/ethereum/contracts/cache";
  49. const path = `${cacheDir}/${key}.json`;
  50. if (fs.existsSync(path)) {
  51. return JSON.parse(fs.readFileSync(path).toString());
  52. }
  53. const result = await fn();
  54. fs.writeFileSync(path, JSON.stringify(result));
  55. return result;
  56. }
  57. async function main() {
  58. const argv = await parser.argv;
  59. const privateKey = argv["private-key"];
  60. const network = argv["network"];
  61. const setupInfo = await import(argv["contract"] + "/ReceiverSetup.json");
  62. const implementationInfo = await import(
  63. argv["contract"] + "/ReceiverImplementation.json"
  64. );
  65. const receiverInfo = await import(
  66. argv["contract"] + "/WormholeReceiver.json"
  67. );
  68. const payloads: Buffer[] = [];
  69. for (const chain of Object.values(DefaultStore.chains)) {
  70. if (
  71. chain instanceof EvmChain &&
  72. chain.isMainnet() === (network === "mainnet")
  73. ) {
  74. if (chain.wormholeChainName === "zksync") continue; // deploy zksync receiver separately
  75. const {
  76. wormholeGovernanceChainId,
  77. wormholeGovernanceContract,
  78. wormholeInitialSigners,
  79. } = getDefaultConfig(chain.getId());
  80. console.log(chain.getId());
  81. const address = await memoize(chain.getId(), async () => {
  82. const setupAddress = await chain.deploy(
  83. privateKey,
  84. setupInfo.abi,
  85. setupInfo.bytecode,
  86. [],
  87. );
  88. console.log("setupAddress", setupAddress);
  89. const implementationAddress = await chain.deploy(
  90. privateKey,
  91. implementationInfo.abi,
  92. implementationInfo.bytecode,
  93. [],
  94. );
  95. console.log("implementationAddress", implementationAddress);
  96. const web3 = new Web3();
  97. const setup = new web3.eth.Contract(setupInfo.abi, setupAddress);
  98. const initData = setup.methods
  99. .setup(
  100. implementationAddress,
  101. wormholeInitialSigners,
  102. CHAINS[chain.wormholeChainName],
  103. wormholeGovernanceChainId,
  104. wormholeGovernanceContract,
  105. )
  106. .encodeABI();
  107. // deploy proxy
  108. const receiverAddress = await chain.deploy(
  109. privateKey,
  110. receiverInfo.abi,
  111. receiverInfo.bytecode,
  112. [setupAddress, initData],
  113. );
  114. const contract = new EvmWormholeContract(chain, receiverAddress);
  115. console.log("receiverAddress", receiverAddress);
  116. await contract.syncMainnetGuardianSets(privateKey);
  117. console.log("synced");
  118. return contract.address;
  119. });
  120. const payload = chain.generateGovernanceSetWormholeAddressPayload(
  121. address.replace("0x", ""),
  122. );
  123. payloads.push(payload);
  124. }
  125. }
  126. let vaultName;
  127. if (network === "mainnet") {
  128. vaultName = "mainnet-beta_FVQyHcooAtThJ83XFrNnv74BcinbRH3bRmfFamAHBfuj";
  129. } else {
  130. vaultName = "devnet_6baWtW1zTUVMSJHJQVxDUXWzqrQeYBr6mu31j3bTKwY3";
  131. }
  132. const vault = DefaultStore.vaults[vaultName];
  133. vault.connect(await loadHotWallet(argv["ops-wallet"]));
  134. await vault.proposeWormholeMessage(payloads);
  135. }
  136. main();