utils.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. import { Transaction } from "@ton/core";
  2. import { Buffer } from "buffer";
  3. const GOVERNANCE_MAGIC = 0x5054474d;
  4. const GOVERNANCE_MODULE = 1;
  5. const AUTHORIZE_UPGRADE_CONTRACT_ACTION = 0;
  6. const TARGET_CHAIN_ID = 1;
  7. function computedGeneric(transaction: Transaction) {
  8. if (transaction.description.type !== "generic")
  9. throw "Expected generic transactionaction";
  10. if (transaction.description.computePhase.type !== "vm")
  11. throw "Compute phase expected";
  12. return transaction.description.computePhase;
  13. }
  14. export function printTxGasStats(name: string, transaction: Transaction) {
  15. const txComputed = computedGeneric(transaction);
  16. console.log(`${name} used ${txComputed.gasUsed} gas`);
  17. console.log(`${name} gas cost: ${txComputed.gasFees}`);
  18. return txComputed.gasFees;
  19. }
  20. export function createAuthorizeUpgradePayload(newCodeHash: Buffer): Buffer {
  21. const payload = Buffer.alloc(8);
  22. payload.writeUInt32BE(GOVERNANCE_MAGIC, 0);
  23. payload.writeUInt8(GOVERNANCE_MODULE, 4);
  24. payload.writeUInt8(AUTHORIZE_UPGRADE_CONTRACT_ACTION, 5);
  25. payload.writeUInt16BE(TARGET_CHAIN_ID, 6);
  26. return Buffer.concat([payload, newCodeHash]);
  27. }