/
abi.ts
245 lines (219 loc) · 7.55 KB
/
abi.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
import type { ResolvedConfig } from './config'
import type { Prettify, Range } from './types'
export type Address = ResolvedConfig['AddressType']
////////////////////////////////////////////////////////////////////////////////////////////////////
// Solidity Types
// Could use `Range`, but listed out for zero overhead
// prettier-ignore
export type MBytes =
| '' | 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
// prettier-ignore
export type MBits =
| '' | 8 | 16 | 24 | 32 | 40 | 48 | 56 | 64 | 72
| 80 | 88 | 96 | 104 | 112 | 120 | 128 | 136 | 144 | 152
| 160 | 168 | 176 | 184 | 192 | 200 | 208 | 216 | 224 | 232
| 240 | 248 | 256
// From https://docs.soliditylang.org/en/latest/abi-spec.html#types
export type SolidityAddress = 'address'
export type SolidityBool = 'bool'
export type SolidityBytes = `bytes${MBytes}` // `bytes<M>`: binary type of `M` bytes, `0 < M <= 32`
export type SolidityFunction = 'function'
export type SolidityString = 'string'
export type SolidityTuple = 'tuple'
export type SolidityInt = `${'u' | ''}int${MBits}` // `(u)int<M>`: (un)signed integer type of `M` bits, `0 < M <= 256`, `M % 8 == 0`
// No need to support "fixed" until Solidity does
// https://github.com/ethereum/solidity/issues/409
// `(u)fixed<M>x<N>`: (un)signed fixed-point decimal number of `M` bits, `8 <= M <= 256`, `M % 8 == 0`,
// and `0 < N <= 80`, which denotes the value `v` as `v / (10 ** N)`
// export type SolidityFixed =
// | `${'u' | ''}fixed`
// | `${'u' | ''}fixed${MBits}x${Range<1, 20>[number]}`
export type SolidityFixedArrayRange = Range<
ResolvedConfig['FixedArrayMinLength'],
ResolvedConfig['FixedArrayMaxLength']
>[number]
export type SolidityFixedArraySizeLookup = {
[Prop in SolidityFixedArrayRange as `${Prop}`]: Prop
}
/**
* Recursively build arrays up to maximum depth
* or use a more broad type when maximum depth is switched "off"
*/
type _BuildArrayTypes<
T extends string,
Depth extends ReadonlyArray<number> = [],
> = ResolvedConfig['ArrayMaxDepth'] extends false
? `${T}[${string}]`
: Depth['length'] extends ResolvedConfig['ArrayMaxDepth']
? T
: T extends `${any}[${SolidityFixedArrayRange | ''}]`
? _BuildArrayTypes<T | `${T}[${SolidityFixedArrayRange | ''}]`, [...Depth, 1]>
: _BuildArrayTypes<`${T}[${SolidityFixedArrayRange | ''}]`, [...Depth, 1]>
// Modeling fixed-length (`<type>[M]`) and dynamic (`<type>[]`) arrays
// Tuple and non-tuple versions are separated out for narrowing anywhere structs show up
export type SolidityArrayWithoutTuple = _BuildArrayTypes<
| SolidityAddress
| SolidityBool
| SolidityBytes
| SolidityFunction
| SolidityInt
| SolidityString
>
export type SolidityArrayWithTuple = _BuildArrayTypes<SolidityTuple>
export type SolidityArray = SolidityArrayWithoutTuple | SolidityArrayWithTuple
////////////////////////////////////////////////////////////////////////////////////////////////////
// Abi Types
export type AbiType =
| SolidityArray
| SolidityAddress
| SolidityBool
| SolidityBytes
| SolidityFunction
| SolidityInt
| SolidityString
| SolidityTuple
type ResolvedAbiType = ResolvedConfig['StrictAbiType'] extends true
? AbiType
: string
export type AbiInternalType =
| ResolvedAbiType
| `address ${string}`
| `contract ${string}`
| `enum ${string}`
| `struct ${string}`
export type AbiParameter = Prettify<
{
type: ResolvedAbiType
name?: string
/** Representation used by Solidity compiler */
internalType?: AbiInternalType
} & (
| { type: Exclude<ResolvedAbiType, SolidityTuple | SolidityArrayWithTuple> }
| {
type: SolidityTuple | SolidityArrayWithTuple
components: readonly AbiParameter[]
}
)
>
/**
* State mutability for {@link AbiFunction}
*
* @see https://docs.soliditylang.org/en/latest/contracts.html#state-mutability
*/
export type AbiStateMutability = 'pure' | 'view' | 'nonpayable' | 'payable'
/** Kind of {@link AbiParameter} */
export type AbiParameterKind = 'inputs' | 'outputs'
/** ABI ["function"](https://docs.soliditylang.org/en/latest/abi-spec.html#json) type */
export type AbiFunction = {
type: 'function'
/**
* @deprecated use `pure` or `view` from {@link AbiStateMutability} instead
* @see https://github.com/ethereum/solidity/issues/992
*/
constant?: boolean
/**
* @deprecated Vyper used to provide gas estimates
* @see https://github.com/vyperlang/vyper/issues/2151
*/
gas?: number
inputs: readonly AbiParameter[]
name: string
outputs: readonly AbiParameter[]
/**
* @deprecated use `payable` or `nonpayable` from {@link AbiStateMutability} instead
* @see https://github.com/ethereum/solidity/issues/992
*/
payable?: boolean
stateMutability: AbiStateMutability
}
/** ABI ["constructor"](https://docs.soliditylang.org/en/latest/abi-spec.html#json) type */
export type AbiConstructor = {
type: 'constructor'
inputs: readonly AbiParameter[]
/**
* @deprecated use `payable` or `nonpayable` from {@link AbiStateMutability} instead
* @see https://github.com/ethereum/solidity/issues/992
*/
payable?: boolean
stateMutability: Extract<AbiStateMutability, 'payable' | 'nonpayable'>
}
/** ABI ["fallback"](https://docs.soliditylang.org/en/latest/abi-spec.html#json) type */
export type AbiFallback = {
type: 'fallback'
inputs?: readonly []
/**
* @deprecated use `payable` or `nonpayable` from {@link AbiStateMutability} instead
* @see https://github.com/ethereum/solidity/issues/992
*/
payable?: boolean
stateMutability: Extract<AbiStateMutability, 'payable' | 'nonpayable'>
}
/** ABI ["receive"](https://docs.soliditylang.org/en/latest/contracts.html#receive-ether-function) type */
export type AbiReceive = {
type: 'receive'
stateMutability: Extract<AbiStateMutability, 'payable'>
}
/** ABI ["event"](https://docs.soliditylang.org/en/latest/abi-spec.html#events) type */
export type AbiEvent = {
type: 'event'
anonymous?: boolean
inputs: readonly (AbiParameter & { indexed?: boolean })[]
name: string
}
/** ABI ["error"](https://docs.soliditylang.org/en/latest/abi-spec.html#errors) type */
export type AbiError = {
type: 'error'
inputs: readonly AbiParameter[]
name: string
}
/** `"type"` name for {@link Abi} items. */
export type AbiItemType =
| 'constructor'
| 'error'
| 'event'
| 'fallback'
| 'function'
| 'receive'
/**
* Contract [ABI Specification](https://docs.soliditylang.org/en/latest/abi-spec.html#json)
*/
export type Abi = readonly (
| AbiConstructor
| AbiError
| AbiEvent
| AbiFallback
| AbiFunction
| AbiReceive
)[]
////////////////////////////////////////////////////////////////////////////////////////////////////
// Typed Data Types
export type TypedDataDomain = {
chainId?: string | number | bigint
name?: string
salt?: ResolvedConfig['BytesType']['outputs']
verifyingContract?: Address
version?: string
}
// Subset of `AbiType` that excludes `tuple`, `function`, and dynamic aliases `int` and `uint`
export type TypedDataType = Exclude<
AbiType,
SolidityFunction | SolidityTuple | SolidityArrayWithTuple | 'int' | 'uint'
>
export type TypedDataParameter = {
name: string
type: TypedDataType | keyof TypedData | `${keyof TypedData}[${string | ''}]`
}
/**
* [EIP-712](https://eips.ethereum.org/EIPS/eip-712#definition-of-typed-structured-data-%F0%9D%95%8A) Typed Data Specification
*/
export type TypedData = Prettify<
{
[key: string]: readonly TypedDataParameter[]
} & {
// Disallow `TypedDataType` as key names (e.g. `address`)
[_ in TypedDataType]?: never
}
>