viem 1.4.2 → 1.5.2

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 (225) hide show
  1. package/dist/cjs/accounts/index.js +1 -1
  2. package/dist/cjs/accounts/index.js.map +1 -1
  3. package/dist/cjs/accounts/utils/signMessage.js +1 -1
  4. package/dist/cjs/accounts/utils/signMessage.js.map +1 -1
  5. package/dist/cjs/accounts/utils/signTypedData.js +1 -1
  6. package/dist/cjs/accounts/utils/signTypedData.js.map +1 -1
  7. package/dist/cjs/actions/public/createContractEventFilter.js.map +1 -1
  8. package/dist/cjs/actions/public/createEventFilter.js +16 -8
  9. package/dist/cjs/actions/public/createEventFilter.js.map +1 -1
  10. package/dist/cjs/actions/public/getBlock.js +3 -1
  11. package/dist/cjs/actions/public/getBlock.js.map +1 -1
  12. package/dist/cjs/actions/public/getBlockNumber.js +2 -2
  13. package/dist/cjs/actions/public/getBlockNumber.js.map +1 -1
  14. package/dist/cjs/actions/public/getFilterChanges.js.map +1 -1
  15. package/dist/cjs/actions/public/getFilterLogs.js +1 -1
  16. package/dist/cjs/actions/public/getFilterLogs.js.map +1 -1
  17. package/dist/cjs/actions/public/getLogs.js +16 -10
  18. package/dist/cjs/actions/public/getLogs.js.map +1 -1
  19. package/dist/cjs/actions/public/getTransaction.js +2 -1
  20. package/dist/cjs/actions/public/getTransaction.js.map +1 -1
  21. package/dist/cjs/actions/public/watchBlockNumber.js +1 -1
  22. package/dist/cjs/actions/public/watchBlockNumber.js.map +1 -1
  23. package/dist/cjs/actions/public/watchBlocks.js +2 -1
  24. package/dist/cjs/actions/public/watchBlocks.js.map +1 -1
  25. package/dist/cjs/actions/public/watchContractEvent.js +3 -0
  26. package/dist/cjs/actions/public/watchContractEvent.js.map +1 -1
  27. package/dist/cjs/actions/public/watchEvent.js +7 -2
  28. package/dist/cjs/actions/public/watchEvent.js.map +1 -1
  29. package/dist/cjs/clients/createClient.js +2 -1
  30. package/dist/cjs/clients/createClient.js.map +1 -1
  31. package/dist/cjs/clients/createPublicClient.js.map +1 -1
  32. package/dist/cjs/clients/createTestClient.js.map +1 -1
  33. package/dist/cjs/clients/createWalletClient.js.map +1 -1
  34. package/dist/cjs/clients/decorators/public.js.map +1 -1
  35. package/dist/cjs/constants/number.js +101 -0
  36. package/dist/cjs/constants/number.js.map +1 -0
  37. package/dist/cjs/errors/version.js +1 -1
  38. package/dist/cjs/index.js +110 -7
  39. package/dist/cjs/index.js.map +1 -1
  40. package/dist/cjs/utils/formatters/block.js.map +1 -1
  41. package/dist/cjs/utils/formatters/formatter.js +1 -0
  42. package/dist/cjs/utils/formatters/formatter.js.map +1 -1
  43. package/dist/cjs/utils/formatters/transaction.js.map +1 -1
  44. package/dist/cjs/utils/hash/hashFunction.js +3 -2
  45. package/dist/cjs/utils/hash/hashFunction.js.map +1 -1
  46. package/dist/cjs/utils/promise/withCache.js +3 -3
  47. package/dist/cjs/utils/promise/withCache.js.map +1 -1
  48. package/dist/cjs/utils/signature/hexToSignature.js +12 -0
  49. package/dist/cjs/utils/signature/hexToSignature.js.map +1 -0
  50. package/dist/cjs/utils/signature/signatureToHex.js.map +1 -0
  51. package/dist/esm/accounts/index.js +1 -1
  52. package/dist/esm/accounts/index.js.map +1 -1
  53. package/dist/esm/accounts/utils/signMessage.js +1 -1
  54. package/dist/esm/accounts/utils/signMessage.js.map +1 -1
  55. package/dist/esm/accounts/utils/signTypedData.js +1 -1
  56. package/dist/esm/accounts/utils/signTypedData.js.map +1 -1
  57. package/dist/esm/actions/public/createContractEventFilter.js.map +1 -1
  58. package/dist/esm/actions/public/createEventFilter.js +16 -8
  59. package/dist/esm/actions/public/createEventFilter.js.map +1 -1
  60. package/dist/esm/actions/public/getBlock.js +3 -1
  61. package/dist/esm/actions/public/getBlock.js.map +1 -1
  62. package/dist/esm/actions/public/getBlockNumber.js +2 -2
  63. package/dist/esm/actions/public/getBlockNumber.js.map +1 -1
  64. package/dist/esm/actions/public/getFilterChanges.js.map +1 -1
  65. package/dist/esm/actions/public/getFilterLogs.js +1 -1
  66. package/dist/esm/actions/public/getFilterLogs.js.map +1 -1
  67. package/dist/esm/actions/public/getLogs.js +16 -10
  68. package/dist/esm/actions/public/getLogs.js.map +1 -1
  69. package/dist/esm/actions/public/getTransaction.js +2 -1
  70. package/dist/esm/actions/public/getTransaction.js.map +1 -1
  71. package/dist/esm/actions/public/watchBlockNumber.js +1 -1
  72. package/dist/esm/actions/public/watchBlockNumber.js.map +1 -1
  73. package/dist/esm/actions/public/watchBlocks.js +2 -1
  74. package/dist/esm/actions/public/watchBlocks.js.map +1 -1
  75. package/dist/esm/actions/public/watchContractEvent.js +5 -0
  76. package/dist/esm/actions/public/watchContractEvent.js.map +1 -1
  77. package/dist/esm/actions/public/watchEvent.js +9 -2
  78. package/dist/esm/actions/public/watchEvent.js.map +1 -1
  79. package/dist/esm/clients/createClient.js +2 -1
  80. package/dist/esm/clients/createClient.js.map +1 -1
  81. package/dist/esm/clients/createPublicClient.js.map +1 -1
  82. package/dist/esm/clients/createTestClient.js.map +1 -1
  83. package/dist/esm/clients/createWalletClient.js.map +1 -1
  84. package/dist/esm/clients/decorators/public.js.map +1 -1
  85. package/dist/esm/constants/number.js +97 -0
  86. package/dist/esm/constants/number.js.map +1 -0
  87. package/dist/esm/errors/version.js +1 -1
  88. package/dist/esm/index.js +4 -1
  89. package/dist/esm/index.js.map +1 -1
  90. package/dist/esm/utils/formatters/block.js.map +1 -1
  91. package/dist/esm/utils/formatters/formatter.js +1 -0
  92. package/dist/esm/utils/formatters/formatter.js.map +1 -1
  93. package/dist/esm/utils/formatters/transaction.js.map +1 -1
  94. package/dist/esm/utils/hash/hashFunction.js +3 -2
  95. package/dist/esm/utils/hash/hashFunction.js.map +1 -1
  96. package/dist/esm/utils/promise/withCache.js +3 -3
  97. package/dist/esm/utils/promise/withCache.js.map +1 -1
  98. package/dist/esm/utils/signature/hexToSignature.js +18 -0
  99. package/dist/esm/utils/signature/hexToSignature.js.map +1 -0
  100. package/dist/esm/utils/signature/signatureToHex.js +21 -0
  101. package/dist/esm/utils/signature/signatureToHex.js.map +1 -0
  102. package/dist/types/accounts/index.d.ts +1 -1
  103. package/dist/types/accounts/index.d.ts.map +1 -1
  104. package/dist/types/actions/public/createContractEventFilter.d.ts +5 -5
  105. package/dist/types/actions/public/createContractEventFilter.d.ts.map +1 -1
  106. package/dist/types/actions/public/createEventFilter.d.ts +20 -7
  107. package/dist/types/actions/public/createEventFilter.d.ts.map +1 -1
  108. package/dist/types/actions/public/getBlock.d.ts +5 -5
  109. package/dist/types/actions/public/getBlock.d.ts.map +1 -1
  110. package/dist/types/actions/public/getBlockNumber.d.ts +4 -2
  111. package/dist/types/actions/public/getBlockNumber.d.ts.map +1 -1
  112. package/dist/types/actions/public/getFilterChanges.d.ts +5 -8
  113. package/dist/types/actions/public/getFilterChanges.d.ts.map +1 -1
  114. package/dist/types/actions/public/getFilterLogs.d.ts +5 -4
  115. package/dist/types/actions/public/getFilterLogs.d.ts.map +1 -1
  116. package/dist/types/actions/public/getLogs.d.ts +17 -6
  117. package/dist/types/actions/public/getLogs.d.ts.map +1 -1
  118. package/dist/types/actions/public/getTransaction.d.ts +4 -4
  119. package/dist/types/actions/public/getTransaction.d.ts.map +1 -1
  120. package/dist/types/actions/public/watchBlocks.d.ts +10 -10
  121. package/dist/types/actions/public/watchBlocks.d.ts.map +1 -1
  122. package/dist/types/actions/public/watchContractEvent.d.ts +1 -1
  123. package/dist/types/actions/public/watchContractEvent.d.ts.map +1 -1
  124. package/dist/types/actions/public/watchEvent.d.ts +19 -8
  125. package/dist/types/actions/public/watchEvent.d.ts.map +1 -1
  126. package/dist/types/chains/formatters/celo.d.ts +4 -0
  127. package/dist/types/chains/formatters/celo.d.ts.map +1 -1
  128. package/dist/types/chains/formatters/optimism.d.ts +5 -3
  129. package/dist/types/chains/formatters/optimism.d.ts.map +1 -1
  130. package/dist/types/chains/index.d.ts +44 -12
  131. package/dist/types/chains/index.d.ts.map +1 -1
  132. package/dist/types/clients/createClient.d.ts +7 -0
  133. package/dist/types/clients/createClient.d.ts.map +1 -1
  134. package/dist/types/clients/createPublicClient.d.ts +1 -1
  135. package/dist/types/clients/createPublicClient.d.ts.map +1 -1
  136. package/dist/types/clients/createTestClient.d.ts +1 -1
  137. package/dist/types/clients/createTestClient.d.ts.map +1 -1
  138. package/dist/types/clients/createWalletClient.d.ts +1 -1
  139. package/dist/types/clients/createWalletClient.d.ts.map +1 -1
  140. package/dist/types/clients/decorators/public.d.ts +10 -9
  141. package/dist/types/clients/decorators/public.d.ts.map +1 -1
  142. package/dist/types/constants/number.d.ts +97 -0
  143. package/dist/types/constants/number.d.ts.map +1 -0
  144. package/dist/types/errors/version.d.ts +1 -1
  145. package/dist/types/index.d.ts +4 -1
  146. package/dist/types/index.d.ts.map +1 -1
  147. package/dist/types/types/block.d.ts +7 -7
  148. package/dist/types/types/block.d.ts.map +1 -1
  149. package/dist/types/types/contract.d.ts +1 -1
  150. package/dist/types/types/contract.d.ts.map +1 -1
  151. package/dist/types/types/filter.d.ts +7 -3
  152. package/dist/types/types/filter.d.ts.map +1 -1
  153. package/dist/types/types/formatter.d.ts +3 -0
  154. package/dist/types/types/formatter.d.ts.map +1 -1
  155. package/dist/types/types/log.d.ts +8 -8
  156. package/dist/types/types/log.d.ts.map +1 -1
  157. package/dist/types/types/misc.d.ts.map +1 -1
  158. package/dist/types/types/rpc.d.ts +3 -3
  159. package/dist/types/types/rpc.d.ts.map +1 -1
  160. package/dist/types/types/transaction.d.ts +8 -8
  161. package/dist/types/types/transaction.d.ts.map +1 -1
  162. package/dist/types/utils/formatters/block.d.ts +13 -3
  163. package/dist/types/utils/formatters/block.d.ts.map +1 -1
  164. package/dist/types/utils/formatters/formatter.d.ts +1 -0
  165. package/dist/types/utils/formatters/formatter.d.ts.map +1 -1
  166. package/dist/types/utils/formatters/transaction.d.ts +10 -3
  167. package/dist/types/utils/formatters/transaction.d.ts.map +1 -1
  168. package/dist/types/utils/formatters/transactionReceipt.d.ts +1 -0
  169. package/dist/types/utils/formatters/transactionReceipt.d.ts.map +1 -1
  170. package/dist/types/utils/formatters/transactionRequest.d.ts +1 -0
  171. package/dist/types/utils/formatters/transactionRequest.d.ts.map +1 -1
  172. package/dist/types/utils/hash/hashFunction.d.ts +1 -1
  173. package/dist/types/utils/hash/hashFunction.d.ts.map +1 -1
  174. package/dist/types/utils/promise/withCache.d.ts +3 -3
  175. package/dist/types/utils/promise/withCache.d.ts.map +1 -1
  176. package/dist/types/utils/signature/hexToSignature.d.ts +13 -0
  177. package/dist/types/utils/signature/hexToSignature.d.ts.map +1 -0
  178. package/dist/types/utils/signature/signatureToHex.d.ts +17 -0
  179. package/dist/types/utils/signature/signatureToHex.d.ts.map +1 -0
  180. package/package.json +2 -1
  181. package/src/accounts/index.ts +1 -1
  182. package/src/accounts/utils/signMessage.ts +1 -1
  183. package/src/accounts/utils/signTypedData.ts +1 -1
  184. package/src/actions/public/createContractEventFilter.ts +30 -7
  185. package/src/actions/public/createEventFilter.ts +78 -24
  186. package/src/actions/public/getBlock.ts +18 -8
  187. package/src/actions/public/getBlockNumber.ts +5 -3
  188. package/src/actions/public/getFilterChanges.ts +46 -10
  189. package/src/actions/public/getFilterLogs.ts +26 -10
  190. package/src/actions/public/getLogs.ts +73 -18
  191. package/src/actions/public/getTransaction.ts +15 -8
  192. package/src/actions/public/watchBlockNumber.ts +1 -1
  193. package/src/actions/public/watchBlocks.ts +37 -18
  194. package/src/actions/public/watchContractEvent.ts +9 -5
  195. package/src/actions/public/watchEvent.ts +54 -17
  196. package/src/clients/createClient.ts +9 -0
  197. package/src/clients/createPublicClient.ts +7 -1
  198. package/src/clients/createTestClient.ts +7 -1
  199. package/src/clients/createWalletClient.ts +7 -1
  200. package/src/clients/decorators/public.ts +114 -25
  201. package/src/constants/number.ts +98 -0
  202. package/src/errors/version.ts +1 -1
  203. package/src/index.ts +101 -1
  204. package/src/types/block.ts +25 -10
  205. package/src/types/contract.ts +7 -3
  206. package/src/types/filter.ts +32 -26
  207. package/src/types/formatter.ts +9 -0
  208. package/src/types/log.ts +13 -8
  209. package/src/types/misc.ts +3 -0
  210. package/src/types/rpc.ts +20 -6
  211. package/src/types/transaction.ts +22 -11
  212. package/src/utils/formatters/block.ts +31 -4
  213. package/src/utils/formatters/formatter.ts +1 -0
  214. package/src/utils/formatters/transaction.ts +25 -2
  215. package/src/utils/hash/hashFunction.ts +3 -2
  216. package/src/utils/promise/withCache.ts +5 -5
  217. package/src/utils/signature/hexToSignature.ts +20 -0
  218. package/src/utils/signature/signatureToHex.ts +26 -0
  219. package/dist/cjs/accounts/utils/signatureToHex.js.map +0 -1
  220. package/dist/esm/accounts/utils/signatureToHex.js +0 -7
  221. package/dist/esm/accounts/utils/signatureToHex.js.map +0 -1
  222. package/dist/types/accounts/utils/signatureToHex.d.ts +0 -3
  223. package/dist/types/accounts/utils/signatureToHex.d.ts.map +0 -1
  224. package/src/accounts/utils/signatureToHex.ts +0 -12
  225. /package/dist/cjs/{accounts/utils → utils/signature}/signatureToHex.js +0 -0
@@ -11,7 +11,7 @@ import {
11
11
  formatTransaction,
12
12
  } from '../../utils/formatters/transaction.js'
13
13
 
14
- export type GetTransactionParameters =
14
+ export type GetTransactionParameters<TBlockTag extends BlockTag = 'latest'> =
15
15
  | {
16
16
  /** The block hash */
17
17
  blockHash: Hash
@@ -34,7 +34,7 @@ export type GetTransactionParameters =
34
34
  blockHash?: never
35
35
  blockNumber?: never
36
36
  /** The block tag. */
37
- blockTag: BlockTag
37
+ blockTag: TBlockTag | BlockTag
38
38
  hash?: never
39
39
  /** The index of the transaction on the block. */
40
40
  index: number
@@ -48,8 +48,10 @@ export type GetTransactionParameters =
48
48
  index?: number
49
49
  }
50
50
 
51
- export type GetTransactionReturnType<TChain extends Chain | undefined = Chain> =
52
- FormattedTransaction<TChain>
51
+ export type GetTransactionReturnType<
52
+ TChain extends Chain | undefined = Chain,
53
+ TBlockTag extends BlockTag = 'latest',
54
+ > = FormattedTransaction<TChain, TBlockTag>
53
55
 
54
56
  /**
55
57
  * Returns information about a [Transaction](https://viem.sh/docs/glossary/terms.html#transaction) given a hash or block identifier.
@@ -75,16 +77,21 @@ export type GetTransactionReturnType<TChain extends Chain | undefined = Chain> =
75
77
  * hash: '0x4ca7ee652d57678f26e887c149ab0735f41de37bcad58c9f6d3ed5824f15b74d',
76
78
  * })
77
79
  */
78
- export async function getTransaction<TChain extends Chain | undefined>(
80
+ export async function getTransaction<
81
+ TChain extends Chain | undefined,
82
+ TBlockTag extends BlockTag = 'latest',
83
+ >(
79
84
  client: Client<Transport, TChain>,
80
85
  {
81
86
  blockHash,
82
87
  blockNumber,
83
- blockTag = 'latest',
88
+ blockTag: blockTag_,
84
89
  hash,
85
90
  index,
86
- }: GetTransactionParameters,
87
- ): Promise<GetTransactionReturnType<TChain>> {
91
+ }: GetTransactionParameters<TBlockTag>,
92
+ ): Promise<GetTransactionReturnType<TChain, TBlockTag>> {
93
+ const blockTag = blockTag_ || 'latest'
94
+
88
95
  const blockNumberHex =
89
96
  blockNumber !== undefined ? numberToHex(blockNumber) : undefined
90
97
 
@@ -105,7 +105,7 @@ export function watchBlockNumber<
105
105
  poll(
106
106
  async () => {
107
107
  try {
108
- const blockNumber = await getBlockNumber(client, { maxAge: 0 })
108
+ const blockNumber = await getBlockNumber(client, { cacheTime: 0 })
109
109
 
110
110
  if (prevBlockNumber) {
111
111
  // If the current block number is the same as the previous,
@@ -10,23 +10,35 @@ import { stringify } from '../../utils/stringify.js'
10
10
 
11
11
  import { type GetBlockReturnType, getBlock } from './getBlock.js'
12
12
 
13
- export type OnBlockParameter<TChain extends Chain | undefined = Chain> =
14
- GetBlockReturnType<TChain>
13
+ export type OnBlockParameter<
14
+ TChain extends Chain | undefined = Chain,
15
+ TIncludeTransactions extends boolean = false,
16
+ TBlockTag extends BlockTag = 'latest',
17
+ > = GetBlockReturnType<TChain, TIncludeTransactions, TBlockTag>
15
18
 
16
- export type OnBlock<TChain extends Chain | undefined = Chain> = (
17
- block: OnBlockParameter<TChain>,
18
- prevBlock: OnBlockParameter<TChain> | undefined,
19
+ export type OnBlock<
20
+ TChain extends Chain | undefined = Chain,
21
+ TIncludeTransactions extends boolean = false,
22
+ TBlockTag extends BlockTag = 'latest',
23
+ > = (
24
+ block: OnBlockParameter<TChain, TIncludeTransactions, TBlockTag>,
25
+ prevBlock:
26
+ | OnBlockParameter<TChain, TIncludeTransactions, TBlockTag>
27
+ | undefined,
19
28
  ) => void
20
29
 
21
- type PollOptions = {
30
+ type PollOptions<
31
+ TIncludeTransactions extends boolean = false,
32
+ TBlockTag extends BlockTag = 'latest',
33
+ > = {
22
34
  /** The block tag. Defaults to "latest". */
23
- blockTag?: BlockTag
35
+ blockTag?: TBlockTag | BlockTag
24
36
  /** Whether or not to emit the missed blocks to the callback. */
25
37
  emitMissed?: boolean
26
38
  /** Whether or not to emit the block to the callback when the subscription opens. */
27
39
  emitOnBegin?: boolean
28
40
  /** Whether or not to include transaction data in the response. */
29
- includeTransactions?: boolean
41
+ includeTransactions?: TIncludeTransactions
30
42
  /** Polling frequency (in ms). Defaults to the client's pollingInterval config. */
31
43
  pollingInterval?: number
32
44
  }
@@ -34,9 +46,11 @@ type PollOptions = {
34
46
  export type WatchBlocksParameters<
35
47
  TTransport extends Transport = Transport,
36
48
  TChain extends Chain | undefined = Chain,
49
+ TIncludeTransactions extends boolean = false,
50
+ TBlockTag extends BlockTag = 'latest',
37
51
  > = {
38
52
  /** The callback to call when a new block is received. */
39
- onBlock: OnBlock<TChain>
53
+ onBlock: OnBlock<TChain, TIncludeTransactions, TBlockTag>
40
54
  /** The callback to call when an error occurred when trying to get for a new block. */
41
55
  onError?: (error: Error) => void
42
56
  } & (GetTransportConfig<TTransport>['type'] extends 'webSocket'
@@ -50,8 +64,8 @@ export type WatchBlocksParameters<
50
64
  poll?: false
51
65
  pollingInterval?: never
52
66
  }
53
- | (PollOptions & { poll?: true })
54
- : PollOptions & { poll?: true })
67
+ | (PollOptions<TIncludeTransactions, TBlockTag> & { poll?: true })
68
+ : PollOptions<TIncludeTransactions, TBlockTag> & { poll?: true })
55
69
 
56
70
  export type WatchBlocksReturnType = () => void
57
71
 
@@ -83,6 +97,8 @@ export type WatchBlocksReturnType = () => void
83
97
  export function watchBlocks<
84
98
  TTransport extends Transport,
85
99
  TChain extends Chain | undefined,
100
+ TIncludeTransactions extends boolean = false,
101
+ TBlockTag extends BlockTag = 'latest',
86
102
  >(
87
103
  client: Client<TTransport, TChain>,
88
104
  {
@@ -91,15 +107,18 @@ export function watchBlocks<
91
107
  emitOnBegin = false,
92
108
  onBlock,
93
109
  onError,
94
- includeTransactions = false,
110
+ includeTransactions: includeTransactions_,
95
111
  poll: poll_,
96
112
  pollingInterval = client.pollingInterval,
97
- }: WatchBlocksParameters<TTransport, TChain>,
113
+ }: WatchBlocksParameters<TTransport, TChain, TIncludeTransactions, TBlockTag>,
98
114
  ): WatchBlocksReturnType {
99
115
  const enablePolling =
100
116
  typeof poll_ !== 'undefined' ? poll_ : client.transport.type !== 'webSocket'
117
+ const includeTransactions = includeTransactions_ ?? false
101
118
 
102
- let prevBlock: GetBlockReturnType<TChain> | undefined
119
+ let prevBlock:
120
+ | GetBlockReturnType<TChain, false | TIncludeTransactions, 'latest'>
121
+ | undefined
103
122
 
104
123
  const pollBlocks = () => {
105
124
  const observerId = stringify([
@@ -132,7 +151,7 @@ export function watchBlocks<
132
151
  blockNumber: i,
133
152
  includeTransactions,
134
153
  })
135
- emit.onBlock(block, prevBlock)
154
+ emit.onBlock(block as any, prevBlock as any)
136
155
  prevBlock = block
137
156
  }
138
157
  }
@@ -147,8 +166,8 @@ export function watchBlocks<
147
166
  // We don't want to emit blocks in the past.
148
167
  (block.number && block.number > prevBlock.number)
149
168
  ) {
150
- emit.onBlock(block, prevBlock)
151
- prevBlock = block
169
+ emit.onBlock(block as any, prevBlock as any)
170
+ prevBlock = block as any
152
171
  }
153
172
  } catch (err) {
154
173
  emit.onError?.(err as Error)
@@ -174,7 +193,7 @@ export function watchBlocks<
174
193
  const format =
175
194
  client.chain?.formatters?.block?.format || formatBlock
176
195
  const block = format(data.result)
177
- onBlock(block, prevBlock)
196
+ onBlock(block, prevBlock as any)
178
197
  prevBlock = block
179
198
  },
180
199
  onError(error: Error) {
@@ -1,4 +1,4 @@
1
- import type { Abi, Address, ExtractAbiEvent, Narrow } from 'abitype'
1
+ import type { Abi, AbiEvent, Address, ExtractAbiEvent, Narrow } from 'abitype'
2
2
 
3
3
  import type { Client } from '../../clients/createClient.js'
4
4
  import type { Transport } from '../../clients/transports/createTransport.js'
@@ -14,13 +14,14 @@ import { observe } from '../../utils/observe.js'
14
14
  import { poll } from '../../utils/poll.js'
15
15
  import { stringify } from '../../utils/stringify.js'
16
16
 
17
+ import { InvalidInputRpcError } from '../../index.js'
17
18
  import {
18
19
  type CreateContractEventFilterParameters,
19
20
  createContractEventFilter,
20
21
  } from './createContractEventFilter.js'
21
22
  import { getBlockNumber } from './getBlockNumber.js'
22
23
  import { getFilterChanges } from './getFilterChanges.js'
23
- import { type GetLogsParameters, getLogs } from './getLogs.js'
24
+ import { getLogs } from './getLogs.js'
24
25
  import { uninstallFilter } from './uninstallFilter.js'
25
26
 
26
27
  export type WatchContractEventOnLogsParameter<
@@ -28,7 +29,7 @@ export type WatchContractEventOnLogsParameter<
28
29
  TEventName extends string = string,
29
30
  TStrict extends boolean | undefined = undefined,
30
31
  > = TAbi extends Abi
31
- ? Log<bigint, number, ExtractAbiEvent<TAbi, TEventName>, TStrict>[]
32
+ ? Log<bigint, number, false, ExtractAbiEvent<TAbi, TEventName>, TStrict>[]
32
33
  : Log[]
33
34
  export type WatchContractEventOnLogsFn<
34
35
  TAbi extends Abi | readonly unknown[] = readonly unknown[],
@@ -173,8 +174,8 @@ export function watchContractEvent<
173
174
  event: getAbiItem({
174
175
  abi,
175
176
  name: eventName,
176
- } as unknown as GetAbiItemParameters),
177
- } as unknown as GetLogsParameters)
177
+ } as unknown as GetAbiItemParameters) as AbiEvent,
178
+ })
178
179
  } else {
179
180
  logs = []
180
181
  }
@@ -185,6 +186,9 @@ export function watchContractEvent<
185
186
  if (batch) emit.onLogs(logs as any)
186
187
  else logs.forEach((log) => emit.onLogs([log] as any))
187
188
  } catch (err) {
189
+ // If a filter has been set and gets uninstalled, providers will throw an InvalidInput error.
190
+ // Reinitalize the filter when this occurs
191
+ if (filter && err instanceof InvalidInputRpcError) initialized = false
188
192
  emit.onError?.(err as Error)
189
193
  }
190
194
  },
@@ -1,4 +1,4 @@
1
- import type { AbiEvent, Address } from 'abitype'
1
+ import type { AbiEvent, Address, Narrow } from 'abitype'
2
2
 
3
3
  import type { Client } from '../../clients/createClient.js'
4
4
  import type { Transport } from '../../clients/transports/createTransport.js'
@@ -13,30 +13,45 @@ import { observe } from '../../utils/observe.js'
13
13
  import { poll } from '../../utils/poll.js'
14
14
  import { stringify } from '../../utils/stringify.js'
15
15
 
16
+ import { InvalidInputRpcError } from '../../errors/rpc.js'
16
17
  import {
17
18
  type CreateEventFilterParameters,
18
19
  createEventFilter,
19
20
  } from './createEventFilter.js'
20
21
  import { getBlockNumber } from './getBlockNumber.js'
21
22
  import { getFilterChanges } from './getFilterChanges.js'
22
- import { getLogs } from './getLogs.js'
23
+ import { type GetLogsParameters, getLogs } from './getLogs.js'
23
24
  import { uninstallFilter } from './uninstallFilter.js'
24
25
 
25
26
  export type WatchEventOnLogsParameter<
26
27
  TAbiEvent extends AbiEvent | undefined = undefined,
28
+ TAbiEvents extends
29
+ | readonly AbiEvent[]
30
+ | readonly unknown[]
31
+ | undefined = TAbiEvent extends AbiEvent ? [TAbiEvent] : undefined,
27
32
  TStrict extends boolean | undefined = undefined,
28
33
  TEventName extends string | undefined = MaybeAbiEventName<TAbiEvent>,
29
- > = Log<bigint, number, TAbiEvent, TStrict, [TAbiEvent], TEventName>[]
34
+ > = Log<bigint, number, false, TAbiEvent, TStrict, TAbiEvents, TEventName>[]
30
35
  export type WatchEventOnLogsFn<
31
36
  TAbiEvent extends AbiEvent | undefined = undefined,
37
+ TAbiEvents extends
38
+ | readonly AbiEvent[]
39
+ | readonly unknown[]
40
+ | undefined = TAbiEvent extends AbiEvent ? [TAbiEvent] : undefined,
32
41
  TStrict extends boolean | undefined = undefined,
33
- TEventName extends string | undefined = MaybeAbiEventName<TAbiEvent>,
34
- > = (logs: WatchEventOnLogsParameter<TAbiEvent, TStrict, TEventName>) => void
42
+ _EventName extends string | undefined = MaybeAbiEventName<TAbiEvent>,
43
+ > = (
44
+ logs: WatchEventOnLogsParameter<TAbiEvent, TAbiEvents, TStrict, _EventName>,
45
+ ) => void
35
46
 
36
47
  export type WatchEventParameters<
37
48
  TAbiEvent extends AbiEvent | undefined = undefined,
49
+ TAbiEvents extends
50
+ | readonly AbiEvent[]
51
+ | readonly unknown[]
52
+ | undefined = TAbiEvent extends AbiEvent ? [TAbiEvent] : undefined,
38
53
  TStrict extends boolean | undefined = undefined,
39
- TEventName extends string | undefined = MaybeAbiEventName<TAbiEvent>,
54
+ _EventName extends string | undefined = MaybeAbiEventName<TAbiEvent>,
40
55
  > = {
41
56
  /** The address of the contract. */
42
57
  address?: Address | Address[]
@@ -48,13 +63,24 @@ export type WatchEventParameters<
48
63
  /** The callback to call when an error occurred when trying to get for a new block. */
49
64
  onError?: (error: Error) => void
50
65
  /** The callback to call when new event logs are received. */
51
- onLogs: WatchEventOnLogsFn<TAbiEvent, TStrict, TEventName>
66
+ onLogs: WatchEventOnLogsFn<TAbiEvent, TAbiEvents, TStrict, _EventName>
52
67
  /** Polling frequency (in ms). Defaults to Client's pollingInterval config. */
53
68
  pollingInterval?: number
54
69
  } & (
55
70
  | {
56
- event: TAbiEvent
57
- args?: MaybeExtractEventArgsFromAbi<[TAbiEvent], TEventName>
71
+ event: Narrow<TAbiEvent>
72
+ events?: never
73
+ args?: MaybeExtractEventArgsFromAbi<TAbiEvents, _EventName>
74
+ /**
75
+ * Whether or not the logs must match the indexed/non-indexed arguments on `event`.
76
+ * @default false
77
+ */
78
+ strict?: TStrict
79
+ }
80
+ | {
81
+ event?: never
82
+ events?: Narrow<TAbiEvents>
83
+ args?: never
58
84
  /**
59
85
  * Whether or not the logs must match the indexed/non-indexed arguments on `event`.
60
86
  * @default false
@@ -63,6 +89,7 @@ export type WatchEventParameters<
63
89
  }
64
90
  | {
65
91
  event?: never
92
+ events?: never
66
93
  args?: never
67
94
  strict?: never
68
95
  }
@@ -104,9 +131,13 @@ export type WatchEventReturnType = () => void
104
131
  */
105
132
  export function watchEvent<
106
133
  TChain extends Chain | undefined,
107
- TAbiEvent extends AbiEvent | undefined,
108
- TEventName extends string | undefined,
134
+ TAbiEvent extends AbiEvent | undefined = undefined,
135
+ TAbiEvents extends
136
+ | readonly AbiEvent[]
137
+ | readonly unknown[]
138
+ | undefined = TAbiEvent extends AbiEvent ? [TAbiEvent] : undefined,
109
139
  TStrict extends boolean | undefined = undefined,
140
+ _EventName extends string | undefined = undefined,
110
141
  >(
111
142
  client: Client<Transport, TChain>,
112
143
  {
@@ -114,11 +145,12 @@ export function watchEvent<
114
145
  args,
115
146
  batch = true,
116
147
  event,
148
+ events,
117
149
  onError,
118
150
  onLogs,
119
151
  pollingInterval = client.pollingInterval,
120
152
  strict: strict_,
121
- }: WatchEventParameters<TAbiEvent, TStrict>,
153
+ }: WatchEventParameters<TAbiEvent, TAbiEvents, TStrict>,
122
154
  ): WatchEventReturnType {
123
155
  const observerId = stringify([
124
156
  'watchEvent',
@@ -133,7 +165,7 @@ export function watchEvent<
133
165
 
134
166
  return observe(observerId, { onLogs, onError }, (emit) => {
135
167
  let previousBlockNumber: bigint
136
- let filter: Filter<'event', [TAbiEvent], TEventName, any>
168
+ let filter: Filter<'event', TAbiEvents, _EventName, any>
137
169
  let initialized = false
138
170
 
139
171
  const unwatch = poll(
@@ -144,11 +176,12 @@ export function watchEvent<
144
176
  address,
145
177
  args,
146
178
  event: event!,
179
+ events,
147
180
  strict,
148
181
  } as unknown as CreateEventFilterParameters)) as unknown as Filter<
149
182
  'event',
150
- [TAbiEvent],
151
- TEventName
183
+ TAbiEvents,
184
+ _EventName
152
185
  >
153
186
  } catch {}
154
187
  initialized = true
@@ -173,10 +206,11 @@ export function watchEvent<
173
206
  logs = await getLogs(client, {
174
207
  address,
175
208
  args,
209
+ event: event!,
210
+ events,
176
211
  fromBlock: previousBlockNumber + 1n,
177
212
  toBlock: blockNumber,
178
- event: event!,
179
- })
213
+ } as unknown as GetLogsParameters)
180
214
  } else {
181
215
  logs = []
182
216
  }
@@ -187,6 +221,9 @@ export function watchEvent<
187
221
  if (batch) emit.onLogs(logs as any)
188
222
  else logs.forEach((log) => emit.onLogs([log] as any))
189
223
  } catch (err) {
224
+ // If a filter has been set and gets uninstalled, providers will throw an InvalidInput error.
225
+ // Reinitalize the filter when this occurs
226
+ if (filter && err instanceof InvalidInputRpcError) initialized = false
190
227
  emit.onError?.(err as Error)
191
228
  }
192
229
  },
@@ -29,6 +29,11 @@ export type ClientConfig<
29
29
  multicall?: boolean | Prettify<MulticallBatchOptions> | undefined
30
30
  }
31
31
  | undefined
32
+ /**
33
+ * Time (in ms) that cached data will remain in memory.
34
+ * @default 4_000
35
+ */
36
+ cacheTime?: number | undefined
32
37
  /** Chain for the client. */
33
38
  chain?: Chain | undefined | chain
34
39
  /** A key for the client. */
@@ -79,6 +84,8 @@ type Client_Base<
79
84
  account: account
80
85
  /** Flags for batch settings. */
81
86
  batch?: ClientConfig['batch']
87
+ /** Time (in ms) that cached data will remain in memory. */
88
+ cacheTime: number
82
89
  /** Chain for the client. */
83
90
  chain: chain
84
91
  /** A key for the client. */
@@ -135,6 +142,7 @@ export function createClient<
135
142
  export function createClient(parameters: ClientConfig): Client {
136
143
  const {
137
144
  batch,
145
+ cacheTime = parameters.pollingInterval ?? 4_000,
138
146
  key = 'base',
139
147
  name = 'Base Client',
140
148
  pollingInterval = 4_000,
@@ -154,6 +162,7 @@ export function createClient(parameters: ClientConfig): Client {
154
162
  const client = {
155
163
  account,
156
164
  batch,
165
+ cacheTime,
157
166
  chain,
158
167
  key,
159
168
  name,
@@ -11,7 +11,13 @@ export type PublicClientConfig<
11
11
  > = Prettify<
12
12
  Pick<
13
13
  ClientConfig<transport, chain>,
14
- 'batch' | 'chain' | 'key' | 'name' | 'pollingInterval' | 'transport'
14
+ | 'batch'
15
+ | 'cacheTime'
16
+ | 'chain'
17
+ | 'key'
18
+ | 'name'
19
+ | 'pollingInterval'
20
+ | 'transport'
15
21
  >
16
22
  >
17
23
 
@@ -21,7 +21,13 @@ export type TestClientConfig<
21
21
  > = Prettify<
22
22
  Pick<
23
23
  ClientConfig<transport, chain, accountOrAddress>,
24
- 'account' | 'chain' | 'key' | 'name' | 'pollingInterval' | 'transport'
24
+ | 'account'
25
+ | 'cacheTime'
26
+ | 'chain'
27
+ | 'key'
28
+ | 'name'
29
+ | 'pollingInterval'
30
+ | 'transport'
25
31
  > & {
26
32
  /** Mode of the test client. */
27
33
  mode: mode | ('anvil' | 'hardhat' | 'ganache') // TODO: Type utility that expands `TestClientMode`
@@ -19,7 +19,13 @@ export type WalletClientConfig<
19
19
  > = Prettify<
20
20
  Pick<
21
21
  ClientConfig<transport, chain, accountOrAddress>,
22
- 'account' | 'chain' | 'key' | 'name' | 'pollingInterval' | 'transport'
22
+ | 'account'
23
+ | 'cacheTime'
24
+ | 'chain'
25
+ | 'key'
26
+ | 'name'
27
+ | 'pollingInterval'
28
+ | 'transport'
23
29
  >
24
30
  >
25
31