-
Notifications
You must be signed in to change notification settings - Fork 5.1k
/
Copy pathtypes.ts
207 lines (178 loc) · 6.11 KB
/
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
import {
Address,
Bytes,
Numbers,
ObjectValueToTuple,
ArrayToIndexObject,
FixedSizeArray,
} from 'web3-utils';
import { ConvertToNumber } from './number_map_type';
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>;
};
type FragmentTypes = 'constructor' | 'event' | 'function' | 'fallback';
export type AbiBaseFragment = {
readonly type: FragmentTypes;
};
// https://docs.soliditylang.org/en/latest/abi-spec.html#json
export type AbiConstructorFragment = AbiBaseFragment & {
readonly type: 'constructor';
readonly stateMutability: '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: 'function';
readonly stateMutability: '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: 'fallback';
readonly stateMutability: '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: 'event';
readonly inputs: ReadonlyArray<AbiParameter>;
readonly anonymous?: boolean;
};
// https://docs.soliditylang.org/en/latest/abi-spec.html#json
export type AbiFragment =
| AbiConstructorFragment
| AbiFunctionFragment
| AbiEventFragment
| AbiFallbackFragment;
export type ContractAbi = ReadonlyArray<AbiFragment>;
export type AbiInput = string | AbiParameter | { readonly [key: string]: unknown };
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,
Components extends ReadonlyArray<AbiParameter> | undefined = [],
> = Components extends ReadonlyArray<AbiParameter>
? Type extends 'tuple'
? {
// eslint-disable-next-line no-use-before-define
[Param in Components[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 Components[number] as Param['name']]: MatchPrimitiveType<
Param['type'],
Param['components']
>;
},
Size
>
: never
: never;
export type MatchPrimitiveType<
Type extends string,
Components extends ReadonlyArray<AbiParameter> | undefined,
> =
| PrimitiveAddressType<Type>
| PrimitiveStringType<Type>
| PrimitiveBooleanType<Type>
| PrimitiveIntegerType<Type>
| PrimitiveBytesType<Type>
| PrimitiveTupleType<Type, Components>
| never;
// Only intended to use locally so why not exported
// TODO: Inspect Record<string, AbiParameter> not working constraint
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type _ExtractParameterType<T extends Record<string, any>> = {
[K in keyof T]: MatchPrimitiveType<T[K]['type'], T[K]['components']>;
};
export type ContractMethodOutputParameters<Params extends ReadonlyArray<AbiParameter>> =
ObjectValueToTuple<_ExtractParameterType<ArrayToIndexObject<Params>>>;
export type ContractMethodInputParameters<Params extends ReadonlyArray<AbiParameter>> =
Params extends readonly []
? never
: {
[Param in Params[number] as Param['name']]: MatchPrimitiveType<
Param['type'],
Param['components']
>;
};
export type ContractConstructor<Abis extends ContractAbi> = {
[Abi in FilterAbis<Abis, AbiConstructorFragment> as 'constructor']: {
readonly Abi: Abi;
readonly Inputs: ContractMethodInputParameters<Abi['inputs']>;
};
}['constructor'];
export type ContractMethods<Abis extends ContractAbi> = {
[Abi in FilterAbis<Abis, AbiFunctionFragment> as Abi['name']]: {
readonly Abi: Abi;
readonly Inputs: ContractMethodInputParameters<Abi['inputs']>;
readonly Outputs: ContractMethodOutputParameters<Abi['outputs']>;
};
};
export type ContractEvents<Abis extends ContractAbi> = {
[Abi in FilterAbis<Abis, AbiEventFragment> as Abi['name']]: {
readonly Abi: Abi;
readonly Inputs: ContractMethodInputParameters<Abi['inputs']>;
};
};