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
decodeFunctionCall
▸ decodeFunctionCall(functionsAbi
, data
, methodSignatureProvided?
): DecodedParams
& { __method__
: string
}
Decodes a function call data using its JSON interface
object.
The JSON interface spec documentation https://docs.soliditylang.org/en/latest/abi-spec.html#json
Parameters
Name | Type | Default value | Description |
---|---|---|---|
functionsAbi | AbiConstructorFragment | AbiFunctionFragment | undefined | The JSON interface object of the function. |
data | string | undefined | The data to decode |
methodSignatureProvided | boolean | true | (Optional) if false do not remove the first 4 bytes that would rather contain the function signature. |
Returns
DecodedParams
& { __method__
: string
}
- The data decoded according to the passed ABI.
Example
const data =
'0xa413686200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000548656c6c6f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010416e6f74686572204772656574696e6700000000000000000000000000000000';
const params = decodeFunctionCall(
{
inputs: [
{ internalType: 'string', name: '_greeting', type: 'string' },
{ internalType: 'string', name: '_second_greeting', type: 'string' },
],
name: 'setGreeting',
outputs: [
{ internalType: 'bool', name: '', type: 'bool' },
{ internalType: 'string', name: '', type: 'string' },
],
stateMutability: 'nonpayable',
type: 'function',
},
data,
);
console.log(params);
> {
> '0': 'Hello',
> '1': 'Another Greeting',
> __length__: 2,
> __method__: 'setGreeting(string,string)',
> _greeting: 'Hello',
> _second_greeting: 'Another Greeting',
> }
decodeFunctionReturn
▸ decodeFunctionReturn(functionsAbi
, returnValues?
): unknown
Decodes a function call data using its JSON interface
object.
The JSON interface spec documentation https://docs.soliditylang.org/en/latest/abi-spec.html#json
Parameters
Name | Type | Description |
---|---|---|
functionsAbi | AbiFunctionFragment | The JSON interface object of the function. |
returnValues? | string | The data (the function-returned-values) to decoded |
Returns
unknown
-
The ABI encoded function call, which, means the function signature and the parameters passed.
-
The function-returned-values decoded according to the passed ABI. If there are multiple values, it returns them as an object as the example below. But if it is a single value, it returns it only for simplicity.
Example
// decode a multi-value data of a method
const data =
'0x00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000548656c6c6f000000000000000000000000000000000000000000000000000000';
const decodedResult = decodeFunctionReturn(
{
inputs: [
{ internalType: 'string', name: '_greeting', type: 'string' }
],
name: 'setGreeting',
outputs: [
{ internalType: 'string', name: '', type: 'string' },
{ internalType: 'bool', name: '', type: 'bool' },
],
stateMutability: 'nonpayable',
type: 'function',
},
data,
);
console.log(decodedResult);
> { '0': 'Hello', '1': true, __length__: 2 }
// decode a single-value data of a method
const data =
'0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000548656c6c6f000000000000000000000000000000000000000000000000000000';
const decodedResult = decodeFunctionReturn(
{
inputs: [
{ internalType: 'string', name: '_greeting', type: 'string' }
],
name: 'setGreeting',
outputs: [{ internalType: 'string', name: '', type: 'string' }],
stateMutability: 'nonpayable',
type: 'function',
},
data,
);
console.log(decodedResult);
> 'Hello'
decodeLog
▸ decodeLog<ReturnType
>(inputs
, data
, topics
): ReturnType
Decodes ABI-encoded log data and indexed topic data.
Type parameters
Name | Type |
---|---|
ReturnType | extends DecodedParams <ReturnType > |
Parameters
Name | Type | Description |
---|---|---|
inputs | readonly AbiParameter [] | AbiParameter [] | A AbiParameter input array. See the Solidity documentation for a list of types. |
data | string | The ABI byte code in the data field of a log. |
topics | string | 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
Name | Type | Description |
---|---|---|
abi | AbiInput | The type of the parameter. See the Solidity documentation for a list of types. |
bytes | string | The 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
Name | Type | Description |
---|---|---|
abi | AbiInput [] | readonly AbiInput [] | An array of AbiInput. See the Solidity documentation for a list of types. |
bytes | string | The ABI byte code to decode |
Returns
Object
- The result object containing the decoded parameters.
Name | Type |
---|---|
__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
Name | Type |
---|---|
abis | AbiInput [] | readonly AbiInput [] |
bytes | string |
loose | boolean |
Returns
Object
Name | Type |
---|---|
__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
Name | Type |
---|---|
functionName | string | 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
Name | Type | Description |
---|---|---|
functionName | string | AbiEventFragment | The 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
Name | Type | Description |
---|---|---|
jsonInterface | AbiFunctionFragment | The JSON interface object of the function. |
params | unknown [] | 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
Name | Type | Description |
---|---|---|
functionName | string | AbiFunctionFragment | The 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
Name | Type | Description |
---|---|---|
abi | AbiInput | The type of the parameter. See the Solidity documentation for a list of types. |
param | unknown | The 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
Name | Type | Description |
---|---|---|
abi | readonly AbiInput [] | An array of AbiInput. See Solidity's documentation for more details. |
params | unknown [] | 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
Name | Type |
---|---|
typedData | Eip712TypedData |
hash? | boolean |
Returns
string
inferTypesAndEncodeParameters
▸ inferTypesAndEncodeParameters(params
): string
Infer a smart contract method parameter type and then encode this parameter.
Parameters
Name | Type | Description |
---|---|---|
params | unknown [] | 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