viem 0.0.1-alpha.21 → 0.0.1-alpha.23

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 (181) hide show
  1. package/dist/{chain-32f56cfa.d.ts → chain-4b39613a.d.ts} +2 -2
  2. package/dist/{chain-f12cdc7f.d.ts → chain-aa4898d0.d.ts} +1 -1
  3. package/dist/chains.d.ts +3 -3
  4. package/dist/chains.js +46 -46
  5. package/dist/chains.mjs +1 -1
  6. package/dist/{chunk-EC3NUIJE.js → chunk-7Y5QVLX7.js} +13 -13
  7. package/dist/{chunk-EC3NUIJE.js.map → chunk-7Y5QVLX7.js.map} +1 -1
  8. package/dist/{chunk-KEHGSYDO.js → chunk-BV5NTHUX.js} +14 -13
  9. package/dist/chunk-BV5NTHUX.js.map +1 -0
  10. package/dist/{chunk-46BO7YAQ.mjs → chunk-EGVXCZNJ.mjs} +4 -3
  11. package/dist/chunk-EGVXCZNJ.mjs.map +1 -0
  12. package/dist/{chunk-57ZOFERP.mjs → chunk-GX2KDAM3.mjs} +2 -2
  13. package/dist/{chunk-57ZOFERP.mjs.map → chunk-GX2KDAM3.mjs.map} +0 -0
  14. package/dist/{chunk-W7BWWAC4.js → chunk-NW6724MI.js} +14 -14
  15. package/dist/{chunk-W7BWWAC4.js.map → chunk-NW6724MI.js.map} +0 -0
  16. package/dist/{chunk-DGO77E2H.mjs → chunk-RJLUZTJS.mjs} +86 -3
  17. package/dist/chunk-RJLUZTJS.mjs.map +1 -0
  18. package/dist/{chunk-O2GYLJVD.js → chunk-SSPRUPGN.js} +405 -165
  19. package/dist/chunk-SSPRUPGN.js.map +1 -0
  20. package/dist/{chunk-DSPMAIDO.mjs → chunk-TXHOG6KU.mjs} +2 -2
  21. package/dist/{chunk-DSPMAIDO.mjs.map → chunk-TXHOG6KU.mjs.map} +1 -1
  22. package/dist/{chunk-62VTYU2V.mjs → chunk-XBUH66KN.mjs} +406 -166
  23. package/dist/chunk-XBUH66KN.mjs.map +1 -0
  24. package/dist/{chunk-KZMJR27B.js → chunk-ZKYGWITF.js} +147 -64
  25. package/dist/chunk-ZKYGWITF.js.map +1 -0
  26. package/dist/clients/index.d.ts +7 -7
  27. package/dist/clients/index.js +3 -3
  28. package/dist/clients/index.mjs +2 -2
  29. package/dist/{contract-9af4608b.d.ts → contract-4c3a37b3.d.ts} +14 -3
  30. package/dist/{createClient-5d316c7e.d.ts → createClient-55a04188.d.ts} +2 -2
  31. package/dist/{createPublicClient-9d2b42e1.d.ts → createPublicClient-3b27b282.d.ts} +3 -3
  32. package/dist/{createTestClient-79498dab.d.ts → createTestClient-93f9eac6.d.ts} +3 -3
  33. package/dist/{createWalletClient-f69a5230.d.ts → createWalletClient-c10df94d.d.ts} +3 -3
  34. package/dist/{eip1193-6c485d63.d.ts → eip1193-4c24765a.d.ts} +1 -1
  35. package/dist/ens.d.ts +1 -0
  36. package/dist/ens.js +11 -0
  37. package/dist/ens.js.map +1 -0
  38. package/dist/ens.mjs +11 -0
  39. package/dist/ens.mjs.map +1 -0
  40. package/dist/index.d.ts +174 -113
  41. package/dist/index.js +16 -6
  42. package/dist/index.mjs +15 -5
  43. package/dist/normalize-ef9240c0.d.ts +33 -0
  44. package/dist/{parseGwei-4308ad80.d.ts → parseGwei-3411cf2d.d.ts} +101 -37
  45. package/dist/public.d.ts +9 -9
  46. package/dist/public.js +4 -4
  47. package/dist/public.mjs +3 -3
  48. package/dist/{sendTransaction-1c8290a9.d.ts → sendTransaction-f17a2389.d.ts} +3 -3
  49. package/dist/{stopImpersonatingAccount-7781842a.d.ts → stopImpersonatingAccount-afb26486.d.ts} +2 -2
  50. package/dist/test.d.ts +5 -5
  51. package/dist/test.js +3 -3
  52. package/dist/test.mjs +2 -2
  53. package/dist/{transactionRequest-341b6ed2.d.ts → transactionRequest-93e9f001.d.ts} +2 -2
  54. package/dist/utils/index.d.ts +14 -16
  55. package/dist/utils/index.js +8 -2
  56. package/dist/utils/index.mjs +7 -1
  57. package/dist/wallet.d.ts +7 -7
  58. package/dist/wallet.js +3 -3
  59. package/dist/wallet.mjs +2 -2
  60. package/dist/{watchAsset-afaad3c7.d.ts → watchAsset-efd3dd05.d.ts} +3 -3
  61. package/dist/{watchPendingTransactions-3b722547.d.ts → watchPendingTransactions-57df1a13.d.ts} +17 -27
  62. package/dist/{webSocket-b180e679.d.ts → webSocket-d2e7bd0e.d.ts} +2 -2
  63. package/dist/window.d.ts +2 -2
  64. package/package.json +8 -2
  65. package/src/_test/abis.ts +7 -0
  66. package/src/_test/bench.ts +4 -4
  67. package/src/_test/constants.ts +4 -3
  68. package/src/_test/index.ts +4 -2
  69. package/src/_test/utils.ts +39 -1
  70. package/src/actions/index.test.ts +1 -0
  71. package/src/actions/index.ts +2 -0
  72. package/src/actions/public/call.bench.ts +5 -5
  73. package/src/actions/public/deployContract.ts +4 -4
  74. package/src/actions/public/estimateGas.bench.ts +6 -6
  75. package/src/actions/public/getBlock.bench.ts +5 -5
  76. package/src/actions/public/getBlockNumber.bench.ts +5 -5
  77. package/src/actions/public/getFilterChanges.test.ts +9 -9
  78. package/src/actions/public/getFilterLogs.test.ts +7 -7
  79. package/src/actions/public/getGasPrice.bench.ts +1 -10
  80. package/src/actions/public/getLogs.test.ts +7 -7
  81. package/src/actions/public/getTransaction.bench.ts +5 -5
  82. package/src/actions/public/getTransactionReceipt.bench.ts +5 -5
  83. package/src/actions/public/index.test.ts +1 -0
  84. package/src/actions/public/index.ts +3 -0
  85. package/src/actions/public/multicall.test.ts +452 -0
  86. package/src/actions/public/multicall.ts +103 -0
  87. package/src/actions/public/readContract.test.ts +226 -26
  88. package/src/actions/public/readContract.ts +13 -27
  89. package/src/actions/public/simulateContract.bench.ts +10 -0
  90. package/src/actions/public/simulateContract.test.ts +209 -37
  91. package/src/actions/public/simulateContract.ts +17 -21
  92. package/src/actions/public/watchContractEvent.test.ts +61 -57
  93. package/src/actions/public/watchEvent.test.ts +42 -37
  94. package/src/actions/test/impersonateAccount.test.ts +4 -6
  95. package/src/actions/test/sendUnsignedTransaction.test.ts +2 -2
  96. package/src/actions/test/stopImpersonatingAccount.test.ts +5 -7
  97. package/src/actions/wallet/signMessage.test.ts +0 -1
  98. package/src/actions/wallet/switchChain.test.ts +1 -2
  99. package/src/actions/wallet/watchAsset.test.ts +1 -2
  100. package/src/actions/wallet/writeContract.test.ts +37 -7
  101. package/src/actions/wallet/writeContract.ts +5 -14
  102. package/src/clients/transports/fallback.test.ts +34 -0
  103. package/src/clients/transports/fallback.ts +3 -1
  104. package/src/clients/transports/http.test.ts +0 -1
  105. package/src/clients/transports/webSocket.test.ts +0 -1
  106. package/src/constants/abis.test.ts +53 -0
  107. package/src/constants/abis.ts +44 -0
  108. package/src/constants/index.test.ts +14 -0
  109. package/src/constants/index.ts +3 -0
  110. package/src/constants/solidity.test.ts +41 -0
  111. package/src/constants/solidity.ts +35 -0
  112. package/src/ens.ts +5 -0
  113. package/src/errors/abi.test.ts +0 -2
  114. package/src/errors/base.test.ts +44 -2
  115. package/src/errors/base.ts +12 -12
  116. package/src/errors/block.test.ts +6 -6
  117. package/src/errors/contract.test.ts +233 -0
  118. package/src/errors/contract.ts +133 -80
  119. package/src/errors/index.ts +4 -2
  120. package/src/errors/request.test.ts +3 -4
  121. package/src/errors/request.ts +17 -17
  122. package/src/errors/rpc.test.ts +1 -2
  123. package/src/errors/transaction.test.ts +12 -12
  124. package/src/errors/transport.test.ts +0 -1
  125. package/src/index.test.ts +47 -0
  126. package/src/index.ts +10 -0
  127. package/src/types/contract.ts +55 -4
  128. package/src/types/index.ts +5 -0
  129. package/src/types/multicall.ts +82 -0
  130. package/src/utils/abi/decodeAbi.test.ts +1 -2
  131. package/src/utils/abi/decodeAbi.ts +11 -7
  132. package/src/utils/abi/decodeDeployData.test.ts +7 -15
  133. package/src/utils/abi/decodeDeployData.ts +10 -7
  134. package/src/utils/abi/decodeErrorResult.test.ts +103 -3
  135. package/src/utils/abi/decodeErrorResult.ts +30 -13
  136. package/src/utils/abi/decodeFunctionData.test.ts +0 -1
  137. package/src/utils/abi/decodeFunctionResult.test.ts +80 -9
  138. package/src/utils/abi/decodeFunctionResult.ts +15 -11
  139. package/src/utils/abi/encodeAbi.test.ts +3 -4
  140. package/src/utils/abi/encodeAbi.ts +19 -10
  141. package/src/utils/abi/encodeDeployData.test.ts +6 -13
  142. package/src/utils/abi/encodeDeployData.ts +10 -7
  143. package/src/utils/abi/encodeErrorResult.test.ts +2 -7
  144. package/src/utils/abi/encodeErrorResult.ts +18 -11
  145. package/src/utils/abi/encodeEventTopics.test.ts +11 -14
  146. package/src/utils/abi/encodeEventTopics.ts +15 -9
  147. package/src/utils/abi/encodeFunctionData.test.ts +5 -7
  148. package/src/utils/abi/encodeFunctionData.ts +16 -9
  149. package/src/utils/abi/encodeFunctionResult.test.ts +10 -15
  150. package/src/utils/abi/encodeFunctionResult.ts +9 -7
  151. package/src/utils/abi/getAbiItem.test.ts +547 -0
  152. package/src/utils/abi/getAbiItem.ts +93 -3
  153. package/src/utils/abi/index.ts +5 -1
  154. package/src/utils/address/getAddress.test.ts +6 -6
  155. package/src/utils/contract/getContractError.test.ts +247 -62
  156. package/src/utils/contract/getContractError.ts +30 -38
  157. package/src/utils/data/pad.bench.ts +0 -9
  158. package/src/utils/data/trim.bench.ts +0 -16
  159. package/src/utils/encoding/encodeBytes.bench.ts +0 -12
  160. package/src/utils/encoding/encodeHex.bench.ts +0 -11
  161. package/src/utils/ens/index.test.ts +13 -0
  162. package/src/utils/ens/index.ts +5 -0
  163. package/src/utils/ens/labelhash.test.ts +55 -0
  164. package/src/utils/ens/labelhash.ts +16 -0
  165. package/src/utils/ens/namehash.test.ts +65 -0
  166. package/src/utils/ens/namehash.ts +28 -0
  167. package/src/utils/ens/normalize.bench.ts +14 -0
  168. package/src/utils/ens/normalize.test.ts +35 -0
  169. package/src/utils/ens/normalize.ts +14 -0
  170. package/src/utils/hash/keccak256.ts +3 -5
  171. package/src/utils/index.test.ts +3 -0
  172. package/src/utils/index.ts +4 -0
  173. package/src/utils/rpc.test.ts +3 -6
  174. package/src/utils/unit/formatUnit.bench.ts +0 -5
  175. package/src/utils/unit/parseUnit.bench.ts +0 -5
  176. package/dist/chunk-46BO7YAQ.mjs.map +0 -1
  177. package/dist/chunk-62VTYU2V.mjs.map +0 -1
  178. package/dist/chunk-DGO77E2H.mjs.map +0 -1
  179. package/dist/chunk-KEHGSYDO.js.map +0 -1
  180. package/dist/chunk-KZMJR27B.js.map +0 -1
  181. package/dist/chunk-O2GYLJVD.js.map +0 -1
@@ -11,7 +11,7 @@ test('constructor()', () => {
11
11
  stateMutability: 'nonpayable',
12
12
  type: 'constructor',
13
13
  },
14
- ] as const,
14
+ ],
15
15
  bytecode:
16
16
  '0x6080604052348015600f57600080fd5b50603f80601d6000396000f3fe6080604052600080fdfea2646970667358221220116554d4ba29ee08da9e97dc54ff9a2a65d67a648140d616fc225a25ff08c86364736f6c63430008070033',
17
17
  }),
@@ -20,13 +20,12 @@ test('constructor()', () => {
20
20
  )
21
21
  expect(
22
22
  encodeDeployData({
23
- // @ts-expect-error
24
23
  abi: [
25
24
  {
26
25
  stateMutability: 'nonpayable',
27
26
  type: 'constructor',
28
27
  },
29
- ] as const,
28
+ ],
30
29
  bytecode:
31
30
  '0x6080604052348015600f57600080fd5b50603f80601d6000396000f3fe6080604052600080fdfea2646970667358221220116554d4ba29ee08da9e97dc54ff9a2a65d67a648140d616fc225a25ff08c86364736f6c63430008070033',
32
31
  }),
@@ -50,7 +49,7 @@ test('constructor(uint256)', () => {
50
49
  stateMutability: 'nonpayable',
51
50
  type: 'constructor',
52
51
  },
53
- ] as const,
52
+ ],
54
53
  bytecode:
55
54
  '0x6080604052348015600f57600080fd5b50603f80601d6000396000f3fe6080604052600080fdfea2646970667358221220116554d4ba29ee08da9e97dc54ff9a2a65d67a648140d616fc225a25ff08c86364736f6c63430008070033',
56
55
  args: [69420n],
@@ -63,8 +62,7 @@ test('constructor(uint256)', () => {
63
62
  test('error: constructor not found', () => {
64
63
  expect(() =>
65
64
  encodeDeployData({
66
- // @ts-expect-error
67
- abi: [{}] as const,
65
+ abi: [{}],
68
66
  bytecode:
69
67
  '0x6080604052348015600f57600080fd5b50603f80601d6000396000f3fe6080604052600080fdfea2646970667358221220116554d4ba29ee08da9e97dc54ff9a2a65d67a648140d616fc225a25ff08c86364736f6c63430008070033',
70
68
  args: [69420n],
@@ -74,7 +72,6 @@ test('error: constructor not found', () => {
74
72
  Make sure you are using the correct ABI and that the constructor exists on it.
75
73
 
76
74
  Docs: https://viem.sh/docs/contract/encodeDeployData
77
-
78
75
  Version: viem@1.0.2"
79
76
  `)
80
77
  })
@@ -82,13 +79,12 @@ test('error: constructor not found', () => {
82
79
  test('error: no inputs', () => {
83
80
  expect(() =>
84
81
  encodeDeployData({
85
- // @ts-expect-error
86
82
  abi: [
87
83
  {
88
84
  stateMutability: 'nonpayable',
89
85
  type: 'constructor',
90
86
  },
91
- ] as const,
87
+ ],
92
88
  bytecode:
93
89
  '0x6080604052348015600f57600080fd5b50603f80601d6000396000f3fe6080604052600080fdfea2646970667358221220116554d4ba29ee08da9e97dc54ff9a2a65d67a648140d616fc225a25ff08c86364736f6c63430008070033',
94
90
  args: [69420n],
@@ -99,21 +95,19 @@ test('error: no inputs', () => {
99
95
  Make sure you are using the correct ABI, and that the \`inputs\` attribute on the constructor exists.
100
96
 
101
97
  Docs: https://viem.sh/docs/contract/encodeDeployData
102
-
103
98
  Version: viem@1.0.2"
104
99
  `,
105
100
  )
106
101
 
107
102
  expect(() =>
108
103
  encodeDeployData({
109
- // @ts-expect-error
110
104
  abi: [
111
105
  {
112
106
  inputs: undefined,
113
107
  stateMutability: 'nonpayable',
114
108
  type: 'constructor',
115
109
  },
116
- ] as const,
110
+ ],
117
111
  bytecode:
118
112
  '0x6080604052348015600f57600080fd5b50603f80601d6000396000f3fe6080604052600080fdfea2646970667358221220116554d4ba29ee08da9e97dc54ff9a2a65d67a648140d616fc225a25ff08c86364736f6c63430008070033',
119
113
  args: [69420n],
@@ -124,7 +118,6 @@ test('error: no inputs', () => {
124
118
  Make sure you are using the correct ABI, and that the \`inputs\` attribute on the constructor exists.
125
119
 
126
120
  Docs: https://viem.sh/docs/contract/encodeDeployData
127
-
128
121
  Version: viem@1.0.2"
129
122
  `,
130
123
  )
@@ -1,4 +1,4 @@
1
- import { Abi } from 'abitype'
1
+ import { Abi, Narrow } from 'abitype'
2
2
 
3
3
  import {
4
4
  AbiConstructorNotFoundError,
@@ -10,19 +10,22 @@ import { encodeAbi } from './encodeAbi'
10
10
 
11
11
  const docsPath = '/docs/contract/encodeDeployData'
12
12
 
13
- export type EncodeDeployDataArgs<TAbi extends Abi = Abi> = {
14
- abi: TAbi
15
- bytecode: Hex
16
- } & ExtractConstructorArgsFromAbi<TAbi>
13
+ export type EncodeDeployDataArgs<TAbi extends Abi | readonly unknown[] = Abi> =
14
+ {
15
+ abi: Narrow<TAbi>
16
+ bytecode: Hex
17
+ } & ExtractConstructorArgsFromAbi<TAbi>
17
18
 
18
- export function encodeDeployData<TAbi extends Abi = Abi>({
19
+ export function encodeDeployData<TAbi extends Abi | readonly unknown[]>({
19
20
  abi,
20
21
  args,
21
22
  bytecode,
22
23
  }: EncodeDeployDataArgs<TAbi>) {
23
24
  if (!args || args.length === 0) return bytecode
24
25
 
25
- const description = abi.find((x) => 'type' in x && x.type === 'constructor')
26
+ const description = (abi as Abi).find(
27
+ (x) => 'type' in x && x.type === 'constructor',
28
+ )
26
29
  if (!description) throw new AbiConstructorNotFoundError({ docsPath })
27
30
  if (!('inputs' in description))
28
31
  throw new AbiConstructorParamsNotFoundError({ docsPath })
@@ -18,7 +18,6 @@ test('revert SoldOutError()', () => {
18
18
  expect(
19
19
  encodeErrorResult({
20
20
  abi: [
21
- // @ts-expect-error
22
21
  {
23
22
  name: 'SoldOutError',
24
23
  type: 'error',
@@ -44,7 +43,7 @@ test('revert AccessDeniedError(string)', () => {
44
43
  name: 'AccessDeniedError',
45
44
  type: 'error',
46
45
  },
47
- ] as const,
46
+ ],
48
47
  errorName: 'AccessDeniedError',
49
48
  args: ['you do not have access ser'],
50
49
  }),
@@ -116,6 +115,7 @@ test("errors: error doesn't exist", () => {
116
115
  type: 'error',
117
116
  },
118
117
  ],
118
+ // @ts-expect-error
119
119
  errorName: 'AccessDeniedError',
120
120
  args: [
121
121
  {
@@ -131,7 +131,6 @@ test("errors: error doesn't exist", () => {
131
131
  Make sure you are using the correct ABI and that the error exists on it.
132
132
 
133
133
  Docs: https://viem.sh/docs/contract/encodeErrorResult
134
-
135
134
  Version: viem@1.0.2"
136
135
  `)
137
136
  })
@@ -140,7 +139,6 @@ test('errors: no inputs', () => {
140
139
  expect(() =>
141
140
  encodeErrorResult({
142
141
  abi: [
143
- // @ts-expect-error
144
142
  {
145
143
  name: 'AccessDeniedError',
146
144
  type: 'error',
@@ -162,14 +160,12 @@ test('errors: no inputs', () => {
162
160
  Make sure you are using the correct ABI and that the inputs exist on it.
163
161
 
164
162
  Docs: https://viem.sh/docs/contract/encodeErrorResult
165
-
166
163
  Version: viem@1.0.2"
167
164
  `)
168
165
  expect(() =>
169
166
  encodeErrorResult({
170
167
  abi: [
171
168
  {
172
- // @ts-expect-error
173
169
  inputs: undefined,
174
170
  name: 'AccessDeniedError',
175
171
  type: 'error',
@@ -191,7 +187,6 @@ test('errors: no inputs', () => {
191
187
  Make sure you are using the correct ABI and that the inputs exist on it.
192
188
 
193
189
  Docs: https://viem.sh/docs/contract/encodeErrorResult
194
-
195
190
  Version: viem@1.0.2"
196
191
  `)
197
192
  })
@@ -1,31 +1,38 @@
1
- import { Abi, ExtractAbiErrorNames } from 'abitype'
1
+ import { Abi, Narrow } from 'abitype'
2
2
  import {
3
3
  AbiErrorInputsNotFoundError,
4
4
  AbiErrorNotFoundError,
5
5
  } from '../../errors'
6
-
7
- import { ExtractErrorArgsFromAbi, Hex } from '../../types'
6
+ import {
7
+ ExtractErrorArgsFromAbi,
8
+ ExtractErrorNameFromAbi,
9
+ Hex,
10
+ } from '../../types'
8
11
  import { concatHex } from '../data'
9
12
  import { getFunctionSignature } from '../hash'
10
13
  import { encodeAbi } from './encodeAbi'
11
14
  import { formatAbiItem } from './formatAbiItem'
12
- import { getAbiItem } from './getAbiItem'
15
+ import { getAbiItem, GetAbiItemArgs } from './getAbiItem'
13
16
 
14
17
  const docsPath = '/docs/contract/encodeErrorResult'
15
18
 
16
19
  export type EncodeErrorResultArgs<
17
- TAbi extends Abi = Abi,
18
- TErrorName extends ExtractAbiErrorNames<TAbi> = any,
20
+ TAbi extends Abi | readonly unknown[] = Abi,
21
+ TErrorName extends string = string,
19
22
  > = {
20
- abi: TAbi
21
- errorName: TErrorName
23
+ abi: Narrow<TAbi>
24
+ errorName: ExtractErrorNameFromAbi<TAbi, TErrorName>
22
25
  } & ExtractErrorArgsFromAbi<TAbi, TErrorName>
23
26
 
24
27
  export function encodeErrorResult<
25
- TAbi extends Abi = Abi,
26
- TErrorName extends ExtractAbiErrorNames<TAbi> = any,
28
+ TAbi extends Abi | readonly unknown[],
29
+ TErrorName extends string,
27
30
  >({ abi, errorName, args }: EncodeErrorResultArgs<TAbi, TErrorName>) {
28
- const description = getAbiItem({ abi, name: errorName })
31
+ const description = getAbiItem({
32
+ abi,
33
+ args,
34
+ name: errorName,
35
+ } as GetAbiItemArgs)
29
36
  if (!description) throw new AbiErrorNotFoundError(errorName, { docsPath })
30
37
  const definition = formatAbiItem(description)
31
38
  const signature = getFunctionSignature(definition)
@@ -11,7 +11,7 @@ test('Transfer()', () => {
11
11
  name: 'Transfer',
12
12
  type: 'event',
13
13
  },
14
- ] as const,
14
+ ],
15
15
  eventName: 'Transfer',
16
16
  }),
17
17
  ).toEqual([
@@ -19,13 +19,12 @@ test('Transfer()', () => {
19
19
  ])
20
20
  expect(
21
21
  encodeEventTopics({
22
- // @ts-expect-error
23
22
  abi: [
24
23
  {
25
24
  name: 'Transfer',
26
25
  type: 'event',
27
26
  },
28
- ] as const,
27
+ ],
29
28
  eventName: 'Transfer',
30
29
  }),
31
30
  ).toEqual([
@@ -58,7 +57,7 @@ test('no args: Transfer(address,address,uint256)', () => {
58
57
  name: 'Transfer',
59
58
  type: 'event',
60
59
  },
61
- ] as const,
60
+ ],
62
61
  eventName: 'Transfer',
63
62
  }),
64
63
  ).toEqual([
@@ -91,7 +90,7 @@ test('named args: Transfer(address,address,uint256)', () => {
91
90
  name: 'Transfer',
92
91
  type: 'event',
93
92
  },
94
- ] as const,
93
+ ],
95
94
  eventName: 'Transfer',
96
95
  args: {
97
96
  from: null,
@@ -130,7 +129,7 @@ test('named args: Transfer(address,address,uint256)', () => {
130
129
  name: 'Transfer',
131
130
  type: 'event',
132
131
  },
133
- ] as const,
132
+ ],
134
133
  eventName: 'Transfer',
135
134
  args: {
136
135
  from: null,
@@ -172,7 +171,7 @@ test('unnamed args: Transfer(address,address,uint256)', () => {
172
171
  name: 'Transfer',
173
172
  type: 'event',
174
173
  },
175
- ] as const,
174
+ ],
176
175
  eventName: 'Transfer',
177
176
  args: [null, '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC'],
178
177
  }),
@@ -205,7 +204,7 @@ test('unnamed args: Transfer(address,address,uint256)', () => {
205
204
  name: 'Transfer',
206
205
  type: 'event',
207
206
  },
208
- ] as const,
207
+ ],
209
208
  eventName: 'Transfer',
210
209
  args: [
211
210
  null,
@@ -240,7 +239,7 @@ test('Foo(string)', () => {
240
239
  name: 'Foo',
241
240
  type: 'event',
242
241
  },
243
- ] as const,
242
+ ],
244
243
  eventName: 'Foo',
245
244
  args: {
246
245
  message: 'hello',
@@ -280,7 +279,7 @@ test('Foo((uint,string))', () => {
280
279
  name: 'Bar',
281
280
  type: 'event',
282
281
  },
283
- ] as const,
282
+ ],
284
283
  eventName: 'Bar',
285
284
  args: {
286
285
  x: {
@@ -311,7 +310,7 @@ test("errors: event doesn't exist", () => {
311
310
  name: 'Foo',
312
311
  type: 'event',
313
312
  },
314
- ] as const,
313
+ ],
315
314
  // @ts-expect-error
316
315
  eventName: 'Bar',
317
316
  }),
@@ -320,7 +319,6 @@ test("errors: event doesn't exist", () => {
320
319
  Make sure you are using the correct ABI and that the event exists on it.
321
320
 
322
321
  Docs: https://viem.sh/docs/contract/encodeEventTopics
323
-
324
322
  Version: viem@1.0.2"
325
323
  `)
326
324
  })
@@ -328,14 +326,13 @@ test("errors: event doesn't exist", () => {
328
326
  test("errors: event doesn't exist", () => {
329
327
  expect(
330
328
  encodeEventTopics({
331
- // @ts-expect-error
332
329
  abi: [
333
330
  {
334
331
  inputs: undefined,
335
332
  name: 'Foo',
336
333
  type: 'event',
337
334
  },
338
- ] as const,
335
+ ],
339
336
  eventName: 'Foo',
340
337
  // @ts-expect-error
341
338
  args: {},
@@ -3,32 +3,38 @@ import {
3
3
  AbiParameter,
4
4
  AbiParameterToPrimitiveType,
5
5
  ExtractAbiEventNames,
6
+ Narrow,
6
7
  } from 'abitype'
7
8
 
8
9
  import {
9
10
  AbiEventNotFoundError,
10
11
  FilterTypeNotSupportedError,
11
12
  } from '../../errors'
12
- import { EventDefinition, ExtractEventArgsFromAbi, Hex } from '../../types'
13
+ import {
14
+ EventDefinition,
15
+ ExtractEventArgsFromAbi,
16
+ ExtractEventNameFromAbi,
17
+ Hex,
18
+ } from '../../types'
13
19
  import { encodeBytes } from '../encoding'
14
20
  import { keccak256, getEventSignature } from '../hash'
15
21
  import { encodeAbi } from './encodeAbi'
16
22
  import { formatAbiItem } from './formatAbiItem'
17
- import { getAbiItem } from './getAbiItem'
23
+ import { getAbiItem, GetAbiItemArgs } from './getAbiItem'
18
24
 
19
25
  export type EncodeEventTopicsArgs<
20
- TAbi extends Abi = Abi,
21
- TEventName extends ExtractAbiEventNames<TAbi> = any,
26
+ TAbi extends Abi | readonly unknown[] = Abi,
27
+ TEventName extends string = string,
22
28
  > = {
23
- abi: TAbi
24
- eventName: TEventName
29
+ abi: Narrow<TAbi>
30
+ eventName: ExtractEventNameFromAbi<TAbi, TEventName>
25
31
  } & ExtractEventArgsFromAbi<TAbi, TEventName>
26
32
 
27
33
  export function encodeEventTopics<
28
- TAbi extends Abi = Abi,
29
- TEventName extends ExtractAbiEventNames<TAbi> = any,
34
+ TAbi extends Abi | readonly unknown[],
35
+ TEventName extends string,
30
36
  >({ abi, eventName, args }: EncodeEventTopicsArgs<TAbi, TEventName>) {
31
- const abiItem = getAbiItem({ abi, name: eventName })
37
+ const abiItem = getAbiItem({ abi, args, name: eventName } as GetAbiItemArgs)
32
38
  if (!abiItem)
33
39
  throw new AbiEventNotFoundError(eventName, {
34
40
  docsPath: '/docs/contract/encodeEventTopics',
@@ -13,13 +13,12 @@ test('foo()', () => {
13
13
  stateMutability: 'nonpayable',
14
14
  type: 'function',
15
15
  },
16
- ] as const,
16
+ ],
17
17
  functionName: 'foo',
18
18
  }),
19
19
  ).toEqual('0xc2985578')
20
20
  expect(
21
21
  encodeFunctionData({
22
- // @ts-expect-error
23
22
  abi: [
24
23
  {
25
24
  name: 'foo',
@@ -27,7 +26,7 @@ test('foo()', () => {
27
26
  stateMutability: 'nonpayable',
28
27
  type: 'function',
29
28
  },
30
- ] as const,
29
+ ],
31
30
  functionName: 'foo',
32
31
  }),
33
32
  ).toEqual('0xc2985578')
@@ -50,7 +49,7 @@ test('bar(uint256)', () => {
50
49
  stateMutability: 'nonpayable',
51
50
  type: 'function',
52
51
  },
53
- ] as const,
52
+ ],
54
53
  functionName: 'bar',
55
54
  args: [1n],
56
55
  }),
@@ -98,7 +97,7 @@ test('getVoter((uint256,bool,address,uint256))', () => {
98
97
  stateMutability: 'nonpayable',
99
98
  type: 'function',
100
99
  },
101
- ] as const,
100
+ ],
102
101
  functionName: 'getVoter',
103
102
  args: [
104
103
  {
@@ -125,7 +124,7 @@ test("errors: function doesn't exist", () => {
125
124
  stateMutability: 'nonpayable',
126
125
  type: 'function',
127
126
  },
128
- ] as const,
127
+ ],
129
128
  // @ts-expect-error
130
129
  functionName: 'bar',
131
130
  }),
@@ -134,7 +133,6 @@ test("errors: function doesn't exist", () => {
134
133
  Make sure you are using the correct ABI and that the function exists on it.
135
134
 
136
135
  Docs: https://viem.sh/docs/contract/encodeFunctionData
137
-
138
136
  Version: viem@1.0.2"
139
137
  `)
140
138
  })
@@ -1,26 +1,33 @@
1
- import { Abi, ExtractAbiFunctionNames } from 'abitype'
1
+ import { Abi, Narrow } from 'abitype'
2
2
 
3
- import { AbiFunctionNotFoundError } from '../../errors'
3
+ import {
4
+ AbiEncodingLengthMismatchError,
5
+ AbiFunctionNotFoundError,
6
+ } from '../../errors'
4
7
  import { ExtractArgsFromAbi, ExtractFunctionNameFromAbi } from '../../types'
5
8
  import { concatHex } from '../data'
6
9
  import { getFunctionSignature } from '../hash'
7
10
  import { encodeAbi } from './encodeAbi'
8
11
  import { formatAbiItem } from './formatAbiItem'
9
- import { getAbiItem } from './getAbiItem'
12
+ import { getAbiItem, GetAbiItemArgs } from './getAbiItem'
10
13
 
11
14
  export type EncodeFunctionDataArgs<
12
- TAbi extends Abi = Abi,
13
- TFunctionName extends string = any,
15
+ TAbi extends Abi | readonly unknown[] = Abi,
16
+ TFunctionName extends string = string,
14
17
  > = {
15
- abi: TAbi
18
+ abi: Narrow<TAbi>
16
19
  functionName: ExtractFunctionNameFromAbi<TAbi, TFunctionName>
17
20
  } & ExtractArgsFromAbi<TAbi, TFunctionName>
18
21
 
19
22
  export function encodeFunctionData<
20
- TAbi extends Abi = Abi,
21
- TFunctionName extends string = any,
23
+ TAbi extends Abi | readonly unknown[],
24
+ TFunctionName extends string,
22
25
  >({ abi, args, functionName }: EncodeFunctionDataArgs<TAbi, TFunctionName>) {
23
- const description = getAbiItem({ abi, name: functionName })
26
+ const description = getAbiItem({
27
+ abi,
28
+ args,
29
+ name: functionName,
30
+ } as GetAbiItemArgs)
24
31
  if (!description)
25
32
  throw new AbiFunctionNotFoundError(functionName, {
26
33
  docsPath: '/docs/contract/encodeFunctionData',
@@ -30,7 +30,7 @@ test('returns ()', () => {
30
30
  },
31
31
  ],
32
32
  functionName: 'foo',
33
- result: [undefined],
33
+ result: undefined,
34
34
  }),
35
35
  ).toEqual('0x')
36
36
  expect(
@@ -45,7 +45,6 @@ test('returns ()', () => {
45
45
  },
46
46
  ],
47
47
  functionName: 'foo',
48
- result: [],
49
48
  }),
50
49
  ).toEqual('0x')
51
50
  })
@@ -67,7 +66,7 @@ test('returns (address)', () => {
67
66
  stateMutability: 'pure',
68
67
  type: 'function',
69
68
  },
70
- ] as const,
69
+ ],
71
70
  functionName: 'foo',
72
71
  result: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
73
72
  }),
@@ -129,17 +128,15 @@ test('returns (Bar)', () => {
129
128
  },
130
129
  ],
131
130
  functionName: 'bar',
132
- result: [
133
- {
134
- foo: {
135
- sender: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
136
- x: 69420n,
137
- y: true,
138
- },
131
+ result: {
132
+ foo: {
139
133
  sender: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
140
- z: 69,
134
+ x: 69420n,
135
+ y: true,
141
136
  },
142
- ],
137
+ sender: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
138
+ z: 69,
139
+ },
143
140
  }),
144
141
  ).toEqual(
145
142
  '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac0000000000000000000000000000000000000000000000000000000000010f2c0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac0000000000000000000000000000000000000000000000000000000000000045',
@@ -240,6 +237,7 @@ test("error: function doesn't exist", () => {
240
237
  type: 'function',
241
238
  },
242
239
  ],
240
+ // @ts-expect-error
243
241
  functionName: 'baz',
244
242
  result: ['0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC'],
245
243
  }),
@@ -249,7 +247,6 @@ test("error: function doesn't exist", () => {
249
247
  Make sure you are using the correct ABI and that the function exists on it.
250
248
 
251
249
  Docs: https://viem.sh/docs/contract/encodeFunctionResult
252
-
253
250
  Version: viem@1.0.2"
254
251
  `,
255
252
  )
@@ -259,7 +256,6 @@ test("error: function doesn't exist", () => {
259
256
  expect(() =>
260
257
  encodeFunctionResult({
261
258
  abi: [
262
- // @ts-expect-error
263
259
  {
264
260
  inputs: [],
265
261
  name: 'foo',
@@ -277,7 +273,6 @@ test("error: function doesn't exist", () => {
277
273
  Make sure you are using the correct ABI and that the function exists on it.
278
274
 
279
275
  Docs: https://viem.sh/docs/contract/encodeFunctionResult
280
-
281
276
  Version: viem@1.0.2"
282
277
  `,
283
278
  )
@@ -1,4 +1,4 @@
1
- import { Abi, ExtractAbiFunctionNames } from 'abitype'
1
+ import { Abi, ExtractAbiFunctionNames, Narrow } from 'abitype'
2
2
  import {
3
3
  AbiFunctionNotFoundError,
4
4
  AbiFunctionOutputsNotFoundError,
@@ -10,23 +10,25 @@ import { encodeAbi } from './encodeAbi'
10
10
  const docsPath = '/docs/contract/encodeFunctionResult'
11
11
 
12
12
  export type EncodeFunctionResultArgs<
13
- TAbi extends Abi = Abi,
14
- TFunctionName extends string = any,
13
+ TAbi extends Abi | readonly unknown[] = Abi,
14
+ TFunctionName extends string = string,
15
15
  > = {
16
- abi: TAbi
16
+ abi: Narrow<TAbi>
17
17
  functionName: ExtractFunctionNameFromAbi<TAbi, TFunctionName>
18
18
  result?: ExtractResultFromAbi<TAbi, TFunctionName>
19
19
  }
20
20
 
21
21
  export function encodeFunctionResult<
22
- TAbi extends Abi = Abi,
23
- TFunctionName extends string = any,
22
+ TAbi extends Abi | readonly unknown[],
23
+ TFunctionName extends string,
24
24
  >({
25
25
  abi,
26
26
  functionName,
27
27
  result,
28
28
  }: EncodeFunctionResultArgs<TAbi, TFunctionName>) {
29
- const description = abi.find((x) => 'name' in x && x.name === functionName)
29
+ const description = (abi as Abi).find(
30
+ (x) => 'name' in x && x.name === functionName,
31
+ )
30
32
  if (!description)
31
33
  throw new AbiFunctionNotFoundError(functionName, { docsPath })
32
34
  if (!('outputs' in description))