viem 0.0.1-alpha.21 → 0.0.1-alpha.22

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 (170) 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-46BO7YAQ.mjs → chunk-JMFNAGCD.mjs} +4 -3
  7. package/dist/chunk-JMFNAGCD.mjs.map +1 -0
  8. package/dist/{chunk-KZMJR27B.js → chunk-MVD4Y3AE.js} +67 -63
  9. package/dist/chunk-MVD4Y3AE.js.map +1 -0
  10. package/dist/{chunk-57ZOFERP.mjs → chunk-OZIBQJYR.mjs} +2 -2
  11. package/dist/{chunk-57ZOFERP.mjs.map → chunk-OZIBQJYR.mjs.map} +0 -0
  12. package/dist/{chunk-KEHGSYDO.js → chunk-PMPJGAHY.js} +14 -13
  13. package/dist/chunk-PMPJGAHY.js.map +1 -0
  14. package/dist/{chunk-W7BWWAC4.js → chunk-QA3IE5DU.js} +14 -14
  15. package/dist/{chunk-W7BWWAC4.js.map → chunk-QA3IE5DU.js.map} +0 -0
  16. package/dist/{chunk-DSPMAIDO.mjs → chunk-QN2NCTEN.mjs} +2 -2
  17. package/dist/{chunk-DSPMAIDO.mjs.map → chunk-QN2NCTEN.mjs.map} +1 -1
  18. package/dist/{chunk-EC3NUIJE.js → chunk-S77NLWHO.js} +13 -13
  19. package/dist/{chunk-EC3NUIJE.js.map → chunk-S77NLWHO.js.map} +1 -1
  20. package/dist/{chunk-O2GYLJVD.js → chunk-V5U5S7PQ.js} +403 -165
  21. package/dist/chunk-V5U5S7PQ.js.map +1 -0
  22. package/dist/{chunk-62VTYU2V.mjs → chunk-WWJ5YPTJ.mjs} +404 -166
  23. package/dist/chunk-WWJ5YPTJ.mjs.map +1 -0
  24. package/dist/{chunk-DGO77E2H.mjs → chunk-ZXXEENRD.mjs} +7 -3
  25. package/dist/chunk-ZXXEENRD.mjs.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-74f34ac9.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 +125 -113
  41. package/dist/index.js +14 -6
  42. package/dist/index.mjs +13 -5
  43. package/dist/normalize-ef9240c0.d.ts +33 -0
  44. package/dist/{parseGwei-4308ad80.d.ts → parseGwei-b323fb0a.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-a8688b26.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/index.ts +3 -1
  68. package/src/_test/utils.ts +39 -1
  69. package/src/actions/public/call.bench.ts +5 -5
  70. package/src/actions/public/deployContract.ts +4 -4
  71. package/src/actions/public/estimateGas.bench.ts +6 -6
  72. package/src/actions/public/getBlock.bench.ts +5 -5
  73. package/src/actions/public/getBlockNumber.bench.ts +5 -5
  74. package/src/actions/public/getGasPrice.bench.ts +1 -10
  75. package/src/actions/public/getTransaction.bench.ts +5 -5
  76. package/src/actions/public/getTransactionReceipt.bench.ts +5 -5
  77. package/src/actions/public/multicall.test.ts +452 -0
  78. package/src/actions/public/multicall.ts +103 -0
  79. package/src/actions/public/readContract.test.ts +226 -26
  80. package/src/actions/public/readContract.ts +13 -27
  81. package/src/actions/public/simulateContract.bench.ts +10 -0
  82. package/src/actions/public/simulateContract.test.ts +209 -37
  83. package/src/actions/public/simulateContract.ts +17 -21
  84. package/src/actions/public/watchContractEvent.test.ts +36 -32
  85. package/src/actions/public/watchEvent.test.ts +34 -29
  86. package/src/actions/wallet/signMessage.test.ts +0 -1
  87. package/src/actions/wallet/switchChain.test.ts +1 -2
  88. package/src/actions/wallet/watchAsset.test.ts +1 -2
  89. package/src/actions/wallet/writeContract.test.ts +37 -7
  90. package/src/actions/wallet/writeContract.ts +5 -14
  91. package/src/clients/transports/fallback.test.ts +34 -0
  92. package/src/clients/transports/fallback.ts +3 -1
  93. package/src/clients/transports/http.test.ts +0 -1
  94. package/src/clients/transports/webSocket.test.ts +0 -1
  95. package/src/constants/abis.test.ts +53 -0
  96. package/src/constants/abis.ts +44 -0
  97. package/src/constants/index.test.ts +14 -0
  98. package/src/constants/index.ts +3 -0
  99. package/src/constants/solidity.test.ts +41 -0
  100. package/src/constants/solidity.ts +35 -0
  101. package/src/ens.ts +5 -0
  102. package/src/errors/abi.test.ts +0 -2
  103. package/src/errors/base.test.ts +44 -2
  104. package/src/errors/base.ts +12 -12
  105. package/src/errors/block.test.ts +6 -6
  106. package/src/errors/contract.test.ts +233 -0
  107. package/src/errors/contract.ts +133 -80
  108. package/src/errors/index.ts +4 -2
  109. package/src/errors/request.test.ts +3 -4
  110. package/src/errors/request.ts +17 -17
  111. package/src/errors/rpc.test.ts +1 -2
  112. package/src/errors/transaction.test.ts +12 -12
  113. package/src/errors/transport.test.ts +0 -1
  114. package/src/index.test.ts +46 -0
  115. package/src/index.ts +8 -0
  116. package/src/types/contract.ts +55 -4
  117. package/src/types/index.ts +5 -0
  118. package/src/types/multicall.ts +82 -0
  119. package/src/utils/abi/decodeAbi.test.ts +1 -2
  120. package/src/utils/abi/decodeAbi.ts +11 -7
  121. package/src/utils/abi/decodeDeployData.test.ts +7 -15
  122. package/src/utils/abi/decodeDeployData.ts +10 -7
  123. package/src/utils/abi/decodeErrorResult.test.ts +103 -3
  124. package/src/utils/abi/decodeErrorResult.ts +30 -13
  125. package/src/utils/abi/decodeFunctionData.test.ts +0 -1
  126. package/src/utils/abi/decodeFunctionResult.test.ts +80 -9
  127. package/src/utils/abi/decodeFunctionResult.ts +15 -11
  128. package/src/utils/abi/encodeAbi.test.ts +3 -4
  129. package/src/utils/abi/encodeAbi.ts +19 -10
  130. package/src/utils/abi/encodeDeployData.test.ts +6 -13
  131. package/src/utils/abi/encodeDeployData.ts +10 -7
  132. package/src/utils/abi/encodeErrorResult.test.ts +2 -7
  133. package/src/utils/abi/encodeErrorResult.ts +18 -11
  134. package/src/utils/abi/encodeEventTopics.test.ts +11 -14
  135. package/src/utils/abi/encodeEventTopics.ts +15 -9
  136. package/src/utils/abi/encodeFunctionData.test.ts +5 -7
  137. package/src/utils/abi/encodeFunctionData.ts +16 -9
  138. package/src/utils/abi/encodeFunctionResult.test.ts +10 -15
  139. package/src/utils/abi/encodeFunctionResult.ts +9 -7
  140. package/src/utils/abi/getAbiItem.test.ts +547 -0
  141. package/src/utils/abi/getAbiItem.ts +93 -3
  142. package/src/utils/abi/index.ts +5 -1
  143. package/src/utils/address/getAddress.test.ts +6 -6
  144. package/src/utils/contract/getContractError.test.ts +247 -62
  145. package/src/utils/contract/getContractError.ts +30 -38
  146. package/src/utils/data/pad.bench.ts +0 -9
  147. package/src/utils/data/trim.bench.ts +0 -16
  148. package/src/utils/encoding/encodeBytes.bench.ts +0 -12
  149. package/src/utils/encoding/encodeHex.bench.ts +0 -11
  150. package/src/utils/ens/index.test.ts +13 -0
  151. package/src/utils/ens/index.ts +5 -0
  152. package/src/utils/ens/labelhash.test.ts +55 -0
  153. package/src/utils/ens/labelhash.ts +16 -0
  154. package/src/utils/ens/namehash.test.ts +65 -0
  155. package/src/utils/ens/namehash.ts +28 -0
  156. package/src/utils/ens/normalize.bench.ts +14 -0
  157. package/src/utils/ens/normalize.test.ts +35 -0
  158. package/src/utils/ens/normalize.ts +14 -0
  159. package/src/utils/hash/keccak256.ts +3 -5
  160. package/src/utils/index.test.ts +3 -0
  161. package/src/utils/index.ts +4 -0
  162. package/src/utils/rpc.test.ts +3 -6
  163. package/src/utils/unit/formatUnit.bench.ts +0 -5
  164. package/src/utils/unit/parseUnit.bench.ts +0 -5
  165. package/dist/chunk-46BO7YAQ.mjs.map +0 -1
  166. package/dist/chunk-62VTYU2V.mjs.map +0 -1
  167. package/dist/chunk-DGO77E2H.mjs.map +0 -1
  168. package/dist/chunk-KEHGSYDO.js.map +0 -1
  169. package/dist/chunk-KZMJR27B.js.map +0 -1
  170. package/dist/chunk-O2GYLJVD.js.map +0 -1
@@ -1,13 +1,7 @@
1
1
  import { Abi } from 'abitype'
2
2
 
3
3
  import type { WalletClient } from '../../clients'
4
- import type {
5
- Address,
6
- Chain,
7
- ExtractArgsFromAbi,
8
- ExtractFunctionNameFromAbi,
9
- GetValue,
10
- } from '../../types'
4
+ import type { Chain, ContractConfig, GetValue } from '../../types'
11
5
  import { EncodeFunctionDataArgs, encodeFunctionData } from '../../utils'
12
6
  import {
13
7
  sendTransaction,
@@ -18,20 +12,17 @@ import {
18
12
  export type WriteContractArgs<
19
13
  TChain extends Chain = Chain,
20
14
  TAbi extends Abi | readonly unknown[] = Abi,
21
- TFunctionName extends string = any,
15
+ TFunctionName extends string = string,
22
16
  > = Omit<SendTransactionArgs<TChain>, 'to' | 'data' | 'value'> & {
23
- address: Address
24
- abi: TAbi
25
- functionName: ExtractFunctionNameFromAbi<TAbi, TFunctionName>
26
17
  value?: GetValue<TAbi, TFunctionName, SendTransactionArgs<TChain>['value']>
27
- } & ExtractArgsFromAbi<TAbi, TFunctionName>
18
+ } & ContractConfig<TAbi, TFunctionName, 'payable' | 'nonpayable'>
28
19
 
29
20
  export type WriteContractResponse = SendTransactionResponse
30
21
 
31
22
  export async function writeContract<
32
23
  TChain extends Chain,
33
- TAbi extends Abi = Abi,
34
- TFunctionName extends string = any,
24
+ TAbi extends Abi | readonly unknown[],
25
+ TFunctionName extends string,
35
26
  >(
36
27
  client: WalletClient,
37
28
  {
@@ -126,6 +126,40 @@ describe('request', () => {
126
126
  // ensure `retryCount` on transport is adhered
127
127
  expect(count).toBe(6)
128
128
  })
129
+
130
+ test('error (rpc)', async () => {
131
+ let count = 0
132
+ const server1 = await createHttpServer((req, res) => {
133
+ count++
134
+ res.writeHead(200, {
135
+ 'Content-Type': 'application/json',
136
+ })
137
+ res.end(JSON.stringify({ error: 'ngmi' }))
138
+ })
139
+ const server2 = await createHttpServer((req, res) => {
140
+ count++
141
+ res.writeHead(500)
142
+ res.end()
143
+ })
144
+ const server3 = await createHttpServer((req, res) => {
145
+ count++
146
+ res.writeHead(200, {
147
+ 'Content-Type': 'application/json',
148
+ })
149
+ res.end(JSON.stringify({ result: '0x1' }))
150
+ })
151
+
152
+ let transport = fallback([
153
+ http(server1.url),
154
+ http(server2.url),
155
+ http(server3.url),
156
+ ])({
157
+ chain: localhost,
158
+ })
159
+ await expect(() =>
160
+ transport.config.request({ method: 'eth_blockNumber' }),
161
+ ).rejects.toThrowError()
162
+ })
129
163
  })
130
164
 
131
165
  describe('client', () => {
@@ -1,3 +1,4 @@
1
+ import { HttpRequestError, RpcError } from '../../errors'
1
2
  import type { Transport, TransportConfig } from './createTransport'
2
3
  import { createTransport } from './createTransport'
3
4
 
@@ -31,7 +32,8 @@ export function fallback(
31
32
  params,
32
33
  } as any)
33
34
  } catch (err) {
34
- if (i < transports.length - 1) return fetch(i + 1)
35
+ if (!(err instanceof RpcError) && i < transports.length - 1)
36
+ return fetch(i + 1)
35
37
  throw err
36
38
  }
37
39
  }
@@ -103,7 +103,6 @@ test('no url', () => {
103
103
  "No URL was provided to the Transport. Please provide a valid RPC URL to the Transport.
104
104
 
105
105
  Docs: https://viem.sh/docs/clients/intro
106
-
107
106
  Version: viem@1.0.2"
108
107
  `,
109
108
  )
@@ -158,7 +158,6 @@ test('no url', () => {
158
158
  "No URL was provided to the Transport. Please provide a valid RPC URL to the Transport.
159
159
 
160
160
  Docs: https://viem.sh/docs/clients/intro
161
-
162
161
  Version: viem@1.0.2"
163
162
  `)
164
163
  })
@@ -0,0 +1,53 @@
1
+ import { expect, test } from 'vitest'
2
+
3
+ import * as abis from './abis'
4
+
5
+ test('exports abis', () => {
6
+ expect(abis).toMatchInlineSnapshot(`
7
+ {
8
+ "multicall3Abi": [
9
+ {
10
+ "inputs": [
11
+ {
12
+ "components": [
13
+ {
14
+ "name": "target",
15
+ "type": "address",
16
+ },
17
+ {
18
+ "name": "allowFailure",
19
+ "type": "bool",
20
+ },
21
+ {
22
+ "name": "callData",
23
+ "type": "bytes",
24
+ },
25
+ ],
26
+ "name": "calls",
27
+ "type": "tuple[]",
28
+ },
29
+ ],
30
+ "name": "aggregate3",
31
+ "outputs": [
32
+ {
33
+ "components": [
34
+ {
35
+ "name": "success",
36
+ "type": "bool",
37
+ },
38
+ {
39
+ "name": "returnData",
40
+ "type": "bytes",
41
+ },
42
+ ],
43
+ "name": "returnData",
44
+ "type": "tuple[]",
45
+ },
46
+ ],
47
+ "stateMutability": "view",
48
+ "type": "function",
49
+ },
50
+ ],
51
+ }
52
+ `)
53
+ })
@@ -0,0 +1,44 @@
1
+ /* [Multicall3](https://github.com/mds1/multicall) */
2
+ export const multicall3Abi = [
3
+ {
4
+ inputs: [
5
+ {
6
+ components: [
7
+ {
8
+ name: 'target',
9
+ type: 'address',
10
+ },
11
+ {
12
+ name: 'allowFailure',
13
+ type: 'bool',
14
+ },
15
+ {
16
+ name: 'callData',
17
+ type: 'bytes',
18
+ },
19
+ ],
20
+ name: 'calls',
21
+ type: 'tuple[]',
22
+ },
23
+ ],
24
+ name: 'aggregate3',
25
+ outputs: [
26
+ {
27
+ components: [
28
+ {
29
+ name: 'success',
30
+ type: 'bool',
31
+ },
32
+ {
33
+ name: 'returnData',
34
+ type: 'bytes',
35
+ },
36
+ ],
37
+ name: 'returnData',
38
+ type: 'tuple[]',
39
+ },
40
+ ],
41
+ stateMutability: 'view',
42
+ type: 'function',
43
+ },
44
+ ] as const
@@ -0,0 +1,14 @@
1
+ import { expect, test } from 'vitest'
2
+
3
+ import * as index from './index'
4
+
5
+ test('exports index', () => {
6
+ expect(Object.keys(index)).toMatchInlineSnapshot(`
7
+ [
8
+ "multicall3Abi",
9
+ "panicReasons",
10
+ "solidityError",
11
+ "solidityPanic",
12
+ ]
13
+ `)
14
+ })
@@ -0,0 +1,3 @@
1
+ export { multicall3Abi } from './abis'
2
+
3
+ export { panicReasons, solidityError, solidityPanic } from './solidity'
@@ -0,0 +1,41 @@
1
+ import { expect, test } from 'vitest'
2
+
3
+ import * as solidity from './solidity'
4
+
5
+ test('exports solidity constants', () => {
6
+ expect(solidity).toMatchInlineSnapshot(`
7
+ {
8
+ "panicReasons": {
9
+ "1": "An \`assert\` condition failed.",
10
+ "17": "Arithmic operation resulted in underflow or overflow.",
11
+ "18": "Division or modulo by zero (e.g. \`5 / 0\` or \`23 % 0\`).",
12
+ "33": "Attempted to convert to an invalid type.",
13
+ "34": "Attempted to access a storage byte array that is incorrectly encoded.",
14
+ "49": "Performed \`.pop()\` on an empty array",
15
+ "50": "Array index is out of bounds.",
16
+ "65": "Allocated too much memory or created an array which is too large.",
17
+ "81": "Attempted to call a zero-initialized variable of internal function type.",
18
+ },
19
+ "solidityError": {
20
+ "inputs": [
21
+ {
22
+ "name": "message",
23
+ "type": "string",
24
+ },
25
+ ],
26
+ "name": "Error",
27
+ "type": "error",
28
+ },
29
+ "solidityPanic": {
30
+ "inputs": [
31
+ {
32
+ "name": "reason",
33
+ "type": "uint256",
34
+ },
35
+ ],
36
+ "name": "Panic",
37
+ "type": "error",
38
+ },
39
+ }
40
+ `)
41
+ })
@@ -0,0 +1,35 @@
1
+ import { AbiError } from 'abitype'
2
+
3
+ // https://docs.soliditylang.org/en/v0.8.16/control-structures.html#panic-via-assert-and-error-via-require
4
+ export const panicReasons = {
5
+ 1: 'An `assert` condition failed.',
6
+ 17: 'Arithmic operation resulted in underflow or overflow.',
7
+ 18: 'Division or modulo by zero (e.g. `5 / 0` or `23 % 0`).',
8
+ 33: 'Attempted to convert to an invalid type.',
9
+ 34: 'Attempted to access a storage byte array that is incorrectly encoded.',
10
+ 49: 'Performed `.pop()` on an empty array',
11
+ 50: 'Array index is out of bounds.',
12
+ 65: 'Allocated too much memory or created an array which is too large.',
13
+ 81: 'Attempted to call a zero-initialized variable of internal function type.',
14
+ } as const
15
+
16
+ export const solidityError: AbiError = {
17
+ inputs: [
18
+ {
19
+ name: 'message',
20
+ type: 'string',
21
+ },
22
+ ],
23
+ name: 'Error',
24
+ type: 'error',
25
+ }
26
+ export const solidityPanic: AbiError = {
27
+ inputs: [
28
+ {
29
+ name: 'reason',
30
+ type: 'uint256',
31
+ },
32
+ ],
33
+ name: 'Panic',
34
+ type: 'error',
35
+ }
package/src/ens.ts ADDED
@@ -0,0 +1,5 @@
1
+ export {
2
+ labelhash,
3
+ namehash,
4
+ normalize,
5
+ } from './utils'
@@ -25,7 +25,6 @@ test('InvalidAbiDecodingTypeError', () => {
25
25
  Please provide a valid ABI type.
26
26
 
27
27
  Docs: https://viem.sh/lol
28
-
29
28
  Version: viem@1.0.2]
30
29
  `)
31
30
  })
@@ -69,7 +68,6 @@ test('InvalidAbiEncodingTypeError', () => {
69
68
  Please provide a valid ABI type.
70
69
 
71
70
  Docs: https://viem.sh/lol
72
-
73
71
  Version: viem@1.0.2]
74
72
  `)
75
73
  })
@@ -17,6 +17,13 @@ test('BaseError', () => {
17
17
  Details: details
18
18
  Version: viem@1.0.2]
19
19
  `)
20
+
21
+ expect(new BaseError('', { details: 'details' })).toMatchInlineSnapshot(`
22
+ [ViemError: An error occurred.
23
+
24
+ Details: details
25
+ Version: viem@1.0.2]
26
+ `)
20
27
  })
21
28
 
22
29
  test('BaseError (w/ docsPath)', () => {
@@ -29,6 +36,43 @@ test('BaseError (w/ docsPath)', () => {
29
36
  [ViemError: An error occurred.
30
37
 
31
38
  Docs: https://viem.sh/lol
39
+ Details: details
40
+ Version: viem@1.0.2]
41
+ `)
42
+ expect(
43
+ new BaseError('An error occurred.', {
44
+ cause: new BaseError('error', { docsPath: '/docs' }),
45
+ }),
46
+ ).toMatchInlineSnapshot(`
47
+ [ViemError: An error occurred.
48
+
49
+ Docs: https://viem.sh/docs
50
+ Version: viem@1.0.2]
51
+ `)
52
+ expect(
53
+ new BaseError('An error occurred.', {
54
+ cause: new BaseError('error'),
55
+ docsPath: '/lol',
56
+ }),
57
+ ).toMatchInlineSnapshot(`
58
+ [ViemError: An error occurred.
59
+
60
+ Docs: https://viem.sh/lol
61
+ Version: viem@1.0.2]
62
+ `)
63
+ })
64
+
65
+ test('BaseError (w/ metaMessages)', () => {
66
+ expect(
67
+ new BaseError('An error occurred.', {
68
+ details: 'details',
69
+ metaMessages: ['Reason: idk', 'Cause: lol'],
70
+ }),
71
+ ).toMatchInlineSnapshot(`
72
+ [ViemError: An error occurred.
73
+
74
+ Reason: idk
75
+ Cause: lol
32
76
 
33
77
  Details: details
34
78
  Version: viem@1.0.2]
@@ -48,7 +92,6 @@ test('inherited BaseError', () => {
48
92
  [ViemError: An internal error occurred.
49
93
 
50
94
  Docs: https://viem.sh/lol
51
-
52
95
  Details: details
53
96
  Version: viem@1.0.2]
54
97
  `)
@@ -65,7 +108,6 @@ test('inherited Error', () => {
65
108
  [ViemError: An internal error occurred.
66
109
 
67
110
  Docs: https://viem.sh/lol
68
-
69
111
  Details: details
70
112
  Version: viem@1.0.2]
71
113
  `)
@@ -1,11 +1,13 @@
1
1
  // @ts-ignore
2
2
  import pkg from '../../package.json'
3
- import { stringify } from '../utils/stringify'
4
3
 
5
4
  /* c8 ignore next */
6
5
  const version = process.env.TEST ? '1.0.2' : pkg.version
7
6
 
8
- type BaseErrorArgs = { docsPath?: string } & (
7
+ type BaseErrorArgs = {
8
+ docsPath?: string
9
+ metaMessages?: string[]
10
+ } & (
9
11
  | {
10
12
  cause?: never
11
13
  details?: string
@@ -17,13 +19,14 @@ type BaseErrorArgs = { docsPath?: string } & (
17
19
  )
18
20
 
19
21
  export class BaseError extends Error {
20
- humanMessage: string
21
22
  details: string
22
23
  docsPath?: string
24
+ metaMessages?: string[]
25
+ shortMessage: string
23
26
 
24
27
  name = 'ViemError'
25
28
 
26
- constructor(humanMessage: string, args: BaseErrorArgs = {}) {
29
+ constructor(shortMessage: string, args: BaseErrorArgs = {}) {
27
30
  const details =
28
31
  args.cause instanceof BaseError
29
32
  ? args.cause.details
@@ -35,16 +38,12 @@ export class BaseError extends Error {
35
38
  ? args.cause.docsPath || args.docsPath
36
39
  : args.docsPath
37
40
  const message = [
38
- humanMessage,
39
- ...(docsPath ? ['', `Docs: https://viem.sh${docsPath}`] : []),
41
+ shortMessage || 'An error occurred.',
40
42
  '',
43
+ ...(args.metaMessages ? [...args.metaMessages, ''] : []),
44
+ ...(docsPath ? [`Docs: https://viem.sh${docsPath}`] : []),
41
45
  ...(details ? [`Details: ${details}`] : []),
42
46
  `Version: viem@${version}`,
43
- ...(args.cause &&
44
- !(args.cause instanceof BaseError) &&
45
- Object.keys(args.cause).length > 0
46
- ? [`Internal Error: ${stringify(args.cause)}`]
47
- : []),
48
47
  ].join('\n')
49
48
 
50
49
  super(message)
@@ -52,6 +51,7 @@ export class BaseError extends Error {
52
51
  if (args.cause) this.cause = args.cause
53
52
  this.details = details
54
53
  this.docsPath = docsPath
55
- this.humanMessage = humanMessage
54
+ this.metaMessages = args.metaMessages
55
+ this.shortMessage = shortMessage
56
56
  }
57
57
  }
@@ -5,17 +5,17 @@ test('BlockNotFoundError', () => {
5
5
  expect(
6
6
  new BlockNotFoundError({ blockNumber: 69420n }),
7
7
  ).toMatchInlineSnapshot(`
8
- [BlockNotFoundError: Block at number "69420" could not be found.
8
+ [BlockNotFoundError: Block at number "69420" could not be found.
9
9
 
10
- Version: viem@1.0.2]
11
- `)
10
+ Version: viem@1.0.2]
11
+ `)
12
12
  expect(
13
13
  new BlockNotFoundError({ blockHash: '0x69420' }),
14
14
  ).toMatchInlineSnapshot(`
15
- [BlockNotFoundError: Block at hash "0x69420" could not be found.
15
+ [BlockNotFoundError: Block at hash "0x69420" could not be found.
16
16
 
17
- Version: viem@1.0.2]
18
- `)
17
+ Version: viem@1.0.2]
18
+ `)
19
19
  expect(new BlockNotFoundError({})).toMatchInlineSnapshot(`
20
20
  [BlockNotFoundError: Block could not be found.
21
21