-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
eth_abi_types.ts
364 lines (317 loc) · 10.4 KB
/
eth_abi_types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
This file is part of web3.js.
web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
import { Address } from './eth_types.js';
import { Bytes, Numbers } from './primitives_types.js';
import { FixedSizeArray } from './utility_types.js';
type _SolidityIndexRange =
| 1
| 2
| 3
| 4
| 5
| 6
| 7
| 8
| 9
| 10
| 11
| 12
| 13
| 14
| 15
| 16
| 17
| 18
| 19
| 20
| 21
| 22
| 25
| 26
| 27
| 28
| 29
| 30;
export type ConvertToNumber<
T extends string,
Range extends number = _SolidityIndexRange,
> = Range extends unknown ? (`${Range}` extends T ? Range : never) : never;
export type Components = {
name: string;
type: string;
indexed?: boolean;
components?: Components[];
};
export interface AbiStruct {
[key: string]: unknown;
name: string;
type: string;
}
export interface AbiCoderStruct extends AbiStruct {
[key: string]: unknown;
components?: Array<AbiStruct>;
}
// https://docs.soliditylang.org/en/latest/abi-spec.html#json
export type AbiParameter = {
readonly name: string;
readonly type: string;
readonly baseType?: string;
readonly indexed?: boolean;
readonly components?: ReadonlyArray<AbiParameter>;
readonly arrayLength?: number;
readonly arrayChildren?: ReadonlyArray<AbiParameter>;
readonly internalType?: string;
};
type FragmentTypes = 'constructor' | 'event' | 'function' | 'fallback' | 'receive';
export type AbiBaseFragment = {
// type will default to string if passed ABI is declared without "as const"
readonly type: string | FragmentTypes;
};
// To assign an ABI which is not declared `as const` need to specify a generic string
// https://docs.soliditylang.org/en/latest/abi-spec.html#json
export type AbiConstructorFragment = AbiBaseFragment & {
readonly type: string | 'constructor';
readonly stateMutability: string | 'nonpayable' | 'payable';
readonly inputs?: ReadonlyArray<AbiParameter>;
};
// https://docs.soliditylang.org/en/latest/abi-spec.html#json
export type AbiFunctionFragment = AbiBaseFragment & {
readonly name: string;
readonly type: string | 'function';
readonly stateMutability?: string | 'nonpayable' | 'payable' | 'pure' | 'view';
readonly inputs?: ReadonlyArray<AbiParameter>;
readonly outputs?: ReadonlyArray<AbiParameter>;
readonly constant?: boolean; // stateMutability == 'pure' or stateMutability == 'view'
readonly payable?: boolean; // stateMutability == 'payable'
};
export type AbiFallbackFragment = AbiBaseFragment & {
readonly name: never;
readonly type: string | 'fallback';
readonly stateMutability: string | 'nonpayable' | 'payable' | 'pure' | 'view';
readonly inputs: never;
readonly outputs: never;
// legacy properties
readonly constant?: boolean; // stateMutability == 'pure' or stateMutability == 'view'
readonly payable?: boolean; // stateMutability == 'payable'
};
// https://docs.soliditylang.org/en/latest/abi-spec.html#json
export type AbiEventFragment = AbiBaseFragment & {
readonly name: string;
readonly type: string | 'event';
readonly inputs?: ReadonlyArray<AbiParameter>;
readonly anonymous?: boolean;
};
// https://docs.soliditylang.org/en/latest/abi-spec.html#errors
export type AbiErrorFragment = AbiBaseFragment & {
readonly name: string;
readonly type: string | 'error';
readonly inputs?: ReadonlyArray<AbiParameter>;
};
// https://docs.soliditylang.org/en/latest/abi-spec.html#json
export type AbiFragment =
| AbiConstructorFragment
| AbiFunctionFragment
| AbiEventFragment
| AbiErrorFragment
| AbiFallbackFragment;
export type ContractAbi = ReadonlyArray<AbiFragment>;
export type AbiInput =
| string
| AbiParameter
| {
name: string;
type: string;
components?: Components;
index?: boolean;
internalType?: string;
}
| { readonly [key: string]: unknown };
// https://docs.soliditylang.org/en/develop/abi-spec.html#json
export type JsonFunctionInterface = {
type: 'function';
name: string;
inputs: Components[];
outputs?: AbiInput[];
stateMutability?: string;
};
export type JsonEventInterface = {
type: 'event';
name: string;
inputs: Components[];
indexed: boolean;
anonymous: boolean;
};
export type FilterAbis<Abis extends ContractAbi, Filter, Abi = Abis[number]> = Abi extends Filter
? Abi
: never;
type _TypedArray<Type, Size extends string> = Size extends ''
? Type[]
: FixedSizeArray<Type, ConvertToNumber<Size>>;
export type PrimitiveAddressType<Type extends string> = Type extends `address[${infer Size}]`
? _TypedArray<Address, Size>
: Type extends 'address'
? Address
: never;
export type PrimitiveStringType<Type extends string> = Type extends `string${string}[${infer Size}]`
? _TypedArray<string, Size>
: Type extends 'string' | `string${string}`
? string
: never;
export type PrimitiveBooleanType<Type extends string> = Type extends `bool[${infer Size}]`
? _TypedArray<boolean, Size>
: Type extends 'bool'
? boolean
: never;
export type PrimitiveIntegerType<Type extends string> = Type extends
| `uint${string}[${infer Size}]`
| `int${string}[${infer Size}]`
? _TypedArray<Numbers, Size>
: Type extends 'uint' | 'int' | `int${string}` | `uint${string}`
? Numbers
: never;
export type PrimitiveBytesType<Type extends string> = Type extends `bytes${string}[${infer Size}]`
? _TypedArray<Bytes, Size>
: Type extends 'bytes' | `bytes${string}`
? Bytes
: never;
export type PrimitiveTupleType<
Type extends string,
TypeComponents extends ReadonlyArray<AbiParameter> | undefined | unknown = [],
> = TypeComponents extends ReadonlyArray<AbiParameter>
? Type extends 'tuple'
? {
// eslint-disable-next-line no-use-before-define
[Param in TypeComponents[number] as Param['name']]: MatchPrimitiveType<
Param['type'],
Param['components']
>;
}
: Type extends `tuple[${infer Size}]`
? _TypedArray<
{
// eslint-disable-next-line no-use-before-define
[Param in TypeComponents[number] as Param['name']]: MatchPrimitiveType<
Param['type'],
Param['components']
>;
},
Size
>
: never
: never;
type ObjectToArray<T extends unknown[]> = T extends [...infer R, infer A]
? Record<R['length'], A> & ObjectToArray<R>
: T;
type ArrToObjectWithFunctions<T extends unknown[]> = Array<unknown> & ObjectToArray<T>;
export type MatchPrimitiveType<
Type extends string,
TypeComponents extends ReadonlyArray<AbiParameter> | undefined | unknown,
> =
| PrimitiveAddressType<Type>
| PrimitiveStringType<Type>
| PrimitiveBooleanType<Type>
| PrimitiveIntegerType<Type>
| PrimitiveBytesType<Type>
| PrimitiveTupleType<Type, TypeComponents>
| never;
type ContractMethodOutputParametersRecursiveArray<
Params extends ReadonlyArray<unknown> | undefined,
> =
// check if params are empty array
Params extends readonly []
? []
: Params extends readonly [infer H, ...infer R] // check if Params is an array
? H extends AbiParameter
? [
MatchPrimitiveType<H['type'], H['components']>,
...ContractMethodOutputParametersRecursiveArray<R>,
]
: []
: [];
type ContractMethodOutputParametersRecursiveRecord<
Params extends ReadonlyArray<unknown> | undefined,
> =
// check if params are empty array
Params extends readonly []
? []
: Params extends readonly [infer H, ...infer R] // check if Params is an array
? H extends AbiParameter
? H['name'] extends '' // check if output param name is empty string
? ContractMethodOutputParametersRecursiveRecord<R>
: Record<H['name'], MatchPrimitiveType<H['type'], H['components']>> & // sets key-value pair of output param name and type
ContractMethodOutputParametersRecursiveRecord<R>
: ContractMethodOutputParametersRecursiveRecord<R>
: Params extends undefined | unknown // param is not array, check if undefined
? []
: Params;
export type ContractMethodOutputParameters<Params extends ReadonlyArray<unknown> | undefined> =
// check if params are empty array
Params extends readonly []
? void
: Params extends readonly [infer H, ...infer R] // check if Params is an array
? R extends readonly [] // if only one output in array
? H extends AbiParameter
? MatchPrimitiveType<H['type'], H['components']>
: []
: // if more than one output
ArrToObjectWithFunctions<[...ContractMethodOutputParametersRecursiveArray<Params>]> &
ContractMethodOutputParametersRecursiveRecord<Params>
: [];
export type ContractMethodInputParameters<Params extends ReadonlyArray<unknown> | undefined> =
Params extends readonly []
? []
: Params extends readonly [infer H, ...infer R]
? H extends AbiParameter
? // TODO: Find a way to set name for tuple item
[MatchPrimitiveType<H['type'], H['components']>, ...ContractMethodInputParameters<R>]
: ContractMethodInputParameters<R>
: Params extends undefined | unknown
? []
: Params;
export type ContractConstructor<Abis extends ContractAbi> = {
[Abi in FilterAbis<Abis, AbiConstructorFragment & { type: 'constructor' }> as 'constructor']: {
readonly Abi: Abi;
readonly Inputs: ContractMethodInputParameters<Abi['inputs']>;
};
}['constructor'];
export type ContractConstructorArgs<Abis extends ContractAbi> = {
[Abi in FilterAbis<
Abis,
AbiConstructorFragment & { type: 'constructor' }
> as 'constructor']: ContractMethodInputParameters<Abi['inputs']>;
}['constructor'];
export type ContractMethod<Abi extends AbiFunctionFragment> = {
readonly Abi: Abi;
readonly Inputs: ContractMethodInputParameters<Abi['inputs']>;
readonly Outputs: ContractMethodOutputParameters<Abi['outputs']>;
};
export type ContractMethods<Abis extends ContractAbi> = {
[Abi in FilterAbis<
Abis,
AbiFunctionFragment & { type: 'function' }
> as Abi['name']]: ContractMethod<Abi>;
};
export type ContractEvent<Abi extends AbiEventFragment> = {
readonly Abi: Abi;
readonly Inputs: ContractMethodInputParameters<Abi['inputs']>;
};
export type ContractEvents<Abis extends ContractAbi> = {
[Abi in FilterAbis<
Abis,
AbiEventFragment & { type: 'event' }
> as Abi['name']]: ContractEvent<Abi>;
};
export interface DecodedParams extends Record<string, unknown> {
__length__: number;
}