0
# Parameter Processing
1
2
Low-level parameter encoding and decoding operations for individual values and parameter arrays. Supports all Solidity data types including complex structs, arrays, and custom types.
3
4
## Capabilities
5
6
### Encode Single Parameter
7
8
Encodes a parameter based on its type to its ABI representation.
9
10
```typescript { .api }
11
/**
12
* Encodes a parameter based on its type to its ABI representation
13
* @param abi - The type of the parameter (Solidity type)
14
* @param param - The actual parameter to encode
15
* @returns The ABI encoded parameter
16
*/
17
function encodeParameter(abi: AbiInput, param: unknown): string;
18
```
19
20
**Usage Examples:**
21
22
```typescript
23
import { encodeParameter } from "web3-eth-abi";
24
25
// Encode uint256
26
const encoded1 = encodeParameter("uint256", "2345675643");
27
console.log(encoded1);
28
// 0x000000000000000000000000000000000000000000000000000000008bd02b7b
29
30
// Encode bytes32
31
const encoded2 = encodeParameter("bytes32", "0xdf3234");
32
console.log(encoded2);
33
// 0xdf32340000000000000000000000000000000000000000000000000000000000
34
35
// Encode dynamic bytes
36
const encoded3 = encodeParameter("bytes", "0xdf3234");
37
console.log(encoded3);
38
// 0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003df32340000000000000000000000000000000000000000000000000000000000
39
40
// Encode array
41
const encoded4 = encodeParameter("bytes32[]", ["0xdf3234", "0xfdfd"]);
42
console.log(encoded4);
43
// 0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002df32340000000000000000000000000000000000000000000000000000000000fdfd000000000000000000000000000000000000000000000000000000000000
44
45
// Encode struct
46
const encoded5 = encodeParameter(
47
{
48
ParentStruct: {
49
propertyOne: "uint256",
50
propertyTwo: "uint256",
51
childStruct: {
52
propertyOne: "uint256",
53
propertyTwo: "uint256"
54
}
55
}
56
},
57
{
58
propertyOne: 42,
59
propertyTwo: 56,
60
childStruct: {
61
propertyOne: 45,
62
propertyTwo: 78
63
}
64
}
65
);
66
console.log(encoded5);
67
// 0x000000000000000000000000000000000000000000000000000000000000002a0000000000000000000000000000000000000000000000000000000000000038000000000000000000000000000000000000000000000000000000000000002d000000000000000000000000000000000000000000000000000000000000004e
68
```
69
70
### Encode Multiple Parameters
71
72
Encodes multiple parameters based on their types to ABI representation.
73
74
```typescript { .api }
75
/**
76
* Encodes multiple parameters based on their types to ABI representation
77
* @param abi - Array of parameter types
78
* @param params - Array of parameter values to encode
79
* @returns The ABI encoded parameters
80
*/
81
function encodeParameters(
82
abi: AbiInput[] | ReadonlyArray<AbiInput>,
83
params: unknown[]
84
): string;
85
```
86
87
**Usage Examples:**
88
89
```typescript
90
import { encodeParameters } from "web3-eth-abi";
91
92
// Encode multiple basic types
93
const encoded1 = encodeParameters(
94
["uint256", "string"],
95
["2345675643", "Hello!%"]
96
);
97
console.log(encoded1);
98
// 0x000000000000000000000000000000000000000000000000000000008bd02b7b0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000748656c6c6f212500000000000000000000000000000000000000000000000000
99
100
// Encode with named parameters
101
const encoded2 = encodeParameters(
102
[
103
{ type: "uint256", name: "myNumber" },
104
{ type: "string", name: "myString" }
105
],
106
["2345675643", "Hello!%"]
107
);
108
console.log(encoded2);
109
// Same output as above
110
```
111
112
### Infer Types and Encode Parameters
113
114
Automatically infers parameter types and encodes them. Useful when working with JavaScript values without explicit type definitions.
115
116
```typescript { .api }
117
/**
118
* Infers parameter types from values and encodes them
119
* @param params - Array of parameter values to infer types from and encode
120
* @returns The ABI encoded parameters with inferred types
121
*/
122
function inferTypesAndEncodeParameters(params: unknown[]): string;
123
```
124
125
### Decode Single Parameter
126
127
Decodes an ABI encoded parameter to its JavaScript type.
128
129
```typescript { .api }
130
/**
131
* Decodes an ABI encoded parameter to its JavaScript type
132
* @param abi - The type of the parameter
133
* @param bytes - The ABI byte code to decode
134
* @returns The decoded parameter
135
*/
136
function decodeParameter(abi: AbiInput, bytes: HexString): unknown;
137
```
138
139
**Usage Examples:**
140
141
```typescript
142
import { decodeParameter } from "web3-eth-abi";
143
144
// Decode uint256
145
const result1 = decodeParameter(
146
"uint256",
147
"0x0000000000000000000000000000000000000000000000000000000000000010"
148
);
149
console.log(result1); // 16n
150
151
// Decode string
152
const result2 = decodeParameter(
153
"string",
154
"0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000848656c6c6f212521000000000000000000000000000000000000000000000000"
155
);
156
console.log(result2); // "Hello!%!"
157
158
// Decode struct
159
const result3 = decodeParameter(
160
{
161
ParentStruct: {
162
propertyOne: "uint256",
163
propertyTwo: "uint256",
164
childStruct: {
165
propertyOne: "uint256",
166
propertyTwo: "uint256"
167
}
168
}
169
},
170
"0x000000000000000000000000000000000000000000000000000000000000002a0000000000000000000000000000000000000000000000000000000000000038000000000000000000000000000000000000000000000000000000000000002d000000000000000000000000000000000000000000000000000000000000004e"
171
);
172
console.log(result3);
173
// {
174
// '0': 42n,
175
// '1': 56n,
176
// '2': {
177
// '0': 45n,
178
// '1': 78n,
179
// __length__: 2,
180
// propertyOne: 45n,
181
// propertyTwo: 78n
182
// },
183
// __length__: 3,
184
// propertyOne: 42n,
185
// propertyTwo: 56n,
186
// childStruct: {
187
// '0': 45n,
188
// '1': 78n,
189
// __length__: 2,
190
// propertyOne: 45n,
191
// propertyTwo: 78n
192
// }
193
// }
194
```
195
196
### Decode Multiple Parameters
197
198
Decodes ABI encoded parameters to their JavaScript types.
199
200
```typescript { .api }
201
/**
202
* Decodes ABI encoded parameters to their JavaScript types
203
* @param abi - Array of parameter types
204
* @param bytes - The ABI byte code to decode
205
* @returns Object containing decoded parameters with numeric and named keys
206
*/
207
function decodeParameters(
208
abi: AbiInput[] | ReadonlyArray<AbiInput>,
209
bytes: HexString
210
): { [key: string]: unknown; __length__: number };
211
```
212
213
**Usage Examples:**
214
215
```typescript
216
import { decodeParameters } from "web3-eth-abi";
217
218
// Decode multiple basic types
219
const result1 = decodeParameters(
220
["string", "uint256"],
221
"0x000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000ea000000000000000000000000000000000000000000000000000000000000000848656c6c6f212521000000000000000000000000000000000000000000000000"
222
);
223
console.log(result1);
224
// { '0': 'Hello!%!', '1': 234n, __length__: 2 }
225
226
// Decode with named parameters
227
const result2 = decodeParameters(
228
[
229
{ type: "string", name: "myString" },
230
{ type: "uint256", name: "myNumber" }
231
],
232
"0x000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000ea000000000000000000000000000000000000000000000000000000000000000848656c6c6f212521000000000000000000000000000000000000000000000000"
233
);
234
console.log(result2);
235
// {
236
// '0': 'Hello!%!',
237
// '1': 234n,
238
// __length__: 2,
239
// myString: 'Hello!%!',
240
// myNumber: 234n
241
// }
242
243
// Decode complex structure with array and struct
244
const result3 = decodeParameters(
245
[
246
"uint8[]",
247
{
248
ParentStruct: {
249
propertyOne: "uint256",
250
propertyTwo: "uint256",
251
childStruct: {
252
propertyOne: "uint256",
253
propertyTwo: "uint256"
254
}
255
}
256
}
257
],
258
"0x00000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002a0000000000000000000000000000000000000000000000000000000000000038000000000000000000000000000000000000000000000000000000000000002d000000000000000000000000000000000000000000000000000000000000004e0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000002a0000000000000000000000000000000000000000000000000000000000000018"
259
);
260
console.log(result3);
261
// Complex nested structure with array and struct
262
```
263
264
### Decode Parameters with Loose Mode
265
266
Decodes parameters with optional loose mode for more permissive decoding.
267
268
```typescript { .api }
269
/**
270
* Decodes parameters with loose mode option
271
* @param abis - Array of parameter types
272
* @param bytes - The ABI byte code to decode
273
* @param loose - Enable loose mode for permissive decoding
274
* @returns Object containing decoded parameters
275
*/
276
function decodeParametersWith(
277
abis: AbiInput[] | ReadonlyArray<AbiInput>,
278
bytes: HexString,
279
loose: boolean
280
): { [key: string]: unknown; __length__: number };
281
```
282
283
## Types
284
285
```typescript { .api }
286
interface AbiInput {
287
name?: string;
288
type: string;
289
indexed?: boolean;
290
components?: AbiInput[];
291
}
292
293
type HexString = string;
294
```