@kaiachain/web3js-ext
Version:
web3.js extension for kaiachain blockchain
71 lines (67 loc) • 3.4 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
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseTransactionError = void 0;
exports.getRevertReason = getRevertReason;
const web3_errors_1 = require("web3-errors");
const web3_eth_1 = require("web3-eth");
const web3_eth_abi_1 = require("web3-eth-abi");
const web3_types_1 = require("web3-types");
const parseTransactionError = (error, contractAbi) => {
if (error instanceof web3_errors_1.ContractExecutionError &&
error.innerError instanceof web3_errors_1.Eip838ExecutionError) {
if (contractAbi !== undefined) {
const errorsAbi = contractAbi.filter((abi) => (0, web3_eth_abi_1.isAbiErrorFragment)(abi));
(0, web3_eth_abi_1.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 web3_errors_1.InvalidResponseError &&
!Array.isArray(error.innerError) &&
error.innerError !== undefined) {
return error.innerError.message;
}
throw error;
};
exports.parseTransactionError = parseTransactionError;
/**
* 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
*/
async function getRevertReason(web3Context, transaction, contractAbi, returnFormat = web3_types_1.DEFAULT_RETURN_FORMAT) {
try {
await (0, web3_eth_1.call)(web3Context, transaction, web3Context.defaultBlock, returnFormat);
return undefined;
}
catch (error) {
return (0, exports.parseTransactionError)(error, contractAbi);
}
}