viem 0.0.0-w-20230816185709 → 0.0.0-w-20230818182440
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.
- package/dist/cjs/actions/public/multicall.js +3 -6
- package/dist/cjs/actions/public/multicall.js.map +1 -1
- package/dist/cjs/clients/decorators/public.js.map +1 -1
- package/dist/cjs/constants/number.js +96 -96
- package/dist/cjs/constants/number.js.map +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/esm/actions/public/multicall.js +4 -7
- package/dist/esm/actions/public/multicall.js.map +1 -1
- package/dist/esm/clients/decorators/public.js.map +1 -1
- package/dist/esm/constants/number.js +96 -96
- package/dist/esm/constants/number.js.map +1 -1
- package/dist/esm/version.js +1 -1
- package/dist/types/actions/public/multicall.d.ts +16 -10
- package/dist/types/actions/public/multicall.d.ts.map +1 -1
- package/dist/types/clients/decorators/public.d.ts +2 -2
- package/dist/types/clients/decorators/public.d.ts.map +1 -1
- package/dist/types/constants/number.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/types/contract.d.ts +19 -2
- package/dist/types/types/contract.d.ts.map +1 -1
- package/dist/types/types/multicall.d.ts +60 -37
- package/dist/types/types/multicall.d.ts.map +1 -1
- package/dist/types/types/utils.d.ts +8 -0
- package/dist/types/types/utils.d.ts.map +1 -1
- package/dist/types/version.d.ts +1 -1
- package/package.json +3 -3
- package/src/actions/public/multicall.ts +25 -30
- package/src/clients/decorators/public.ts +4 -5
- package/src/constants/number.ts +96 -96
- package/src/index.ts +1 -1
- package/src/types/contract.ts +102 -0
- package/src/types/multicall.ts +181 -90
- package/src/types/utils.ts +33 -0
- package/src/version.ts +1 -1
@@ -7,20 +7,14 @@ import { AbiDecodingZeroDataError } from '../../errors/abi.js'
|
|
7
7
|
import type { BaseError } from '../../errors/base.js'
|
8
8
|
import { RawContractError } from '../../errors/contract.js'
|
9
9
|
import type { Chain } from '../../types/chain.js'
|
10
|
-
import type {
|
11
|
-
ContractFunctionConfig,
|
12
|
-
ContractParameters,
|
13
|
-
} from '../../types/contract.js'
|
14
10
|
import type { Hex } from '../../types/misc.js'
|
15
11
|
import type {
|
12
|
+
MulticallContract,
|
16
13
|
MulticallContracts,
|
17
14
|
MulticallResults,
|
18
15
|
} from '../../types/multicall.js'
|
19
16
|
import { decodeFunctionResult } from '../../utils/abi/decodeFunctionResult.js'
|
20
|
-
import {
|
21
|
-
type EncodeFunctionDataParameters,
|
22
|
-
encodeFunctionData,
|
23
|
-
} from '../../utils/abi/encodeFunctionData.js'
|
17
|
+
import { encodeFunctionData } from '../../utils/abi/encodeFunctionData.js'
|
24
18
|
import { getChainContractAddress } from '../../utils/chain.js'
|
25
19
|
import { getContractError } from '../../utils/errors/getContractError.js'
|
26
20
|
|
@@ -28,23 +22,33 @@ import type { CallParameters } from './call.js'
|
|
28
22
|
import { readContract } from './readContract.js'
|
29
23
|
|
30
24
|
export type MulticallParameters<
|
31
|
-
contracts extends readonly
|
25
|
+
contracts extends readonly unknown[] = readonly MulticallContract[],
|
32
26
|
allowFailure extends boolean = true,
|
27
|
+
options extends {
|
28
|
+
optional?: boolean
|
29
|
+
properties?: Record<string, any>
|
30
|
+
} = {},
|
33
31
|
> = Pick<CallParameters, 'blockNumber' | 'blockTag'> & {
|
34
32
|
allowFailure?: allowFailure | undefined
|
35
|
-
/**
|
36
|
-
* The maximum size (in bytes) for each calldata chunk. Set to `0` to disable the size limit.
|
37
|
-
* @default 1_024
|
38
|
-
*/
|
39
33
|
batchSize?: number | undefined
|
40
|
-
contracts:
|
34
|
+
contracts: MulticallContracts<
|
35
|
+
Narrow<contracts>,
|
36
|
+
{ mutability: 'pure' | 'view' } & options
|
37
|
+
>
|
41
38
|
multicallAddress?: Address | undefined
|
42
39
|
}
|
43
40
|
|
44
41
|
export type MulticallReturnType<
|
45
|
-
contracts extends readonly
|
42
|
+
contracts extends readonly unknown[] = readonly MulticallContract[],
|
46
43
|
allowFailure extends boolean = true,
|
47
|
-
|
44
|
+
options extends {
|
45
|
+
error?: Error
|
46
|
+
} = { error: Error },
|
47
|
+
> = MulticallResults<
|
48
|
+
contracts,
|
49
|
+
allowFailure,
|
50
|
+
{ mutability: 'pure' | 'view' } & options
|
51
|
+
>
|
48
52
|
|
49
53
|
/**
|
50
54
|
* Similar to [`readContract`](https://viem.sh/docs/contract/readContract.html), but batches up multiple functions on a contract in a single RPC call via the [`multicall3` contract](https://github.com/mds1/multicall).
|
@@ -86,7 +90,7 @@ export type MulticallReturnType<
|
|
86
90
|
* // [{ result: 424122n, status: 'success' }, { result: 1000000n, status: 'success' }]
|
87
91
|
*/
|
88
92
|
export async function multicall<
|
89
|
-
const contracts extends readonly
|
93
|
+
const contracts extends readonly unknown[],
|
90
94
|
chain extends Chain | undefined,
|
91
95
|
allowFailure extends boolean = true,
|
92
96
|
>(
|
@@ -98,9 +102,9 @@ export async function multicall<
|
|
98
102
|
batchSize: batchSize_,
|
99
103
|
blockNumber,
|
100
104
|
blockTag,
|
101
|
-
contracts,
|
102
105
|
multicallAddress: multicallAddress_,
|
103
106
|
} = parameters
|
107
|
+
const contracts = parameters.contracts as MulticallContract[]
|
104
108
|
|
105
109
|
const batchSize =
|
106
110
|
batchSize_ ??
|
@@ -132,16 +136,9 @@ export async function multicall<
|
|
132
136
|
let currentChunk = 0
|
133
137
|
let currentChunkSize = 0
|
134
138
|
for (let i = 0; i < contracts.length; i++) {
|
135
|
-
const { abi, address, args, functionName } = contracts[
|
136
|
-
i
|
137
|
-
] as ContractFunctionConfig
|
139
|
+
const { abi, address, args, functionName } = contracts[i]
|
138
140
|
try {
|
139
|
-
const callData = encodeFunctionData({
|
140
|
-
abi,
|
141
|
-
args,
|
142
|
-
functionName,
|
143
|
-
} as unknown as EncodeFunctionDataParameters)
|
144
|
-
|
141
|
+
const callData = encodeFunctionData({ abi, args, functionName })
|
145
142
|
currentChunkSize += callData.length
|
146
143
|
if (batchSize > 0 && currentChunkSize > batchSize) {
|
147
144
|
currentChunk++
|
@@ -193,9 +190,7 @@ export async function multicall<
|
|
193
190
|
return results.flat().map(({ returnData, success }, i) => {
|
194
191
|
const calls = chunkedCalls.flat()
|
195
192
|
const { callData } = calls[i]
|
196
|
-
const { abi, address, functionName, args } = contracts[
|
197
|
-
i
|
198
|
-
] as ContractFunctionConfig
|
193
|
+
const { abi, address, functionName, args } = contracts[i]
|
199
194
|
try {
|
200
195
|
if (callData === '0x') throw new AbiDecodingZeroDataError()
|
201
196
|
if (!success) throw new RawContractError({ data: returnData })
|
@@ -200,7 +200,6 @@ import type { Account } from '../../types/account.js'
|
|
200
200
|
import type { BlockNumber, BlockTag } from '../../types/block.js'
|
201
201
|
import type { Chain } from '../../types/chain.js'
|
202
202
|
import type {
|
203
|
-
ContractFunctionConfig,
|
204
203
|
MaybeAbiEventName,
|
205
204
|
MaybeExtractEventArgsFromAbi,
|
206
205
|
} from '../../types/contract.js'
|
@@ -1133,11 +1132,11 @@ export type PublicActions<
|
|
1133
1132
|
* // [{ result: 424122n, status: 'success' }, { result: 1000000n, status: 'success' }]
|
1134
1133
|
*/
|
1135
1134
|
multicall: <
|
1136
|
-
const
|
1137
|
-
|
1135
|
+
const contracts extends readonly unknown[],
|
1136
|
+
allowFailure extends boolean = true,
|
1138
1137
|
>(
|
1139
|
-
args: MulticallParameters<
|
1140
|
-
) => Promise<MulticallReturnType<
|
1138
|
+
args: MulticallParameters<contracts, allowFailure>,
|
1139
|
+
) => Promise<MulticallReturnType<contracts, allowFailure>>
|
1141
1140
|
/**
|
1142
1141
|
* Calls a read-only function on a contract, and returns the response.
|
1143
1142
|
*
|
package/src/constants/number.ts
CHANGED
@@ -1,98 +1,98 @@
|
|
1
|
-
export const maxInt8 = 2n ** (8n - 1n)
|
2
|
-
export const maxInt16 = 2n ** (16n - 1n)
|
3
|
-
export const maxInt24 = 2n ** (24n - 1n)
|
4
|
-
export const maxInt32 = 2n ** (32n - 1n)
|
5
|
-
export const maxInt40 = 2n ** (40n - 1n)
|
6
|
-
export const maxInt48 = 2n ** (48n - 1n)
|
7
|
-
export const maxInt56 = 2n ** (56n - 1n)
|
8
|
-
export const maxInt64 = 2n ** (64n - 1n)
|
9
|
-
export const maxInt72 = 2n ** (72n - 1n)
|
10
|
-
export const maxInt80 = 2n ** (80n - 1n)
|
11
|
-
export const maxInt88 = 2n ** (88n - 1n)
|
12
|
-
export const maxInt96 = 2n ** (96n - 1n)
|
13
|
-
export const maxInt104 = 2n ** (104n - 1n)
|
14
|
-
export const maxInt112 = 2n ** (112n - 1n)
|
15
|
-
export const maxInt120 = 2n ** (120n - 1n)
|
16
|
-
export const maxInt128 = 2n ** (128n - 1n)
|
17
|
-
export const maxInt136 = 2n ** (136n - 1n)
|
18
|
-
export const maxInt144 = 2n ** (144n - 1n)
|
19
|
-
export const maxInt152 = 2n ** (152n - 1n)
|
20
|
-
export const maxInt160 = 2n ** (160n - 1n)
|
21
|
-
export const maxInt168 = 2n ** (168n - 1n)
|
22
|
-
export const maxInt176 = 2n ** (176n - 1n)
|
23
|
-
export const maxInt184 = 2n ** (184n - 1n)
|
24
|
-
export const maxInt192 = 2n ** (192n - 1n)
|
25
|
-
export const maxInt200 = 2n ** (200n - 1n)
|
26
|
-
export const maxInt208 = 2n ** (208n - 1n)
|
27
|
-
export const maxInt216 = 2n ** (216n - 1n)
|
28
|
-
export const maxInt224 = 2n ** (224n - 1n)
|
29
|
-
export const maxInt232 = 2n ** (232n - 1n)
|
30
|
-
export const maxInt240 = 2n ** (240n - 1n)
|
31
|
-
export const maxInt248 = 2n ** (248n - 1n)
|
32
|
-
export const maxInt256 = 2n ** (256n - 1n)
|
1
|
+
export const maxInt8 = 2n ** (8n - 1n) - 1n
|
2
|
+
export const maxInt16 = 2n ** (16n - 1n) - 1n
|
3
|
+
export const maxInt24 = 2n ** (24n - 1n) - 1n
|
4
|
+
export const maxInt32 = 2n ** (32n - 1n) - 1n
|
5
|
+
export const maxInt40 = 2n ** (40n - 1n) - 1n
|
6
|
+
export const maxInt48 = 2n ** (48n - 1n) - 1n
|
7
|
+
export const maxInt56 = 2n ** (56n - 1n) - 1n
|
8
|
+
export const maxInt64 = 2n ** (64n - 1n) - 1n
|
9
|
+
export const maxInt72 = 2n ** (72n - 1n) - 1n
|
10
|
+
export const maxInt80 = 2n ** (80n - 1n) - 1n
|
11
|
+
export const maxInt88 = 2n ** (88n - 1n) - 1n
|
12
|
+
export const maxInt96 = 2n ** (96n - 1n) - 1n
|
13
|
+
export const maxInt104 = 2n ** (104n - 1n) - 1n
|
14
|
+
export const maxInt112 = 2n ** (112n - 1n) - 1n
|
15
|
+
export const maxInt120 = 2n ** (120n - 1n) - 1n
|
16
|
+
export const maxInt128 = 2n ** (128n - 1n) - 1n
|
17
|
+
export const maxInt136 = 2n ** (136n - 1n) - 1n
|
18
|
+
export const maxInt144 = 2n ** (144n - 1n) - 1n
|
19
|
+
export const maxInt152 = 2n ** (152n - 1n) - 1n
|
20
|
+
export const maxInt160 = 2n ** (160n - 1n) - 1n
|
21
|
+
export const maxInt168 = 2n ** (168n - 1n) - 1n
|
22
|
+
export const maxInt176 = 2n ** (176n - 1n) - 1n
|
23
|
+
export const maxInt184 = 2n ** (184n - 1n) - 1n
|
24
|
+
export const maxInt192 = 2n ** (192n - 1n) - 1n
|
25
|
+
export const maxInt200 = 2n ** (200n - 1n) - 1n
|
26
|
+
export const maxInt208 = 2n ** (208n - 1n) - 1n
|
27
|
+
export const maxInt216 = 2n ** (216n - 1n) - 1n
|
28
|
+
export const maxInt224 = 2n ** (224n - 1n) - 1n
|
29
|
+
export const maxInt232 = 2n ** (232n - 1n) - 1n
|
30
|
+
export const maxInt240 = 2n ** (240n - 1n) - 1n
|
31
|
+
export const maxInt248 = 2n ** (248n - 1n) - 1n
|
32
|
+
export const maxInt256 = 2n ** (256n - 1n) - 1n
|
33
33
|
|
34
|
-
export const minInt8 = -(2n ** 8n)
|
35
|
-
export const minInt16 = -(2n ** 16n)
|
36
|
-
export const minInt24 = -(2n ** 24n)
|
37
|
-
export const minInt32 = -(2n ** 32n)
|
38
|
-
export const minInt40 = -(2n ** 40n)
|
39
|
-
export const minInt48 = -(2n ** 48n)
|
40
|
-
export const minInt56 = -(2n ** 56n)
|
41
|
-
export const minInt64 = -(2n ** 64n)
|
42
|
-
export const minInt72 = -(2n ** 72n)
|
43
|
-
export const minInt80 = -(2n ** 80n)
|
44
|
-
export const minInt88 = -(2n ** 88n)
|
45
|
-
export const minInt96 = -(2n ** 96n)
|
46
|
-
export const minInt104 = -(2n ** 104n)
|
47
|
-
export const minInt112 = -(2n ** 112n)
|
48
|
-
export const minInt120 = -(2n ** 120n)
|
49
|
-
export const minInt128 = -(2n ** 128n)
|
50
|
-
export const minInt136 = -(2n ** 136n)
|
51
|
-
export const minInt144 = -(2n ** 144n)
|
52
|
-
export const minInt152 = -(2n ** 152n)
|
53
|
-
export const minInt160 = -(2n ** 160n)
|
54
|
-
export const minInt168 = -(2n ** 168n)
|
55
|
-
export const minInt176 = -(2n ** 176n)
|
56
|
-
export const minInt184 = -(2n ** 184n)
|
57
|
-
export const minInt192 = -(2n ** 192n)
|
58
|
-
export const minInt200 = -(2n ** 200n)
|
59
|
-
export const minInt208 = -(2n ** 208n)
|
60
|
-
export const minInt216 = -(2n ** 216n)
|
61
|
-
export const minInt224 = -(2n ** 224n)
|
62
|
-
export const minInt232 = -(2n ** 232n)
|
63
|
-
export const minInt240 = -(2n ** 240n)
|
64
|
-
export const minInt248 = -(2n ** 248n)
|
65
|
-
export const minInt256 = -(2n ** 256n)
|
34
|
+
export const minInt8 = -(2n ** (8n - 1n))
|
35
|
+
export const minInt16 = -(2n ** (16n - 1n))
|
36
|
+
export const minInt24 = -(2n ** (24n - 1n))
|
37
|
+
export const minInt32 = -(2n ** (32n - 1n))
|
38
|
+
export const minInt40 = -(2n ** (40n - 1n))
|
39
|
+
export const minInt48 = -(2n ** (48n - 1n))
|
40
|
+
export const minInt56 = -(2n ** (56n - 1n))
|
41
|
+
export const minInt64 = -(2n ** (64n - 1n))
|
42
|
+
export const minInt72 = -(2n ** (72n - 1n))
|
43
|
+
export const minInt80 = -(2n ** (80n - 1n))
|
44
|
+
export const minInt88 = -(2n ** (88n - 1n))
|
45
|
+
export const minInt96 = -(2n ** (96n - 1n))
|
46
|
+
export const minInt104 = -(2n ** (104n - 1n))
|
47
|
+
export const minInt112 = -(2n ** (112n - 1n))
|
48
|
+
export const minInt120 = -(2n ** (120n - 1n))
|
49
|
+
export const minInt128 = -(2n ** (128n - 1n))
|
50
|
+
export const minInt136 = -(2n ** (136n - 1n))
|
51
|
+
export const minInt144 = -(2n ** (144n - 1n))
|
52
|
+
export const minInt152 = -(2n ** (152n - 1n))
|
53
|
+
export const minInt160 = -(2n ** (160n - 1n))
|
54
|
+
export const minInt168 = -(2n ** (168n - 1n))
|
55
|
+
export const minInt176 = -(2n ** (176n - 1n))
|
56
|
+
export const minInt184 = -(2n ** (184n - 1n))
|
57
|
+
export const minInt192 = -(2n ** (192n - 1n))
|
58
|
+
export const minInt200 = -(2n ** (200n - 1n))
|
59
|
+
export const minInt208 = -(2n ** (208n - 1n))
|
60
|
+
export const minInt216 = -(2n ** (216n - 1n))
|
61
|
+
export const minInt224 = -(2n ** (224n - 1n))
|
62
|
+
export const minInt232 = -(2n ** (232n - 1n))
|
63
|
+
export const minInt240 = -(2n ** (240n - 1n))
|
64
|
+
export const minInt248 = -(2n ** (248n - 1n))
|
65
|
+
export const minInt256 = -(2n ** (256n - 1n))
|
66
66
|
|
67
|
-
export const maxUint8 = 2n ** 8n
|
68
|
-
export const maxUint16 = 2n ** 16n
|
69
|
-
export const maxUint24 = 2n ** 24n
|
70
|
-
export const maxUint32 = 2n ** 32n
|
71
|
-
export const maxUint40 = 2n ** 40n
|
72
|
-
export const maxUint48 = 2n ** 48n
|
73
|
-
export const maxUint56 = 2n ** 56n
|
74
|
-
export const maxUint64 = 2n ** 64n
|
75
|
-
export const maxUint72 = 2n ** 72n
|
76
|
-
export const maxUint80 = 2n ** 80n
|
77
|
-
export const maxUint88 = 2n ** 88n
|
78
|
-
export const maxUint96 = 2n ** 96n
|
79
|
-
export const maxUint104 = 2n ** 104n
|
80
|
-
export const maxUint112 = 2n ** 112n
|
81
|
-
export const maxUint120 = 2n ** 120n
|
82
|
-
export const maxUint128 = 2n ** 128n
|
83
|
-
export const maxUint136 = 2n ** 136n
|
84
|
-
export const maxUint144 = 2n ** 144n
|
85
|
-
export const maxUint152 = 2n ** 152n
|
86
|
-
export const maxUint160 = 2n ** 160n
|
87
|
-
export const maxUint168 = 2n ** 168n
|
88
|
-
export const maxUint176 = 2n ** 176n
|
89
|
-
export const maxUint184 = 2n ** 184n
|
90
|
-
export const maxUint192 = 2n ** 192n
|
91
|
-
export const maxUint200 = 2n ** 200n
|
92
|
-
export const maxUint208 = 2n ** 208n
|
93
|
-
export const maxUint216 = 2n ** 216n
|
94
|
-
export const maxUint224 = 2n ** 224n
|
95
|
-
export const maxUint232 = 2n ** 232n
|
96
|
-
export const maxUint240 = 2n ** 240n
|
97
|
-
export const maxUint248 = 2n ** 248n
|
98
|
-
export const maxUint256 = 2n ** 256n
|
67
|
+
export const maxUint8 = 2n ** 8n - 1n
|
68
|
+
export const maxUint16 = 2n ** 16n - 1n
|
69
|
+
export const maxUint24 = 2n ** 24n - 1n
|
70
|
+
export const maxUint32 = 2n ** 32n - 1n
|
71
|
+
export const maxUint40 = 2n ** 40n - 1n
|
72
|
+
export const maxUint48 = 2n ** 48n - 1n
|
73
|
+
export const maxUint56 = 2n ** 56n - 1n
|
74
|
+
export const maxUint64 = 2n ** 64n - 1n
|
75
|
+
export const maxUint72 = 2n ** 72n - 1n
|
76
|
+
export const maxUint80 = 2n ** 80n - 1n
|
77
|
+
export const maxUint88 = 2n ** 88n - 1n
|
78
|
+
export const maxUint96 = 2n ** 96n - 1n
|
79
|
+
export const maxUint104 = 2n ** 104n - 1n
|
80
|
+
export const maxUint112 = 2n ** 112n - 1n
|
81
|
+
export const maxUint120 = 2n ** 120n - 1n
|
82
|
+
export const maxUint128 = 2n ** 128n - 1n
|
83
|
+
export const maxUint136 = 2n ** 136n - 1n
|
84
|
+
export const maxUint144 = 2n ** 144n - 1n
|
85
|
+
export const maxUint152 = 2n ** 152n - 1n
|
86
|
+
export const maxUint160 = 2n ** 160n - 1n
|
87
|
+
export const maxUint168 = 2n ** 168n - 1n
|
88
|
+
export const maxUint176 = 2n ** 176n - 1n
|
89
|
+
export const maxUint184 = 2n ** 184n - 1n
|
90
|
+
export const maxUint192 = 2n ** 192n - 1n
|
91
|
+
export const maxUint200 = 2n ** 200n - 1n
|
92
|
+
export const maxUint208 = 2n ** 208n - 1n
|
93
|
+
export const maxUint216 = 2n ** 216n - 1n
|
94
|
+
export const maxUint224 = 2n ** 224n - 1n
|
95
|
+
export const maxUint232 = 2n ** 232n - 1n
|
96
|
+
export const maxUint240 = 2n ** 240n - 1n
|
97
|
+
export const maxUint248 = 2n ** 248n - 1n
|
98
|
+
export const maxUint256 = 2n ** 256n - 1n
|
package/src/index.ts
CHANGED
@@ -638,7 +638,7 @@ export type { Log } from './types/log.js'
|
|
638
638
|
export type {
|
639
639
|
MulticallContract,
|
640
640
|
MulticallContracts,
|
641
|
-
|
641
|
+
MulticallResponse,
|
642
642
|
MulticallResults,
|
643
643
|
} from './types/multicall.js'
|
644
644
|
export type { ParseAccount } from './types/account.js'
|
package/src/types/contract.ts
CHANGED
@@ -15,6 +15,7 @@ import type {
|
|
15
15
|
ExtractAbiEventNames,
|
16
16
|
ExtractAbiFunction,
|
17
17
|
ExtractAbiFunctionNames,
|
18
|
+
ResolvedConfig,
|
18
19
|
} from 'abitype'
|
19
20
|
|
20
21
|
import type { Hex, LogTopic } from './misc.js'
|
@@ -22,11 +23,112 @@ import type { TransactionRequest } from './transaction.js'
|
|
22
23
|
import type {
|
23
24
|
Filter,
|
24
25
|
IsNarrowable,
|
26
|
+
IsUnion,
|
25
27
|
MaybeRequired,
|
26
28
|
NoUndefined,
|
27
29
|
Prettify,
|
30
|
+
UnionToTuple,
|
28
31
|
} from './utils.js'
|
29
32
|
|
33
|
+
export type FunctionName<
|
34
|
+
abi extends Abi = Abi,
|
35
|
+
mutability extends AbiStateMutability = AbiStateMutability,
|
36
|
+
> = ExtractAbiFunctionNames<abi, mutability>
|
37
|
+
|
38
|
+
export type Args<
|
39
|
+
abi extends Abi = Abi,
|
40
|
+
mutability extends AbiStateMutability = AbiStateMutability,
|
41
|
+
functionName extends ExtractAbiFunctionNames<
|
42
|
+
abi,
|
43
|
+
mutability
|
44
|
+
> = ExtractAbiFunctionNames<abi, mutability>,
|
45
|
+
> = AbiParametersToPrimitiveTypes<
|
46
|
+
ExtractAbiFunction<abi, functionName, mutability>['inputs'],
|
47
|
+
'inputs'
|
48
|
+
>
|
49
|
+
|
50
|
+
export type Widen<type> =
|
51
|
+
| ([unknown] extends [type] ? unknown : never)
|
52
|
+
| (type extends Function ? type : never)
|
53
|
+
| (type extends ResolvedConfig['BigIntType'] ? bigint : never)
|
54
|
+
| (type extends boolean ? boolean : never)
|
55
|
+
| (type extends ResolvedConfig['IntType'] ? number : never)
|
56
|
+
| (type extends string
|
57
|
+
? type extends ResolvedConfig['AddressType']
|
58
|
+
? ResolvedConfig['AddressType']
|
59
|
+
: type extends ResolvedConfig['BytesType']['inputs']
|
60
|
+
? ResolvedConfig['BytesType']
|
61
|
+
: string
|
62
|
+
: never)
|
63
|
+
| (type extends readonly [] ? readonly [] : never)
|
64
|
+
| (type extends Record<string, unknown>
|
65
|
+
? { [K in keyof type]: Widen<type[K]> }
|
66
|
+
: never)
|
67
|
+
| (type extends { length: number }
|
68
|
+
? {
|
69
|
+
[K in keyof type]: Widen<type[K]>
|
70
|
+
} extends infer Val extends readonly unknown[]
|
71
|
+
? readonly [...Val]
|
72
|
+
: never
|
73
|
+
: never)
|
74
|
+
|
75
|
+
export type ExtractAbiFunctionForArgs<
|
76
|
+
abi extends Abi,
|
77
|
+
mutability extends AbiStateMutability,
|
78
|
+
functionName extends FunctionName<abi, mutability>,
|
79
|
+
args extends Args<abi, mutability, functionName>,
|
80
|
+
> = ExtractAbiFunction<
|
81
|
+
abi,
|
82
|
+
functionName,
|
83
|
+
mutability
|
84
|
+
> extends infer abiFunction extends AbiFunction
|
85
|
+
? IsUnion<abiFunction> extends true // narrow overloads using `args` by converting to tuple and filtering out overloads that don't match
|
86
|
+
? UnionToTuple<abiFunction> extends infer abiFunctions extends readonly AbiFunction[]
|
87
|
+
? {
|
88
|
+
[k in keyof abiFunctions]: args extends AbiParametersToPrimitiveTypes<
|
89
|
+
abiFunctions[k]['inputs'],
|
90
|
+
'inputs'
|
91
|
+
>
|
92
|
+
? abiFunctions[k]
|
93
|
+
: never
|
94
|
+
}[number] // convert back to union (removes `never` tuple entries: `['foo', never, 'bar'][number]` => `'foo' | 'bar'`)
|
95
|
+
: never
|
96
|
+
: abiFunction
|
97
|
+
: never
|
98
|
+
|
99
|
+
export type ContractFunctionReturnType<
|
100
|
+
contract,
|
101
|
+
mutability extends AbiStateMutability,
|
102
|
+
> = contract extends {
|
103
|
+
abi: infer abi extends Abi
|
104
|
+
functionName?: infer functionName extends string
|
105
|
+
args?: infer args
|
106
|
+
}
|
107
|
+
? Abi extends abi
|
108
|
+
? unknown
|
109
|
+
: AbiParametersToPrimitiveTypes<
|
110
|
+
ExtractAbiFunctionForArgs<
|
111
|
+
abi,
|
112
|
+
mutability,
|
113
|
+
functionName,
|
114
|
+
// fallback to `readonly []` if `args` is not defined (e.g. not required `"inputs": []`)
|
115
|
+
(
|
116
|
+
readonly [] extends args
|
117
|
+
? readonly []
|
118
|
+
: args
|
119
|
+
) extends infer args2 extends Args<abi, mutability, functionName>
|
120
|
+
? args2
|
121
|
+
: never
|
122
|
+
>['outputs']
|
123
|
+
> extends infer types
|
124
|
+
? types extends readonly []
|
125
|
+
? void
|
126
|
+
: types extends readonly [infer type]
|
127
|
+
? type
|
128
|
+
: types
|
129
|
+
: never
|
130
|
+
: unknown
|
131
|
+
|
30
132
|
export type AbiItem = Abi[number]
|
31
133
|
|
32
134
|
export type EventDefinition = `${string}(${string})`
|