|
| 1 | +import yargs from "yargs"; |
| 2 | +import { hideBin } from "yargs/helpers"; |
| 3 | +import { EvmChain } from "../src/chains"; |
| 4 | +import { DefaultStore } from "../src/store"; |
| 5 | +import { |
| 6 | + DeploymentType, |
| 7 | + toDeploymentType, |
| 8 | + toPrivateKey, |
| 9 | + EvmPulseContract, |
| 10 | + PULSE_DEFAULT_PROVIDER, |
| 11 | + PULSE_DEFAULT_KEEPER, |
| 12 | +} from "../src"; |
| 13 | +import { |
| 14 | + COMMON_DEPLOY_OPTIONS, |
| 15 | + deployIfNotCached, |
| 16 | + getWeb3Contract, |
| 17 | + getOrDeployWormholeContract, |
| 18 | + BaseDeployConfig, |
| 19 | + topupAccountsIfNecessary, |
| 20 | + DefaultAddresses, |
| 21 | +} from "./common"; |
| 22 | +import fs from "fs"; |
| 23 | +import path from "path"; |
| 24 | + |
| 25 | +interface DeploymentConfig extends BaseDeployConfig { |
| 26 | + type: DeploymentType; |
| 27 | + saveContract: boolean; |
| 28 | +} |
| 29 | + |
| 30 | +const CACHE_FILE = ".cache-deploy-evm-pulse-contracts"; |
| 31 | + |
| 32 | +const parser = yargs(hideBin(process.argv)) |
| 33 | + .scriptName("deploy_evm_pulse_contracts.ts") |
| 34 | + .usage( |
| 35 | + "Usage: $0 --std-output-dir <path/to/std-output-dir/> --private-key <private-key> --chain <chain> --default-provider <default-provider> --wormhole-addr <wormhole-addr>" |
| 36 | + ) |
| 37 | + .options({ |
| 38 | + ...COMMON_DEPLOY_OPTIONS, |
| 39 | + chain: { |
| 40 | + type: "string", |
| 41 | + demandOption: true, |
| 42 | + desc: "Chain to upload the contract on. Can be one of the evm chains available in the store", |
| 43 | + }, |
| 44 | + }); |
| 45 | + |
| 46 | +async function deployPulseContracts( |
| 47 | + chain: EvmChain, |
| 48 | + config: DeploymentConfig, |
| 49 | + executorAddr: string |
| 50 | +): Promise<string> { |
| 51 | + console.log("Deploying PulseUpgradeable on", chain.getId(), "..."); |
| 52 | + |
| 53 | + // Get the artifact and ensure bytecode is properly formatted |
| 54 | + const pulseArtifact = JSON.parse( |
| 55 | + fs.readFileSync( |
| 56 | + path.join(config.jsonOutputDir, "PulseUpgradeable.json"), |
| 57 | + "utf8" |
| 58 | + ) |
| 59 | + ); |
| 60 | + console.log("PulseArtifact bytecode type:", typeof pulseArtifact.bytecode); |
| 61 | + |
| 62 | + const pulseImplAddr = await deployIfNotCached( |
| 63 | + CACHE_FILE, |
| 64 | + chain, |
| 65 | + config, |
| 66 | + "PulseUpgradeable", |
| 67 | + [] |
| 68 | + ); |
| 69 | + |
| 70 | + console.log("PulseUpgradeable implementation deployed at:", pulseImplAddr); |
| 71 | + |
| 72 | + const pulseImplContract = getWeb3Contract( |
| 73 | + config.jsonOutputDir, |
| 74 | + "PulseUpgradeable", |
| 75 | + pulseImplAddr |
| 76 | + ); |
| 77 | + |
| 78 | + console.log("Preparing initialization data..."); |
| 79 | + |
| 80 | + const pulseInitData = pulseImplContract.methods |
| 81 | + .initialize( |
| 82 | + executorAddr, // owner |
| 83 | + executorAddr, // admin |
| 84 | + "1", // pythFeeInWei |
| 85 | + executorAddr, // pythAddress - using executor as a placeholder |
| 86 | + chain.isMainnet() |
| 87 | + ? PULSE_DEFAULT_PROVIDER.mainnet |
| 88 | + : PULSE_DEFAULT_PROVIDER.testnet, |
| 89 | + true, // prefillRequestStorage |
| 90 | + 3600 // exclusivityPeriodSeconds - 1 hour |
| 91 | + ) |
| 92 | + .encodeABI(); |
| 93 | + |
| 94 | + console.log("Deploying ERC1967Proxy for Pulse..."); |
| 95 | + |
| 96 | + return await deployIfNotCached( |
| 97 | + CACHE_FILE, |
| 98 | + chain, |
| 99 | + config, |
| 100 | + "ERC1967Proxy", |
| 101 | + [pulseImplAddr, pulseInitData], |
| 102 | + // NOTE: we are deploying a ERC1967Proxy when deploying executor |
| 103 | + // we need to provide a different cache key. As the `artifactname` |
| 104 | + // is same in both case which means the cache key will be same |
| 105 | + `${chain.getId()}-ERC1967Proxy-PULSE1` |
| 106 | + ); |
| 107 | +} |
| 108 | + |
| 109 | +async function topupPulseAccountsIfNecessary( |
| 110 | + chain: EvmChain, |
| 111 | + deploymentConfig: DeploymentConfig |
| 112 | +) { |
| 113 | + const accounts: Array<[string, DefaultAddresses]> = [ |
| 114 | + ["keeper", PULSE_DEFAULT_KEEPER], |
| 115 | + ["provider", PULSE_DEFAULT_PROVIDER], |
| 116 | + ]; |
| 117 | + |
| 118 | + await topupAccountsIfNecessary(chain, deploymentConfig, accounts); |
| 119 | +} |
| 120 | + |
| 121 | +async function main() { |
| 122 | + const argv = await parser.argv; |
| 123 | + |
| 124 | + const chainName = argv.chain; |
| 125 | + const chain = DefaultStore.chains[chainName]; |
| 126 | + if (!chain) { |
| 127 | + throw new Error(`Chain ${chainName} not found`); |
| 128 | + } else if (!(chain instanceof EvmChain)) { |
| 129 | + throw new Error(`Chain ${chainName} is not an EVM chain`); |
| 130 | + } |
| 131 | + |
| 132 | + const deploymentConfig: DeploymentConfig = { |
| 133 | + type: toDeploymentType(argv.deploymentType), |
| 134 | + gasMultiplier: argv.gasMultiplier, |
| 135 | + gasPriceMultiplier: argv.gasPriceMultiplier, |
| 136 | + privateKey: toPrivateKey(argv.privateKey), |
| 137 | + jsonOutputDir: argv.stdOutputDir, |
| 138 | + saveContract: argv.saveContract, |
| 139 | + }; |
| 140 | + |
| 141 | + const wormholeContract = await getOrDeployWormholeContract( |
| 142 | + chain, |
| 143 | + deploymentConfig, |
| 144 | + CACHE_FILE |
| 145 | + ); |
| 146 | + |
| 147 | + await topupPulseAccountsIfNecessary(chain, deploymentConfig); |
| 148 | + |
| 149 | + console.log( |
| 150 | + `Deployment config: ${JSON.stringify(deploymentConfig, null, 2)}\n` |
| 151 | + ); |
| 152 | + |
| 153 | + console.log(`Deploying pulse contracts on ${chain.getId()}...`); |
| 154 | + |
| 155 | + const executorAddr = wormholeContract.address; // Using wormhole contract as executor for Pulse |
| 156 | + const pulseAddr = await deployPulseContracts( |
| 157 | + chain, |
| 158 | + deploymentConfig, |
| 159 | + executorAddr |
| 160 | + ); |
| 161 | + |
| 162 | + if (deploymentConfig.saveContract) { |
| 163 | + console.log("Saving the contract in the store..."); |
| 164 | + const contract = new EvmPulseContract(chain, pulseAddr); |
| 165 | + DefaultStore.pulse_contracts[contract.getId()] = contract; |
| 166 | + DefaultStore.saveAllContracts(); |
| 167 | + } |
| 168 | + |
| 169 | + console.log( |
| 170 | + `✅ Deployed pulse contracts on ${chain.getId()} at ${pulseAddr}\n\n` |
| 171 | + ); |
| 172 | +} |
| 173 | + |
| 174 | +main(); |
0 commit comments