upgrade_ton_contract.ts 2.4 KB

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