|
| 1 | +import { HardhatRuntimeEnvironment } from "hardhat/types"; |
| 2 | +import { ethers } from "hardhat"; |
| 3 | +import loadEnv from "../scripts/loadEnv"; |
| 4 | +import { CHAINS } from "@pythnetwork/xc-governance-sdk"; |
| 5 | +import { assert } from "chai"; |
| 6 | +import { writeFileSync } from "fs"; |
| 7 | + |
| 8 | +loadEnv("./"); |
| 9 | + |
| 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 | + |
| 18 | +async function main() { |
| 19 | + // Deploy WormholeReceiver contract. |
| 20 | + const initialSigners = JSON.parse(envOrErr("INIT_SIGNERS")); |
| 21 | + const whGovernanceChainId = envOrErr("INIT_GOV_CHAIN_ID"); |
| 22 | + const whGovernanceContract = envOrErr("INIT_GOV_CONTRACT"); // bytes32 |
| 23 | + |
| 24 | + const chainName = envOrErr("WORMHOLE_CHAIN_NAME"); |
| 25 | + const wormholeReceiverChainId = CHAINS[chainName]; |
| 26 | + assert(wormholeReceiverChainId !== undefined); |
| 27 | + |
| 28 | + const receiverSetupArtifact = await ethers.getContractFactory( |
| 29 | + "ReceiverSetup" |
| 30 | + ); |
| 31 | + const receiverImplArtifact = await ethers.getContractFactory( |
| 32 | + "ReceiverImplementation" |
| 33 | + ); |
| 34 | + const wormholeReceiverArtifact = await ethers.getContractFactory( |
| 35 | + "WormholeReceiver" |
| 36 | + ); |
| 37 | + |
| 38 | + console.log("BLOCK: ", await ethers.provider.getBlockNumber()); |
| 39 | + |
| 40 | + const receiverSetupContract = await receiverSetupArtifact.deploy(); |
| 41 | + |
| 42 | + // deploy implementation |
| 43 | + const receiverImplContract = await receiverImplArtifact.deploy(); |
| 44 | + |
| 45 | + // encode initialisation data |
| 46 | + const whInitData = receiverSetupContract.interface.encodeFunctionData( |
| 47 | + "setup", |
| 48 | + [ |
| 49 | + receiverImplContract.address, |
| 50 | + initialSigners, |
| 51 | + wormholeReceiverChainId, |
| 52 | + whGovernanceChainId, |
| 53 | + whGovernanceContract, |
| 54 | + ] |
| 55 | + ); |
| 56 | + |
| 57 | + // deploy proxy |
| 58 | + const wormholeReceiverContract = await wormholeReceiverArtifact.deploy( |
| 59 | + receiverSetupContract.address, |
| 60 | + whInitData |
| 61 | + ); |
| 62 | + |
| 63 | + console.log( |
| 64 | + `Deployed WormholeReceiver on ${wormholeReceiverContract.address}` |
| 65 | + ); |
| 66 | + |
| 67 | + // Deploy Pyth contract. |
| 68 | + const emitterChainIds = [ |
| 69 | + envOrErr("SOLANA_CHAIN_ID"), |
| 70 | + envOrErr("PYTHNET_CHAIN_ID"), |
| 71 | + ]; |
| 72 | + const emitterAddresses = [ |
| 73 | + envOrErr("SOLANA_EMITTER"), |
| 74 | + envOrErr("PYTHNET_EMITTER"), |
| 75 | + ]; |
| 76 | + const governanceChainId = envOrErr("GOVERNANCE_CHAIN_ID"); |
| 77 | + const governanceEmitter = envOrErr("GOVERNANCE_EMITTER"); |
| 78 | + // Default value for this field is 0 |
| 79 | + const governanceInitialSequence = Number( |
| 80 | + process.env.GOVERNANCE_INITIAL_SEQUENCE ?? "0" |
| 81 | + ); |
| 82 | + |
| 83 | + const validTimePeriodSeconds = Number(envOrErr("VALID_TIME_PERIOD_SECONDS")); |
| 84 | + const singleUpdateFeeInWei = Number(envOrErr("SINGLE_UPDATE_FEE_IN_WEI")); |
| 85 | + |
| 86 | + const pythImplArtifact = await ethers.getContractFactory("PythUpgradable"); |
| 87 | + const pythProxyArtifact = await ethers.getContractFactory("ERC1967Proxy"); |
| 88 | + |
| 89 | + const pythImplContract = await pythImplArtifact.deploy(); |
| 90 | + |
| 91 | + const pythInitData = pythImplContract.interface.encodeFunctionData( |
| 92 | + "initialize", |
| 93 | + [ |
| 94 | + wormholeReceiverContract.address, |
| 95 | + emitterChainIds, |
| 96 | + emitterAddresses, |
| 97 | + governanceChainId, |
| 98 | + governanceEmitter, |
| 99 | + governanceInitialSequence, |
| 100 | + validTimePeriodSeconds, |
| 101 | + singleUpdateFeeInWei, |
| 102 | + ] |
| 103 | + ); |
| 104 | + |
| 105 | + const pythProxyContract = await pythProxyArtifact.deploy([ |
| 106 | + pythImplContract.address, |
| 107 | + pythInitData, |
| 108 | + ]); |
| 109 | + |
| 110 | + console.log(`Deployed Pyth contract on ${pythProxyContract.address}`); |
| 111 | + |
| 112 | + const networkId = (await ethers.getDefaultProvider().getNetwork()).chainId; |
| 113 | + const registryPath = `networks/${networkId}.json`; |
| 114 | + console.log(`Saving addresses in ${registryPath}`); |
| 115 | + writeFileSync( |
| 116 | + registryPath, |
| 117 | + JSON.stringify( |
| 118 | + [ |
| 119 | + { |
| 120 | + contractName: "WormholeReceiver", |
| 121 | + address: wormholeReceiverContract.address, |
| 122 | + }, |
| 123 | + { |
| 124 | + contractName: "PythUpgradable", |
| 125 | + address: pythProxyContract.address, |
| 126 | + }, |
| 127 | + ], |
| 128 | + null, |
| 129 | + 2 |
| 130 | + ) |
| 131 | + ); |
| 132 | +} |
| 133 | + |
| 134 | +main(); |
0 commit comments