upgrade_ton_contract.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import yargs from "yargs";
  2. import { hideBin } from "yargs/helpers";
  3. import { DefaultStore } from "../src/node/utils/store";
  4. import { TonPriceFeedContract } from "../src/core/contracts";
  5. import { toPrivateKey } from "../src/core/base";
  6. import fs from "fs";
  7. import path from "path";
  8. import { Cell } from "@ton/ton";
  9. // This script upgrades the Pyth contract on TON after the governance has authorized the upgrade
  10. // If you are starting over, the process is like the following:
  11. // 1. create a governance proposal using generate_upgrade_ton_contract_proposal script
  12. // 2. once approved and executed, relay it to TON using sync_governance_vaas script
  13. // 3. upgrade the contract on TON using this script
  14. const parser = yargs(hideBin(process.argv))
  15. .usage(
  16. "Upgrades the Pyth contract on TON and creates a governance proposal for it.\n" +
  17. "Usage: $0 --contract <contract_name> --private-key <private_key>",
  18. )
  19. .options({
  20. contract: {
  21. type: "string",
  22. description: "Contract name",
  23. demandOption: true,
  24. },
  25. "private-key": {
  26. type: "string",
  27. description: "Private key of the sender",
  28. demandOption: true,
  29. },
  30. });
  31. async function main() {
  32. const argv = await parser.argv;
  33. const contract = DefaultStore.contracts[
  34. argv.contract
  35. ] as TonPriceFeedContract;
  36. // Read the compiled contract from the build directory
  37. // NOTE: Remember to rebuild contract_manager before running this script because it will also build the ton contract
  38. const compiledPath = path.resolve(
  39. __dirname,
  40. "../../target_chains/ton/contracts/build/Main.compiled.json",
  41. );
  42. const compiled = JSON.parse(fs.readFileSync(compiledPath, "utf8"));
  43. const newCode = Cell.fromHex(compiled.hex);
  44. console.log(newCode);
  45. const tx = await contract.upgradeContract(
  46. toPrivateKey(argv["private-key"]),
  47. newCode,
  48. );
  49. console.log("Upgrade transaction:", tx);
  50. }
  51. main().catch((error) => {
  52. console.error("Error during upgrade:", error);
  53. process.exit(1);
  54. });