Web3 module encode and decode EVM in/output.
—
Contract error signature encoding and comprehensive error data decoding with support for EIP-838 execution errors. Essential for handling smart contract errors and providing meaningful error messages.
Encodes the error name to its ABI signature, which is the sha3 hash of the error name including input types.
/**
* Encodes the error name to its ABI signature (sha3 hash)
* @param functionName - The error name to encode or the AbiErrorFragment object
* @returns The ABI signature of the error (32 bytes)
*/
function encodeErrorSignature(functionName: string | AbiErrorFragment): string;Usage Examples:
import { encodeErrorSignature } from "web3-eth-abi";
// Using error name string
const signature1 = encodeErrorSignature('InsufficientBalance(uint256,uint256)');
console.log(signature1);
// 0x...hash of error signature
// Using JSON interface object
const signature2 = encodeErrorSignature({
name: "InsufficientBalance",
type: "error",
inputs: [
{ type: "uint256", name: "available" },
{ type: "uint256", name: "required" }
]
});
console.log(signature2);
// Same hash as above
// Simple error with no parameters
const signature3 = encodeErrorSignature({
name: "Paused",
type: "error",
inputs: []
});
console.log(signature3);
// Hash of "Paused()"Decodes contract error data using error ABI fragments. Supports custom errors defined in smart contracts as well as standard errors like "Error(string)" and "Panic(uint256)".
/**
* Decodes contract error data using error ABI fragments
* @param errorsAbi - Array of error ABI fragments to match against
* @param error - EIP-838 execution error with data field to decode
* @returns Modifies the error object with decoded properties
*/
function decodeContractErrorData(
errorsAbi: AbiErrorFragment[],
error: Eip838ExecutionError
): void;Usage Examples:
import { decodeContractErrorData } from "web3-eth-abi";
// Define error ABI fragments
const errorAbi = [
{
name: "InsufficientBalance",
type: "error",
inputs: [
{ name: "available", type: "uint256" },
{ name: "required", type: "uint256" }
]
},
{
name: "InvalidAddress",
type: "error",
inputs: [
{ name: "addr", type: "address" }
]
},
{
name: "Paused",
type: "error",
inputs: []
}
];
// Mock error object (would come from web3 transaction)
const executionError = {
message: "Transaction reverted",
data: "0x12345678000000000000000000000000000000000000000000000000000000000000006400000000000000000000000000000000000000000000000000000000000000c8", // encoded error data
setDecodedProperties: function(name, signature, args) {
this.errorName = name;
this.errorSignature = signature;
this.errorArgs = args;
}
};
// Decode the error data
decodeContractErrorData(errorAbi, executionError);
console.log(executionError.errorName); // "InsufficientBalance"
console.log(executionError.errorSignature); // "InsufficientBalance(uint256,uint256)"
console.log(executionError.errorArgs);
// {
// '0': 100n,
// '1': 200n,
// __length__: 2,
// available: 100n,
// required: 200n
// }The decodeContractErrorData function automatically handles standard Ethereum errors even when no ABI is provided:
Standard revert with string message.
// Error with signature 0x08c379a0... will be decoded as:
// {
// errorName: "Error",
// errorSignature: "Error(string)",
// errorArgs: { '0': "error message", __length__: 1, message: "error message" }
// }Panic errors with numeric codes.
// Error with signature 0x4e487b71... will be decoded as:
// {
// errorName: "Panic",
// errorSignature: "Panic(uint256)",
// errorArgs: { '0': 18n, __length__: 1, code: 18n } // 18 = arithmetic overflow
// }0x01: Assert false0x11: Arithmetic overflow/underflow0x12: Division by zero0x21: Invalid enum value0x22: Invalid storage byte array access0x31: Pop on empty array0x32: Array out of bounds access0x41: Out of memory0x51: Invalid function selectorinterface AbiErrorFragment {
type: "error";
name?: string;
inputs?: AbiInput[];
}
interface AbiInput {
name?: string;
type: string;
components?: AbiInput[];
}
interface Eip838ExecutionError extends Error {
data?: string;
setDecodedProperties(
errorName: string,
errorSignature?: string,
errorArgs?: { [K in string]: unknown }
): void;
}Install with Tessl CLI
npx tessl i tessl/npm-web3-eth-abi