batchDeployReceivers.ts 4.4 KB

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