|
| 1 | +import yargs from "yargs"; |
| 2 | +import { hideBin } from "yargs/helpers"; |
| 3 | +import { DefaultStore, loadHotWallet } from "../src"; |
| 4 | +import { TonChain } from "../src/chains"; |
| 5 | +import { CHAINS, toChainName } from "@pythnetwork/xc-admin-common"; |
| 6 | +import fs from "fs"; |
| 7 | +import path from "path"; |
| 8 | + |
| 9 | +const parser = yargs(hideBin(process.argv)) |
| 10 | + .usage( |
| 11 | + "Upgrades the Pyth contract on TON and creates a governance proposal for it.\n" + |
| 12 | + "Usage: $0 --network <mainnet|testnet> --contract-address <address> --ops-key-path <ops_key_path>" |
| 13 | + ) |
| 14 | + .options({ |
| 15 | + network: { |
| 16 | + type: "string", |
| 17 | + choices: ["mainnet", "testnet"], |
| 18 | + description: "Network to deploy to", |
| 19 | + demandOption: true, |
| 20 | + }, |
| 21 | + "contract-address": { |
| 22 | + type: "string", |
| 23 | + description: "Address of the contract to upgrade", |
| 24 | + demandOption: true, |
| 25 | + }, |
| 26 | + "ops-key-path": { |
| 27 | + type: "string", |
| 28 | + description: "Path to operations key file", |
| 29 | + demandOption: true, |
| 30 | + }, |
| 31 | + }); |
| 32 | + |
| 33 | +async function main() { |
| 34 | + const argv = await parser.argv; |
| 35 | + const isMainnet = argv.network === "mainnet"; |
| 36 | + |
| 37 | + // Get chain ID and name from CHAINS mapping |
| 38 | + const chainId = isMainnet ? CHAINS.ton_mainnet : CHAINS.ton_testnet; |
| 39 | + const wormholeChainName = toChainName(chainId); |
| 40 | + |
| 41 | + // Get the TON chain instance with appropriate RPC URL based on network |
| 42 | + const chain = new TonChain( |
| 43 | + chainId.toString(), |
| 44 | + isMainnet, |
| 45 | + wormholeChainName, |
| 46 | + undefined, |
| 47 | + isMainnet |
| 48 | + ? "https://toncenter.com/api/v2/jsonRPC" |
| 49 | + : "https://testnet.toncenter.com/api/v2/jsonRPC" |
| 50 | + ); |
| 51 | + |
| 52 | + const vault = |
| 53 | + DefaultStore.vaults[ |
| 54 | + "mainnet-beta_FVQyHcooAtThJ83XFrNnv74BcinbRH3bRmfFamAHBfuj" |
| 55 | + ]; |
| 56 | + |
| 57 | + console.log( |
| 58 | + `Upgrading contract on TON ${argv.network} (Chain ID: ${chainId}, Wormhole Chain Name: ${wormholeChainName})` |
| 59 | + ); |
| 60 | + |
| 61 | + // Read the compiled contract from the build directory |
| 62 | + // NOTE: Remember to rebuild contract_manager before running this script because it will also build the ton contract |
| 63 | + const compiledPath = path.resolve( |
| 64 | + __dirname, |
| 65 | + "../../target_chains/ton/contracts/build/Main.compiled.json" |
| 66 | + ); |
| 67 | + const compiled = JSON.parse(fs.readFileSync(compiledPath, "utf8")); |
| 68 | + const newCodeHash = compiled.hash; |
| 69 | + console.log("New code hash:", newCodeHash); |
| 70 | + |
| 71 | + // Generate governance payload for the upgrade |
| 72 | + const payload = chain.generateGovernanceUpgradePayload(newCodeHash); |
| 73 | + console.log("Generated governance payload"); |
| 74 | + console.log("Payload:", payload); |
| 75 | + |
| 76 | + // Create and submit governance proposal |
| 77 | + console.log("Using vault for proposal:", vault.getId()); |
| 78 | + const keypair = await loadHotWallet(argv["ops-key-path"] as string); |
| 79 | + console.log("Using wallet:", keypair.publicKey.toBase58()); |
| 80 | + vault.connect(keypair); |
| 81 | + const proposal = await vault.proposeWormholeMessage([payload]); |
| 82 | + console.log("Proposal address:", proposal.address.toBase58()); |
| 83 | +} |
| 84 | + |
| 85 | +main().catch((error) => { |
| 86 | + console.error("Error during upgrade:", error); |
| 87 | + process.exit(1); |
| 88 | +}); |
0 commit comments