@kaiachain/web3js-ext
Version:
web3.js extension for kaiachain blockchain
66 lines (62 loc) • 3.16 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_revert_reason.ts
import { ContractExecutionError, Eip838ExecutionError, InvalidResponseError } from "web3-errors";
import { call } from "web3-eth";
import { decodeContractErrorData, isAbiErrorFragment } from "web3-eth-abi";
import { DEFAULT_RETURN_FORMAT, } from "web3-types";
export const parseTransactionError = (error, contractAbi) => {
if (error instanceof ContractExecutionError &&
error.innerError instanceof Eip838ExecutionError) {
if (contractAbi !== undefined) {
const errorsAbi = contractAbi.filter((abi) => isAbiErrorFragment(abi));
decodeContractErrorData(errorsAbi, error.innerError);
return {
reason: error.innerError.message,
signature: error.innerError.data?.slice(0, 10),
data: error.innerError.data?.substring(10),
customErrorName: error.innerError.errorName,
customErrorDecodedSignature: error.innerError.errorSignature,
customErrorArguments: error.innerError.errorArgs,
};
}
return {
reason: error.innerError.message,
signature: error.innerError.data?.slice(0, 10),
data: error.innerError.data?.substring(10),
};
}
if (error instanceof InvalidResponseError &&
!Array.isArray(error.innerError) &&
error.innerError !== undefined) {
return error.innerError.message;
}
throw error;
};
/**
* Returns the revert reason generated by the EVM if the transaction were to be executed.
*
* @param web3Context - ({@link Web3Context}) Web3 configuration object that contains things such as the provider, request manager, wallet, etc.
* @param transaction - A transaction object where all properties are optional except `to`, however it's recommended to include the `from` property or it may default to `0x0000000000000000000000000000000000000000` depending on your node or provider.
* @returns `undefined` if no revert reason was given, a revert reason object, a revert reason string, or an `unknown` error
*/
export async function getRevertReason(web3Context, transaction, contractAbi, returnFormat = DEFAULT_RETURN_FORMAT) {
try {
await call(web3Context, transaction, web3Context.defaultBlock, returnFormat);
return undefined;
}
catch (error) {
return parseTransactionError(error, contractAbi);
}
}