batchDeployReceivers.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. 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(
  59. argv["contract"] + "/ReceiverImplementation.json",
  60. );
  61. const receiverInfo = require(argv["contract"] + "/WormholeReceiver.json");
  62. const payloads: Buffer[] = [];
  63. for (const chain of Object.values(DefaultStore.chains)) {
  64. if (
  65. chain instanceof EvmChain &&
  66. chain.isMainnet() === (network === "mainnet")
  67. ) {
  68. if (chain.wormholeChainName === "zksync") continue; // deploy zksync receiver separately
  69. const {
  70. wormholeGovernanceChainId,
  71. wormholeGovernanceContract,
  72. wormholeInitialSigners,
  73. } = getDefaultConfig(chain.getId());
  74. console.log(chain.getId());
  75. const address = await memoize(chain.getId(), async () => {
  76. const setupAddress = await chain.deploy(
  77. privateKey,
  78. setupInfo.abi,
  79. setupInfo.bytecode,
  80. [],
  81. );
  82. console.log("setupAddress", setupAddress);
  83. const implementationAddress = await chain.deploy(
  84. privateKey,
  85. implementationInfo.abi,
  86. implementationInfo.bytecode,
  87. [],
  88. );
  89. console.log("implementationAddress", implementationAddress);
  90. const web3 = new Web3();
  91. const setup = new web3.eth.Contract(setupInfo.abi, setupAddress);
  92. const initData = setup.methods
  93. .setup(
  94. implementationAddress,
  95. wormholeInitialSigners,
  96. CHAINS[chain.wormholeChainName],
  97. wormholeGovernanceChainId,
  98. wormholeGovernanceContract,
  99. )
  100. .encodeABI();
  101. // deploy proxy
  102. const receiverAddress = await chain.deploy(
  103. privateKey,
  104. receiverInfo.abi,
  105. receiverInfo.bytecode,
  106. [setupAddress, initData],
  107. );
  108. const contract = new EvmWormholeContract(chain, receiverAddress);
  109. console.log("receiverAddress", receiverAddress);
  110. await contract.syncMainnetGuardianSets(privateKey);
  111. console.log("synced");
  112. return contract.address;
  113. });
  114. const payload = chain.generateGovernanceSetWormholeAddressPayload(
  115. address.replace("0x", ""),
  116. );
  117. payloads.push(payload);
  118. }
  119. }
  120. let vaultName;
  121. if (network === "mainnet") {
  122. vaultName = "mainnet-beta_FVQyHcooAtThJ83XFrNnv74BcinbRH3bRmfFamAHBfuj";
  123. } else {
  124. vaultName = "devnet_6baWtW1zTUVMSJHJQVxDUXWzqrQeYBr6mu31j3bTKwY3";
  125. }
  126. const vault = DefaultStore.vaults[vaultName];
  127. vault.connect(await loadHotWallet(argv["ops-wallet"]));
  128. await vault.proposeWormholeMessage(payloads);
  129. }
  130. main();