viem 0.0.1-alpha.19 → 0.0.1-alpha.20

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 (151) hide show
  1. package/dist/{rpc-858670f1.d.ts → chain-32f56cfa.d.ts} +68 -18
  2. package/dist/chain-f12cdc7f.d.ts +5 -0
  3. package/dist/chains.d.ts +757 -170
  4. package/dist/chains.js +80 -86
  5. package/dist/chains.js.map +1 -1
  6. package/dist/chains.mjs +120 -126
  7. package/dist/chains.mjs.map +1 -1
  8. package/dist/{chunk-PHSAQQGZ.mjs → chunk-5Q6FSUU2.mjs} +75 -37
  9. package/dist/chunk-5Q6FSUU2.mjs.map +1 -0
  10. package/dist/{chunk-24EKKMUO.js → chunk-FHXXG7I6.js} +12 -12
  11. package/dist/chunk-FHXXG7I6.js.map +1 -0
  12. package/dist/{chunk-HRE6LYF3.js → chunk-IAQPMSGJ.js} +13 -13
  13. package/dist/chunk-IAQPMSGJ.js.map +1 -0
  14. package/dist/{chunk-IVWG22RQ.mjs → chunk-MYQNZSWD.mjs} +69 -12
  15. package/dist/chunk-MYQNZSWD.mjs.map +1 -0
  16. package/dist/{chunk-NYMS65YI.js → chunk-NHAKUPTF.js} +124 -67
  17. package/dist/chunk-NHAKUPTF.js.map +1 -0
  18. package/dist/{chunk-BZFF747V.js → chunk-SF4I2NKC.js} +81 -43
  19. package/dist/chunk-SF4I2NKC.js.map +1 -0
  20. package/dist/{chunk-VAT5CEF7.mjs → chunk-T7CBAKLI.mjs} +2 -2
  21. package/dist/chunk-T7CBAKLI.mjs.map +1 -0
  22. package/dist/{chunk-CTU4PQLY.js → chunk-YFKR74XG.js} +14 -14
  23. package/dist/{chunk-CTU4PQLY.js.map → chunk-YFKR74XG.js.map} +0 -0
  24. package/dist/{chunk-NSRORL3O.mjs → chunk-YODUQCHJ.mjs} +2 -2
  25. package/dist/{chunk-NSRORL3O.mjs.map → chunk-YODUQCHJ.mjs.map} +0 -0
  26. package/dist/{chunk-E7LH5H76.mjs → chunk-ZVGTYLKU.mjs} +2 -2
  27. package/dist/chunk-ZVGTYLKU.mjs.map +1 -0
  28. package/dist/clients/index.d.ts +7 -8
  29. package/dist/clients/index.js +3 -3
  30. package/dist/clients/index.mjs +2 -2
  31. package/dist/{transactionReceipt-aed524b4.d.ts → contract-9af4608b.d.ts} +30 -18
  32. package/dist/{createClient-03ba79e8.d.ts → createClient-5d316c7e.d.ts} +2 -2
  33. package/dist/{createPublicClient-818241ba.d.ts → createPublicClient-9d2b42e1.d.ts} +3 -3
  34. package/dist/{createTestClient-24eb7be8.d.ts → createTestClient-79498dab.d.ts} +3 -3
  35. package/dist/{createWalletClient-7cbd6ad1.d.ts → createWalletClient-f69a5230.d.ts} +3 -3
  36. package/dist/{eip1193-71d8e27d.d.ts → eip1193-6c485d63.d.ts} +1 -1
  37. package/dist/index.d.ts +45 -23
  38. package/dist/index.js +10 -6
  39. package/dist/index.mjs +9 -5
  40. package/dist/{parseGwei-dbd12305.d.ts → parseGwei-4308ad80.d.ts} +17 -4
  41. package/dist/public.d.ts +9 -10
  42. package/dist/public.js +4 -4
  43. package/dist/public.mjs +3 -3
  44. package/dist/{sendTransaction-d09961a7.d.ts → sendTransaction-1c8290a9.d.ts} +3 -4
  45. package/dist/{stopImpersonatingAccount-c8a00be7.d.ts → stopImpersonatingAccount-7781842a.d.ts} +2 -2
  46. package/dist/test.d.ts +5 -6
  47. package/dist/test.js +3 -3
  48. package/dist/test.mjs +2 -2
  49. package/dist/transactionRequest-341b6ed2.d.ts +15 -0
  50. package/dist/utils/index.d.ts +9 -8
  51. package/dist/utils/index.js +22 -2
  52. package/dist/utils/index.mjs +25 -5
  53. package/dist/wallet.d.ts +7 -8
  54. package/dist/wallet.js +3 -3
  55. package/dist/wallet.mjs +2 -2
  56. package/dist/{watchAsset-c39645b7.d.ts → watchAsset-afaad3c7.d.ts} +3 -4
  57. package/dist/{watchPendingTransactions-01e6be4f.d.ts → watchPendingTransactions-3b722547.d.ts} +21 -16
  58. package/dist/{webSocket-3a00015b.d.ts → webSocket-b180e679.d.ts} +2 -2
  59. package/dist/window.d.ts +3 -2
  60. package/package.json +3 -3
  61. package/src/_test/abis.ts +188 -0
  62. package/src/_test/constants.ts +0 -3
  63. package/src/_test/index.ts +5 -2
  64. package/src/actions/index.test.ts +2 -0
  65. package/src/actions/index.ts +6 -0
  66. package/src/actions/public/call.ts +2 -1
  67. package/src/actions/public/createContractEventFilter.test.ts +119 -0
  68. package/src/actions/public/createContractEventFilter.ts +69 -0
  69. package/src/actions/public/createEventFilter.ts +5 -8
  70. package/src/actions/public/deployContract.ts +1 -2
  71. package/src/actions/public/getBlock.test.ts +3 -3
  72. package/src/actions/public/getBlock.ts +1 -2
  73. package/src/actions/public/getBlockTransactionCount.ts +1 -2
  74. package/src/actions/public/getFilterChanges.test.ts +9 -7
  75. package/src/actions/public/getFilterLogs.test.ts +7 -5
  76. package/src/actions/public/getLogs.test.ts +7 -5
  77. package/src/actions/public/getLogs.ts +3 -2
  78. package/src/actions/public/getTransaction.ts +1 -2
  79. package/src/actions/public/getTransactionConfirmations.ts +1 -2
  80. package/src/actions/public/getTransactionReceipt.test.ts +24 -62
  81. package/src/actions/public/getTransactionReceipt.ts +1 -2
  82. package/src/actions/public/index.test.ts +4 -0
  83. package/src/actions/public/index.ts +18 -0
  84. package/src/actions/public/readContract.ts +1 -1
  85. package/src/actions/public/simulateContract.ts +1 -1
  86. package/src/actions/public/waitForTransactionReceipt.ts +1 -2
  87. package/src/actions/public/watchBlocks.test.ts +27 -2
  88. package/src/actions/public/watchBlocks.ts +33 -14
  89. package/src/actions/public/watchContractEvent.test.ts +301 -0
  90. package/src/actions/public/watchContractEvent.ts +108 -0
  91. package/src/actions/public/watchEvent.test.ts +190 -0
  92. package/src/actions/public/watchEvent.ts +95 -0
  93. package/src/actions/public/watchPendingTransactions.ts +1 -0
  94. package/src/actions/wallet/addChain.ts +1 -1
  95. package/src/actions/wallet/sendTransaction.ts +2 -1
  96. package/src/actions/wallet/switchChain.ts +1 -1
  97. package/src/actions/wallet/writeContract.ts +1 -1
  98. package/src/chains.test.ts +830 -52
  99. package/src/chains.ts +90 -158
  100. package/src/clients/createClient.ts +1 -1
  101. package/src/clients/createPublicClient.ts +1 -1
  102. package/src/clients/createTestClient.ts +1 -1
  103. package/src/clients/createWalletClient.ts +1 -1
  104. package/src/clients/transports/createTransport.ts +1 -1
  105. package/src/index.test.ts +2 -0
  106. package/src/index.ts +10 -2
  107. package/src/types/chain.ts +6 -0
  108. package/src/types/contract.ts +17 -2
  109. package/src/types/formatter.ts +23 -0
  110. package/src/types/index.ts +6 -0
  111. package/src/types/transaction.ts +1 -1
  112. package/src/utils/abi/decodeErrorResult.ts +2 -2
  113. package/src/utils/abi/decodeFunctionData.ts +2 -2
  114. package/src/utils/abi/encodeErrorResult.ts +2 -2
  115. package/src/utils/abi/encodeEventTopics.ts +4 -4
  116. package/src/utils/abi/encodeFunctionData.ts +2 -2
  117. package/src/utils/abi/{formatAbiItemWithParams.test.ts → formatAbiItem.test.ts} +11 -11
  118. package/src/utils/abi/{formatAbiItemWithParams.ts → formatAbiItem.ts} +1 -1
  119. package/src/utils/abi/index.test.ts +1 -1
  120. package/src/utils/abi/index.ts +1 -1
  121. package/src/utils/chain.test.ts +43 -0
  122. package/src/utils/chain.ts +8 -0
  123. package/src/utils/contract/getContractError.ts +2 -6
  124. package/src/utils/formatters/block.ts +4 -3
  125. package/src/utils/formatters/extract.ts +1 -1
  126. package/src/utils/formatters/format.ts +39 -2
  127. package/src/utils/formatters/index.test.ts +10 -0
  128. package/src/utils/formatters/index.ts +15 -5
  129. package/src/utils/formatters/transaction.ts +16 -4
  130. package/src/utils/formatters/transactionReceipt.ts +13 -4
  131. package/src/utils/formatters/transactionRequest.ts +12 -3
  132. package/src/utils/hash/getEventSignature.ts +2 -2
  133. package/src/utils/index.test.ts +24 -1
  134. package/src/utils/index.ts +12 -1
  135. package/src/{constants.test.ts → utils/unit/constants.test.ts} +0 -5
  136. package/src/{constants.ts → utils/unit/constants.ts} +0 -6
  137. package/src/utils/unit/formatEther.ts +1 -1
  138. package/src/utils/unit/formatGwei.ts +1 -1
  139. package/src/utils/unit/index.test.ts +12 -0
  140. package/src/utils/unit/index.ts +1 -0
  141. package/src/utils/unit/parseEther.ts +1 -1
  142. package/src/utils/unit/parseGwei.ts +1 -1
  143. package/dist/chunk-24EKKMUO.js.map +0 -1
  144. package/dist/chunk-BZFF747V.js.map +0 -1
  145. package/dist/chunk-E7LH5H76.mjs.map +0 -1
  146. package/dist/chunk-HRE6LYF3.js.map +0 -1
  147. package/dist/chunk-IVWG22RQ.mjs.map +0 -1
  148. package/dist/chunk-NYMS65YI.js.map +0 -1
  149. package/dist/chunk-PHSAQQGZ.mjs.map +0 -1
  150. package/dist/chunk-VAT5CEF7.mjs.map +0 -1
  151. package/dist/transactionRequest-8e970b0e.d.ts +0 -33
@@ -5,6 +5,7 @@ import type {
5
5
  Address,
6
6
  BlockNumber,
7
7
  BlockTag,
8
+ EventDefinition,
8
9
  Hash,
9
10
  LogTopic,
10
11
  } from '../../types'
@@ -12,7 +13,7 @@ import { numberToHex } from '../../utils'
12
13
  import { formatLog } from '../../utils/formatters/log'
13
14
  import { buildFilterTopics, EventFilterArgs } from './createEventFilter'
14
15
 
15
- export type GetLogsArgs<TEventDefinition extends `${string}(${string})`> = {
16
+ export type GetLogsArgs<TEventDefinition extends EventDefinition> = {
16
17
  /** Address or list of addresses from which logs originated */
17
18
  address?: Address | Address[]
18
19
  } & (
@@ -43,7 +44,7 @@ export type GetLogsResponse = Log[]
43
44
  /**
44
45
  * @description Returns a collection of event logs.
45
46
  */
46
- export async function getLogs<TEventDefinition extends `${string}(${string})`,>(
47
+ export async function getLogs<TEventDefinition extends EventDefinition,>(
47
48
  client: PublicClient,
48
49
  {
49
50
  address,
@@ -1,7 +1,6 @@
1
- import type { Chain } from '../../chains'
2
1
  import type { PublicClient } from '../../clients'
3
2
  import { TransactionNotFoundError } from '../../errors'
4
- import type { BlockTag, Hash, RpcTransaction } from '../../types'
3
+ import type { BlockTag, Chain, Hash, RpcTransaction } from '../../types'
5
4
  import { format, numberToHex } from '../../utils'
6
5
  import type {
7
6
  FormattedTransaction,
@@ -1,6 +1,5 @@
1
- import type { Chain } from '../../chains'
2
1
  import type { PublicClient } from '../../clients'
3
- import type { Hash } from '../../types'
2
+ import type { Chain, Hash } from '../../types'
4
3
  import type {
5
4
  FormattedTransactionReceipt,
6
5
  TransactionReceiptFormatter,
@@ -1,9 +1,9 @@
1
1
  import { assertType, describe, expect, it, test } from 'vitest'
2
2
 
3
3
  import { accounts, publicClient, testClient, walletClient } from '../../_test'
4
- import { celo, defineTransactionReceipt, localhost } from '../../chains'
4
+ import { celo } from '../../chains'
5
5
  import { createPublicClient, http } from '../../clients'
6
- import type { TransactionReceipt } from '../../types'
6
+ import type { Address, TransactionReceipt } from '../../types'
7
7
  import { parseEther, parseGwei } from '../../utils'
8
8
  import { wait } from '../../utils/wait'
9
9
  import { mine } from '../test'
@@ -70,77 +70,39 @@ test('gets transaction receipt', async () => {
70
70
 
71
71
  test('chain w/ custom block type', async () => {
72
72
  const client = createPublicClient({
73
- chain: {
74
- ...celo,
75
- rpcUrls: localhost.rpcUrls,
76
- formatters: {
77
- transactionReceipt: defineTransactionReceipt({
78
- exclude: ['effectiveGasPrice'],
79
- format: () => ({
80
- foo: 'bar' as const,
81
- }),
82
- }),
83
- },
84
- },
73
+ chain: celo,
85
74
  transport: http(),
86
75
  })
87
76
  const receipt = await getTransactionReceipt(client, {
88
- hash: '0xa4b1f606b66105fa45cb5db23d2f6597075701e7f0e2367f4e6a39d17a8cf98b',
77
+ hash: '0xe47fce1dffbe8e94e8f7c23b0af18160fbc19a1e80e9cb107cafe0856dd4a3f7',
89
78
  })
90
79
 
91
80
  assertType<
92
- Omit<TransactionReceipt, 'effectiveGasPrice'> & {
93
- foo: 'bar'
81
+ TransactionReceipt & {
82
+ feeCurrency: Address | null
83
+ gatewayFee: bigint | null
84
+ gatewayFeeRecipient: Address | null
94
85
  }
95
86
  >(receipt)
96
-
97
87
  expect(receipt).toMatchInlineSnapshot(`
98
88
  {
99
- "blockHash": "0x89644bbd5c8d682a2e9611170e6c1f02573d866d286f006cbf517eec7254ec2d",
100
- "blockNumber": 15131999n,
89
+ "blockHash": "0xfc279eb47ec9edbd7cb5c6affa4b3367daff930085d532d3cc4a1e004da46bd9",
90
+ "blockNumber": 17582734n,
101
91
  "contractAddress": null,
102
- "cumulativeGasUsed": 5814407n,
103
- "foo": "bar",
104
- "from": "0xa152f8bb749c55e9943a3a0a3111d18ee2b3f94e",
105
- "gasUsed": 37976n,
106
- "logs": [
107
- {
108
- "address": "0x15d4c048f83bd7e37d49ea4c83a07267ec4203da",
109
- "blockHash": "0x89644bbd5c8d682a2e9611170e6c1f02573d866d286f006cbf517eec7254ec2d",
110
- "blockNumber": 15131999n,
111
- "data": "0x0000000000000000000000000000000000000000000000000000002b3b6fb3d0",
112
- "logIndex": 108n,
113
- "removed": false,
114
- "topics": [
115
- "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
116
- "0x000000000000000000000000a00f99bc38b1ecda1fd70eaa1cd31d576a9f46b0",
117
- "0x000000000000000000000000f16e9b0d03470827a95cdfd0cb8a8a3b46969b91",
118
- ],
119
- "transactionHash": "0xa4b1f606b66105fa45cb5db23d2f6597075701e7f0e2367f4e6a39d17a8cf98b",
120
- "transactionIndex": 69n,
121
- },
122
- {
123
- "address": "0x15d4c048f83bd7e37d49ea4c83a07267ec4203da",
124
- "blockHash": "0x89644bbd5c8d682a2e9611170e6c1f02573d866d286f006cbf517eec7254ec2d",
125
- "blockNumber": 15131999n,
126
- "data": "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffd4c4904c2f",
127
- "logIndex": 109n,
128
- "removed": false,
129
- "topics": [
130
- "0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925",
131
- "0x000000000000000000000000a00f99bc38b1ecda1fd70eaa1cd31d576a9f46b0",
132
- "0x000000000000000000000000a152f8bb749c55e9943a3a0a3111d18ee2b3f94e",
133
- ],
134
- "transactionHash": "0xa4b1f606b66105fa45cb5db23d2f6597075701e7f0e2367f4e6a39d17a8cf98b",
135
- "transactionIndex": 69n,
136
- },
137
- ],
138
- "logsBloom": "0x08000000000000000000000000000000000000000000000000001000002000000000000000000000000000000000000000000000080000000000000000200000000000000000000000000008400000000000000000000000000000000000100000000000000000000040000008000000000004000000000000000010000000000000000000000000000000000000000000000000000000000000000000000004020000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000002090000000000000000000000000000000000000000000000000000000000000",
139
- "status": "success",
140
- "to": "0x15d4c048f83bd7e37d49ea4c83a07267ec4203da",
141
- "transactionHash": "0xa4b1f606b66105fa45cb5db23d2f6597075701e7f0e2367f4e6a39d17a8cf98b",
142
- "transactionIndex": 69,
143
- "type": "eip1559",
92
+ "cumulativeGasUsed": 1765686n,
93
+ "effectiveGasPrice": 5000000001n,
94
+ "feeCurrency": undefined,
95
+ "from": "0x0372cffb61f23703efae601b1962efee825204bc",
96
+ "gasUsed": 245109n,
97
+ "gatewayFee": null,
98
+ "gatewayFeeRecipient": undefined,
99
+ "logs": [],
100
+ "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
101
+ "status": "reverted",
102
+ "to": "0x865340af8e50eaf0acec9ebc789b2b425c7e0193",
103
+ "transactionHash": "0xe47fce1dffbe8e94e8f7c23b0af18160fbc19a1e80e9cb107cafe0856dd4a3f7",
104
+ "transactionIndex": 4,
105
+ "type": "legacy",
144
106
  }
145
107
  `)
146
108
  })
@@ -1,7 +1,6 @@
1
- import type { Chain } from '../../chains'
2
1
  import type { PublicClient } from '../../clients'
3
2
  import { TransactionReceiptNotFoundError } from '../../errors'
4
- import type { Hash } from '../../types'
3
+ import type { Chain, Hash } from '../../types'
5
4
  import { format } from '../../utils'
6
5
  import type {
7
6
  FormattedTransactionReceipt,
@@ -7,6 +7,8 @@ test('exports actions', () => {
7
7
  {
8
8
  "call": [Function],
9
9
  "createBlockFilter": [Function],
10
+ "createContractEventFilter": [Function],
11
+ "createEventFilter": [Function],
10
12
  "createPendingTransactionFilter": [Function],
11
13
  "deployContract": [Function],
12
14
  "estimateGas": [Function],
@@ -33,6 +35,8 @@ test('exports actions', () => {
33
35
  "waitForTransactionReceipt": [Function],
34
36
  "watchBlockNumber": [Function],
35
37
  "watchBlocks": [Function],
38
+ "watchContractEvent": [Function],
39
+ "watchEvent": [Function],
36
40
  "watchPendingTransactions": [Function],
37
41
  }
38
42
  `)
@@ -13,6 +13,18 @@ export type { CreatePendingTransactionFilterResponse } from './createPendingTran
13
13
  export { createBlockFilter } from './createBlockFilter'
14
14
  export type { CreateBlockFilterResponse } from './createBlockFilter'
15
15
 
16
+ export { createEventFilter } from './createEventFilter'
17
+ export type {
18
+ CreateEventFilterArgs,
19
+ CreateEventFilterResponse,
20
+ } from './createEventFilter'
21
+
22
+ export { createContractEventFilter } from './createContractEventFilter'
23
+ export type {
24
+ CreateContractEventFilterArgs,
25
+ CreateContractEventFilterResponse,
26
+ } from './createContractEventFilter'
27
+
16
28
  export { deployContract } from './deployContract'
17
29
  export type {
18
30
  DeployContractArgs,
@@ -123,6 +135,12 @@ export type {
123
135
  export { watchBlocks } from './watchBlocks'
124
136
  export type { WatchBlocksArgs, OnBlock, OnBlockResponse } from './watchBlocks'
125
137
 
138
+ export { watchContractEvent } from './watchContractEvent'
139
+ export type { WatchContractEventArgs } from './watchContractEvent'
140
+
141
+ export { watchEvent } from './watchEvent'
142
+ export type { WatchEventArgs, OnLogs, OnLogsResponse } from './watchEvent'
143
+
126
144
  export { watchPendingTransactions } from './watchPendingTransactions'
127
145
  export type {
128
146
  OnTransactions,
@@ -1,12 +1,12 @@
1
1
  import { Abi } from 'abitype'
2
2
 
3
- import type { Chain, Formatter } from '../../chains'
4
3
  import type { PublicClient } from '../../clients'
5
4
  import type {
6
5
  Address,
7
6
  ExtractArgsFromAbi,
8
7
  ExtractResultFromAbi,
9
8
  ExtractFunctionNameFromAbi,
9
+ Formatter,
10
10
  } from '../../types'
11
11
  import {
12
12
  EncodeFunctionDataArgs,
@@ -1,9 +1,9 @@
1
1
  import { Abi } from 'abitype'
2
2
 
3
- import type { Chain, Formatter } from '../../chains'
4
3
  import type { PublicClient } from '../../clients'
5
4
  import type {
6
5
  Address,
6
+ Chain,
7
7
  ExtractArgsFromAbi,
8
8
  ExtractResultFromAbi,
9
9
  ExtractFunctionNameFromAbi,
@@ -1,11 +1,10 @@
1
- import type { Chain } from '../../chains'
2
1
  import type { PublicClient } from '../../clients'
3
2
  import {
4
3
  TransactionNotFoundError,
5
4
  TransactionReceiptNotFoundError,
6
5
  WaitForTransactionReceiptTimeoutError,
7
6
  } from '../../errors'
8
- import type { Hash, Transaction } from '../../types'
7
+ import type { Chain, Hash, Transaction } from '../../types'
9
8
  import { observe } from '../../utils/observe'
10
9
  import { getBlock, watchBlockNumber } from '../public'
11
10
 
@@ -4,11 +4,13 @@ import type { OnBlockResponse } from './watchBlocks'
4
4
  import * as getBlock from './getBlock'
5
5
  import { watchBlocks } from './watchBlocks'
6
6
  import { mine } from '../test/mine'
7
- import { publicClient, testClient } from '../../_test'
7
+ import { accounts, publicClient, testClient, walletClient } from '../../_test'
8
8
  import { wait } from '../../utils/wait'
9
- import { celo, localhost } from '../../chains'
9
+ import { celo, Chain, localhost } from '../../chains'
10
10
  import { createPublicClient, http } from '../../clients'
11
11
  import { setIntervalMining } from '../test'
12
+ import { sendTransaction } from '../wallet'
13
+ import { parseEther } from '../../utils'
12
14
 
13
15
  test('watches for new blocks', async () => {
14
16
  const blocks: OnBlockResponse[] = []
@@ -25,6 +27,29 @@ test('watches for new blocks', async () => {
25
27
  expect(prevBlocks.length).toBe(3)
26
28
  })
27
29
 
30
+ test('args: includeTransactions', async () => {
31
+ await mine(testClient, { blocks: 1 })
32
+
33
+ const blocks: OnBlockResponse<Chain, true>[] = []
34
+ const unwatch = watchBlocks(publicClient, {
35
+ includeTransactions: true,
36
+ onBlock: (block) => {
37
+ blocks.push(block)
38
+ },
39
+ })
40
+
41
+ await sendTransaction(walletClient, {
42
+ from: accounts[0].address,
43
+ to: accounts[1].address,
44
+ value: parseEther('1'),
45
+ })
46
+ await wait(2000)
47
+
48
+ unwatch()
49
+ expect(blocks.length).toBe(1)
50
+ expect(blocks[0].transactions.length).toBe(1)
51
+ })
52
+
28
53
  describe('emitMissed', () => {
29
54
  test('emits on missed blocks', async () => {
30
55
  await setIntervalMining(testClient, { interval: 0 })
@@ -1,16 +1,23 @@
1
- import type { Chain } from '../../chains'
2
1
  import type { PublicClient } from '../../clients'
3
- import type { BlockTag } from '../../types'
2
+ import type { BlockTag, Chain } from '../../types'
4
3
  import { observe } from '../../utils/observe'
5
4
  import { poll } from '../../utils/poll'
6
5
  import type { GetBlockResponse } from './getBlock'
7
6
  import { getBlock } from './getBlock'
8
7
 
9
- export type OnBlockResponse<TChain extends Chain = Chain> =
10
- GetBlockResponse<TChain>
11
- export type OnBlock<TChain extends Chain = Chain> = (
12
- block: OnBlockResponse<TChain>,
13
- prevBlock: OnBlockResponse<TChain> | undefined,
8
+ export type OnBlockResponse<
9
+ TChain extends Chain = Chain,
10
+ TIncludeTransactions = false,
11
+ > = Omit<
12
+ GetBlockResponse<TChain>,
13
+ TIncludeTransactions extends false ? 'transactions' : ''
14
+ >
15
+ export type OnBlock<
16
+ TChain extends Chain = Chain,
17
+ TIncludeTransactions = false,
18
+ > = (
19
+ block: OnBlockResponse<TChain, TIncludeTransactions>,
20
+ prevBlock: OnBlockResponse<TChain, TIncludeTransactions> | undefined,
14
21
  ) => void
15
22
 
16
23
  export type WatchBlocksArgs<TChain extends Chain = Chain> = {
@@ -20,18 +27,30 @@ export type WatchBlocksArgs<TChain extends Chain = Chain> = {
20
27
  emitMissed?: boolean
21
28
  /** Whether or not to emit the block to the callback when the subscription opens. */
22
29
  emitOnBegin?: boolean
23
- /** The callback to call when a new block is received. */
24
- onBlock: OnBlock<TChain>
25
30
  /** The callback to call when an error occurred when trying to get for a new block. */
26
31
  onError?: (error: Error) => void
27
- /** Whether or not to include transaction data in the response. */
28
- includeTransactions?: boolean
29
32
  /** Polling frequency (in ms). Defaults to the client's pollingInterval config. */
30
33
  pollingInterval?: number
31
- }
34
+ } & (
35
+ | {
36
+ /** Whether or not to include transaction data in the response. */
37
+ includeTransactions: true
38
+ /** The callback to call when a new block is received. */
39
+ onBlock: OnBlock<TChain, true>
40
+ }
41
+ | {
42
+ /** Whether or not to include transaction data in the response. */
43
+ includeTransactions?: false
44
+ /** The callback to call when a new block is received. */
45
+ onBlock: OnBlock<TChain>
46
+ }
47
+ )
32
48
 
33
49
  /** @description Watches and returns information for incoming blocks. */
34
- export function watchBlocks<TChain extends Chain>(
50
+ export function watchBlocks<
51
+ TChain extends Chain,
52
+ TWatchBlocksArgs extends WatchBlocksArgs<TChain>,
53
+ >(
35
54
  client: PublicClient<any, TChain>,
36
55
  {
37
56
  blockTag = 'latest',
@@ -41,7 +60,7 @@ export function watchBlocks<TChain extends Chain>(
41
60
  onError,
42
61
  includeTransactions = false,
43
62
  pollingInterval = client.pollingInterval,
44
- }: WatchBlocksArgs<TChain>,
63
+ }: TWatchBlocksArgs,
45
64
  ) {
46
65
  const observerId = JSON.stringify([
47
66
  'watchBlocks',
@@ -0,0 +1,301 @@
1
+ import { afterAll, beforeAll, describe, expect, test, vi } from 'vitest'
2
+ import { wait } from '../../utils/wait'
3
+ import {
4
+ accounts,
5
+ publicClient,
6
+ testClient,
7
+ usdcContractConfig,
8
+ vitalikAddress,
9
+ walletClient,
10
+ } from '../../_test'
11
+ import { impersonateAccount, stopImpersonatingAccount } from '../test'
12
+ import { writeContract } from '../wallet'
13
+ import * as createContractEventFilter from './createContractEventFilter'
14
+ import * as getFilterChanges from './getFilterChanges'
15
+ import { OnLogsResponse, watchContractEvent } from './watchContractEvent'
16
+
17
+ beforeAll(async () => {
18
+ await impersonateAccount(testClient, {
19
+ address: vitalikAddress,
20
+ })
21
+ await impersonateAccount(testClient, {
22
+ address: '0x5414d89a8bf7e99d732bc52f3e6a3ef461c0c078',
23
+ })
24
+ })
25
+
26
+ afterAll(async () => {
27
+ await stopImpersonatingAccount(testClient, {
28
+ address: vitalikAddress,
29
+ })
30
+ await stopImpersonatingAccount(testClient, {
31
+ address: '0x5414d89a8bf7e99d732bc52f3e6a3ef461c0c078',
32
+ })
33
+ })
34
+
35
+ test('default', async () => {
36
+ let logs: OnLogsResponse[] = []
37
+
38
+ const unwatch = watchContractEvent(publicClient, {
39
+ ...usdcContractConfig,
40
+ onLogs: (logs_) => logs.push(logs_),
41
+ })
42
+
43
+ await writeContract(walletClient, {
44
+ ...usdcContractConfig,
45
+ from: vitalikAddress,
46
+ functionName: 'transfer',
47
+ args: [vitalikAddress, 1n],
48
+ })
49
+ await writeContract(walletClient, {
50
+ ...usdcContractConfig,
51
+ from: vitalikAddress,
52
+ functionName: 'transfer',
53
+ args: [vitalikAddress, 1n],
54
+ })
55
+ await wait(1000)
56
+ await writeContract(walletClient, {
57
+ ...usdcContractConfig,
58
+ from: vitalikAddress,
59
+ functionName: 'approve',
60
+ args: [vitalikAddress, 1n],
61
+ })
62
+
63
+ await wait(2000)
64
+ unwatch()
65
+
66
+ expect(logs.length).toBe(2)
67
+ expect(logs[0].length).toBe(2)
68
+ expect(logs[1].length).toBe(1)
69
+ })
70
+
71
+ test('args: batch', async () => {
72
+ let logs: OnLogsResponse[] = []
73
+
74
+ const unwatch = watchContractEvent(publicClient, {
75
+ ...usdcContractConfig,
76
+ batch: false,
77
+ onLogs: (logs_) => logs.push(logs_),
78
+ })
79
+
80
+ await writeContract(walletClient, {
81
+ ...usdcContractConfig,
82
+ from: vitalikAddress,
83
+ functionName: 'transfer',
84
+ args: [vitalikAddress, 1n],
85
+ })
86
+ await writeContract(walletClient, {
87
+ ...usdcContractConfig,
88
+ from: vitalikAddress,
89
+ functionName: 'transfer',
90
+ args: [vitalikAddress, 1n],
91
+ })
92
+ await wait(1000)
93
+ await writeContract(walletClient, {
94
+ ...usdcContractConfig,
95
+ from: vitalikAddress,
96
+ functionName: 'approve',
97
+ args: [vitalikAddress, 1n],
98
+ })
99
+
100
+ await wait(2000)
101
+ unwatch()
102
+
103
+ expect(logs.length).toBe(3)
104
+ expect(logs[0].length).toBe(1)
105
+ expect(logs[1].length).toBe(1)
106
+ expect(logs[2].length).toBe(1)
107
+ })
108
+
109
+ test('args: eventName', async () => {
110
+ let logs: OnLogsResponse[] = []
111
+
112
+ const unwatch = watchContractEvent(publicClient, {
113
+ ...usdcContractConfig,
114
+ eventName: 'Transfer',
115
+ onLogs: (logs_) => logs.push(logs_),
116
+ })
117
+
118
+ await writeContract(walletClient, {
119
+ ...usdcContractConfig,
120
+ from: vitalikAddress,
121
+ functionName: 'transfer',
122
+ args: [vitalikAddress, 1n],
123
+ })
124
+ await writeContract(walletClient, {
125
+ ...usdcContractConfig,
126
+ from: vitalikAddress,
127
+ functionName: 'transfer',
128
+ args: [vitalikAddress, 1n],
129
+ })
130
+ await wait(1000)
131
+ await writeContract(walletClient, {
132
+ ...usdcContractConfig,
133
+ from: vitalikAddress,
134
+ functionName: 'approve',
135
+ args: [vitalikAddress, 1n],
136
+ })
137
+
138
+ await wait(2000)
139
+ unwatch()
140
+
141
+ expect(logs.length).toBe(1)
142
+ expect(logs[0].length).toBe(2)
143
+ })
144
+
145
+ test('args: args', async () => {
146
+ let logs: OnLogsResponse[] = []
147
+
148
+ const unwatch = watchContractEvent(publicClient, {
149
+ ...usdcContractConfig,
150
+ eventName: 'Transfer',
151
+ args: {
152
+ to: accounts[0].address,
153
+ },
154
+ onLogs: (logs_) => logs.push(logs_),
155
+ })
156
+
157
+ await writeContract(walletClient, {
158
+ ...usdcContractConfig,
159
+ from: vitalikAddress,
160
+ functionName: 'transfer',
161
+ args: [accounts[0].address, 1n],
162
+ })
163
+ await writeContract(walletClient, {
164
+ ...usdcContractConfig,
165
+ from: vitalikAddress,
166
+ functionName: 'transfer',
167
+ args: [accounts[1].address, 1n],
168
+ })
169
+ await wait(1000)
170
+ await writeContract(walletClient, {
171
+ ...usdcContractConfig,
172
+ from: vitalikAddress,
173
+ functionName: 'approve',
174
+ args: [vitalikAddress, 1n],
175
+ })
176
+
177
+ await wait(2000)
178
+ unwatch()
179
+
180
+ expect(logs.length).toBe(1)
181
+ expect(logs[0].length).toBe(1)
182
+ })
183
+
184
+ test('args: args', async () => {
185
+ let logs: OnLogsResponse[] = []
186
+
187
+ const unwatch = watchContractEvent(publicClient, {
188
+ ...usdcContractConfig,
189
+ eventName: 'Transfer',
190
+ args: {
191
+ to: [accounts[0].address, accounts[1].address],
192
+ },
193
+ onLogs: (logs_) => logs.push(logs_),
194
+ })
195
+
196
+ await writeContract(walletClient, {
197
+ ...usdcContractConfig,
198
+ from: vitalikAddress,
199
+ functionName: 'transfer',
200
+ args: [accounts[0].address, 1n],
201
+ })
202
+ await writeContract(walletClient, {
203
+ ...usdcContractConfig,
204
+ from: vitalikAddress,
205
+ functionName: 'transfer',
206
+ args: [accounts[1].address, 1n],
207
+ })
208
+ await wait(1000)
209
+ await writeContract(walletClient, {
210
+ ...usdcContractConfig,
211
+ from: vitalikAddress,
212
+ functionName: 'approve',
213
+ args: [vitalikAddress, 1n],
214
+ })
215
+
216
+ await wait(2000)
217
+ unwatch()
218
+
219
+ expect(logs.length).toBe(1)
220
+ expect(logs[0].length).toBe(2)
221
+ })
222
+
223
+ test('args: args', async () => {
224
+ let logs: OnLogsResponse[] = []
225
+
226
+ const unwatch = watchContractEvent(publicClient, {
227
+ ...usdcContractConfig,
228
+ eventName: 'Transfer',
229
+ args: {
230
+ from: '0x5414d89a8bf7e99d732bc52f3e6a3ef461c0c078',
231
+ },
232
+ onLogs: (logs_) => logs.push(logs_),
233
+ })
234
+
235
+ await writeContract(walletClient, {
236
+ ...usdcContractConfig,
237
+ from: '0x5414d89a8bf7e99d732bc52f3e6a3ef461c0c078',
238
+ functionName: 'transfer',
239
+ args: [accounts[0].address, 1n],
240
+ })
241
+ await writeContract(walletClient, {
242
+ ...usdcContractConfig,
243
+ from: vitalikAddress,
244
+ functionName: 'transfer',
245
+ args: [accounts[1].address, 1n],
246
+ })
247
+ await wait(1000)
248
+ await writeContract(walletClient, {
249
+ ...usdcContractConfig,
250
+ from: vitalikAddress,
251
+ functionName: 'approve',
252
+ args: [vitalikAddress, 1n],
253
+ })
254
+
255
+ await wait(2000)
256
+ unwatch()
257
+
258
+ expect(logs.length).toBe(1)
259
+ expect(logs[0].length).toBe(1)
260
+ })
261
+
262
+ describe('errors', () => {
263
+ test('handles error thrown from creating filter', async () => {
264
+ vi.spyOn(
265
+ createContractEventFilter,
266
+ 'createContractEventFilter',
267
+ ).mockRejectedValueOnce(new Error('foo'))
268
+
269
+ let unwatch: () => void = () => null
270
+ const error = await new Promise((resolve) => {
271
+ unwatch = watchContractEvent(publicClient, {
272
+ ...usdcContractConfig,
273
+ onLogs: () => null,
274
+ onError: resolve,
275
+ })
276
+ })
277
+ expect(error).toMatchInlineSnapshot('[Error: foo]')
278
+ unwatch()
279
+ })
280
+
281
+ test(
282
+ 'handles error thrown from filter changes',
283
+ async () => {
284
+ vi.spyOn(getFilterChanges, 'getFilterChanges').mockRejectedValueOnce(
285
+ new Error('bar'),
286
+ )
287
+
288
+ let unwatch: () => void = () => null
289
+ const error = await new Promise((resolve) => {
290
+ unwatch = watchContractEvent(publicClient, {
291
+ ...usdcContractConfig,
292
+ onLogs: () => null,
293
+ onError: resolve,
294
+ })
295
+ })
296
+ expect(error).toMatchInlineSnapshot('[Error: bar]')
297
+ unwatch()
298
+ },
299
+ { retry: 3 },
300
+ )
301
+ })