Web3 module encode and decode EVM in/output.
npx @tessl/cli install tessl/npm-web3-eth-abi@4.4.0web3-eth-abi is a comprehensive TypeScript library for encoding and decoding Ethereum Virtual Machine (EVM) input and output data, specifically handling Application Binary Interface (ABI) operations. It provides functions for encoding function calls and constructor parameters, decoding function return values and event logs, handling contract error data decoding, and supporting EIP-712 typed data encoding.
npm install web3-eth-abiimport {
encodeFunctionSignature,
encodeFunctionCall,
decodeFunctionCall,
decodeFunctionReturn,
encodeParameter,
encodeParameters,
decodeParameter,
decodeParameters,
encodeEventSignature,
decodeLog,
encodeErrorSignature,
decodeContractErrorData,
getEncodedEip712Data
} from "web3-eth-abi";For CommonJS:
const {
encodeFunctionSignature,
encodeFunctionCall,
decodeFunctionCall,
decodeFunctionReturn,
encodeParameter,
encodeParameters,
decodeParameter,
decodeParameters,
encodeEventSignature,
decodeLog,
encodeErrorSignature,
decodeContractErrorData,
getEncodedEip712Data
} = require("web3-eth-abi");import {
encodeFunctionCall,
decodeFunctionReturn,
encodeParameters,
decodeLog
} from "web3-eth-abi";
// Encode a function call
const encodedCall = encodeFunctionCall(
{
name: "transfer",
type: "function",
inputs: [
{ type: "address", name: "to" },
{ type: "uint256", name: "amount" }
]
},
["0x742d35Cc6638C2532C1CCF6344D4039be6498C2f", "1000000000000000000"]
);
// Decode function return value
const decodedReturn = decodeFunctionReturn(
{
name: "balanceOf",
type: "function",
inputs: [{ type: "address", name: "account" }],
outputs: [{ type: "uint256", name: "" }]
},
"0x000000000000000000000000000000000000000000000000016345785d8a0000"
);
// Encode parameters
const encoded = encodeParameters(
["string", "uint256"],
["Hello World", "42"]
);
// Decode event logs
const decodedLog = decodeLog(
[
{ type: "address", name: "from", indexed: true },
{ type: "address", name: "to", indexed: true },
{ type: "uint256", name: "value", indexed: false }
],
"0x000000000000000000000000000000000000000000000000016345785d8a0000",
[
"0x000000000000000000000000a0b86a33e6288db8e74b93f60e5c5e7b56b7b8f5",
"0x000000000000000000000000742d35cc6638c2532c1ccf6344d4039be6498c2f"
]
);web3-eth-abi is built around several key components:
Core functionality for encoding function calls and decoding function return values. Essential for smart contract interactions and transaction building.
function encodeFunctionSignature(functionName: string | AbiFunctionFragment): string;
function encodeFunctionCall(
jsonInterface: AbiFunctionFragment,
params: unknown[]
): string;
function decodeFunctionCall(
functionsAbi: AbiFunctionFragment | AbiConstructorFragment,
data: HexString,
methodSignatureProvided?: boolean
): DecodedParams & { __method__: string };
function decodeFunctionReturn(
functionsAbi: AbiFunctionFragment,
returnValues?: HexString
): unknown;Low-level parameter encoding and decoding operations for individual values and parameter arrays. Supports all Solidity data types including complex structs.
function encodeParameter(abi: AbiInput, param: unknown): string;
function encodeParameters(
abi: AbiInput[] | ReadonlyArray<AbiInput>,
params: unknown[]
): string;
function inferTypesAndEncodeParameters(params: unknown[]): string;
function decodeParameter(abi: AbiInput, bytes: HexString): unknown;
function decodeParameters(
abi: AbiInput[] | ReadonlyArray<AbiInput>,
bytes: HexString
): { [key: string]: unknown; __length__: number };
function decodeParametersWith(
abis: AbiInput[] | ReadonlyArray<AbiInput>,
bytes: HexString,
loose: boolean
): { [key: string]: unknown; __length__: number };Event signature encoding and log data decoding with support for indexed parameters and anonymous events.
function encodeEventSignature(functionName: string | AbiEventFragment): string;
function decodeLog<ReturnType extends DecodedParams>(
inputs: Array<AbiParameter> | ReadonlyArray<AbiParameter>,
data: HexString,
topics: string | string[]
): ReturnType;Contract error signature encoding and comprehensive error data decoding with support for EIP-838 execution errors.
function encodeErrorSignature(functionName: string | AbiErrorFragment): string;
function decodeContractErrorData(
errorsAbi: AbiErrorFragment[],
error: Eip838ExecutionError
): void;Complete support for EIP-712 structured data encoding and message preparation for typed data signing.
function getEncodedEip712Data(
typedData: Eip712TypedData,
hash?: boolean
): string;Helper functions for working with ABI fragments, type validation, and data formatting.
function isAbiFunctionFragment(item: unknown): item is AbiFunctionFragment;
function isAbiEventFragment(item: unknown): item is AbiEventFragment;
function isAbiErrorFragment(item: unknown): item is AbiErrorFragment;
function isAbiConstructorFragment(item: unknown): item is AbiConstructorFragment;
function isAbiFragment(item: unknown): item is AbiFragment;
function jsonInterfaceMethodToString(json: AbiFragment): string;
function flattenTypes(includeTuple: boolean, puts: ReadonlyArray<AbiParameter>): string[];
function formatParam(type: string, param: unknown): unknown;
function isOddHexstring(param: unknown): boolean;
function formatOddHexstrings(param: string): string;
function mapStructNameAndType(structName: string): AbiStruct;
function mapStructToCoderFormat(struct: AbiStruct): Array<AbiCoderStruct>;
function mapTypes(types: AbiInput[]): Array<string | AbiParameter | Record<string, unknown>>;
function isSimplifiedStructFormat(
type: string | Partial<AbiParameter> | Partial<AbiInput>
): boolean;interface AbiFunctionFragment {
type: "function";
name?: string;
inputs?: AbiInput[];
outputs?: AbiOutput[];
stateMutability?: "pure" | "view" | "nonpayable" | "payable";
}
interface AbiEventFragment {
type: "event";
name?: string;
inputs?: AbiParameter[];
anonymous?: boolean;
}
interface AbiErrorFragment {
type: "error";
name?: string;
inputs?: AbiInput[];
}
interface AbiConstructorFragment {
type: "constructor";
inputs?: AbiInput[];
stateMutability?: "nonpayable" | "payable";
}
interface AbiInput {
name?: string;
type: string;
indexed?: boolean;
components?: AbiInput[];
}
interface AbiOutput {
name?: string;
type: string;
components?: AbiOutput[];
}
interface AbiParameter extends AbiInput {
name: string;
}
interface DecodedParams {
[key: string]: unknown;
__length__: number;
}
type HexString = string;
interface AbiFragment {
type: "function" | "event" | "constructor" | "error";
name?: string;
inputs?: AbiInput[];
}
interface AbiStruct {
[key: string]: string | AbiStruct;
}
interface AbiCoderStruct {
name: string;
type: string;
components?: AbiCoderStruct[];
}
interface Eip712TypedData {
types: Record<string, Array<{ name: string; type: string }>>;
primaryType: string;
domain: Record<string, unknown>;
message: Record<string, unknown>;
}
interface Eip838ExecutionError extends Error {
data?: string;
setDecodedProperties(
errorName: string,
errorSignature?: string,
errorArgs?: { [K in string]: unknown }
): void;
}