viem 0.0.0-w-20230818230619 → 0.0.0-w-20230821160922

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 (64) hide show
  1. package/dist/cjs/actions/getContract.js.map +1 -1
  2. package/dist/cjs/actions/public/estimateContractGas.js +7 -14
  3. package/dist/cjs/actions/public/estimateContractGas.js.map +1 -1
  4. package/dist/cjs/actions/public/multicall.js.map +1 -1
  5. package/dist/cjs/actions/public/readContract.js +6 -10
  6. package/dist/cjs/actions/public/readContract.js.map +1 -1
  7. package/dist/cjs/actions/public/simulateContract.js +6 -9
  8. package/dist/cjs/actions/public/simulateContract.js.map +1 -1
  9. package/dist/cjs/actions/wallet/writeContract.js +1 -5
  10. package/dist/cjs/actions/wallet/writeContract.js.map +1 -1
  11. package/dist/cjs/clients/decorators/public.js.map +1 -1
  12. package/dist/cjs/clients/decorators/wallet.js.map +1 -1
  13. package/dist/esm/actions/getContract.js.map +1 -1
  14. package/dist/esm/actions/public/estimateContractGas.js +8 -15
  15. package/dist/esm/actions/public/estimateContractGas.js.map +1 -1
  16. package/dist/esm/actions/public/multicall.js.map +1 -1
  17. package/dist/esm/actions/public/readContract.js +8 -12
  18. package/dist/esm/actions/public/readContract.js.map +1 -1
  19. package/dist/esm/actions/public/simulateContract.js +8 -11
  20. package/dist/esm/actions/public/simulateContract.js.map +1 -1
  21. package/dist/esm/actions/wallet/writeContract.js +2 -6
  22. package/dist/esm/actions/wallet/writeContract.js.map +1 -1
  23. package/dist/esm/clients/decorators/public.js.map +1 -1
  24. package/dist/esm/clients/decorators/wallet.js +1 -0
  25. package/dist/esm/clients/decorators/wallet.js.map +1 -1
  26. package/dist/esm/types/contract.js +1 -1
  27. package/dist/esm/types/contract.js.map +1 -1
  28. package/dist/types/actions/getContract.d.ts +13 -13
  29. package/dist/types/actions/getContract.d.ts.map +1 -1
  30. package/dist/types/actions/public/estimateContractGas.d.ts +3 -3
  31. package/dist/types/actions/public/estimateContractGas.d.ts.map +1 -1
  32. package/dist/types/actions/public/multicall.d.ts +4 -3
  33. package/dist/types/actions/public/multicall.d.ts.map +1 -1
  34. package/dist/types/actions/public/readContract.d.ts +4 -4
  35. package/dist/types/actions/public/readContract.d.ts.map +1 -1
  36. package/dist/types/actions/public/simulateContract.d.ts +11 -11
  37. package/dist/types/actions/public/simulateContract.d.ts.map +1 -1
  38. package/dist/types/actions/wallet/writeContract.d.ts +3 -3
  39. package/dist/types/actions/wallet/writeContract.d.ts.map +1 -1
  40. package/dist/types/clients/decorators/public.d.ts +4 -4
  41. package/dist/types/clients/decorators/public.d.ts.map +1 -1
  42. package/dist/types/clients/decorators/wallet.d.ts +2 -1
  43. package/dist/types/clients/decorators/wallet.d.ts.map +1 -1
  44. package/dist/types/index.d.ts +2 -2
  45. package/dist/types/index.d.ts.map +1 -1
  46. package/dist/types/types/contract.d.ts +15 -28
  47. package/dist/types/types/contract.d.ts.map +1 -1
  48. package/dist/types/types/multicall.d.ts +18 -35
  49. package/dist/types/types/multicall.d.ts.map +1 -1
  50. package/dist/types/types/utils.d.ts +4 -0
  51. package/dist/types/types/utils.d.ts.map +1 -1
  52. package/package.json +1 -1
  53. package/src/actions/getContract.ts +251 -191
  54. package/src/actions/public/estimateContractGas.ts +48 -38
  55. package/src/actions/public/multicall.ts +4 -5
  56. package/src/actions/public/readContract.ts +39 -33
  57. package/src/actions/public/simulateContract.ts +91 -59
  58. package/src/actions/wallet/writeContract.ts +28 -15
  59. package/src/clients/decorators/public.ts +32 -12
  60. package/src/clients/decorators/wallet.ts +10 -4
  61. package/src/index.ts +4 -4
  62. package/src/types/contract.ts +98 -117
  63. package/src/types/multicall.ts +67 -121
  64. package/src/types/utils.ts +9 -0
@@ -6,27 +6,42 @@ import type { Client } from '../../clients/createClient.js'
6
6
  import type { Transport } from '../../clients/transports/createTransport.js'
7
7
  import type { BaseError } from '../../errors/base.js'
8
8
  import type { Chain } from '../../types/chain.js'
9
- import type { ContractFunctionConfig, GetValue } from '../../types/contract.js'
9
+ import type {
10
+ ContractFunctionArgs,
11
+ ContractFunctionName,
12
+ ContractFunctionParameters,
13
+ GetValue,
14
+ } from '../../types/contract.js'
10
15
  import type { UnionOmit } from '../../types/utils.js'
11
- import {
12
- type EncodeFunctionDataParameters,
13
- encodeFunctionData,
14
- } from '../../utils/abi/encodeFunctionData.js'
16
+ import { encodeFunctionData } from '../../utils/abi/encodeFunctionData.js'
15
17
  import { getContractError } from '../../utils/errors/getContractError.js'
16
18
  import { type EstimateGasParameters, estimateGas } from './estimateGas.js'
17
19
 
18
20
  export type EstimateContractGasParameters<
19
- TAbi extends Abi | readonly unknown[] = Abi,
20
- TFunctionName extends string = string,
21
- TChain extends Chain | undefined = Chain | undefined,
22
- TAccount extends Account | undefined = undefined,
23
- > = ContractFunctionConfig<TAbi, TFunctionName, 'payable' | 'nonpayable'> &
24
- UnionOmit<EstimateGasParameters<TChain, TAccount>, 'data' | 'to' | 'value'> &
21
+ abi extends Abi | readonly unknown[] = Abi,
22
+ functionName extends ContractFunctionName<
23
+ abi,
24
+ 'nonpayable' | 'payable'
25
+ > = ContractFunctionName<abi, 'nonpayable' | 'payable'>,
26
+ args extends ContractFunctionArgs<
27
+ abi,
28
+ 'nonpayable' | 'payable',
29
+ functionName
30
+ > = ContractFunctionArgs<abi, 'nonpayable' | 'payable', functionName>,
31
+ chain extends Chain | undefined = Chain | undefined,
32
+ account extends Account | undefined = undefined,
33
+ > = ContractFunctionParameters<
34
+ abi,
35
+ 'nonpayable' | 'payable',
36
+ functionName,
37
+ args
38
+ > &
39
+ UnionOmit<EstimateGasParameters<chain, account>, 'data' | 'to' | 'value'> &
25
40
  GetValue<
26
- TAbi,
27
- TFunctionName,
28
- EstimateGasParameters<TChain> extends EstimateGasParameters
29
- ? EstimateGasParameters<TChain>['value']
41
+ abi,
42
+ functionName,
43
+ EstimateGasParameters<chain> extends EstimateGasParameters
44
+ ? EstimateGasParameters<chain>['value']
30
45
  : EstimateGasParameters['value']
31
46
  >
32
47
 
@@ -60,36 +75,31 @@ export type EstimateContractGasReturnType = bigint
60
75
  * })
61
76
  */
62
77
  export async function estimateContractGas<
63
- const TAbi extends Abi | readonly unknown[],
64
- TFunctionName extends string,
65
- TChain extends Chain | undefined,
66
- TAccount extends Account | undefined = undefined,
78
+ const abi extends Abi | readonly unknown[],
79
+ functionName extends ContractFunctionName<abi, 'nonpayable' | 'payable'>,
80
+ args extends ContractFunctionArgs<abi, 'pure' | 'view', functionName>,
81
+ chain extends Chain | undefined,
82
+ account extends Account | undefined = undefined,
67
83
  >(
68
- client: Client<Transport, TChain, TAccount>,
69
- {
84
+ client: Client<Transport, chain, account>,
85
+ parameters: EstimateContractGasParameters<
70
86
  abi,
71
- address,
72
- args,
73
87
  functionName,
74
- ...request
75
- }: EstimateContractGasParameters<TAbi, TFunctionName, TChain, TAccount>,
76
- ): Promise<EstimateContractGasReturnType> {
77
- const data = encodeFunctionData({
78
- abi,
79
88
  args,
80
- functionName,
81
- } as unknown as EncodeFunctionDataParameters<TAbi, TFunctionName>)
89
+ chain,
90
+ account
91
+ >,
92
+ ): Promise<EstimateContractGasReturnType> {
93
+ const { abi, address, args, functionName, ...request } =
94
+ parameters as EstimateContractGasParameters
95
+ const data = encodeFunctionData({ abi, args, functionName })
82
96
  try {
83
- const gas = await estimateGas(client, {
84
- data,
85
- to: address,
86
- ...request,
87
- } as unknown as EstimateGasParameters<TChain>)
97
+ const gas = await estimateGas(client, { data, to: address, ...request })
88
98
  return gas
89
- } catch (err) {
99
+ } catch (error) {
90
100
  const account = request.account ? parseAccount(request.account) : undefined
91
- throw getContractError(err as BaseError, {
92
- abi: abi as Abi,
101
+ throw getContractError(error as BaseError, {
102
+ abi,
93
103
  address,
94
104
  args,
95
105
  docsPath: '/docs/contract/estimateContractGas',
@@ -7,9 +7,9 @@ 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 { ContractFunctionParameters } from '../../types/contract.js'
10
11
  import type { Hex } from '../../types/misc.js'
11
12
  import type {
12
- MulticallContract,
13
13
  MulticallContracts,
14
14
  MulticallResults,
15
15
  } from '../../types/multicall.js'
@@ -17,12 +17,11 @@ import { decodeFunctionResult } from '../../utils/abi/decodeFunctionResult.js'
17
17
  import { encodeFunctionData } from '../../utils/abi/encodeFunctionData.js'
18
18
  import { getChainContractAddress } from '../../utils/chain.js'
19
19
  import { getContractError } from '../../utils/errors/getContractError.js'
20
-
21
20
  import type { CallParameters } from './call.js'
22
21
  import { readContract } from './readContract.js'
23
22
 
24
23
  export type MulticallParameters<
25
- contracts extends readonly unknown[] = readonly MulticallContract[],
24
+ contracts extends readonly unknown[] = readonly ContractFunctionParameters[],
26
25
  allowFailure extends boolean = true,
27
26
  options extends {
28
27
  optional?: boolean
@@ -39,7 +38,7 @@ export type MulticallParameters<
39
38
  }
40
39
 
41
40
  export type MulticallReturnType<
42
- contracts extends readonly unknown[] = readonly MulticallContract[],
41
+ contracts extends readonly unknown[] = readonly ContractFunctionParameters[],
43
42
  allowFailure extends boolean = true,
44
43
  options extends {
45
44
  error?: Error
@@ -104,7 +103,7 @@ export async function multicall<
104
103
  blockTag,
105
104
  multicallAddress: multicallAddress_,
106
105
  } = parameters
107
- const contracts = parameters.contracts as MulticallContract[]
106
+ const contracts = parameters.contracts as ContractFunctionParameters[]
108
107
 
109
108
  const batchSize =
110
109
  batchSize_ ??
@@ -5,31 +5,42 @@ 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
- ContractFunctionResult,
8
+ ContractFunctionArgs,
9
+ ContractFunctionName,
10
+ ContractFunctionParameters,
11
+ ContractFunctionReturnType,
10
12
  } from '../../types/contract.js'
11
- import {
12
- type DecodeFunctionResultParameters,
13
- decodeFunctionResult,
14
- } from '../../utils/abi/decodeFunctionResult.js'
15
- import {
16
- type EncodeFunctionDataParameters,
17
- encodeFunctionData,
18
- } from '../../utils/abi/encodeFunctionData.js'
13
+ import { decodeFunctionResult } from '../../utils/abi/decodeFunctionResult.js'
14
+ import { encodeFunctionData } from '../../utils/abi/encodeFunctionData.js'
19
15
  import { getContractError } from '../../utils/errors/getContractError.js'
20
-
21
16
  import { type CallParameters, call } from './call.js'
22
17
 
23
18
  export type ReadContractParameters<
24
19
  abi extends Abi | readonly unknown[] = Abi,
25
- functionName extends string = string,
20
+ functionName extends ContractFunctionName<
21
+ abi,
22
+ 'pure' | 'view'
23
+ > = ContractFunctionName<abi, 'pure' | 'view'>,
24
+ args extends ContractFunctionArgs<
25
+ abi,
26
+ 'pure' | 'view',
27
+ functionName
28
+ > = ContractFunctionArgs<abi, 'pure' | 'view', functionName>,
26
29
  > = Pick<CallParameters, 'account' | 'blockNumber' | 'blockTag'> &
27
- ContractFunctionConfig<abi, functionName, 'view' | 'pure'>
30
+ ContractFunctionParameters<abi, 'pure' | 'view', functionName, args>
28
31
 
29
32
  export type ReadContractReturnType<
30
- TAbi extends Abi | readonly unknown[] = Abi,
31
- TFunctionName extends string = string,
32
- > = ContractFunctionResult<TAbi, TFunctionName>
33
+ abi extends Abi | readonly unknown[] = Abi,
34
+ functionName extends ContractFunctionName<
35
+ abi,
36
+ 'pure' | 'view'
37
+ > = ContractFunctionName<abi, 'pure' | 'view'>,
38
+ args extends ContractFunctionArgs<
39
+ abi,
40
+ 'pure' | 'view',
41
+ functionName
42
+ > = ContractFunctionArgs<abi, 'pure' | 'view', functionName>,
43
+ > = ContractFunctionReturnType<abi, 'pure' | 'view', functionName, args>
33
44
 
34
45
  /**
35
46
  * Calls a read-only function on a contract, and returns the response.
@@ -65,35 +76,30 @@ export type ReadContractReturnType<
65
76
  export async function readContract<
66
77
  chain extends Chain | undefined,
67
78
  const abi extends Abi | readonly unknown[],
68
- functionName extends string,
79
+ functionName extends ContractFunctionName<abi, 'pure' | 'view'>,
80
+ args extends ContractFunctionArgs<abi, 'pure' | 'view', functionName>,
69
81
  >(
70
82
  client: Client<Transport, chain>,
71
- parameters: ReadContractParameters<abi, functionName>,
72
- ): Promise<ReadContractReturnType<abi, functionName>> {
73
- const { abi, address, args, functionName, ...callRequest } = parameters
74
- const calldata = encodeFunctionData({
75
- abi,
76
- args,
77
- functionName,
78
- } as unknown as EncodeFunctionDataParameters<abi, functionName>)
83
+ parameters: ReadContractParameters<abi, functionName, args>,
84
+ ): Promise<ReadContractReturnType<abi, functionName, args>> {
85
+ const { abi, address, args, functionName, ...rest } =
86
+ parameters as ReadContractParameters
87
+ const calldata = encodeFunctionData({ abi, args, functionName })
79
88
  try {
80
89
  const { data } = await call(client, {
90
+ ...(rest as CallParameters),
81
91
  data: calldata,
82
92
  to: address,
83
- ...callRequest,
84
- } as unknown as CallParameters)
93
+ })
85
94
  return decodeFunctionResult({
86
95
  abi,
87
96
  args,
88
97
  functionName,
89
98
  data: data || '0x',
90
- } as DecodeFunctionResultParameters<
99
+ }) as ReadContractReturnType<abi, functionName>
100
+ } catch (error) {
101
+ throw getContractError(error as BaseError, {
91
102
  abi,
92
- functionName
93
- >) as ReadContractReturnType<abi, functionName>
94
- } catch (err) {
95
- throw getContractError(err as BaseError, {
96
- abi: abi as Abi,
97
103
  address,
98
104
  args,
99
105
  docsPath: '/docs/contract/readContract',
@@ -6,67 +6,95 @@ import type { Transport } from '../../clients/transports/createTransport.js'
6
6
  import type { BaseError } from '../../errors/base.js'
7
7
  import type { Chain } from '../../types/chain.js'
8
8
  import type {
9
- ContractFunctionConfig,
10
- ContractFunctionResult,
9
+ ContractFunctionArgs,
10
+ ContractFunctionName,
11
+ ContractFunctionParameters,
12
+ ContractFunctionReturnType,
11
13
  GetValue,
12
14
  } from '../../types/contract.js'
13
15
  import type { Hex } from '../../types/misc.js'
14
16
  import type { UnionOmit } from '../../types/utils.js'
15
- import {
16
- type DecodeFunctionResultParameters,
17
- decodeFunctionResult,
18
- } from '../../utils/abi/decodeFunctionResult.js'
19
- import {
20
- type EncodeFunctionDataParameters,
21
- encodeFunctionData,
22
- } from '../../utils/abi/encodeFunctionData.js'
17
+ import { decodeFunctionResult } from '../../utils/abi/decodeFunctionResult.js'
18
+ import { encodeFunctionData } from '../../utils/abi/encodeFunctionData.js'
23
19
  import { getContractError } from '../../utils/errors/getContractError.js'
24
20
  import type { WriteContractParameters } from '../wallet/writeContract.js'
25
21
 
26
22
  import { type CallParameters, call } from './call.js'
27
23
 
28
24
  export type SimulateContractParameters<
29
- TAbi extends Abi | readonly unknown[] = Abi,
30
- TFunctionName extends string = any,
31
- TChain extends Chain | undefined = Chain | undefined,
32
- TChainOverride extends Chain | undefined = Chain | undefined,
25
+ abi extends Abi | readonly unknown[] = Abi,
26
+ functionName extends ContractFunctionName<
27
+ abi,
28
+ 'nonpayable' | 'payable'
29
+ > = ContractFunctionName<abi, 'nonpayable' | 'payable'>,
30
+ args extends ContractFunctionArgs<
31
+ abi,
32
+ 'nonpayable' | 'payable',
33
+ functionName
34
+ > = ContractFunctionArgs<abi, 'nonpayable' | 'payable', functionName>,
35
+ chain extends Chain | undefined = Chain | undefined,
36
+ chainOverride extends Chain | undefined = Chain | undefined,
33
37
  > = {
34
- chain?: TChainOverride
38
+ chain?: chainOverride
35
39
  /** Data to append to the end of the calldata. Useful for adding a ["domain" tag](https://opensea.notion.site/opensea/Seaport-Order-Attributions-ec2d69bf455041a5baa490941aad307f). */
36
40
  dataSuffix?: Hex
37
- } & ContractFunctionConfig<TAbi, TFunctionName, 'payable' | 'nonpayable'> &
41
+ } & ContractFunctionParameters<
42
+ abi,
43
+ 'nonpayable' | 'payable',
44
+ functionName,
45
+ args
46
+ > &
38
47
  UnionOmit<
39
- CallParameters<TChainOverride extends Chain ? TChainOverride : TChain>,
48
+ CallParameters<chainOverride extends Chain ? chainOverride : chain>,
40
49
  'batch' | 'to' | 'data' | 'value'
41
50
  > &
42
51
  GetValue<
43
- TAbi,
44
- TFunctionName,
45
- CallParameters<TChain> extends CallParameters
46
- ? CallParameters<TChain>['value']
52
+ abi,
53
+ functionName,
54
+ CallParameters<chain> extends CallParameters
55
+ ? CallParameters<chain>['value']
47
56
  : CallParameters['value']
48
57
  >
49
58
 
50
59
  export type SimulateContractReturnType<
51
- TAbi extends Abi | readonly unknown[] = Abi,
52
- TFunctionName extends string = string,
53
- TChain extends Chain | undefined = Chain | undefined,
54
- TChainOverride extends Chain | undefined = Chain | undefined,
60
+ abi extends Abi | readonly unknown[] = Abi,
61
+ functionName extends ContractFunctionName<
62
+ abi,
63
+ 'nonpayable' | 'payable'
64
+ > = ContractFunctionName<abi, 'nonpayable' | 'payable'>,
65
+ args extends ContractFunctionArgs<
66
+ abi,
67
+ 'nonpayable' | 'payable',
68
+ functionName
69
+ > = ContractFunctionArgs<abi, 'nonpayable' | 'payable', functionName>,
70
+ chain extends Chain | undefined = Chain | undefined,
71
+ chainOverride extends Chain | undefined = Chain | undefined,
55
72
  > = {
56
- result: ContractFunctionResult<TAbi, TFunctionName>
73
+ result: ContractFunctionReturnType<
74
+ abi,
75
+ 'nonpayable' | 'payable',
76
+ functionName,
77
+ args
78
+ >
57
79
  request: UnionOmit<
58
80
  WriteContractParameters<
59
- TAbi,
60
- TFunctionName,
61
- TChain,
81
+ abi,
82
+ functionName,
83
+ args,
84
+ chain,
62
85
  undefined,
63
- TChainOverride
86
+ chainOverride
64
87
  >,
65
88
  'chain' | 'functionName'
66
89
  > & {
67
- chain: TChainOverride
68
- functionName: TFunctionName
69
- } & ContractFunctionConfig<TAbi, TFunctionName, 'payable' | 'nonpayable'>
90
+ chain: chainOverride
91
+ functionName: functionName
92
+ } & ContractFunctionParameters<
93
+ abi,
94
+ 'nonpayable' | 'payable',
95
+ functionName,
96
+ args
97
+ >
70
98
  }
71
99
 
72
100
  /**
@@ -101,44 +129,47 @@ export type SimulateContractReturnType<
101
129
  * })
102
130
  */
103
131
  export async function simulateContract<
104
- TChain extends Chain | undefined,
105
- const TAbi extends Abi | readonly unknown[],
106
- TFunctionName extends string,
107
- TChainOverride extends Chain | undefined,
132
+ chain extends Chain | undefined,
133
+ const abi extends Abi | readonly unknown[],
134
+ functionName extends ContractFunctionName<abi, 'nonpayable' | 'payable'>,
135
+ args extends ContractFunctionArgs<
136
+ abi,
137
+ 'nonpayable' | 'payable',
138
+ functionName
139
+ >,
140
+ chainOverride extends Chain | undefined,
108
141
  >(
109
- client: Client<Transport, TChain>,
110
- {
142
+ client: Client<Transport, chain>,
143
+ parameters: SimulateContractParameters<
111
144
  abi,
112
- address,
113
- args,
114
- dataSuffix,
115
145
  functionName,
116
- ...callRequest
117
- }: SimulateContractParameters<TAbi, TFunctionName, TChain, TChainOverride>,
146
+ args,
147
+ chain,
148
+ chainOverride
149
+ >,
118
150
  ): Promise<
119
- SimulateContractReturnType<TAbi, TFunctionName, TChain, TChainOverride>
151
+ SimulateContractReturnType<abi, functionName, args, chain, chainOverride>
120
152
  > {
153
+ const { abi, address, args, dataSuffix, functionName, ...callRequest } =
154
+ parameters as SimulateContractParameters
155
+
121
156
  const account = callRequest.account
122
157
  ? parseAccount(callRequest.account)
123
158
  : undefined
124
- const calldata = encodeFunctionData({
125
- abi,
126
- args,
127
- functionName,
128
- } as unknown as EncodeFunctionDataParameters<TAbi, TFunctionName>)
159
+ const calldata = encodeFunctionData({ abi, args, functionName })
129
160
  try {
130
161
  const { data } = await call(client, {
131
162
  batch: false,
132
163
  data: `${calldata}${dataSuffix ? dataSuffix.replace('0x', '') : ''}`,
133
164
  to: address,
134
165
  ...callRequest,
135
- } as unknown as CallParameters<TChain>)
166
+ })
136
167
  const result = decodeFunctionResult({
137
168
  abi,
138
169
  args,
139
170
  functionName,
140
171
  data: data || '0x',
141
- } as DecodeFunctionResultParameters)
172
+ })
142
173
  return {
143
174
  result,
144
175
  request: {
@@ -150,14 +181,15 @@ export async function simulateContract<
150
181
  ...callRequest,
151
182
  },
152
183
  } as unknown as SimulateContractReturnType<
153
- TAbi,
154
- TFunctionName,
155
- TChain,
156
- TChainOverride
184
+ abi,
185
+ functionName,
186
+ args,
187
+ chain,
188
+ chainOverride
157
189
  >
158
- } catch (err) {
159
- throw getContractError(err as BaseError, {
160
- abi: abi as Abi,
190
+ } catch (error) {
191
+ throw getContractError(error as BaseError, {
192
+ abi,
161
193
  address,
162
194
  args,
163
195
  docsPath: '/docs/contract/simulateContract',
@@ -5,13 +5,15 @@ 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 { ContractFunctionConfig, GetValue } from '../../types/contract.js'
8
+ import type {
9
+ ContractFunctionArgs,
10
+ ContractFunctionName,
11
+ ContractFunctionParameters,
12
+ GetValue,
13
+ } from '../../types/contract.js'
9
14
  import type { Hex } from '../../types/misc.js'
10
15
  import type { UnionOmit } from '../../types/utils.js'
11
- import {
12
- type EncodeFunctionDataParameters,
13
- encodeFunctionData,
14
- } from '../../utils/abi/encodeFunctionData.js'
16
+ import { encodeFunctionData } from '../../utils/abi/encodeFunctionData.js'
15
17
  import type { FormattedTransactionRequest } from '../../utils/formatters/transactionRequest.js'
16
18
  import {
17
19
  type SendTransactionParameters,
@@ -21,11 +23,24 @@ import {
21
23
 
22
24
  export type WriteContractParameters<
23
25
  abi extends Abi | readonly unknown[] = Abi,
24
- functionName extends string = string,
26
+ functionName extends ContractFunctionName<
27
+ abi,
28
+ 'nonpayable' | 'payable'
29
+ > = ContractFunctionName<abi, 'nonpayable' | 'payable'>,
30
+ args extends ContractFunctionArgs<
31
+ abi,
32
+ 'nonpayable' | 'payable',
33
+ functionName
34
+ > = ContractFunctionArgs<abi, 'nonpayable' | 'payable', functionName>,
25
35
  chain extends Chain | undefined = Chain,
26
36
  account extends Account | undefined = Account | undefined,
27
37
  chainOverride extends Chain | undefined = Chain | undefined,
28
- > = ContractFunctionConfig<abi, functionName, 'payable' | 'nonpayable'> &
38
+ > = ContractFunctionParameters<
39
+ abi,
40
+ 'nonpayable' | 'payable',
41
+ functionName,
42
+ args
43
+ > &
29
44
  UnionOmit<
30
45
  FormattedTransactionRequest<
31
46
  chainOverride extends Chain ? chainOverride : chain
@@ -105,29 +120,27 @@ export async function writeContract<
105
120
  chain extends Chain | undefined,
106
121
  account extends Account | undefined,
107
122
  const abi extends Abi | readonly unknown[],
108
- functionName extends string,
123
+ functionName extends ContractFunctionName<abi, 'nonpayable' | 'payable'>,
124
+ args extends ContractFunctionArgs<abi, 'pure' | 'view', functionName>,
109
125
  chainOverride extends Chain | undefined,
110
126
  >(
111
127
  client: Client<Transport, chain, account>,
112
128
  parameters: WriteContractParameters<
113
129
  abi,
114
130
  functionName,
131
+ args,
115
132
  chain,
116
133
  account,
117
134
  chainOverride
118
135
  >,
119
136
  ): Promise<WriteContractReturnType> {
120
137
  const { abi, address, args, dataSuffix, functionName, ...request } =
121
- parameters
122
- const data = encodeFunctionData({
123
- abi,
124
- args,
125
- functionName,
126
- } as unknown as EncodeFunctionDataParameters<abi, functionName>)
138
+ parameters as WriteContractParameters
139
+ const data = encodeFunctionData({ abi, args, functionName })
127
140
  const hash = await sendTransaction(client, {
128
141
  data: `${data}${dataSuffix ? dataSuffix.replace('0x', '') : ''}`,
129
142
  to: address,
130
143
  ...request,
131
- } as unknown as SendTransactionParameters<chain, account, chainOverride>)
144
+ })
132
145
  return hash
133
146
  }
@@ -200,6 +200,8 @@ 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
+ ContractFunctionArgs,
204
+ ContractFunctionName,
203
205
  MaybeAbiEventName,
204
206
  MaybeExtractEventArgsFromAbi,
205
207
  } from '../../types/contract.js'
@@ -405,10 +407,21 @@ export type PublicActions<
405
407
  */
406
408
  estimateContractGas: <
407
409
  TChain extends Chain | undefined,
408
- const TAbi extends Abi | readonly unknown[],
409
- TFunctionName extends string,
410
+ const abi extends Abi | readonly unknown[],
411
+ functionName extends ContractFunctionName<abi, 'nonpayable' | 'payable'>,
412
+ args extends ContractFunctionArgs<
413
+ abi,
414
+ 'nonpayable' | 'payable',
415
+ functionName
416
+ >,
410
417
  >(
411
- args: EstimateContractGasParameters<TAbi, TFunctionName, TChain, TAccount>,
418
+ args: EstimateContractGasParameters<
419
+ abi,
420
+ functionName,
421
+ args,
422
+ TChain,
423
+ TAccount
424
+ >,
412
425
  ) => Promise<EstimateContractGasReturnType>
413
426
  /**
414
427
  * Estimates the gas necessary to complete a transaction without submitting it to the network.
@@ -1169,11 +1182,12 @@ export type PublicActions<
1169
1182
  * // 424122n
1170
1183
  */
1171
1184
  readContract: <
1172
- const TAbi extends Abi | readonly unknown[],
1173
- TFunctionName extends string,
1185
+ const abi extends Abi | readonly unknown[],
1186
+ functionName extends ContractFunctionName<abi, 'pure' | 'view'>,
1187
+ args extends ContractFunctionArgs<abi, 'pure' | 'view', functionName>,
1174
1188
  >(
1175
- args: ReadContractParameters<TAbi, TFunctionName>,
1176
- ) => Promise<ReadContractReturnType<TAbi, TFunctionName>>
1189
+ args: ReadContractParameters<abi, functionName, args>,
1190
+ ) => Promise<ReadContractReturnType<abi, functionName, args>>
1177
1191
  /**
1178
1192
  * Simulates/validates a contract interaction. This is useful for retrieving **return data** and **revert reasons** of contract write functions.
1179
1193
  *
@@ -1205,18 +1219,24 @@ export type PublicActions<
1205
1219
  * })
1206
1220
  */
1207
1221
  simulateContract: <
1208
- const TAbi extends Abi | readonly unknown[],
1209
- TFunctionName extends string,
1222
+ const abi extends Abi | readonly unknown[],
1223
+ functionName extends ContractFunctionName<abi, 'nonpayable' | 'payable'>,
1224
+ args extends ContractFunctionArgs<
1225
+ abi,
1226
+ 'nonpayable' | 'payable',
1227
+ functionName
1228
+ >,
1210
1229
  TChainOverride extends Chain | undefined,
1211
1230
  >(
1212
1231
  args: SimulateContractParameters<
1213
- TAbi,
1214
- TFunctionName,
1232
+ abi,
1233
+ functionName,
1234
+ args,
1215
1235
  TChain,
1216
1236
  TChainOverride
1217
1237
  >,
1218
1238
  ) => Promise<
1219
- SimulateContractReturnType<TAbi, TFunctionName, TChain, TChainOverride>
1239
+ SimulateContractReturnType<abi, functionName, args, TChain, TChainOverride>
1220
1240
  >
1221
1241
  verifyMessage: (
1222
1242
  args: VerifyMessageParameters,