Skip to main content

ABI

The web3.eth.abi functions let you encode and decode parameters to ABI (Application Binary Interface) for function calls to the EVM (Ethereum Virtual Machine).

For using Web3 ABI functions, first install Web3 package using npm i web3 or yarn add web3. After that, Web3 ABI functions will be available.

import { Web3 } from 'web3';

const web3 = new Web3();
const encoded = web3.eth.abi.encodeFunctionSignature({
name: 'myMethod',
type: 'function',
inputs: [{
type: 'uint256',
name: 'myNumber'
},{
type: 'string',
name: 'myString'
}]
});

For using individual package install web3-eth-abi package using npm i web3-eth-abi or yarn add web3-eth-abi and only import required functions. This is more efficient approach for building lightweight applications.

import { encodeFunctionSignature } from 'web3-eth-abi';

const encoded = encodeFunctionSignature({
name: 'myMethod',
type: 'function',
inputs: [{
type: 'uint256',
name: 'myNumber'
},{
type: 'string',
name: 'myString'
}]
});

Functions

decodeLog

decodeLog<ReturnType>(inputs, data, topics): ReturnType

Decodes ABI-encoded log data and indexed topic data.

Type parameters

NameType
ReturnTypeextends DecodedParams<ReturnType>

Parameters

NameTypeDescription
inputsAbiParameter[]A AbiParameter input array. See the Solidity documentation for a list of types.
datastringThe ABI byte code in the data field of a log.
topicsstring | string[]An array with the index parameter topics of the log, without the topic[0] if its a non-anonymous event, otherwise with topic[0]

Returns

ReturnType

  • The result object containing the decoded parameters.

Example

let res = web3.eth.abi.decodeLog(
[
{
type: "string",
name: "myString",
},
{
type: "uint256",
name: "myNumber",
indexed: true,
},
{
type: "uint8",
name: "mySmallNumber",
indexed: true,
},
],
"0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000748656c6c6f252100000000000000000000000000000000000000000000000000",
[
"0x000000000000000000000000000000000000000000000000000000000000f310",
"0x0000000000000000000000000000000000000000000000000000000000000010",
]
);
> {
'0': 'Hello%!',
'1': 62224n,
'2': 16n,
__length__: 3,
myString: 'Hello%!',
myNumber: 62224n,
mySmallNumber: 16n
}

decodeParameter

decodeParameter(abi, bytes): unknown

Decodes an ABI encoded parameter to its JavaScript type.

Parameters

NameTypeDescription
abiAbiInputThe type of the parameter. See the Solidity documentation for a list of types.
bytesstringThe ABI byte code to decode

Returns

unknown

  • The decoded parameter

Example

  const res = web3.eth.abi.decodeParameter(
"uint256",
"0x0000000000000000000000000000000000000000000000000000000000000010"
);
console.log(res);
> 16n

const res = web3.eth.abi.decodeParameter(
"string",
"0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000848656c6c6f212521000000000000000000000000000000000000000000000000"
);

console.log(res);
> Hello!%!

const res = web3.eth.abi.decodeParameter(
{
ParentStruct: {
propertyOne: "uint256",
propertyTwo: "uint256",
childStruct: {
propertyOne: "uint256",
propertyTwo: "uint256",
},
},
},
"0x000000000000000000000000000000000000000000000000000000000000002a0000000000000000000000000000000000000000000000000000000000000038000000000000000000000000000000000000000000000000000000000000002d000000000000000000000000000000000000000000000000000000000000004e"
);

console.log(res);
{
'0': 42n,
'1': 56n,
'2': {
'0': 45n,
'1': 78n,
__length__: 2,
propertyOne: 45n,
propertyTwo: 78n
},
__length__: 3,
propertyOne: 42n,
propertyTwo: 56n,
childStruct: {
'0': 45n,
'1': 78n,
__length__: 2,
propertyOne: 45n,
propertyTwo: 78n
}
}

decodeParameters

decodeParameters(abi, bytes): Object

Decodes ABI encoded parameters to its JavaScript types.

Parameters

NameTypeDescription
abiAbiInput[]An array of AbiInput. See the Solidity documentation for a list of types.
bytesstringThe ABI byte code to decode

Returns

Object

  • The result object containing the decoded parameters.
NameType
__length__number

Example

let res = web3.eth.abi.decodeParameters(
["string", "uint256"],
"0x000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000ea000000000000000000000000000000000000000000000000000000000000000848656c6c6f212521000000000000000000000000000000000000000000000000"
);
console.log(res);
> { '0': 'Hello!%!', '1': 234n, __length__: 2 }

let res = web3.eth.abi.decodeParameters(
[
{
type: "string",
name: "myString",
},
{
type: "uint256",
name: "myNumber",
},
],
"0x000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000ea000000000000000000000000000000000000000000000000000000000000000848656c6c6f212521000000000000000000000000000000000000000000000000"
);
console.log(res);
> {
'0': 'Hello!%!',
'1': 234n,
__length__: 2,
myString: 'Hello!%!',
myNumber: 234n
}

const res = web3.eth.abi.decodeParameters(
[
"uint8[]",
{
ParentStruct: {
propertyOne: "uint256",
propertyTwo: "uint256",
childStruct: {
propertyOne: "uint256",
propertyTwo: "uint256",
},
},
},
],
"0x00000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002a0000000000000000000000000000000000000000000000000000000000000038000000000000000000000000000000000000000000000000000000000000002d000000000000000000000000000000000000000000000000000000000000004e0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000002a0000000000000000000000000000000000000000000000000000000000000018"
);
console.log(res);
>
'0': [ 42n, 24n ],
'1': {
'0': 42n,
'1': 56n,
'2': {
'0': 45n,
'1': 78n,
__length__: 2,
propertyOne: 45n,
propertyTwo: 78n
},
__length__: 3,
propertyOne: 42n,
propertyTwo: 56n,
childStruct: {
'0': 45n,
'1': 78n,
__length__: 2,
propertyOne: 45n,
propertyTwo: 78n
}
},
__length__: 2,
ParentStruct: {
'0': 42n,
'1': 56n,
'2': {
'0': 45n,
'1': 78n,
__length__: 2,
propertyOne: 45n,
propertyTwo: 78n
},
__length__: 3,
propertyOne: 42n,
propertyTwo: 56n,
childStruct: {
'0': 45n,
'1': 78n,
__length__: 2,
propertyOne: 45n,
propertyTwo: 78n
}
}
}

decodeParametersWith

decodeParametersWith(abis, bytes, loose): Object

Should be used to decode list of params

Parameters

NameType
abisAbiInput[]
bytesstring
looseboolean

Returns

Object

NameType
__length__number

encodeErrorSignature

encodeErrorSignature(functionName): string

Encodes the error name to its ABI signature, which are the sha3 hash of the error name including input types.

Parameters

NameType
functionNamestring | AbiErrorFragment

Returns

string


encodeEventSignature

encodeEventSignature(functionName): string

Encodes the event name to its ABI signature, which are the sha3 hash of the event name including input types.

Parameters

NameTypeDescription
functionNamestring | AbiEventFragmentThe event name to encode, or the AbiEventFragment object of the event. If string, it has to be in the form of eventName(param1Type,param2Type,...). eg: myEvent(uint256,bytes32).

Returns

string

  • The ABI signature of the event.

Example

const event = web3.eth.abi.encodeEventSignature({
name: "myEvent",
type: "event",
inputs: [
{
type: "uint256",
name: "myNumber",
},
{
type: "bytes32",
name: "myBytes",
},
],
});
console.log(event);
> 0xf2eeb729e636a8cb783be044acf6b7b1e2c5863735b60d6daae84c366ee87d97

const event = web3.eth.abi.encodeEventSignature({
inputs: [
{
indexed: true,
name: "from",
type: "address",
},
{
indexed: true,
name: "to",
type: "address",
},
{
indexed: false,
name: "value",
type: "uint256",
},
],
name: "Transfer",
type: "event",
});
console.log(event);
> 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef

encodeFunctionCall

encodeFunctionCall(jsonInterface, params): string

Encodes a function call using its JSON interface object and given parameters. The JSON interface spec documentation https://docs.soliditylang.org/en/latest/abi-spec.html#json

Parameters

NameTypeDescription
jsonInterfaceAbiFunctionFragmentThe JSON interface object of the function.
paramsunknown[]The parameters to encode

Returns

string

  • The ABI encoded function call, which, means the function signature and the parameters passed.

Example

const sig = web3.eth.abi.encodeFunctionCall(
{
name: "myMethod",
type: "function",
inputs: [
{
type: "uint256",
name: "myNumber",
},
{
type: "string",
name: "myString",
},
],
},
["2345675643", "Hello!%"]
);
console.log(sig);
> 0x24ee0097000000000000000000000000000000000000000000000000000000008bd02b7b0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000748656c6c6f212500000000000000000000000000000000000000000000000000

const sig = web3.eth.abi.encodeFunctionCall(
{
inputs: [
{
name: "account",
type: "address",
},
],
name: "balanceOf",
outputs: [
{
name: "",
type: "uint256",
},
],
stateMutability: "view",
type: "function",
},
["0x1234567890123456789012345678901234567890"]
);

console.log(sig);
> 0x70a082310000000000000000000000001234567890123456789012345678901234567890

encodeFunctionSignature

encodeFunctionSignature(functionName): string

Encodes the function name to its ABI representation, which are the first 4 bytes of the sha3 of the function name including types. The JSON interface spec documentation https://docs.soliditylang.org/en/latest/abi-spec.html#json

Parameters

NameTypeDescription
functionNamestring | AbiFunctionFragmentThe function name to encode or the JSON interface object of the function. If the passed parameter is a string, it has to be in the form of functionName(param1Type,param2Type,...). eg: myFunction(uint256,uint32[],bytes10,bytes)

Returns

string

  • The ABI signature of the function.

Example

const signature = web3.eth.abi.encodeFunctionSignature({
name: "myMethod",
type: "function",
inputs: [
{
type: "uint256",
name: "myNumber",
},
{
type: "string",
name: "myString",
},
],
});
console.log(signature);
> 0x24ee0097

const signature = web3.eth.abi.encodeFunctionSignature('myMethod(uint256,string)')
console.log(signature);
> 0x24ee0097

const signature = web3.eth.abi.encodeFunctionSignature('safeTransferFrom(address,address,uint256,bytes)');
console.log(signature);
> 0xb88d4fde

encodeParameter

encodeParameter(abi, param): string

Encodes a parameter based on its type to its ABI representation.

Parameters

NameTypeDescription
abiAbiInputThe type of the parameter. See the Solidity documentation for a list of types.
paramunknownThe actual parameter to encode.

Returns

string

  • The ABI encoded parameter

Example

 const res = web3.eth.abi.encodeParameter("uint256", "2345675643");
console.log(res);
0x000000000000000000000000000000000000000000000000000000008bd02b7b

const res = web3.eth.abi.encodeParameter("uint", "2345675643");

console.log(res);
>0x000000000000000000000000000000000000000000000000000000008bd02b7b

const res = web3.eth.abi.encodeParameter("bytes32", "0xdf3234");

console.log(res);
>0xdf32340000000000000000000000000000000000000000000000000000000000

const res = web3.eth.abi.encodeParameter("bytes", "0xdf3234");

console.log(res);
> 0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003df32340000000000000000000000000000000000000000000000000000000000

const res = web3.eth.abi.encodeParameter("bytes32[]", ["0xdf3234", "0xfdfd"]);

console.log(res);
> 0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002df32340000000000000000000000000000000000000000000000000000000000fdfd000000000000000000000000000000000000000000000000000000000000

const res = web3.eth.abi.encodeParameter(
{
ParentStruct: {
propertyOne: "uint256",
propertyTwo: "uint256",
childStruct: {
propertyOne: "uint256",
propertyTwo: "uint256",
},
},
},
{
propertyOne: 42,
propertyTwo: 56,
childStruct: {
propertyOne: 45,
propertyTwo: 78,
},
}
);

console.log(res);
> 0x000000000000000000000000000000000000000000000000000000000000002a0000000000000000000000000000000000000000000000000000000000000038000000000000000000000000000000000000000000000000000000000000002d000000000000000000000000000000000000000000000000000000000000004e

encodeParameters

encodeParameters(abi, params): string

Encodes a parameter based on its type to its ABI representation.

Parameters

NameTypeDescription
abireadonly AbiInput[]An array of AbiInput. See Solidity's documentation for more details.
paramsunknown[]The actual parameters to encode.

Returns

string

  • The ABI encoded parameters

Example

const res = web3.eth.abi.encodeParameters(
["uint256", "string"],
["2345675643", "Hello!%"]
);

console.log(res);
> 0x000000000000000000000000000000000000000000000000000000008bd02b7b0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000748656c6c6f212500000000000000000000000000000000000000000000000000

getMessage

getMessage(typedData, hash?): string

Get the EIP-191 encoded message to sign, from the typedData object. If hash is enabled, the message will be hashed with Keccak256.

Parameters

NameType
typedDataEip712TypedData
hash?boolean

Returns

string


inferTypesAndEncodeParameters

inferTypesAndEncodeParameters(params): string

Infer a smart contract method parameter type and then encode this parameter.

Parameters

NameTypeDescription
paramsunknown[]The parameters to encode.

Returns

string

  • The ABI encoded parameters

Remarks

This method is useful when you don't know the type of the parameters you want to encode. It will infer the type of the parameters and then encode them. However, it is not recommended to use this method when you know the type of the parameters you want to encode. In this case, use the encodeParameters method instead. The type inference is not perfect and can lead to unexpected results. Especially when you want to encode an array, uint that is not uint256 or bytes....

Example

const res = web3.eth.abi.encodeParameters(
["2345675643", "Hello!%"]
);

console.log(res);
> 0x000000000000000000000000000000000000000000000000000000008bd02b7b0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000748656c6c6f212500000000000000000000000000000000000000000000000000