viem 0.0.0-w-20230815171822 → 0.0.0-w-20230816153316
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 +2 -2
- package/dist/cjs/actions/public/multicall.js.map +1 -1
- package/dist/cjs/actions/public/readContract.js +2 -1
- package/dist/cjs/actions/public/readContract.js.map +1 -1
- package/dist/cjs/actions/wallet/writeContract.js +2 -1
- package/dist/cjs/actions/wallet/writeContract.js.map +1 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/actions/public/multicall.js +2 -2
- package/dist/esm/actions/public/multicall.js.map +1 -1
- package/dist/esm/actions/public/readContract.js +2 -1
- package/dist/esm/actions/public/readContract.js.map +1 -1
- package/dist/esm/actions/wallet/writeContract.js +2 -1
- package/dist/esm/actions/wallet/writeContract.js.map +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/types/actions/public/multicall.d.ts +12 -9
- package/dist/types/actions/public/multicall.d.ts.map +1 -1
- package/dist/types/actions/public/readContract.d.ts +4 -4
- package/dist/types/actions/public/readContract.d.ts.map +1 -1
- package/dist/types/actions/wallet/writeContract.d.ts +4 -4
- package/dist/types/actions/wallet/writeContract.d.ts.map +1 -1
- package/dist/types/index.d.ts +2 -2
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/types/contract.d.ts +11 -0
- package/dist/types/types/contract.d.ts.map +1 -1
- package/dist/types/types/multicall.d.ts +35 -35
- package/dist/types/types/multicall.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/actions/public/multicall.ts +30 -23
- package/src/actions/public/readContract.ts +19 -21
- package/src/actions/wallet/writeContract.ts +35 -37
- package/src/index.ts +3 -0
- package/src/types/contract.ts +55 -0
- package/src/types/multicall.ts +82 -76
@@ -1,48 +1,48 @@
|
|
1
|
-
import type { Abi, ExtractAbiFunctionNames } from 'abitype';
|
2
|
-
import type {
|
1
|
+
import type { Abi, AbiStateMutability, ExtractAbiFunctionNames } from 'abitype';
|
2
|
+
import type { ContractParameters, ContractReturnType } from './contract.js';
|
3
3
|
type MAXIMUM_DEPTH = 20;
|
4
|
-
export type MulticallContract<
|
5
|
-
abi:
|
6
|
-
functionName:
|
4
|
+
export type MulticallContract<abi extends Abi | readonly unknown[] = Abi | readonly unknown[], stateMutability extends AbiStateMutability = AbiStateMutability, functionName extends ExtractAbiFunctionNames<abi extends Abi ? abi : Abi, stateMutability> = string> = {
|
5
|
+
abi: abi;
|
6
|
+
functionName: functionName;
|
7
7
|
};
|
8
|
-
export type MulticallContracts<
|
9
|
-
...
|
10
|
-
|
11
|
-
] :
|
12
|
-
infer
|
13
|
-
...infer
|
8
|
+
export type MulticallContracts<contracts extends readonly MulticallContract[], properties extends Record<string, any> = object, result extends any[] = [], depth extends readonly number[] = []> = depth['length'] extends MAXIMUM_DEPTH ? (ContractParameters & properties)[] : contracts extends [] ? [] : contracts extends [infer head extends MulticallContract] ? [
|
9
|
+
...result,
|
10
|
+
ContractParameters<head['abi'], 'pure' | 'view', head['functionName']> & properties
|
11
|
+
] : contracts extends [
|
12
|
+
infer head extends MulticallContract,
|
13
|
+
...infer tail extends readonly MulticallContract[]
|
14
14
|
] ? MulticallContracts<[
|
15
|
-
...
|
16
|
-
],
|
17
|
-
...
|
18
|
-
|
15
|
+
...tail
|
16
|
+
], properties, [
|
17
|
+
...result,
|
18
|
+
ContractParameters<head['abi'], 'pure' | 'view', head['functionName']> & properties
|
19
19
|
], [
|
20
|
-
...
|
20
|
+
...depth,
|
21
21
|
1
|
22
|
-
]> : unknown[] extends
|
23
|
-
export type
|
22
|
+
]> : unknown[] extends contracts ? contracts : contracts extends ContractParameters<infer abi, infer _, infer functionName>[] ? (ContractParameters<abi, 'pure' | 'view', functionName> & properties)[] : (ContractParameters & properties)[];
|
23
|
+
export type MulticallResults<contracts extends readonly MulticallContract[], allowFailure extends boolean = true, result extends any[] = [], depth extends readonly number[] = []> = depth['length'] extends MAXIMUM_DEPTH ? MulticallResult<ContractReturnType, allowFailure>[] : contracts extends [] ? [] : contracts extends [infer head extends MulticallContract] ? [
|
24
|
+
...result,
|
25
|
+
MulticallResult<ContractReturnType<head['abi'], 'pure' | 'view', head['functionName']>, allowFailure>
|
26
|
+
] : contracts extends [
|
27
|
+
infer head extends MulticallContract,
|
28
|
+
...infer tail extends readonly MulticallContract[]
|
29
|
+
] ? MulticallResults<[
|
30
|
+
...tail
|
31
|
+
], allowFailure, [
|
32
|
+
...result,
|
33
|
+
MulticallResult<ContractReturnType<head['abi'], 'pure' | 'view', head['functionName']>, allowFailure>
|
34
|
+
], [
|
35
|
+
...depth,
|
36
|
+
1
|
37
|
+
]> : contracts extends ContractParameters<infer abi, infer _, infer functionName>[] ? MulticallResult<ContractReturnType<abi, 'pure' | 'view', functionName>, allowFailure>[] : MulticallResult<ContractReturnType, allowFailure>[];
|
38
|
+
export type MulticallResult<result, allowFailure extends boolean = true> = allowFailure extends true ? {
|
24
39
|
error?: undefined;
|
25
|
-
result:
|
40
|
+
result: result;
|
26
41
|
status: 'success';
|
27
42
|
} | {
|
28
43
|
error: Error;
|
29
44
|
result?: undefined;
|
30
45
|
status: 'failure';
|
31
|
-
} :
|
32
|
-
export type MulticallResults<TContracts extends readonly MulticallContract[], TAllowFailure extends boolean = true, Result extends any[] = [], Depth extends readonly number[] = []> = Depth['length'] extends MAXIMUM_DEPTH ? MulticallResult<ContractFunctionResult, TAllowFailure>[] : TContracts extends [] ? [] : TContracts extends [infer Head extends MulticallContract] ? [
|
33
|
-
...Result,
|
34
|
-
MulticallResult<ContractFunctionResult<Head['abi'], Head['functionName']>, TAllowFailure>
|
35
|
-
] : TContracts extends [
|
36
|
-
infer Head extends MulticallContract,
|
37
|
-
...infer Tail extends readonly MulticallContract[]
|
38
|
-
] ? MulticallResults<[
|
39
|
-
...Tail
|
40
|
-
], TAllowFailure, [
|
41
|
-
...Result,
|
42
|
-
MulticallResult<ContractFunctionResult<Head['abi'], Head['functionName']>, TAllowFailure>
|
43
|
-
], [
|
44
|
-
...Depth,
|
45
|
-
1
|
46
|
-
]> : TContracts extends ContractFunctionConfig<infer TAbi, infer TFunctionName>[] ? MulticallResult<ContractFunctionResult<TAbi, TFunctionName>, TAllowFailure>[] : MulticallResult<ContractFunctionResult, TAllowFailure>[];
|
46
|
+
} : result;
|
47
47
|
export {};
|
48
48
|
//# sourceMappingURL=multicall.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"multicall.d.ts","sourceRoot":"","sources":["../../../src/types/multicall.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,uBAAuB,EAAE,MAAM,SAAS,CAAA;
|
1
|
+
{"version":3,"file":"multicall.d.ts","sourceRoot":"","sources":["../../../src/types/multicall.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,kBAAkB,EAAE,uBAAuB,EAAE,MAAM,SAAS,CAAA;AAE/E,OAAO,KAAK,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAA;AAG3E,KAAK,aAAa,GAAG,EAAE,CAAA;AAEvB,MAAM,MAAM,iBAAiB,CAC3B,GAAG,SAAS,GAAG,GAAG,SAAS,OAAO,EAAE,GAAG,GAAG,GAAG,SAAS,OAAO,EAAE,EAC/D,eAAe,SAAS,kBAAkB,GAAG,kBAAkB,EAC/D,YAAY,SAAS,uBAAuB,CAC1C,GAAG,SAAS,GAAG,GAAG,GAAG,GAAG,GAAG,EAC3B,eAAe,CAChB,GAAG,MAAM,IACR;IAAE,GAAG,EAAE,GAAG,CAAC;IAAC,YAAY,EAAE,YAAY,CAAA;CAAE,CAAA;AAE5C,MAAM,MAAM,kBAAkB,CAC5B,SAAS,SAAS,SAAS,iBAAiB,EAAE,EAC9C,UAAU,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,EAE/C,MAAM,SAAS,GAAG,EAAE,GAAG,EAAE,EACzB,KAAK,SAAS,SAAS,MAAM,EAAE,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GED
@@ -7,7 +7,10 @@ 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 {
|
10
|
+
import type {
|
11
|
+
ContractFunctionConfig,
|
12
|
+
ContractParameters,
|
13
|
+
} from '../../types/contract.js'
|
11
14
|
import type { Hex } from '../../types/misc.js'
|
12
15
|
import type {
|
13
16
|
MulticallContract,
|
@@ -26,20 +29,23 @@ import type { CallParameters } from './call.js'
|
|
26
29
|
import { readContract } from './readContract.js'
|
27
30
|
|
28
31
|
export type MulticallParameters<
|
29
|
-
|
30
|
-
|
32
|
+
contracts extends readonly MulticallContract[] = readonly MulticallContract[],
|
33
|
+
allowFailure extends boolean = true,
|
31
34
|
> = Pick<CallParameters, 'blockNumber' | 'blockTag'> & {
|
32
|
-
allowFailure?:
|
33
|
-
/**
|
34
|
-
|
35
|
-
|
36
|
-
|
35
|
+
allowFailure?: allowFailure | undefined
|
36
|
+
/**
|
37
|
+
* The maximum size (in bytes) for each calldata chunk. Set to `0` to disable the size limit.
|
38
|
+
* @default 1_024
|
39
|
+
*/
|
40
|
+
batchSize?: number | undefined
|
41
|
+
contracts: readonly [...MulticallContracts<contracts>]
|
42
|
+
multicallAddress?: Address | undefined
|
37
43
|
}
|
38
44
|
|
39
45
|
export type MulticallReturnType<
|
40
|
-
|
41
|
-
|
42
|
-
> = MulticallResults<
|
46
|
+
contracts extends readonly MulticallContract[] = readonly MulticallContract[],
|
47
|
+
allowFailure extends boolean = true,
|
48
|
+
> = MulticallResults<contracts, allowFailure>
|
43
49
|
|
44
50
|
/**
|
45
51
|
* 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).
|
@@ -81,18 +87,19 @@ export type MulticallReturnType<
|
|
81
87
|
* // [{ result: 424122n, status: 'success' }, { result: 1000000n, status: 'success' }]
|
82
88
|
*/
|
83
89
|
export async function multicall<
|
84
|
-
const
|
85
|
-
|
86
|
-
const
|
87
|
-
|
88
|
-
|
90
|
+
const abi extends Abi | readonly unknown[],
|
91
|
+
functionName extends string,
|
92
|
+
const contracts extends readonly ContractParameters<
|
93
|
+
abi,
|
94
|
+
'pure' | 'view',
|
95
|
+
functionName
|
89
96
|
>[],
|
90
|
-
|
91
|
-
|
97
|
+
chain extends Chain | undefined,
|
98
|
+
allowFailure extends boolean = true,
|
92
99
|
>(
|
93
|
-
client: Client<Transport,
|
94
|
-
|
95
|
-
): Promise<MulticallReturnType<
|
100
|
+
client: Client<Transport, chain>,
|
101
|
+
parameters: MulticallParameters<contracts, allowFailure>,
|
102
|
+
): Promise<MulticallReturnType<contracts, allowFailure>> {
|
96
103
|
const {
|
97
104
|
allowFailure = true,
|
98
105
|
batchSize: batchSize_,
|
@@ -100,7 +107,7 @@ export async function multicall<
|
|
100
107
|
blockTag,
|
101
108
|
contracts,
|
102
109
|
multicallAddress: multicallAddress_,
|
103
|
-
} =
|
110
|
+
} = parameters
|
104
111
|
|
105
112
|
const batchSize =
|
106
113
|
batchSize_ ??
|
@@ -217,5 +224,5 @@ export async function multicall<
|
|
217
224
|
if (!allowFailure) throw error
|
218
225
|
return { error, result: undefined, status: 'failure' }
|
219
226
|
}
|
220
|
-
}) as MulticallResults<
|
227
|
+
}) as MulticallResults<contracts, allowFailure>
|
221
228
|
}
|
@@ -1,12 +1,12 @@
|
|
1
|
-
import type { Abi } from 'abitype'
|
1
|
+
import type { Abi, ExtractAbiFunctionNames } from 'abitype'
|
2
2
|
|
3
3
|
import type { Client } from '../../clients/createClient.js'
|
4
4
|
import type { Transport } from '../../clients/transports/createTransport.js'
|
5
5
|
import type { BaseError } from '../../errors/base.js'
|
6
6
|
import type { Chain } from '../../types/chain.js'
|
7
7
|
import type {
|
8
|
-
ContractFunctionConfig,
|
9
8
|
ContractFunctionResult,
|
9
|
+
ContractParameters,
|
10
10
|
} from '../../types/contract.js'
|
11
11
|
import {
|
12
12
|
type DecodeFunctionResultParameters,
|
@@ -21,10 +21,13 @@ import { getContractError } from '../../utils/errors/getContractError.js'
|
|
21
21
|
import { type CallParameters, call } from './call.js'
|
22
22
|
|
23
23
|
export type ReadContractParameters<
|
24
|
-
|
25
|
-
|
24
|
+
abi extends Abi | readonly unknown[] = Abi,
|
25
|
+
functionName extends ExtractAbiFunctionNames<
|
26
|
+
abi extends Abi ? abi : Abi,
|
27
|
+
'view' | 'pure'
|
28
|
+
> = string,
|
26
29
|
> = Pick<CallParameters, 'account' | 'blockNumber' | 'blockTag'> &
|
27
|
-
|
30
|
+
ContractParameters<abi, 'view' | 'pure', functionName>
|
28
31
|
|
29
32
|
export type ReadContractReturnType<
|
30
33
|
TAbi extends Abi | readonly unknown[] = Abi,
|
@@ -63,24 +66,19 @@ export type ReadContractReturnType<
|
|
63
66
|
* // 424122n
|
64
67
|
*/
|
65
68
|
export async function readContract<
|
66
|
-
|
67
|
-
const
|
68
|
-
|
69
|
+
chain extends Chain | undefined,
|
70
|
+
const abi extends Abi | readonly unknown[],
|
71
|
+
functionName extends string,
|
69
72
|
>(
|
70
|
-
client: Client<Transport,
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
args,
|
75
|
-
functionName,
|
76
|
-
...callRequest
|
77
|
-
}: ReadContractParameters<TAbi, TFunctionName>,
|
78
|
-
): Promise<ReadContractReturnType<TAbi, TFunctionName>> {
|
73
|
+
client: Client<Transport, chain>,
|
74
|
+
parameters: ReadContractParameters<abi, functionName>,
|
75
|
+
): Promise<ReadContractReturnType<abi, functionName>> {
|
76
|
+
const { abi, address, args, functionName, ...callRequest } = parameters
|
79
77
|
const calldata = encodeFunctionData({
|
80
78
|
abi,
|
81
79
|
args,
|
82
80
|
functionName,
|
83
|
-
} as unknown as EncodeFunctionDataParameters<
|
81
|
+
} as unknown as EncodeFunctionDataParameters<abi, functionName>)
|
84
82
|
try {
|
85
83
|
const { data } = await call(client, {
|
86
84
|
data: calldata,
|
@@ -93,9 +91,9 @@ export async function readContract<
|
|
93
91
|
functionName,
|
94
92
|
data: data || '0x',
|
95
93
|
} as DecodeFunctionResultParameters<
|
96
|
-
|
97
|
-
|
98
|
-
>) as ReadContractReturnType<
|
94
|
+
abi,
|
95
|
+
functionName
|
96
|
+
>) as ReadContractReturnType<abi, functionName>
|
99
97
|
} catch (err) {
|
100
98
|
throw getContractError(err as BaseError, {
|
101
99
|
abi: abi as Abi,
|
@@ -1,11 +1,11 @@
|
|
1
|
-
import type { Abi } from 'abitype'
|
1
|
+
import type { Abi, ExtractAbiFunctionNames } from 'abitype'
|
2
2
|
|
3
3
|
import type { Account } from '../../accounts/types.js'
|
4
4
|
import type { Client } from '../../clients/createClient.js'
|
5
5
|
import type { Transport } from '../../clients/transports/createTransport.js'
|
6
6
|
import type { GetAccountParameter } from '../../types/account.js'
|
7
7
|
import type { Chain, GetChain } from '../../types/chain.js'
|
8
|
-
import type {
|
8
|
+
import type { ContractParameters, GetValue } from '../../types/contract.js'
|
9
9
|
import type { Hex } from '../../types/misc.js'
|
10
10
|
import type { UnionOmit } from '../../types/utils.js'
|
11
11
|
import {
|
@@ -20,29 +20,32 @@ import {
|
|
20
20
|
} from './sendTransaction.js'
|
21
21
|
|
22
22
|
export type WriteContractParameters<
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
23
|
+
abi extends Abi | readonly unknown[] = Abi,
|
24
|
+
functionName extends ExtractAbiFunctionNames<
|
25
|
+
abi extends Abi ? abi : Abi,
|
26
|
+
'payable' | 'nonpayable'
|
27
|
+
> = string,
|
28
|
+
chain extends Chain | undefined = Chain,
|
29
|
+
account extends Account | undefined = Account | undefined,
|
30
|
+
chainOverride extends Chain | undefined = Chain | undefined,
|
31
|
+
> = ContractParameters<abi, 'payable' | 'nonpayable', functionName> &
|
31
32
|
UnionOmit<
|
32
33
|
FormattedTransactionRequest<
|
33
|
-
|
34
|
+
chainOverride extends Chain ? chainOverride : chain
|
34
35
|
>,
|
35
|
-
'
|
36
|
+
'data' | 'from' | 'to' | 'value'
|
36
37
|
> &
|
38
|
+
GetAccountParameter<account> &
|
39
|
+
GetChain<chain, chainOverride> &
|
37
40
|
GetValue<
|
38
|
-
|
39
|
-
|
41
|
+
abi,
|
42
|
+
functionName,
|
40
43
|
SendTransactionParameters<
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
+
chain,
|
45
|
+
account,
|
46
|
+
chainOverride
|
44
47
|
> extends SendTransactionParameters
|
45
|
-
? SendTransactionParameters<
|
48
|
+
? SendTransactionParameters<chain, account, chainOverride>['value']
|
46
49
|
: SendTransactionParameters['value']
|
47
50
|
> & {
|
48
51
|
/** Data to append to the end of the calldata. Useful for adding a ["domain" tag](https://opensea.notion.site/opensea/Seaport-Order-Attributions-ec2d69bf455041a5baa490941aad307f). */
|
@@ -102,37 +105,32 @@ export type WriteContractReturnType = SendTransactionReturnType
|
|
102
105
|
* const hash = await writeContract(client, request)
|
103
106
|
*/
|
104
107
|
export async function writeContract<
|
105
|
-
|
106
|
-
|
107
|
-
const
|
108
|
-
|
109
|
-
|
108
|
+
chain extends Chain | undefined,
|
109
|
+
account extends Account | undefined,
|
110
|
+
const abi extends Abi | readonly unknown[],
|
111
|
+
functionName extends string,
|
112
|
+
chainOverride extends Chain | undefined,
|
110
113
|
>(
|
111
|
-
client: Client<Transport,
|
112
|
-
|
114
|
+
client: Client<Transport, chain, account>,
|
115
|
+
parameters: WriteContractParameters<
|
113
116
|
abi,
|
114
|
-
address,
|
115
|
-
args,
|
116
|
-
dataSuffix,
|
117
117
|
functionName,
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
TFunctionName,
|
122
|
-
TChain,
|
123
|
-
TAccount,
|
124
|
-
TChainOverride
|
118
|
+
chain,
|
119
|
+
account,
|
120
|
+
chainOverride
|
125
121
|
>,
|
126
122
|
): Promise<WriteContractReturnType> {
|
123
|
+
const { abi, address, args, dataSuffix, functionName, ...request } =
|
124
|
+
parameters
|
127
125
|
const data = encodeFunctionData({
|
128
126
|
abi,
|
129
127
|
args,
|
130
128
|
functionName,
|
131
|
-
} as unknown as EncodeFunctionDataParameters<
|
129
|
+
} as unknown as EncodeFunctionDataParameters<abi, functionName>)
|
132
130
|
const hash = await sendTransaction(client, {
|
133
131
|
data: `${data}${dataSuffix ? dataSuffix.replace('0x', '') : ''}`,
|
134
132
|
to: address,
|
135
133
|
...request,
|
136
|
-
} as unknown as SendTransactionParameters<
|
134
|
+
} as unknown as SendTransactionParameters<chain, account, chainOverride>)
|
137
135
|
return hash
|
138
136
|
}
|
package/src/index.ts
CHANGED
@@ -7,6 +7,7 @@ export {
|
|
7
7
|
type AbiParameterToPrimitiveType,
|
8
8
|
type Address,
|
9
9
|
type ExtractAbiFunction,
|
10
|
+
type ExtractAbiFunctionNames,
|
10
11
|
type Narrow,
|
11
12
|
type ParseAbi,
|
12
13
|
type ParseAbiItem,
|
@@ -514,6 +515,8 @@ export { SizeExceedsPaddingSizeError } from './errors/data.js'
|
|
514
515
|
export { UrlRequiredError } from './errors/transport.js'
|
515
516
|
export type {
|
516
517
|
AbiItem,
|
518
|
+
ContractParameters,
|
519
|
+
ContractReturnType,
|
517
520
|
ContractFunctionConfig,
|
518
521
|
ContractFunctionResult,
|
519
522
|
GetConstructorArgs,
|
package/src/types/contract.ts
CHANGED
@@ -31,6 +31,61 @@ export type AbiItem = Abi[number]
|
|
31
31
|
|
32
32
|
export type EventDefinition = `${string}(${string})`
|
33
33
|
|
34
|
+
export type ContractParameters<
|
35
|
+
abi extends Abi | readonly unknown[] = readonly unknown[],
|
36
|
+
stateMutability extends AbiStateMutability = AbiStateMutability,
|
37
|
+
functionName extends ExtractAbiFunctionNames<
|
38
|
+
abi extends Abi ? abi : Abi,
|
39
|
+
stateMutability
|
40
|
+
> = string,
|
41
|
+
///
|
42
|
+
functionNames extends string = abi extends Abi
|
43
|
+
? ExtractAbiFunctionNames<abi, stateMutability>
|
44
|
+
: string,
|
45
|
+
abiFunction extends AbiFunction = abi extends Abi
|
46
|
+
? ExtractAbiFunction<abi, functionName, stateMutability>
|
47
|
+
: AbiFunction,
|
48
|
+
types = AbiParametersToPrimitiveTypes<abiFunction['inputs'], 'inputs'>,
|
49
|
+
args =
|
50
|
+
| types // show all values
|
51
|
+
| (Abi extends abi ? readonly unknown[] | undefined : never) // `abi` declared as `Abi`
|
52
|
+
| (readonly [] extends types ? undefined : never), // `abiFunction` has no inputs
|
53
|
+
isArgsOptional extends boolean = Abi extends abi
|
54
|
+
? true
|
55
|
+
: readonly [] extends types
|
56
|
+
? true
|
57
|
+
: false,
|
58
|
+
> = {
|
59
|
+
abi: abi
|
60
|
+
address: Address
|
61
|
+
functionName:
|
62
|
+
| functionNames // show all values
|
63
|
+
| (functionName extends functionNames ? functionName : never) // validate `functionName`
|
64
|
+
| ([functionNames] extends [never] ? string : never) // `abi` declared as `Abi`
|
65
|
+
} & (isArgsOptional extends true ? { args?: args | undefined } : { args: args })
|
66
|
+
|
67
|
+
export type ContractReturnType<
|
68
|
+
abi extends Abi | readonly unknown[] = readonly unknown[],
|
69
|
+
stateMutability extends AbiStateMutability = AbiStateMutability,
|
70
|
+
functionName extends ExtractAbiFunctionNames<
|
71
|
+
abi extends Abi ? abi : Abi,
|
72
|
+
stateMutability
|
73
|
+
> = string,
|
74
|
+
///
|
75
|
+
abiFunction extends AbiFunction = abi extends Abi
|
76
|
+
? ExtractAbiFunction<abi, functionName, stateMutability>
|
77
|
+
: AbiFunction,
|
78
|
+
types = AbiParametersToPrimitiveTypes<abiFunction['outputs'], 'outputs'>,
|
79
|
+
> = [abiFunction] extends [never]
|
80
|
+
? unknown // `abiFunction` was not inferrable (e.g. `abi` declared as `Abi`)
|
81
|
+
: readonly unknown[] extends types
|
82
|
+
? unknown // `abiFunction` was not inferrable (e.g. `abi` not const asserted)
|
83
|
+
: types extends readonly [] // unwrap `types`
|
84
|
+
? void // no outputs
|
85
|
+
: types extends readonly [infer type]
|
86
|
+
? type // single output
|
87
|
+
: types
|
88
|
+
|
34
89
|
export type ContractFunctionConfig<
|
35
90
|
TAbi extends Abi | readonly unknown[] = Abi,
|
36
91
|
TFunctionName extends string = string,
|