Skip to content

Commit 1261d33

Browse files
authored
feat(contract_manager): add ton upgrade script (#2139)
* add ton upgrade script * update script * fix build script * use mainnet vault * add CI * update node version * build deps * fix * fix * fix * address comments
1 parent 51bf542 commit 1261d33

File tree

5 files changed

+127
-25
lines changed

5 files changed

+127
-25
lines changed

contract_manager/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,13 @@
3737
"@pythnetwork/pyth-starknet-js": "^0.2.1",
3838
"@pythnetwork/pyth-sui-js": "workspace:*",
3939
"@pythnetwork/pyth-ton-js": "workspace:*",
40+
"@pythnetwork/pyth-ton": "workspace:*",
4041
"@pythnetwork/solana-utils": "workspace:^",
4142
"@pythnetwork/xc-admin-common": "workspace:*",
4243
"@solana/web3.js": "^1.73.0",
4344
"@sqds/mesh": "^1.0.6",
45+
"@ton/blueprint": "^0.22.0",
46+
"@ton/core": "^0.59.0",
4447
"@ton/crypto": "^3.3.0",
4548
"@ton/ton": "^15.1.0",
4649
"@types/yargs": "^17.0.32",
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
});

contract_manager/src/chains.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,6 @@ export class TonChain extends Chain {
807807
* @param digest hex string of the 32 byte digest for the new package without the 0x prefix
808808
*/
809809
generateGovernanceUpgradePayload(digest: string): Buffer {
810-
// This might throw an error because the Fuel contract doesn't support upgrades yet (blocked on Fuel releasing Upgradeability standard)
811810
return new UpgradeContract256Bit(this.wormholeChainName, digest).encode();
812811
}
813812

pnpm-lock.yaml

Lines changed: 35 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

target_chains/ton/contracts/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"private": true,
55
"scripts": {
66
"start": "blueprint run",
7-
"build:blueprint": "echo Pyth | blueprint build",
7+
"build": "blueprint build Main",
88
"test:unit": "jest --verbose"
99
},
1010
"devDependencies": {

0 commit comments

Comments
 (0)