Skip to content

feat(contract_manager): add ton upgrade script #2139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions contract_manager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@
"@pythnetwork/pyth-starknet-js": "^0.2.1",
"@pythnetwork/pyth-sui-js": "workspace:*",
"@pythnetwork/pyth-ton-js": "workspace:*",
"@pythnetwork/pyth-ton": "workspace:*",
"@pythnetwork/solana-utils": "workspace:^",
"@pythnetwork/xc-admin-common": "workspace:*",
"@solana/web3.js": "^1.73.0",
"@sqds/mesh": "^1.0.6",
"@ton/blueprint": "^0.22.0",
"@ton/core": "^0.59.0",
"@ton/crypto": "^3.3.0",
"@ton/ton": "^15.1.0",
"@types/yargs": "^17.0.32",
Expand Down
88 changes: 88 additions & 0 deletions contract_manager/scripts/upgrade_ton_contract.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { DefaultStore, loadHotWallet } from "../src";
import { TonChain } from "../src/chains";
import { CHAINS, toChainName } from "@pythnetwork/xc-admin-common";
import fs from "fs";
import path from "path";

const parser = yargs(hideBin(process.argv))
.usage(
"Upgrades the Pyth contract on TON and creates a governance proposal for it.\n" +
"Usage: $0 --network <mainnet|testnet> --contract-address <address> --ops-key-path <ops_key_path>"
)
.options({
network: {
type: "string",
choices: ["mainnet", "testnet"],
description: "Network to deploy to",
demandOption: true,
},
"contract-address": {
type: "string",
description: "Address of the contract to upgrade",
demandOption: true,
},
"ops-key-path": {
type: "string",
description: "Path to operations key file",
demandOption: true,
},
});

async function main() {
const argv = await parser.argv;
const isMainnet = argv.network === "mainnet";

// Get chain ID and name from CHAINS mapping
const chainId = isMainnet ? CHAINS.ton_mainnet : CHAINS.ton_testnet;
const wormholeChainName = toChainName(chainId);

// Get the TON chain instance with appropriate RPC URL based on network
const chain = new TonChain(
chainId.toString(),
isMainnet,
wormholeChainName,
undefined,
isMainnet
? "https://toncenter.com/api/v2/jsonRPC"
: "https://testnet.toncenter.com/api/v2/jsonRPC"
);

const vault =
DefaultStore.vaults[
"mainnet-beta_FVQyHcooAtThJ83XFrNnv74BcinbRH3bRmfFamAHBfuj"
];

console.log(
`Upgrading contract on TON ${argv.network} (Chain ID: ${chainId}, Wormhole Chain Name: ${wormholeChainName})`
);

// Read the compiled contract from the build directory
// NOTE: Remember to rebuild contract_manager before running this script because it will also build the ton contract
const compiledPath = path.resolve(
__dirname,
"../../target_chains/ton/contracts/build/Main.compiled.json"
);
const compiled = JSON.parse(fs.readFileSync(compiledPath, "utf8"));
const newCodeHash = compiled.hash;
console.log("New code hash:", newCodeHash);

// Generate governance payload for the upgrade
const payload = chain.generateGovernanceUpgradePayload(newCodeHash);
console.log("Generated governance payload");
console.log("Payload:", payload);

// Create and submit governance proposal
console.log("Using vault for proposal:", vault.getId());
const keypair = await loadHotWallet(argv["ops-key-path"] as string);
console.log("Using wallet:", keypair.publicKey.toBase58());
vault.connect(keypair);
const proposal = await vault.proposeWormholeMessage([payload]);
console.log("Proposal address:", proposal.address.toBase58());
}

main().catch((error) => {
console.error("Error during upgrade:", error);
process.exit(1);
});
1 change: 0 additions & 1 deletion contract_manager/src/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,6 @@ export class TonChain extends Chain {
* @param digest hex string of the 32 byte digest for the new package without the 0x prefix
*/
generateGovernanceUpgradePayload(digest: string): Buffer {
// This might throw an error because the Fuel contract doesn't support upgrades yet (blocked on Fuel releasing Upgradeability standard)
return new UpgradeContract256Bit(this.wormholeChainName, digest).encode();
}

Expand Down
58 changes: 35 additions & 23 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion target_chains/ton/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"private": true,
"scripts": {
"start": "blueprint run",
"build:blueprint": "echo Pyth | blueprint build",
"build": "blueprint build Main",
"test:unit": "jest --verbose"
},
"devDependencies": {
Expand Down
Loading