|
| 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 "@pythnetwork/xc-governance-sdk"; |
| 6 | +import { assert } from "chai"; |
| 7 | +import { writeFileSync } from "fs"; |
| 8 | + |
| 9 | +loadEnv("./"); |
| 10 | + |
| 11 | +function envOrErr(name: string): string { |
| 12 | + const res = process.env[name]; |
| 13 | + if (res === undefined) { |
| 14 | + throw new Error(`${name} environment variable is not set.`); |
| 15 | + } |
| 16 | + return res; |
| 17 | +} |
| 18 | + |
| 19 | +export default async function (hre: HardhatRuntimeEnvironment) { |
| 20 | + // Initialize the wallet. |
| 21 | + const wallet = Wallet.fromMnemonic(envOrErr("MNEMONIC")); |
| 22 | + |
| 23 | + // Create deployer object and load the artifact of the contract we want to deploy. |
| 24 | + const deployer = new Deployer(hre, wallet); |
| 25 | + |
| 26 | + // Deposit some funds to L2 in order to be able to perform L2 transactions. Uncomment |
| 27 | + // this if the deployment account is unfunded. |
| 28 | + // |
| 29 | + // const depositAmount = ethers.utils.parseEther("0.005"); |
| 30 | + // const depositHandle = await deployer.zkWallet.deposit({ |
| 31 | + // to: deployer.zkWallet.address, |
| 32 | + // token: utils.ETH_ADDRESS, |
| 33 | + // amount: depositAmount, |
| 34 | + // }); |
| 35 | + // // Wait until the deposit is processed on zkSync |
| 36 | + // await depositHandle.wait(); |
| 37 | + |
| 38 | + // Deploy WormholeReceiver contract. |
| 39 | + const initialSigners = JSON.parse(envOrErr("INIT_SIGNERS")); |
| 40 | + const whGovernanceChainId = envOrErr("INIT_GOV_CHAIN_ID"); |
| 41 | + const whGovernanceContract = envOrErr("INIT_GOV_CONTRACT"); // bytes32 |
| 42 | + |
| 43 | + const chainName = envOrErr("WORMHOLE_CHAIN_NAME"); |
| 44 | + const wormholeReceiverChainId = CHAINS[chainName]; |
| 45 | + assert(wormholeReceiverChainId !== undefined); |
| 46 | + |
| 47 | + const receiverSetupArtifact = await deployer.loadArtifact("ReceiverSetup"); |
| 48 | + const receiverImplArtifact = await deployer.loadArtifact( |
| 49 | + "ReceiverImplementation" |
| 50 | + ); |
| 51 | + const wormholeReceiverArtifact = await deployer.loadArtifact( |
| 52 | + "WormholeReceiver" |
| 53 | + ); |
| 54 | + |
| 55 | + const receiverSetupContract = await deployer.deploy(receiverSetupArtifact); |
| 56 | + |
| 57 | + // deploy implementation |
| 58 | + const receiverImplContract = await deployer.deploy(receiverImplArtifact); |
| 59 | + |
| 60 | + // encode initialisation data |
| 61 | + const whInitData = receiverSetupContract.interface.encodeFunctionData( |
| 62 | + "setup", |
| 63 | + [ |
| 64 | + receiverImplContract.address, |
| 65 | + initialSigners, |
| 66 | + wormholeReceiverChainId, |
| 67 | + whGovernanceChainId, |
| 68 | + whGovernanceContract, |
| 69 | + ] |
| 70 | + ); |
| 71 | + |
| 72 | + // deploy proxy |
| 73 | + const wormholeReceiverContract = await deployer.deploy( |
| 74 | + wormholeReceiverArtifact, |
| 75 | + [receiverSetupContract.address, whInitData] |
| 76 | + ); |
| 77 | + |
| 78 | + console.log( |
| 79 | + `Deployed WormholeReceiver on ${wormholeReceiverContract.address}` |
| 80 | + ); |
| 81 | + |
| 82 | + // Deploy Pyth contract. |
| 83 | + const emitterChainIds = [ |
| 84 | + envOrErr("SOLANA_CHAIN_ID"), |
| 85 | + envOrErr("PYTHNET_CHAIN_ID"), |
| 86 | + ]; |
| 87 | + const emitterAddresses = [ |
| 88 | + envOrErr("SOLANA_EMITTER"), |
| 89 | + envOrErr("PYTHNET_EMITTER"), |
| 90 | + ]; |
| 91 | + const governanceChainId = envOrErr("GOVERNANCE_CHAIN_ID"); |
| 92 | + const governanceEmitter = envOrErr("GOVERNANCE_EMITTER"); |
| 93 | + // Default value for this field is 0 |
| 94 | + const governanceInitialSequence = Number( |
| 95 | + process.env.GOVERNANCE_INITIAL_SEQUENCE ?? "0" |
| 96 | + ); |
| 97 | + |
| 98 | + const validTimePeriodSeconds = Number(envOrErr("VALID_TIME_PERIOD_SECONDS")); |
| 99 | + const singleUpdateFeeInWei = Number(envOrErr("SINGLE_UPDATE_FEE_IN_WEI")); |
| 100 | + |
| 101 | + const pythImplArtifact = await deployer.loadArtifact("PythUpgradable"); |
| 102 | + const pythProxyArtifact = await deployer.loadArtifact("ERC1967Proxy"); |
| 103 | + |
| 104 | + const pythImplContract = await deployer.deploy(pythImplArtifact); |
| 105 | + |
| 106 | + const pythInitData = pythImplContract.interface.encodeFunctionData( |
| 107 | + "initialize", |
| 108 | + [ |
| 109 | + wormholeReceiverContract.address, |
| 110 | + emitterChainIds, |
| 111 | + emitterAddresses, |
| 112 | + governanceChainId, |
| 113 | + governanceEmitter, |
| 114 | + governanceInitialSequence, |
| 115 | + validTimePeriodSeconds, |
| 116 | + singleUpdateFeeInWei, |
| 117 | + ] |
| 118 | + ); |
| 119 | + |
| 120 | + const pythProxyContract = await deployer.deploy(pythProxyArtifact, [ |
| 121 | + pythImplContract.address, |
| 122 | + pythInitData, |
| 123 | + ]); |
| 124 | + |
| 125 | + console.log(`Deployed Pyth contract on ${pythProxyContract.address}`); |
| 126 | + |
| 127 | + const networkId = hre.network.config.chainId; |
| 128 | + const registryPath = `networks/${networkId}.json`; |
| 129 | + console.log(`Saving addresses in ${registryPath}`); |
| 130 | + writeFileSync( |
| 131 | + registryPath, |
| 132 | + JSON.stringify( |
| 133 | + [ |
| 134 | + { |
| 135 | + contractName: "WormholeReceiver", |
| 136 | + address: wormholeReceiverContract.address, |
| 137 | + }, |
| 138 | + { |
| 139 | + contractName: "PythUpgradable", |
| 140 | + address: pythProxyContract.address, |
| 141 | + }, |
| 142 | + ], |
| 143 | + null, |
| 144 | + 2 |
| 145 | + ) |
| 146 | + ); |
| 147 | +} |
0 commit comments