viem 0.0.0-w-20230816184753 → 0.0.0-w-20230818170445

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/dist/cjs/actions/public/multicall.js +3 -6
  2. package/dist/cjs/actions/public/multicall.js.map +1 -1
  3. package/dist/cjs/clients/decorators/public.js.map +1 -1
  4. package/dist/cjs/constants/number.js +96 -96
  5. package/dist/cjs/constants/number.js.map +1 -1
  6. package/dist/cjs/version.js +1 -1
  7. package/dist/esm/actions/public/multicall.js +4 -7
  8. package/dist/esm/actions/public/multicall.js.map +1 -1
  9. package/dist/esm/clients/decorators/public.js.map +1 -1
  10. package/dist/esm/constants/number.js +96 -96
  11. package/dist/esm/constants/number.js.map +1 -1
  12. package/dist/esm/version.js +1 -1
  13. package/dist/types/actions/public/multicall.d.ts +16 -10
  14. package/dist/types/actions/public/multicall.d.ts.map +1 -1
  15. package/dist/types/clients/decorators/public.d.ts +2 -2
  16. package/dist/types/clients/decorators/public.d.ts.map +1 -1
  17. package/dist/types/constants/number.d.ts.map +1 -1
  18. package/dist/types/index.d.ts +1 -1
  19. package/dist/types/index.d.ts.map +1 -1
  20. package/dist/types/types/contract.d.ts +21 -3
  21. package/dist/types/types/contract.d.ts.map +1 -1
  22. package/dist/types/types/multicall.d.ts +60 -37
  23. package/dist/types/types/multicall.d.ts.map +1 -1
  24. package/dist/types/types/utils.d.ts +8 -0
  25. package/dist/types/types/utils.d.ts.map +1 -1
  26. package/dist/types/version.d.ts +1 -1
  27. package/package.json +3 -3
  28. package/src/actions/public/multicall.ts +25 -37
  29. package/src/clients/decorators/public.ts +5 -11
  30. package/src/constants/number.ts +96 -96
  31. package/src/index.ts +1 -1
  32. package/src/types/contract.ts +106 -1
  33. package/src/types/multicall.ts +181 -90
  34. package/src/types/utils.ts +33 -0
  35. package/src/version.ts +1 -1
@@ -1,114 +1,205 @@
1
- import type { Abi, AbiStateMutability, ExtractAbiFunctionNames } from 'abitype'
1
+ import type { Abi, AbiStateMutability, Address } from 'abitype'
2
2
 
3
- import type { ContractParameters, ContractReturnType } from './contract.js'
4
-
5
- // Avoid TS depth-limit error in case of large array literal
6
- type MAXIMUM_DEPTH = 20
3
+ import type {
4
+ Args,
5
+ ContractFunctionReturnType,
6
+ FunctionName,
7
+ Widen,
8
+ } from './contract.js'
9
+ import type { MaybePartial } from './utils.js'
7
10
 
8
11
  export type MulticallContract<
9
- abi extends Abi | readonly unknown[] = Abi | readonly unknown[],
10
- stateMutability extends AbiStateMutability = AbiStateMutability,
11
- functionName extends ExtractAbiFunctionNames<
12
+ abi extends Abi | readonly unknown[] = Abi,
13
+ mutability extends AbiStateMutability = AbiStateMutability,
14
+ functionName extends FunctionName<
12
15
  abi extends Abi ? abi : Abi,
13
- stateMutability
16
+ mutability
14
17
  > = string,
15
- > = { abi: abi; functionName: functionName }
18
+ args extends Args<
19
+ abi extends Abi ? abi : Abi,
20
+ mutability,
21
+ functionName
22
+ > = Args<abi extends Abi ? abi : Abi, mutability, functionName>,
23
+ > = readonly [] extends args
24
+ ? {
25
+ address: Address
26
+ abi: abi
27
+ functionName:
28
+ | functionName
29
+ | FunctionName<abi extends Abi ? abi : Abi, mutability>
30
+ args?:
31
+ | Widen<args>
32
+ | Args<abi extends Abi ? abi : Abi, mutability, functionName>
33
+ | undefined
34
+ }
35
+ : {
36
+ address: Address
37
+ abi: abi
38
+ functionName:
39
+ | functionName
40
+ | FunctionName<abi extends Abi ? abi : Abi, mutability>
41
+ args:
42
+ | Widen<args>
43
+ | Args<abi extends Abi ? abi : Abi, mutability, functionName>
44
+ }
45
+
46
+ export type MulticallResponse<
47
+ allowFailure extends boolean = true,
48
+ result = unknown,
49
+ error = unknown,
50
+ > = allowFailure extends true
51
+ ?
52
+ | { error?: undefined; result: result; status: 'success' }
53
+ | {
54
+ error: unknown extends error ? Error : error
55
+ result?: undefined
56
+ status: 'failure'
57
+ }
58
+ : result
16
59
 
17
60
  export type MulticallContracts<
18
- contracts extends readonly MulticallContract[],
19
- properties extends Record<string, any> = object,
61
+ contracts extends readonly unknown[],
62
+ options extends {
63
+ mutability: AbiStateMutability
64
+ optional?: boolean
65
+ properties?: Record<string, any>
66
+ } = { mutability: AbiStateMutability },
20
67
  ///
21
- result extends any[] = [],
22
- depth extends readonly number[] = [],
23
- > = depth['length'] extends MAXIMUM_DEPTH
24
- ? (ContractParameters & properties)[]
25
- : contracts extends []
26
- ? []
27
- : contracts extends [infer head extends MulticallContract]
28
- ? [
68
+ result extends readonly any[] = [],
69
+ > = contracts extends readonly []
70
+ ? readonly []
71
+ : contracts extends readonly [infer contract] // One contract left before returning `result`
72
+ ? readonly [
29
73
  ...result,
30
- ContractParameters<head['abi'], 'pure' | 'view', head['functionName']> &
31
- properties,
32
- ]
33
- : contracts extends [
34
- infer head extends MulticallContract,
35
- ...infer tail extends readonly MulticallContract[],
74
+ GetMulticallContract<contract, options['mutability']> extends [never] // If `GetContract` returns `never`, then `contract` is invalid
75
+ ? // Fallback to just `abi`
76
+ MaybePartial<
77
+ Omit<MulticallContract, 'args'> & {
78
+ args?: readonly unknown[] | undefined
79
+ } & options['properties'],
80
+ options['optional']
81
+ >
82
+ : // We know `contract` is inferrable, let's get to it!
83
+ MaybePartial<
84
+ GetMulticallContract<contract, options['mutability']> &
85
+ options['properties'],
86
+ options['optional']
87
+ >,
36
88
  ]
89
+ : contracts extends readonly [infer contract, ...infer rest] // Grab first contract and recurse
37
90
  ? MulticallContracts<
38
- [...tail],
39
- properties,
91
+ [...rest],
92
+ options,
40
93
  [
41
94
  ...result,
42
- ContractParameters<head['abi'], 'pure' | 'view', head['functionName']> &
43
- properties,
44
- ],
45
- [...depth, 1]
95
+ GetMulticallContract<contract, options['mutability']> extends [never] // If `GetContract` returns `never`, then `contract` is invalid
96
+ ? // Fallback to just `abi`
97
+ MaybePartial<
98
+ Omit<MulticallContract, 'args'> & {
99
+ args?: readonly unknown[] | undefined
100
+ } & options['properties'],
101
+ options['optional']
102
+ >
103
+ : // We know `contract` is inferrable, let's get to it!
104
+ MaybePartial<
105
+ GetMulticallContract<contract, options['mutability']> &
106
+ options['properties'],
107
+ options['optional']
108
+ >,
109
+ ]
46
110
  >
47
- : unknown[] extends contracts
111
+ : readonly unknown[] extends contracts
48
112
  ? contracts
49
113
  : // If `contracts` is *some* array but we couldn't assign `unknown[]` to it, then it must hold some known/homogenous type!
50
114
  // use this to infer the param types in the case of Array.map() argument
51
- contracts extends ContractParameters<infer abi, infer _, infer functionName>[]
52
- ? (ContractParameters<abi, 'pure' | 'view', functionName> & properties)[]
53
- : // Fallback
54
- (ContractParameters & properties)[]
55
-
56
- export type MulticallResults<
57
- contracts extends readonly MulticallContract[],
58
- allowFailure extends boolean = true,
59
- ///
60
- result extends any[] = [],
61
- depth extends readonly number[] = [],
62
- > = depth['length'] extends MAXIMUM_DEPTH
63
- ? MulticallResult<ContractReturnType, allowFailure>[]
64
- : contracts extends []
65
- ? []
66
- : contracts extends [infer head extends MulticallContract]
67
- ? [
68
- ...result,
69
- MulticallResult<
70
- ContractReturnType<head['abi'], 'pure' | 'view', head['functionName']>,
71
- allowFailure
72
- >,
73
- ]
74
- : contracts extends [
75
- infer head extends MulticallContract,
76
- ...infer tail extends readonly MulticallContract[],
77
- ]
78
- ? MulticallResults<
79
- [...tail],
80
- allowFailure,
81
- [
82
- ...result,
83
- MulticallResult<
84
- ContractReturnType<
85
- head['abi'],
86
- 'pure' | 'view',
87
- head['functionName']
88
- >,
89
- allowFailure
90
- >,
91
- ],
92
- [...depth, 1]
93
- >
94
- : contracts extends ContractParameters<
115
+ contracts extends readonly MulticallContract<
95
116
  infer abi,
96
117
  infer _,
97
- infer functionName
118
+ infer functionName,
119
+ infer args
98
120
  >[]
99
- ? // Dynamic-size (homogenous) UseQueryOptions array: map directly to array of results
100
- MulticallResult<
101
- ContractReturnType<abi, 'pure' | 'view', functionName>,
102
- allowFailure
121
+ ? readonly MaybePartial<
122
+ MulticallContract<abi, options['mutability'], functionName, args> &
123
+ options['properties'],
124
+ options['optional']
103
125
  >[]
104
126
  : // Fallback
105
- MulticallResult<ContractReturnType, allowFailure>[]
127
+ readonly MaybePartial<
128
+ MulticallContract & options['properties'],
129
+ options['optional']
130
+ >[]
106
131
 
107
- export type MulticallResult<
108
- result,
132
+ export type MulticallResults<
133
+ contracts extends readonly unknown[] = readonly MulticallContract[],
109
134
  allowFailure extends boolean = true,
110
- > = allowFailure extends true
111
- ?
112
- | { error?: undefined; result: result; status: 'success' }
113
- | { error: Error; result?: undefined; status: 'failure' }
114
- : result
135
+ options extends {
136
+ error?: Error
137
+ mutability: AbiStateMutability
138
+ } = { error: Error; mutability: AbiStateMutability },
139
+ > = {
140
+ [index in keyof contracts]: MulticallResponse<
141
+ allowFailure,
142
+ ContractFunctionReturnType<
143
+ contracts[index],
144
+ options['mutability']
145
+ > extends infer result
146
+ ? // If `result` is `never`, then `contracts` could be unknown type (e.g. non-const-asserted array)
147
+ [result] extends [never]
148
+ ? unknown
149
+ : result
150
+ : unknown,
151
+ options['error']
152
+ >
153
+ }
154
+
155
+ type GetMulticallContract<
156
+ contract,
157
+ mutability extends AbiStateMutability,
158
+ > = contract extends { abi: infer abi extends Abi } // 1. Check if `abi` is const-asserted, defined inline, or declared as `Abi`
159
+ ? // 1a. Check if `functionName` is valid for `abi`
160
+ contract extends {
161
+ functionName: infer functionName extends FunctionName<abi, mutability>
162
+ }
163
+ ? // 1aa. Check if `args` is valid for `abi` and `functionName`
164
+ contract extends {
165
+ args: infer args extends Args<abi, mutability, functionName>
166
+ }
167
+ ? // 1aa. Check if `args` can be empty
168
+ readonly [] extends Args<abi, mutability, functionName>
169
+ ? // `args` can be empty, mark `args` as `undefined`
170
+ MulticallContract<abi, mutability, functionName, readonly []>
171
+ : // `contract` is valid as-is, return it
172
+ MulticallContract<abi, mutability, functionName, args>
173
+ : // 1ab. `args` is invalid, infer types for `args`
174
+ readonly [] extends Args<abi, mutability, functionName>
175
+ ? // `args` can be empty, mark `args` as `undefined`
176
+ MulticallContract<abi, mutability, functionName, readonly []>
177
+ : // infer `args`
178
+ MulticallContract<abi, mutability, functionName>
179
+ : // 1b. `functionName` is invalid, check if `abi` is declared as `Abi`
180
+ Abi extends abi
181
+ ? // `abi` declared as `Abi`, unable to infer types further
182
+ MulticallContract
183
+ : // `abi` is const-asserted or defined inline, infer types for `functionName` and `args`
184
+ Omit<MulticallContract<abi, mutability>, 'args' | 'functionName'> & {
185
+ // setting `functionName` directly here because `MulticallContract` will allow invalid inputs otherwise (because of `functionName: functionName`)
186
+ functionName: FunctionName<abi, mutability>
187
+ // setting `args` directly here because `MulticallContract` has `Widen` in its definition,
188
+ // which will cause that to show up in editor autocomplete for overloaded functions
189
+ args: Args<abi, mutability, FunctionName<abi, mutability>>
190
+ }
191
+ : // 2. Check if `abi` property exists in `contract`
192
+ contract extends { abi: infer abi }
193
+ ? // 2a. `abi` exists in `contract`, check if `abi` is of type `Abi`
194
+ abi extends Abi
195
+ ? // return `unknown` since 1 handle type inference
196
+ MulticallContract
197
+ : // return default type since `abi` is not inferrable
198
+ Omit<MulticallContract<readonly unknown[], mutability>, 'args'> & {
199
+ // setting ars directly here because `MulticallContract` has `Widen` in its definition,
200
+ // which will cause `never[]` to be inferred for `args` in this case
201
+ args?: readonly unknown[] | undefined
202
+ }
203
+ : // 2b. `abi` property does not exist in `contract`, unable to infer types further
204
+ // return `never`
205
+ never
@@ -242,3 +242,36 @@ export type Trim<T, Chars extends string = ' '> = TrimLeft<
242
242
  * => string | number
243
243
  */
244
244
  export type ValueOf<T> = T[keyof T]
245
+
246
+ export type UnionToTuple<
247
+ union,
248
+ ///
249
+ last = LastInUnion<union>,
250
+ > = [union] extends [never] ? [] : [...UnionToTuple<Exclude<union, last>>, last]
251
+ type LastInUnion<U> = UnionToIntersection<
252
+ U extends unknown ? (x: U) => 0 : never
253
+ > extends (x: infer l) => 0
254
+ ? l
255
+ : never
256
+ type UnionToIntersection<union> = (
257
+ union extends unknown
258
+ ? (arg: union) => 0
259
+ : never
260
+ ) extends (arg: infer i) => 0
261
+ ? i
262
+ : never
263
+
264
+ export type IsUnion<
265
+ union,
266
+ ///
267
+ union2 = union,
268
+ > = union extends union2 ? ([union2] extends [union] ? false : true) : never
269
+
270
+ export type MaybePartial<
271
+ type,
272
+ enabled extends boolean | undefined,
273
+ > = enabled extends true ? ExactPartial<type> : type
274
+
275
+ export type ExactPartial<type> = {
276
+ [key in keyof type]?: type[key] | undefined
277
+ }
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const version = '1.6.1'
1
+ export const version = '1.6.2'