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
@@ -2,96 +2,281 @@ import { describe, expect, test } from 'vitest'
2
2
 
3
3
  import { accounts } from '../../_test'
4
4
  import { baycContractConfig } from '../../_test/abis'
5
- import { BaseError } from '../../errors'
5
+ import {
6
+ AbiDecodingZeroDataError,
7
+ BaseError,
8
+ RawContractError,
9
+ } from '../../errors'
6
10
  import { getContractError } from './getContractError'
7
11
 
8
12
  describe('getContractError', () => {
9
13
  test('default', () => {
10
- expect(
11
- getContractError(
12
- new BaseError('An RPC error occurred', {
13
- cause: {
14
- code: 3,
15
- message: 'execution reverted: Sale must be active to mint Ape',
16
- data: '0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001f53616c65206d7573742062652061637469766520746f206d696e742041706500',
17
- } as unknown as Error,
18
- }),
19
- {
20
- abi: baycContractConfig.abi,
21
- functionName: 'mintApe',
22
- args: [1n],
23
- sender: accounts[0].address,
24
- },
25
- ),
26
- ).toMatchInlineSnapshot(`
27
- [ContractMethodExecutionError: Sale must be active to mint Ape
28
-
14
+ const error = getContractError(
15
+ new RawContractError({
16
+ message: 'execution reverted: Sale must be active to mint Ape',
17
+ data: '0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001f53616c65206d7573742062652061637469766520746f206d696e742041706500',
18
+ }),
19
+ {
20
+ abi: baycContractConfig.abi,
21
+ functionName: 'mintApe',
22
+ args: [1n],
23
+ sender: accounts[0].address,
24
+ },
25
+ )
26
+ expect(error).toMatchInlineSnapshot(`
27
+ [ContractFunctionExecutionError: The contract function "mintApe" reverted with the following reason:
28
+ Sale must be active to mint Ape
29
+
30
+ Function: mintApe(uint256 numberOfTokens)
31
+ Arguments: (1)
29
32
  Sender: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
33
+
34
+ Version: viem@1.0.2]
35
+ `)
36
+ expect(error.cause).toMatchInlineSnapshot(`
37
+ [ContractFunctionRevertedError: The contract function "mintApe" reverted with the following reason:
38
+ Sale must be active to mint Ape
39
+
40
+ Version: viem@1.0.2]
41
+ `)
42
+ })
43
+
44
+ test('default: rpc', () => {
45
+ const error = getContractError(
46
+ new BaseError('An RPC error occurred', {
47
+ cause: {
48
+ code: 3,
49
+ message: 'execution reverted: Sale must be active to mint Ape',
50
+ data: '0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001f53616c65206d7573742062652061637469766520746f206d696e742041706500',
51
+ } as unknown as Error,
52
+ }),
53
+ {
54
+ abi: baycContractConfig.abi,
55
+ functionName: 'mintApe',
56
+ args: [1n],
57
+ sender: accounts[0].address,
58
+ },
59
+ )
60
+ expect(error).toMatchInlineSnapshot(`
61
+ [ContractFunctionExecutionError: The contract function "mintApe" reverted with the following reason:
62
+ Sale must be active to mint Ape
63
+
30
64
  Function: mintApe(uint256 numberOfTokens)
31
65
  Arguments: (1)
66
+ Sender: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
67
+
68
+ Version: viem@1.0.2]
69
+ `)
70
+ expect(error.cause).toMatchInlineSnapshot(`
71
+ [ContractFunctionRevertedError: The contract function "mintApe" reverted with the following reason:
72
+ Sale must be active to mint Ape
32
73
 
33
- Details: execution reverted: Sale must be active to mint Ape
34
74
  Version: viem@1.0.2]
35
75
  `)
36
76
  })
37
77
 
38
- test('default', () => {
39
- expect(
40
- getContractError(
41
- new BaseError('An RPC error occurred', {
42
- cause: {
43
- code: 3,
44
- message: 'execution reverted: Sale must be active to mint Ape',
45
- data: '0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001f53616c65206d7573742062652061637469766520746f206d696e742041706500',
46
- } as unknown as Error,
47
- }),
48
- {
49
- abi: baycContractConfig.abi,
50
- functionName: 'foo',
51
- args: [1n],
52
- sender: accounts[0].address,
53
- },
54
- ),
55
- ).toMatchInlineSnapshot(`
56
- [ContractMethodExecutionError: Sale must be active to mint Ape
57
-
78
+ test('no data', () => {
79
+ const error = getContractError(
80
+ new BaseError('An RPC error occurred', {
81
+ cause: {
82
+ code: 3,
83
+ message: 'ah no',
84
+ } as unknown as Error,
85
+ }),
86
+ {
87
+ abi: baycContractConfig.abi,
88
+ functionName: 'mintApe',
89
+ args: [1n],
90
+ sender: accounts[0].address,
91
+ },
92
+ )
93
+ expect(error).toMatchInlineSnapshot(`
94
+ [ContractFunctionExecutionError: The contract function "mintApe" reverted with the following reason:
95
+ ah no
96
+
97
+ Function: mintApe(uint256 numberOfTokens)
98
+ Arguments: (1)
58
99
  Sender: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
59
100
 
60
- Details: execution reverted: Sale must be active to mint Ape
101
+ Version: viem@1.0.2]
102
+ `)
103
+ expect(error.cause).toMatchInlineSnapshot(`
104
+ [ContractFunctionRevertedError: The contract function "mintApe" reverted with the following reason:
105
+ ah no
106
+
61
107
  Version: viem@1.0.2]
62
108
  `)
63
109
  })
64
110
 
65
- test('unknown error', () => {
66
- expect(
67
- getContractError(
68
- new BaseError('An RPC error occurred', {
69
- cause: new Error('rarararar i am an error lmaoaoo'),
70
- }),
71
- {
72
- abi: baycContractConfig.abi,
73
- functionName: 'foo',
74
- args: [1n],
75
- sender: accounts[0].address,
76
- },
77
- ),
78
- ).toMatchInlineSnapshot(`
79
- [ViemError: An RPC error occurred
111
+ test('no message', () => {
112
+ const error = getContractError(
113
+ new BaseError('An RPC error occurred', {
114
+ cause: {
115
+ code: 3,
116
+ data: '0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001f53616c65206d7573742062652061637469766520746f206d696e742041706500',
117
+ } as unknown as Error,
118
+ }),
119
+ {
120
+ abi: baycContractConfig.abi,
121
+ functionName: 'mintApe',
122
+ args: [1n],
123
+ sender: accounts[0].address,
124
+ },
125
+ )
126
+ expect(error).toMatchInlineSnapshot(`
127
+ [ContractFunctionExecutionError: The contract function "mintApe" reverted with the following reason:
128
+ Sale must be active to mint Ape
129
+
130
+ Function: mintApe(uint256 numberOfTokens)
131
+ Arguments: (1)
132
+ Sender: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
133
+
134
+ Version: viem@1.0.2]
135
+ `)
136
+ expect(error.cause).toMatchInlineSnapshot(`
137
+ [ContractFunctionRevertedError: The contract function "mintApe" reverted with the following reason:
138
+ Sale must be active to mint Ape
80
139
 
81
- Details: rarararar i am an error lmaoaoo
82
140
  Version: viem@1.0.2]
83
141
  `)
84
- expect(
85
- getContractError(new BaseError('An RPC error occurred'), {
142
+ })
143
+
144
+ test('unknown function', () => {
145
+ const error = getContractError(
146
+ new BaseError('An RPC error occurred', {
147
+ cause: {
148
+ code: 3,
149
+ message: 'execution reverted: Sale must be active to mint Ape',
150
+ data: '0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001f53616c65206d7573742062652061637469766520746f206d696e742041706500',
151
+ } as unknown as Error,
152
+ }),
153
+ {
86
154
  abi: baycContractConfig.abi,
87
- functionName: 'foo',
155
+ functionName: 'mintApe',
88
156
  args: [1n],
89
157
  sender: accounts[0].address,
158
+ },
159
+ )
160
+ expect(error).toMatchInlineSnapshot(`
161
+ [ContractFunctionExecutionError: The contract function "mintApe" reverted with the following reason:
162
+ Sale must be active to mint Ape
163
+
164
+ Function: mintApe(uint256 numberOfTokens)
165
+ Arguments: (1)
166
+ Sender: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
167
+
168
+ Version: viem@1.0.2]
169
+ `)
170
+ expect(error.cause).toMatchInlineSnapshot(`
171
+ [ContractFunctionRevertedError: The contract function "mintApe" reverted with the following reason:
172
+ Sale must be active to mint Ape
173
+
174
+ Version: viem@1.0.2]
175
+ `)
176
+ })
177
+
178
+ test('unknown error', () => {
179
+ const error = getContractError(
180
+ new BaseError('An RPC error occurred', {
181
+ cause: new Error('rarararar i am an error lmaoaoo'),
90
182
  }),
91
- ).toMatchInlineSnapshot(`
183
+ {
184
+ abi: baycContractConfig.abi,
185
+ functionName: 'mintApe',
186
+ args: [1n],
187
+ sender: accounts[0].address,
188
+ },
189
+ )
190
+ expect(error).toMatchInlineSnapshot(`
191
+ [ContractFunctionExecutionError: An RPC error occurred
192
+
193
+ Function: mintApe(uint256 numberOfTokens)
194
+ Arguments: (1)
195
+ Sender: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
196
+
197
+ Details: rarararar i am an error lmaoaoo
198
+ Version: viem@1.0.2]
199
+ `)
200
+ expect(error.cause).toMatchInlineSnapshot(`
201
+ [ViemError: An RPC error occurred
202
+
203
+ Details: rarararar i am an error lmaoaoo
204
+ Version: viem@1.0.2]
205
+ `)
206
+
207
+ const error2 = getContractError(new BaseError('An RPC error occurred'), {
208
+ abi: baycContractConfig.abi,
209
+ functionName: 'mintApe',
210
+ args: [1n],
211
+ sender: accounts[0].address,
212
+ })
213
+ expect(error2).toMatchInlineSnapshot(`
214
+ [ContractFunctionExecutionError: An RPC error occurred
215
+
216
+ Function: mintApe(uint256 numberOfTokens)
217
+ Arguments: (1)
218
+ Sender: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
219
+
220
+ Version: viem@1.0.2]
221
+ `)
222
+ expect(error2.cause).toMatchInlineSnapshot(`
92
223
  [ViemError: An RPC error occurred
93
224
 
94
225
  Version: viem@1.0.2]
95
226
  `)
227
+
228
+ const error3 = getContractError(new BaseError(''), {
229
+ abi: baycContractConfig.abi,
230
+ functionName: 'mintApe',
231
+ args: [1n],
232
+ sender: accounts[0].address,
233
+ })
234
+ expect(error3).toMatchInlineSnapshot(`
235
+ [ContractFunctionExecutionError: An unknown error occurred while executing the contract function "mintApe".
236
+
237
+ Function: mintApe(uint256 numberOfTokens)
238
+ Arguments: (1)
239
+ Sender: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
240
+
241
+ Version: viem@1.0.2]
242
+ `)
243
+ expect(error3.cause).toMatchInlineSnapshot(`
244
+ [ViemError: An error occurred.
245
+
246
+ Version: viem@1.0.2]
247
+ `)
248
+ })
249
+
250
+ test('zero data', () => {
251
+ const error = getContractError(new AbiDecodingZeroDataError(), {
252
+ abi: baycContractConfig.abi,
253
+ functionName: 'mintApe',
254
+ args: [1n],
255
+ sender: accounts[0].address,
256
+ })
257
+ expect(error).toMatchInlineSnapshot(`
258
+ [ContractFunctionExecutionError: The contract function "mintApe" returned no data ("0x").
259
+
260
+ This could be due to any of the following:
261
+ - The contract does not have the function "mintApe",
262
+ - The parameters passed to the contract function may be invalid, or
263
+ - The address is not a contract.
264
+
265
+ Function: mintApe(uint256 numberOfTokens)
266
+ Arguments: (1)
267
+ Sender: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
268
+
269
+ Version: viem@1.0.2]
270
+ `)
271
+ expect(error.cause).toMatchInlineSnapshot(`
272
+ [ContractFunctionZeroDataError: The contract function "mintApe" returned no data ("0x").
273
+
274
+ This could be due to any of the following:
275
+ - The contract does not have the function "mintApe",
276
+ - The parameters passed to the contract function may be invalid, or
277
+ - The address is not a contract.
278
+
279
+ Version: viem@1.0.2]
280
+ `)
96
281
  })
97
282
  })
@@ -1,66 +1,58 @@
1
1
  import { Abi } from 'abitype'
2
2
  import {
3
3
  AbiDecodingZeroDataError,
4
- ContractMethodExecutionError,
5
- ContractMethodZeroDataError,
4
+ BaseError,
5
+ ContractFunctionExecutionError,
6
+ RawContractError,
6
7
  } from '../../errors'
8
+ import {
9
+ ContractFunctionRevertedError,
10
+ ContractFunctionZeroDataError,
11
+ } from '../../errors/contract'
7
12
  import { Address } from '../../types'
8
- import { formatAbiItemWithArgs, formatAbiItem, getAbiItem } from '../abi'
13
+
14
+ const EXECUTION_REVERTED_ERROR_CODE = 3
9
15
 
10
16
  export function getContractError(
11
- err: unknown,
17
+ err: BaseError,
12
18
  {
13
19
  abi,
14
20
  address,
15
21
  args,
22
+ docsPath,
16
23
  functionName,
17
24
  sender,
18
25
  }: {
19
26
  abi: Abi
20
27
  args: any
21
28
  address?: Address
29
+ docsPath?: string
22
30
  functionName: string
23
31
  sender?: Address
24
32
  },
25
33
  ) {
26
- const { code, message } =
27
- ((err as Error).cause as { code?: number; message?: string }) || {}
28
-
29
- const abiItem = getAbiItem({ abi, name: functionName })
30
- const formattedArgs = abiItem
31
- ? formatAbiItemWithArgs({
32
- abiItem,
33
- args,
34
- includeFunctionName: false,
35
- includeName: false,
36
- })
37
- : undefined
38
- const functionWithParams = abiItem
39
- ? formatAbiItem(abiItem, { includeName: true })
40
- : undefined
34
+ const { code, data, message } = (
35
+ err instanceof RawContractError ? err : err.cause || {}
36
+ ) as RawContractError
41
37
 
38
+ let cause = err
42
39
  if (err instanceof AbiDecodingZeroDataError) {
43
- return new ContractMethodZeroDataError({
44
- abi,
45
- args,
46
- cause: err as Error,
47
- contractAddress: address,
48
- functionName,
49
- functionWithParams,
50
- })
51
- }
52
- if (code === 3 || message?.includes('execution reverted')) {
53
- const message_ = message?.replace('execution reverted: ', '')
54
- return new ContractMethodExecutionError(message_, {
40
+ cause = new ContractFunctionZeroDataError({ functionName })
41
+ } else if (code === EXECUTION_REVERTED_ERROR_CODE && (data || message)) {
42
+ cause = new ContractFunctionRevertedError({
55
43
  abi,
56
- args,
57
- cause: err as Error,
58
- contractAddress: address,
59
- formattedArgs,
44
+ data,
60
45
  functionName,
61
- functionWithParams,
62
- sender,
46
+ message,
63
47
  })
64
48
  }
65
- return err
49
+
50
+ return new ContractFunctionExecutionError(cause, {
51
+ abi,
52
+ args,
53
+ contractAddress: address,
54
+ docsPath,
55
+ functionName,
56
+ sender,
57
+ })
66
58
  }
@@ -1,5 +1,4 @@
1
1
  import { hexZeroPad, zeroPad } from 'ethers/lib/utils'
2
- import { hexZeroPad as hexZeroPad2, zeroPad as zeroPad2 } from 'essential-eth'
3
2
 
4
3
  import { bench, describe } from 'vitest'
5
4
  import Web3 from 'web3'
@@ -18,10 +17,6 @@ describe('Pad Hex', () => {
18
17
  bench('web3.js: `padLeft`', () => {
19
18
  Web3.utils.padLeft('0xa4e12a45', 32)
20
19
  })
21
-
22
- bench('essential-eth: `hexZeroPad`', () => {
23
- hexZeroPad2('0xa4e12a45', 32)
24
- })
25
20
  })
26
21
 
27
22
  describe('Pad Bytes', () => {
@@ -32,8 +27,4 @@ describe('Pad Bytes', () => {
32
27
  bench('ethers: `zeroPad`', () => {
33
28
  zeroPad(new Uint8Array([1, 122, 51, 123]), 32)
34
29
  })
35
-
36
- bench('essential-eth: `zeroPad`', () => {
37
- zeroPad2(new Uint8Array([1, 122, 51, 123]), 32)
38
- })
39
30
  })
@@ -1,8 +1,4 @@
1
1
  import { hexStripZeros, stripZeros } from 'ethers/lib/utils'
2
- import {
3
- hexStripZeros as hexStripZeros2,
4
- stripZeros as stripZeros2,
5
- } from 'essential-eth'
6
2
 
7
3
  import { bench, describe } from 'vitest'
8
4
 
@@ -16,10 +12,6 @@ describe('Trim Hex', () => {
16
12
  bench('ethers: `hexStripZeros`', () => {
17
13
  hexStripZeros('0x00000000000000000000000a4e12a45')
18
14
  })
19
-
20
- bench('essential-eth: `hexStripZeros`', () => {
21
- hexStripZeros2('0x00000000000000000000000a4e12a45')
22
- })
23
15
  })
24
16
 
25
17
  describe('Trim Bytes', () => {
@@ -39,12 +31,4 @@ describe('Trim Bytes', () => {
39
31
  ]),
40
32
  )
41
33
  })
42
-
43
- bench('essential-eth: `stripZeros`', () => {
44
- stripZeros2(
45
- new Uint8Array([
46
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 122, 51, 123,
47
- ]),
48
- )
49
- })
50
34
  })
@@ -1,8 +1,4 @@
1
1
  import { arrayify, toUtf8Bytes } from 'ethers/lib/utils'
2
- import {
3
- arrayify as arrayify2,
4
- toUtf8Bytes as toUtf8Bytes2,
5
- } from 'essential-eth'
6
2
  import { bench, describe } from 'vitest'
7
3
  import Web3 from 'web3'
8
4
 
@@ -20,10 +16,6 @@ describe('Hex to Bytes', () => {
20
16
  bench('web3.js: `hexToBytes`', () => {
21
17
  Web3.utils.hexToBytes('0x48656c6c6f20576f726c6421')
22
18
  })
23
-
24
- bench('essential-eth: `arrayify`', () => {
25
- arrayify2('0x48656c6c6f20576f726c6421')
26
- })
27
19
  })
28
20
 
29
21
  describe('String to Bytes', () => {
@@ -34,8 +26,4 @@ describe('String to Bytes', () => {
34
26
  bench('ethers: `toUtf8Bytes`', () => {
35
27
  toUtf8Bytes('Hello world')
36
28
  })
37
-
38
- bench('essential-eth: `toUtf8Bytes`', () => {
39
- toUtf8Bytes2('Hello world')
40
- })
41
29
  })
@@ -1,5 +1,4 @@
1
1
  import { hexlify } from 'ethers/lib/utils'
2
- import { hexlify as hexlify2 } from 'essential-eth'
3
2
  import { bench, describe } from 'vitest'
4
3
  import Web3 from 'web3'
5
4
 
@@ -17,10 +16,6 @@ describe('Number to Hex', () => {
17
16
  bench('web3.js: `numberToHex`', () => {
18
17
  Web3.utils.numberToHex(52)
19
18
  })
20
-
21
- bench('essential-eth: `hexlify`', () => {
22
- hexlify2(52)
23
- })
24
19
  })
25
20
 
26
21
  describe('String to Hex', () => {
@@ -51,10 +46,4 @@ describe('Bytes to Hex', () => {
51
46
  72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33,
52
47
  ])
53
48
  })
54
-
55
- bench('essential-eth: `hexlify`', () => {
56
- hexlify2(
57
- new Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]),
58
- )
59
- })
60
49
  })
@@ -0,0 +1,13 @@
1
+ import { expect, test } from 'vitest'
2
+
3
+ import * as utils from './index'
4
+
5
+ test('exports utils', () => {
6
+ expect(utils).toMatchInlineSnapshot(`
7
+ {
8
+ "labelhash": [Function],
9
+ "namehash": [Function],
10
+ "normalize": [Function],
11
+ }
12
+ `)
13
+ })
@@ -0,0 +1,5 @@
1
+ export { labelhash } from './labelhash'
2
+
3
+ export { namehash } from './namehash'
4
+
5
+ export { normalize } from './normalize'
@@ -0,0 +1,55 @@
1
+ import { test, expect } from 'vitest'
2
+
3
+ import { labelhash } from './labelhash'
4
+
5
+ test.each([
6
+ {
7
+ label: '',
8
+ expected:
9
+ '0x0000000000000000000000000000000000000000000000000000000000000000',
10
+ },
11
+ {
12
+ label: 'eth',
13
+ expected:
14
+ '0x4f5b812789fc606be1b3b16908db13fc7a9adf7ca72641f84d75b47069d3d7f0',
15
+ },
16
+ {
17
+ label: 'awkweb',
18
+ expected:
19
+ '0x7aaad03ddcacc63166440f59c14a1a2c97ee381014b59c58f55b49ab05f31a38',
20
+ },
21
+ {
22
+ label: 'awkw𝝣b',
23
+ expected:
24
+ '0x064cfb20fc5f10bd727bd17232b9b0c8021cec89e596b1c966ff1c611420c72f',
25
+ },
26
+ {
27
+ label: '\u{0061}wkweb',
28
+ expected:
29
+ '0x7aaad03ddcacc63166440f59c14a1a2c97ee381014b59c58f55b49ab05f31a38',
30
+ },
31
+ {
32
+ label: '\u{0061}wkw\u{0065}b',
33
+ expected:
34
+ '0x7aaad03ddcacc63166440f59c14a1a2c97ee381014b59c58f55b49ab05f31a38',
35
+ },
36
+ {
37
+ label: 'awkweb',
38
+ // ^ latin small "a"
39
+ expected:
40
+ '0x7aaad03ddcacc63166440f59c14a1a2c97ee381014b59c58f55b49ab05f31a38',
41
+ },
42
+ {
43
+ label: 'awkweb',
44
+ // ^ latin small "e"
45
+ expected:
46
+ '0x7aaad03ddcacc63166440f59c14a1a2c97ee381014b59c58f55b49ab05f31a38',
47
+ },
48
+ {
49
+ label: 'ʘ‿ʘ',
50
+ expected:
51
+ '0xc142daa955184f4c4992e064a059bd8950e0bff10db566df9068ae2d5379e652',
52
+ },
53
+ ])("labelhash('$label') -> '$expected'", ({ label, expected }) => {
54
+ expect(labelhash(label)).toBe(expected)
55
+ })
@@ -0,0 +1,16 @@
1
+ import { bytesToHex, stringToBytes } from '../encoding'
2
+ import { keccak256 } from '../hash'
3
+ import { normalize } from './normalize'
4
+
5
+ /**
6
+ * @description Hashes ENS label
7
+ *
8
+ * @example
9
+ * labelhash('eth')
10
+ * '0x4f5b812789fc606be1b3b16908db13fc7a9adf7ca72641f84d75b47069d3d7f0'
11
+ */
12
+ export function labelhash(label: string) {
13
+ let result = new Uint8Array(32).fill(0)
14
+ if (!label) return bytesToHex(result)
15
+ return keccak256(stringToBytes(normalize(label)))
16
+ }