@kaiachain/web3js-ext
Version:
web3.js extension for kaiachain blockchain
48 lines (44 loc) • 2.5 kB
JavaScript
/*
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/get_transaction_error.ts
import { TransactionRevertedWithoutReasonError, TransactionRevertInstructionError, TransactionRevertWithCustomError, } from "web3-errors";
import { getRevertReason, parseTransactionError } from "./get_revert_reason.js";
export async function getTransactionError(web3Context, transactionFormatted, transactionReceiptFormatted, receivedError, contractAbi, knownReason) {
let _reason = knownReason;
if (_reason === undefined) {
if (receivedError !== undefined) {
_reason = parseTransactionError(receivedError);
}
else if (web3Context.handleRevert && transactionFormatted !== undefined) {
_reason = await getRevertReason(web3Context, transactionFormatted, contractAbi);
}
}
let error;
if (_reason === undefined) {
error = new TransactionRevertedWithoutReasonError(transactionReceiptFormatted);
}
else if (typeof _reason === "string") {
error = new TransactionRevertInstructionError(_reason, undefined, transactionReceiptFormatted);
}
else if (_reason.customErrorName !== undefined &&
_reason.customErrorDecodedSignature !== undefined &&
_reason.customErrorArguments !== undefined) {
const reasonWithCustomError = _reason;
error = new TransactionRevertWithCustomError(reasonWithCustomError.reason, reasonWithCustomError.customErrorName, reasonWithCustomError.customErrorDecodedSignature, reasonWithCustomError.customErrorArguments, reasonWithCustomError.signature, transactionReceiptFormatted, reasonWithCustomError.data);
}
else {
error = new TransactionRevertInstructionError(_reason.reason, _reason.signature, transactionReceiptFormatted, _reason.data);
}
return error;
}