|
| 1 | +import yargs from "yargs"; |
| 2 | +import { hideBin } from "yargs/helpers"; |
| 3 | +import { EvmChain } from "../src/core/chains"; |
| 4 | +import { |
| 5 | + BaseDeployConfig, |
| 6 | + COMMON_DEPLOY_OPTIONS, |
| 7 | + deployIfNotCached, |
| 8 | + findExecutorContract, |
| 9 | + getOrDeployWormholeContract, |
| 10 | + getWeb3Contract, |
| 11 | +} from "./common"; |
| 12 | +import { |
| 13 | + DeploymentType, |
| 14 | + getDefaultDeploymentConfig, |
| 15 | + toDeploymentType, |
| 16 | + toPrivateKey, |
| 17 | +} from "../src/core/base"; |
| 18 | +import { DefaultStore } from "../src/node/utils/store"; |
| 19 | +import { EvmExecutorContract } from "../src/core/contracts/evm"; |
| 20 | + |
| 21 | +const CACHE_FILE = ".cache-deploy-evm-executor"; |
| 22 | + |
| 23 | +const parser = yargs(hideBin(process.argv)) |
| 24 | + .scriptName("deploy_evm_executor.ts") |
| 25 | + .usage( |
| 26 | + "Usage: $0 --std-output-dir <path/to/std-output-dir/> --private-key <private-key> --chain <chain>", |
| 27 | + ) |
| 28 | + .options({ |
| 29 | + ...COMMON_DEPLOY_OPTIONS, |
| 30 | + chain: { |
| 31 | + type: "string", |
| 32 | + demandOption: true, |
| 33 | + desc: "Chain to upload the contract on. Can be one of the evm chains available in the store", |
| 34 | + }, |
| 35 | + }); |
| 36 | + |
| 37 | +interface DeploymentConfig extends BaseDeployConfig { |
| 38 | + type: DeploymentType; |
| 39 | + saveContract: boolean; |
| 40 | +} |
| 41 | + |
| 42 | +export async function getOrDeployExecutorContract( |
| 43 | + chain: EvmChain, |
| 44 | + config: DeploymentConfig, |
| 45 | + wormholeAddr: string, |
| 46 | +): Promise<EvmExecutorContract> { |
| 47 | + return ( |
| 48 | + findExecutorContract(chain) ?? |
| 49 | + (await deployExecutorContracts(chain, config, wormholeAddr)) |
| 50 | + ); |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Deploys the executor contracts for a given EVM chain. |
| 55 | + * @param {EvmChain} chain The EVM chain to deploy the executor contracts for. |
| 56 | + * @param {DeploymentConfig} config The deployment configuration. |
| 57 | + * @param {string} wormholeAddr The address of the wormhole contract. |
| 58 | + * @returns {Promise<string>} The address of the deployed executor contract. |
| 59 | + */ |
| 60 | +export async function deployExecutorContracts( |
| 61 | + chain: EvmChain, |
| 62 | + config: DeploymentConfig, |
| 63 | + wormholeAddr: string, |
| 64 | +): Promise<EvmExecutorContract> { |
| 65 | + const executorImplAddr = await deployIfNotCached( |
| 66 | + CACHE_FILE, |
| 67 | + chain, |
| 68 | + config, |
| 69 | + "ExecutorUpgradable", |
| 70 | + [], |
| 71 | + ); |
| 72 | + |
| 73 | + // Craft the init data for the proxy contract |
| 74 | + const { governanceDataSource } = getDefaultDeploymentConfig(config.type); |
| 75 | + |
| 76 | + const executorImplContract = getWeb3Contract( |
| 77 | + config.jsonOutputDir, |
| 78 | + "ExecutorUpgradable", |
| 79 | + executorImplAddr, |
| 80 | + ); |
| 81 | + |
| 82 | + const executorInitData = executorImplContract.methods |
| 83 | + .initialize( |
| 84 | + wormholeAddr, |
| 85 | + 0, // lastExecutedSequence, |
| 86 | + chain.getWormholeChainId(), |
| 87 | + governanceDataSource.emitterChain, |
| 88 | + `0x${governanceDataSource.emitterAddress}`, |
| 89 | + ) |
| 90 | + .encodeABI(); |
| 91 | + |
| 92 | + const executorAddr = await deployIfNotCached( |
| 93 | + CACHE_FILE, |
| 94 | + chain, |
| 95 | + config, |
| 96 | + "ERC1967Proxy", |
| 97 | + [executorImplAddr, executorInitData], |
| 98 | + ); |
| 99 | + |
| 100 | + return new EvmExecutorContract(chain, executorAddr); |
| 101 | +} |
| 102 | + |
| 103 | +export async function main() { |
| 104 | + const argv = await parser.argv; |
| 105 | + |
| 106 | + const chain = DefaultStore.getChainOrThrow(argv.chain, EvmChain); |
| 107 | + |
| 108 | + const deploymentConfig: DeploymentConfig = { |
| 109 | + type: toDeploymentType(argv.deploymentType), |
| 110 | + gasMultiplier: argv.gasMultiplier, |
| 111 | + gasPriceMultiplier: argv.gasPriceMultiplier, |
| 112 | + privateKey: toPrivateKey(argv.privateKey), |
| 113 | + jsonOutputDir: argv.stdOutputDir, |
| 114 | + saveContract: argv.saveContract, |
| 115 | + }; |
| 116 | + |
| 117 | + const wormholeContract = await getOrDeployWormholeContract( |
| 118 | + chain, |
| 119 | + deploymentConfig, |
| 120 | + CACHE_FILE, |
| 121 | + ); |
| 122 | + |
| 123 | + console.log( |
| 124 | + `Deployment config: ${JSON.stringify(deploymentConfig, null, 2)}\n`, |
| 125 | + ); |
| 126 | + |
| 127 | + console.log(`Deploying executor contracts on ${chain.getId()}...`); |
| 128 | + |
| 129 | + const executorContract = await getOrDeployExecutorContract( |
| 130 | + chain, |
| 131 | + deploymentConfig, |
| 132 | + wormholeContract.address, |
| 133 | + ); |
| 134 | + |
| 135 | + if (deploymentConfig.saveContract) { |
| 136 | + console.log("Saving the contract in the store..."); |
| 137 | + DefaultStore.executor_contracts[executorContract.getId()] = |
| 138 | + executorContract; |
| 139 | + DefaultStore.saveAllContracts(); |
| 140 | + } |
| 141 | + |
| 142 | + console.log( |
| 143 | + `✅ Executor contract on ${chain.getId()} at ${executorContract.address}\n\n`, |
| 144 | + ); |
| 145 | +} |
| 146 | + |
| 147 | +main(); |
0 commit comments