|
| 1 | +import yargs from "yargs"; |
| 2 | +import { hideBin } from "yargs/helpers"; |
| 3 | +import { DefaultStore, EvmChain, loadHotWallet, toPrivateKey } from "../src"; |
| 4 | +import { existsSync, readFileSync, writeFileSync } from "fs"; |
| 5 | + |
| 6 | +const CACHE_FILE = ".cache-upgrade-evm"; |
| 7 | + |
| 8 | +const parser = yargs(hideBin(process.argv)) |
| 9 | + .usage( |
| 10 | + "Deploys a new PythUpgradable contract to a set of chains and creates a governance proposal for it.\n" + |
| 11 | + `Uses a cache file (${CACHE_FILE}) to avoid deploying contracts twice\n` + |
| 12 | + "Usage: $0 --chain <chain_1> --chain <chain_2> --private-key <private_key> --ops-key-path <ops_key_path> --std-output <std_output>" |
| 13 | + ) |
| 14 | + .options({ |
| 15 | + testnet: { |
| 16 | + type: "boolean", |
| 17 | + default: false, |
| 18 | + desc: "Upgrade testnet contracts instead of mainnet", |
| 19 | + }, |
| 20 | + "all-chains": { |
| 21 | + type: "boolean", |
| 22 | + default: false, |
| 23 | + desc: "Upgrade the contract on all chains. Use with --testnet flag to upgrade all testnet contracts", |
| 24 | + }, |
| 25 | + chain: { |
| 26 | + type: "array", |
| 27 | + string: true, |
| 28 | + desc: "Chains to upgrade the contract on", |
| 29 | + }, |
| 30 | + "private-key": { |
| 31 | + type: "string", |
| 32 | + demandOption: true, |
| 33 | + desc: "Private key to use for the deployment", |
| 34 | + }, |
| 35 | + "ops-key-path": { |
| 36 | + type: "string", |
| 37 | + demandOption: true, |
| 38 | + desc: "Path to the private key of the proposer to use for the operations multisig governance proposal", |
| 39 | + }, |
| 40 | + "std-output": { |
| 41 | + type: "string", |
| 42 | + demandOption: true, |
| 43 | + desc: "Path to the standard JSON output of the pyth contract (build artifact)", |
| 44 | + }, |
| 45 | + }); |
| 46 | + |
| 47 | +async function run_if_not_cached( |
| 48 | + cache_key: string, |
| 49 | + fn: () => Promise<string> |
| 50 | +): Promise<string> { |
| 51 | + const cache = existsSync(CACHE_FILE) |
| 52 | + ? JSON.parse(readFileSync(CACHE_FILE, "utf8")) |
| 53 | + : {}; |
| 54 | + if (cache[cache_key]) { |
| 55 | + return cache[cache_key]; |
| 56 | + } |
| 57 | + const result = await fn(); |
| 58 | + cache[cache_key] = result; |
| 59 | + writeFileSync(CACHE_FILE, JSON.stringify(cache, null, 2)); |
| 60 | + return result; |
| 61 | +} |
| 62 | + |
| 63 | +async function main() { |
| 64 | + const argv = await parser.argv; |
| 65 | + const selectedChains: EvmChain[] = []; |
| 66 | + |
| 67 | + if (argv.allChains && argv.chain) |
| 68 | + throw new Error("Cannot use both --all-chains and --chain"); |
| 69 | + if (!argv.allChains && !argv.chain) |
| 70 | + throw new Error("Must use either --all-chains or --chain"); |
| 71 | + for (const chain of Object.values(DefaultStore.chains)) { |
| 72 | + if (!(chain instanceof EvmChain)) continue; |
| 73 | + if ( |
| 74 | + (argv.allChains && chain.isMainnet() !== argv.testnet) || |
| 75 | + argv.chain?.includes(chain.getId()) |
| 76 | + ) |
| 77 | + selectedChains.push(chain); |
| 78 | + } |
| 79 | + if (argv.chain && selectedChains.length !== argv.chain.length) |
| 80 | + throw new Error( |
| 81 | + `Some chains were not found ${selectedChains |
| 82 | + .map((chain) => chain.getId()) |
| 83 | + .toString()}` |
| 84 | + ); |
| 85 | + for (const chain of selectedChains) { |
| 86 | + if (chain.isMainnet() != selectedChains[0].isMainnet()) |
| 87 | + throw new Error("All chains must be either mainnet or testnet"); |
| 88 | + } |
| 89 | + |
| 90 | + const vault = |
| 91 | + DefaultStore.vaults[ |
| 92 | + "mainnet-beta_FVQyHcooAtThJ83XFrNnv74BcinbRH3bRmfFamAHBfuj" |
| 93 | + ]; |
| 94 | + |
| 95 | + console.log("Using cache file", CACHE_FILE); |
| 96 | + console.log( |
| 97 | + "Upgrading on chains", |
| 98 | + selectedChains.map((c) => c.getId()) |
| 99 | + ); |
| 100 | + |
| 101 | + const payloads: Buffer[] = []; |
| 102 | + for (const chain of selectedChains) { |
| 103 | + const artifact = JSON.parse(readFileSync(argv["std-output"], "utf8")); |
| 104 | + console.log("Deploying contract to", chain.getId()); |
| 105 | + const address = await run_if_not_cached(`deploy-${chain.getId()}`, () => { |
| 106 | + return chain.deploy( |
| 107 | + toPrivateKey(argv["private-key"]), |
| 108 | + artifact["abi"], |
| 109 | + artifact["bytecode"], |
| 110 | + [] |
| 111 | + ); |
| 112 | + }); |
| 113 | + console.log(`Deployed contract at ${address} on ${chain.getId()}`); |
| 114 | + payloads.push( |
| 115 | + chain.generateGovernanceUpgradePayload(address.replace("0x", "")) |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + console.log("Using vault at for proposal", vault.getId()); |
| 120 | + const wallet = await loadHotWallet(argv["ops-key-path"]); |
| 121 | + console.log("Using wallet ", wallet.publicKey.toBase58()); |
| 122 | + await vault.connect(wallet); |
| 123 | + const proposal = await vault.proposeWormholeMessage(payloads); |
| 124 | + console.log("Proposal address", proposal.address.toBase58()); |
| 125 | +} |
| 126 | + |
| 127 | +main(); |
0 commit comments