zkSyncDeploy.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { utils, Wallet } from "zksync-web3";
  2. import { HardhatRuntimeEnvironment } from "hardhat/types";
  3. import { Deployer } from "@matterlabs/hardhat-zksync-deploy";
  4. import loadEnv from "../scripts/loadEnv";
  5. import { CHAINS } from "xc_admin_common";
  6. import { assert } from "chai";
  7. import { writeFileSync } from "fs";
  8. const { getDefaultConfig } = require("../scripts/contractManagerConfig");
  9. loadEnv("./");
  10. function envOrErr(name: string): string {
  11. const res = process.env[name];
  12. if (res === undefined) {
  13. throw new Error(`${name} environment variable is not set.`);
  14. }
  15. return res;
  16. }
  17. export default async function (hre: HardhatRuntimeEnvironment) {
  18. // Initialize the wallet.
  19. const wallet = Wallet.fromMnemonic(envOrErr("MNEMONIC"));
  20. // Create deployer object and load the artifact of the contract we want to deploy.
  21. const deployer = new Deployer(hre, wallet);
  22. // Deposit some funds to L2 in order to be able to perform L2 transactions. Uncomment
  23. // this if the deployment account is unfunded.
  24. //
  25. // const depositAmount = ethers.utils.parseEther("0.005");
  26. // const depositHandle = await deployer.zkWallet.deposit({
  27. // to: deployer.zkWallet.address,
  28. // token: utils.ETH_ADDRESS,
  29. // amount: depositAmount,
  30. // });
  31. // // Wait until the deposit is processed on zkSync
  32. // await depositHandle.wait();
  33. // Deploy WormholeReceiver contract.
  34. const {
  35. wormholeGovernanceChainId,
  36. wormholeGovernanceContract,
  37. wormholeInitialSigners,
  38. governanceEmitter,
  39. governanceChainId,
  40. emitterAddresses,
  41. emitterChainIds,
  42. } = getDefaultConfig(envOrErr("MIGRATIONS_NETWORK"));
  43. const chainName = envOrErr("WORMHOLE_CHAIN_NAME");
  44. const wormholeReceiverChainId = CHAINS[chainName];
  45. assert(wormholeReceiverChainId !== undefined);
  46. const receiverSetupArtifact = await deployer.loadArtifact("ReceiverSetup");
  47. const receiverImplArtifact = await deployer.loadArtifact(
  48. "ReceiverImplementation"
  49. );
  50. const wormholeReceiverArtifact = await deployer.loadArtifact(
  51. "WormholeReceiver"
  52. );
  53. const receiverSetupContract = await deployer.deploy(receiverSetupArtifact);
  54. // deploy implementation
  55. const receiverImplContract = await deployer.deploy(receiverImplArtifact);
  56. // encode initialisation data
  57. const whInitData = receiverSetupContract.interface.encodeFunctionData(
  58. "setup",
  59. [
  60. receiverImplContract.address,
  61. wormholeInitialSigners,
  62. wormholeReceiverChainId,
  63. wormholeGovernanceChainId,
  64. wormholeGovernanceContract,
  65. ]
  66. );
  67. // deploy proxy
  68. const wormholeReceiverContract = await deployer.deploy(
  69. wormholeReceiverArtifact,
  70. [receiverSetupContract.address, whInitData]
  71. );
  72. console.log(
  73. `Deployed WormholeReceiver on ${wormholeReceiverContract.address}`
  74. );
  75. const governanceInitialSequence = Number(
  76. process.env.GOVERNANCE_INITIAL_SEQUENCE ?? "0"
  77. );
  78. const validTimePeriodSeconds = Number(envOrErr("VALID_TIME_PERIOD_SECONDS"));
  79. const singleUpdateFeeInWei = Number(envOrErr("SINGLE_UPDATE_FEE_IN_WEI"));
  80. const pythImplArtifact = await deployer.loadArtifact("PythUpgradable");
  81. const pythProxyArtifact = await deployer.loadArtifact("ERC1967Proxy");
  82. const pythImplContract = await deployer.deploy(pythImplArtifact);
  83. const pythInitData = pythImplContract.interface.encodeFunctionData(
  84. "initialize",
  85. [
  86. wormholeReceiverContract.address,
  87. emitterChainIds,
  88. emitterAddresses,
  89. governanceChainId,
  90. governanceEmitter,
  91. governanceInitialSequence,
  92. validTimePeriodSeconds,
  93. singleUpdateFeeInWei,
  94. ]
  95. );
  96. const pythProxyContract = await deployer.deploy(pythProxyArtifact, [
  97. pythImplContract.address,
  98. pythInitData,
  99. ]);
  100. console.log(`Deployed Pyth contract on ${pythProxyContract.address}`);
  101. const networkId = hre.network.config.chainId;
  102. const registryPath = `networks/${networkId}.json`;
  103. console.log(`Saving addresses in ${registryPath}`);
  104. writeFileSync(
  105. registryPath,
  106. JSON.stringify(
  107. [
  108. {
  109. contractName: "WormholeReceiver",
  110. address: wormholeReceiverContract.address,
  111. },
  112. {
  113. contractName: "PythUpgradable",
  114. address: pythProxyContract.address,
  115. },
  116. ],
  117. null,
  118. 2
  119. )
  120. );
  121. }