UNPKG

@kaiachain/web3js-ext

Version:
150 lines (146 loc) 7.02 kB
"use strict"; /* This file is part of web3.js. web3.js is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. web3.js is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with web3.js. If not, see <http://www.gnu.org/licenses/>. */ // Taken from https://github.com/web3/web3.js/blob/v4.3.0/packages/web3-eth/src/utils/send_tx_helper.ts Object.defineProperty(exports, "__esModule", { value: true }); exports.SendTxHelper = void 0; const web3_errors_1 = require("web3-errors"); const web3_eth_1 = require("web3-eth"); const web3_rpc_methods_1 = require("web3-rpc-methods"); const web3_types_1 = require("web3-types"); const web3_validator_1 = require("web3-validator"); const get_revert_reason_js_1 = require("./get_revert_reason.js"); const get_transaction_error_js_1 = require("./get_transaction_error.js"); const get_transaction_gas_pricing_js_1 = require("./get_transaction_gas_pricing.js"); const try_send_transaction_js_1 = require("./try_send_transaction.js"); const watch_transaction_for_confirmations_js_1 = require("./watch_transaction_for_confirmations.js"); class SendTxHelper { constructor({ options, web3Context, promiEvent, returnFormat, }) { this.options = { checkRevertBeforeSending: true, }; this.options = options; this.web3Context = web3Context; this.promiEvent = promiEvent; this.returnFormat = returnFormat; } getReceiptWithEvents(data) { const result = { ...(data ?? {}) }; if (this.options?.contractAbi && result.logs && result.logs.length > 0) { result.events = {}; for (const log of result.logs) { const event = (0, web3_eth_1.decodeEventABI)(web3_eth_1.ALL_EVENTS_ABI, log, this.options?.contractAbi, this.returnFormat); if (event.event) { result.events[event.event] = event; } } } return result; } async checkRevertBeforeSending(tx) { if (this.options.checkRevertBeforeSending !== false) { const reason = await (0, get_revert_reason_js_1.getRevertReason)(this.web3Context, tx, this.options.contractAbi); if (reason !== undefined) { throw await (0, get_transaction_error_js_1.getTransactionError)(this.web3Context, tx, undefined, undefined, this.options.contractAbi, reason); } } } emitSending(tx) { if (this.promiEvent.listenerCount("sending") > 0) { // @ts-ignore: web3.js has the same error this.promiEvent.emit("sending", tx); } } async populateGasPrice({ transactionFormatted, transaction, }) { let result = transactionFormatted; if (!this.options?.ignoreGasPricing && (0, web3_validator_1.isNullish)(transactionFormatted.gasPrice) && ((0, web3_validator_1.isNullish)(transaction.maxPriorityFeePerGas) || (0, web3_validator_1.isNullish)(transaction.maxFeePerGas))) { result = { ...transactionFormatted, // TODO gasPrice, maxPriorityFeePerGas, maxFeePerGas // should not be included if undefined, but currently are ...(await (0, get_transaction_gas_pricing_js_1.getTransactionGasPricing)( // @ts-ignore: web3.js has the same error transactionFormatted, this.web3Context, web3_types_1.ETH_DATA_FORMAT)), }; } return result; } async signAndSend({ wallet, tx, }) { if (wallet) { // @ts-ignore: web3.js has the same error const signedTransaction = await wallet.signTransaction(tx); return (0, try_send_transaction_js_1.trySendTransaction)(this.web3Context, async () => web3_rpc_methods_1.ethRpcMethods.sendRawTransaction(this.web3Context.requestManager, signedTransaction.rawTransaction), signedTransaction.transactionHash); } return (0, try_send_transaction_js_1.trySendTransaction)(this.web3Context, async () => web3_rpc_methods_1.ethRpcMethods.sendTransaction(this.web3Context.requestManager, tx)); } emitSent(tx) { if (this.promiEvent.listenerCount("sent") > 0) { // @ts-ignore: web3.js has the same error this.promiEvent.emit("sent", tx); } } emitTransactionHash(hash) { if (this.promiEvent.listenerCount("transactionHash") > 0) { this.promiEvent.emit("transactionHash", hash); } } emitReceipt(receipt) { if (this.promiEvent.listenerCount("receipt") > 0) { this.promiEvent.emit("receipt", // @ts-expect-error unknown type fix receipt); } } async handleError({ error, tx }) { let _error = error; if (_error instanceof web3_errors_1.ContractExecutionError && this.web3Context.handleRevert) { _error = await (0, get_transaction_error_js_1.getTransactionError)(this.web3Context, tx, undefined, undefined, this.options?.contractAbi); } if ((_error instanceof web3_errors_1.InvalidResponseError || _error instanceof web3_errors_1.ContractExecutionError || _error instanceof web3_errors_1.TransactionRevertWithCustomError || _error instanceof web3_errors_1.TransactionRevertedWithoutReasonError || _error instanceof web3_errors_1.TransactionRevertInstructionError) && this.promiEvent.listenerCount("error") > 0) { this.promiEvent.emit("error", _error); } return _error; } emitConfirmation({ receipt, transactionHash, }) { if (this.promiEvent.listenerCount("confirmation") > 0) { (0, watch_transaction_for_confirmations_js_1.watchTransactionForConfirmations)(this.web3Context, this.promiEvent, receipt, transactionHash, this.returnFormat); } } async handleResolve({ receipt, tx }) { if (this.options?.transactionResolver) { return this.options?.transactionResolver(receipt); } if (receipt.status === BigInt(0)) { const error = await (0, get_transaction_error_js_1.getTransactionError)(this.web3Context, tx, // @ts-ignore: web3.js has the same error receipt, undefined, this.options?.contractAbi); if (this.promiEvent.listenerCount("error") > 0) { this.promiEvent.emit("error", error); } throw error; } else { return receipt; } } } exports.SendTxHelper = SendTxHelper;