UNPKG

zic-tron-sdk

Version:
104 lines (103 loc) 5.2 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TransactionPayload = void 0; const assert_1 = __importDefault(require("assert")); const tronweb_1 = require("tronweb"); const constants_1 = require("./constants"); const errors_1 = require("./errors"); const utils_1 = require("./utils"); class TransactionPayload { constructor(tronWeb, contract, // readonly transaction: Transaction, functionName, args, options, _signTransaction) { this.tronWeb = tronWeb; this.contract = contract; this.functionName = functionName; this.args = args; this.options = options; this._signTransaction = _signTransaction; } async execute(options) { // console.debug("args:", this.args); const callValue = this.options.callValue ? this.options.callValue : options?.callValue ? options.callValue : 0; const feeLimit = options?.feeLimit ? options.feeLimit : constants_1.DEFAULT_FEE_LIMIT; const shouldPollResponse = options?.shouldPollResponse != undefined ? options.shouldPollResponse : true; let contractAddress = this.contract.address; (0, assert_1.default)(typeof contractAddress === "string"); contractAddress = (0, utils_1.toHex)(contractAddress); const abiFragment = (0, utils_1.findFunctioFragmentByName)(this.contract.abi, this.functionName); const functionSelector = this.contract.methodInstances[this.functionName].functionSelector; const rawParameter = tronweb_1.utils.abi.encodeParamsV2ByABI(abiFragment, this.args); const transaction = await this.tronWeb.transactionBuilder.triggerSmartContract(contractAddress, functionSelector, { rawParameter, callValue, feeLimit, }, [], this.options.from); // console.debug("transaction:", transaction.transaction); if (!transaction.result || !transaction.result.result) { if (transaction.Error) console.debug("transaction.Error:", transaction.Error); throw new Error("Unknown error: " + JSON.stringify(transaction, null, 2)); } let signedTransaction; if (this._signTransaction) { let wTransaction = (0, utils_1.fromTronTransaction)(transaction.transaction); (0, assert_1.default)(tronweb_1.utils.isObject(wTransaction), "Type coversion failed from tron TronWeb Transaction to WalletAdapter Transaction"); let _signedTransaction = await this._signTransaction(wTransaction); signedTransaction = (0, utils_1.toTronSignedTransaction)(_signedTransaction); } else { if (this.tronWeb.defaultPrivateKey) { signedTransaction = await this.tronWeb.trx.sign(transaction.transaction); } else { throw new Error("Both signTranaction and defaultPrivateKey in TronWeb instance is undefined"); } } if (!signedTransaction.signature) { throw new Error("Transaction was not signed properly"); } const broadcast = await this.tronWeb.trx.sendRawTransaction(signedTransaction); console.debug("txid:", broadcast.transaction.txID); if (broadcast.code) { const message = broadcast.message ? this.tronWeb.toUtf8(broadcast.message) : (0, utils_1.broadcastReturnResponseCodeToName)(broadcast.code); throw new errors_1.TransactioBroadcastError(message, broadcast.code); } if (!shouldPollResponse) { return { id: signedTransaction.txID }; } const checkResult = async (index) => { if (index === (options?.pollTimes || 20)) { throw new errors_1.TransactionConfirmationTimeoutError("Cannot find result in solidity node", signedTransaction); } const output = await this.tronWeb.trx.getTransactionInfo(signedTransaction.txID); // console.debug("output:", output); if (!Object.keys(output).length) { await new Promise((r) => setTimeout(r, 3000)); return checkResult(index + 1); } if (output.result && output.result === "FAILED") { throw new errors_1.TransactionFailedError(this.tronWeb.toUtf8(output.resMessage), signedTransaction, output); } if (!tronweb_1.utils.hasProperty(output, "contractResult")) { throw new errors_1.TransactionFailedError("Failed to execute: " + JSON.stringify(output, null, 2), signedTransaction, output); } if (options?.rawResponse) { return output; } let decoded = tronweb_1.utils.abi.decodeParamsV2ByABI(abiFragment, "0x" + output.contractResult[0]); return { decodedContractResult: decoded, ...output }; }; return checkResult(0); } } exports.TransactionPayload = TransactionPayload;