upgrade_ton_contract.ts 1.9 KB

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