|
| 1 | +import { program } from "commander"; |
| 2 | +import { loadContractConfig, ContractType, SyncOp } from "xc_admin_common"; |
| 3 | +import * as fs from "fs"; |
| 4 | + |
| 5 | +// TODO: extract this configuration to a file |
| 6 | +const contractsConfig = [ |
| 7 | + { |
| 8 | + type: ContractType.EvmPythUpgradable, |
| 9 | + networkId: "arbitrum", |
| 10 | + address: "0xff1a0f4744e8582DF1aE09D5611b887B6a12925C", |
| 11 | + }, |
| 12 | + { |
| 13 | + type: ContractType.EvmWormholeReceiver, |
| 14 | + networkId: "canto", |
| 15 | + address: "0x87047526937246727E4869C5f76A347160e08672", |
| 16 | + }, |
| 17 | + { |
| 18 | + type: ContractType.EvmPythUpgradable, |
| 19 | + networkId: "canto", |
| 20 | + address: "0x98046Bd286715D3B0BC227Dd7a956b83D8978603", |
| 21 | + }, |
| 22 | + { |
| 23 | + type: ContractType.EvmPythUpgradable, |
| 24 | + networkId: "avalanche", |
| 25 | + address: "0x4305FB66699C3B2702D4d05CF36551390A4c69C6", |
| 26 | + }, |
| 27 | +]; |
| 28 | + |
| 29 | +const networksConfig = { |
| 30 | + evm: { |
| 31 | + optimism_goerli: { |
| 32 | + url: `https://rpc.ankr.com/optimism_testnet`, |
| 33 | + }, |
| 34 | + arbitrum: { |
| 35 | + url: "https://arb1.arbitrum.io/rpc", |
| 36 | + }, |
| 37 | + avalanche: { |
| 38 | + url: "https://api.avax.network/ext/bc/C/rpc", |
| 39 | + }, |
| 40 | + canto: { |
| 41 | + url: "https://canto.gravitychain.io", |
| 42 | + }, |
| 43 | + }, |
| 44 | +}; |
| 45 | + |
| 46 | +// TODO: we will need configuration of this stuff to decide which multisig to run. |
| 47 | +const multisigs = [ |
| 48 | + { |
| 49 | + name: "", |
| 50 | + wormholeNetwork: "mainnet", |
| 51 | + }, |
| 52 | +]; |
| 53 | + |
| 54 | +program |
| 55 | + .name("pyth_governance") |
| 56 | + .description("CLI for governing Pyth contracts") |
| 57 | + .version("0.1.0"); |
| 58 | + |
| 59 | +program |
| 60 | + .command("get") |
| 61 | + .description("Find Pyth contracts matching the given search criteria") |
| 62 | + .option("-n, --network <network-id>", "Find contracts on the given network") |
| 63 | + .option("-a, --address <address>", "Find contracts with the given address") |
| 64 | + .option("-t, --type <type-id>", "Find contracts of the given type") |
| 65 | + .action(async (options: any) => { |
| 66 | + const contracts = loadContractConfig(contractsConfig, networksConfig); |
| 67 | + |
| 68 | + console.log(JSON.stringify(options)); |
| 69 | + |
| 70 | + const matches = []; |
| 71 | + for (const contract of contracts) { |
| 72 | + if ( |
| 73 | + (options.network === undefined || |
| 74 | + contract.networkId == options.network) && |
| 75 | + (options.address === undefined || |
| 76 | + contract.getAddress() == options.address) && |
| 77 | + (options.type === undefined || contract.type == options.type) |
| 78 | + ) { |
| 79 | + matches.push(contract); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + for (const contract of matches) { |
| 84 | + const state = await contract.getState(); |
| 85 | + console.log({ |
| 86 | + networkId: contract.networkId, |
| 87 | + address: contract.getAddress(), |
| 88 | + type: contract.type, |
| 89 | + state: state, |
| 90 | + }); |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | +class Cache { |
| 95 | + private path: string; |
| 96 | + |
| 97 | + constructor(path: string) { |
| 98 | + this.path = path; |
| 99 | + } |
| 100 | + |
| 101 | + private opFilePath(op: SyncOp): string { |
| 102 | + return `${this.path}/${op.id()}.json`; |
| 103 | + } |
| 104 | + |
| 105 | + public readOpCache(op: SyncOp): Record<string, any> { |
| 106 | + const path = this.opFilePath(op); |
| 107 | + if (fs.existsSync(path)) { |
| 108 | + return JSON.parse(fs.readFileSync(path).toString("utf-8")); |
| 109 | + } else { |
| 110 | + return {}; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + public writeOpCache(op: SyncOp, cache: Record<string, any>) { |
| 115 | + fs.writeFileSync(this.opFilePath(op), JSON.stringify(cache)); |
| 116 | + } |
| 117 | + |
| 118 | + public deleteCache(op: SyncOp) { |
| 119 | + fs.rmSync(this.opFilePath(op)); |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +program |
| 124 | + .command("set") |
| 125 | + .description("Set a configuration parameter for one or more Pyth contracts") |
| 126 | + .option("-n, --network <network-id>", "Find contracts on the given network") |
| 127 | + .option("-a, --address <address>", "Find contracts with the given address") |
| 128 | + .option("-t, --type <type-id>", "Find contracts of the given type") |
| 129 | + .argument("<fields...>", "Fields to set on the given contracts") |
| 130 | + .action(async (fields, options: any, command) => { |
| 131 | + const contracts = loadContractConfig(contractsConfig, networksConfig); |
| 132 | + |
| 133 | + console.log(JSON.stringify(fields)); |
| 134 | + console.log(JSON.stringify(options)); |
| 135 | + |
| 136 | + const setters = fields.map((value: string) => value.split("=")); |
| 137 | + |
| 138 | + const matches = []; |
| 139 | + for (const contract of contracts) { |
| 140 | + if ( |
| 141 | + (options.network === undefined || |
| 142 | + contract.networkId == options.network) && |
| 143 | + (options.address === undefined || |
| 144 | + contract.getAddress() == options.address) && |
| 145 | + (options.type === undefined || contract.type == options.type) |
| 146 | + ) { |
| 147 | + matches.push(contract); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + const ops = []; |
| 152 | + for (const contract of matches) { |
| 153 | + const state = await contract.getState(); |
| 154 | + // TODO: make a decent format for this |
| 155 | + for (const [field, value] of setters) { |
| 156 | + state[field] = value; |
| 157 | + } |
| 158 | + |
| 159 | + ops.push(...(await contract.sync(state))); |
| 160 | + } |
| 161 | + |
| 162 | + // TODO: extract constant |
| 163 | + const cacheDir = "cache"; |
| 164 | + fs.mkdirSync(cacheDir, { recursive: true }); |
| 165 | + const cache = new Cache(cacheDir); |
| 166 | + |
| 167 | + for (const op of ops) { |
| 168 | + const opCache = cache.readOpCache(op); |
| 169 | + const isDone = await op.run(opCache); |
| 170 | + if (isDone) { |
| 171 | + cache.deleteCache(op); |
| 172 | + } else { |
| 173 | + cache.writeOpCache(op, opCache); |
| 174 | + } |
| 175 | + } |
| 176 | + }); |
| 177 | + |
| 178 | +program.parse(); |
0 commit comments