viem 1.4.1 → 1.5.0

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 (246) hide show
  1. package/README.md +6 -0
  2. package/dist/cjs/accounts/index.js +1 -1
  3. package/dist/cjs/accounts/index.js.map +1 -1
  4. package/dist/cjs/accounts/utils/signMessage.js +1 -1
  5. package/dist/cjs/accounts/utils/signMessage.js.map +1 -1
  6. package/dist/cjs/accounts/utils/signTypedData.js +1 -1
  7. package/dist/cjs/accounts/utils/signTypedData.js.map +1 -1
  8. package/dist/cjs/actions/index.js.map +1 -1
  9. package/dist/cjs/actions/public/createContractEventFilter.js.map +1 -1
  10. package/dist/cjs/actions/public/createEventFilter.js +16 -8
  11. package/dist/cjs/actions/public/createEventFilter.js.map +1 -1
  12. package/dist/cjs/actions/public/getBlock.js +3 -1
  13. package/dist/cjs/actions/public/getBlock.js.map +1 -1
  14. package/dist/cjs/actions/public/getBlockNumber.js +2 -2
  15. package/dist/cjs/actions/public/getBlockNumber.js.map +1 -1
  16. package/dist/cjs/actions/public/getFilterChanges.js.map +1 -1
  17. package/dist/cjs/actions/public/getFilterLogs.js +1 -1
  18. package/dist/cjs/actions/public/getFilterLogs.js.map +1 -1
  19. package/dist/cjs/actions/public/getLogs.js +16 -10
  20. package/dist/cjs/actions/public/getLogs.js.map +1 -1
  21. package/dist/cjs/actions/public/getTransaction.js +2 -1
  22. package/dist/cjs/actions/public/getTransaction.js.map +1 -1
  23. package/dist/cjs/actions/public/watchBlockNumber.js +1 -1
  24. package/dist/cjs/actions/public/watchBlockNumber.js.map +1 -1
  25. package/dist/cjs/actions/public/watchBlocks.js +2 -1
  26. package/dist/cjs/actions/public/watchBlocks.js.map +1 -1
  27. package/dist/cjs/actions/public/watchContractEvent.js.map +1 -1
  28. package/dist/cjs/actions/public/watchEvent.js +4 -2
  29. package/dist/cjs/actions/public/watchEvent.js.map +1 -1
  30. package/dist/cjs/clients/createClient.js +2 -1
  31. package/dist/cjs/clients/createClient.js.map +1 -1
  32. package/dist/cjs/clients/createPublicClient.js.map +1 -1
  33. package/dist/cjs/clients/createTestClient.js.map +1 -1
  34. package/dist/cjs/clients/createWalletClient.js.map +1 -1
  35. package/dist/cjs/clients/decorators/public.js.map +1 -1
  36. package/dist/cjs/constants/number.js +101 -0
  37. package/dist/cjs/constants/number.js.map +1 -0
  38. package/dist/cjs/contract.js.map +1 -1
  39. package/dist/cjs/errors/abi.js +8 -1
  40. package/dist/cjs/errors/abi.js.map +1 -1
  41. package/dist/cjs/errors/contract.js +56 -32
  42. package/dist/cjs/errors/contract.js.map +1 -1
  43. package/dist/cjs/errors/version.js +1 -1
  44. package/dist/cjs/index.js +110 -7
  45. package/dist/cjs/index.js.map +1 -1
  46. package/dist/cjs/public.js.map +1 -1
  47. package/dist/cjs/utils/formatters/block.js.map +1 -1
  48. package/dist/cjs/utils/formatters/formatter.js +1 -0
  49. package/dist/cjs/utils/formatters/formatter.js.map +1 -1
  50. package/dist/cjs/utils/formatters/transaction.js.map +1 -1
  51. package/dist/cjs/utils/promise/withCache.js +3 -3
  52. package/dist/cjs/utils/promise/withCache.js.map +1 -1
  53. package/dist/cjs/utils/signature/hexToSignature.js +12 -0
  54. package/dist/cjs/utils/signature/hexToSignature.js.map +1 -0
  55. package/dist/cjs/utils/signature/signatureToHex.js.map +1 -0
  56. package/dist/esm/accounts/index.js +1 -1
  57. package/dist/esm/accounts/index.js.map +1 -1
  58. package/dist/esm/accounts/utils/signMessage.js +1 -1
  59. package/dist/esm/accounts/utils/signMessage.js.map +1 -1
  60. package/dist/esm/accounts/utils/signTypedData.js +1 -1
  61. package/dist/esm/accounts/utils/signTypedData.js.map +1 -1
  62. package/dist/esm/actions/index.js.map +1 -1
  63. package/dist/esm/actions/public/createContractEventFilter.js.map +1 -1
  64. package/dist/esm/actions/public/createEventFilter.js +16 -8
  65. package/dist/esm/actions/public/createEventFilter.js.map +1 -1
  66. package/dist/esm/actions/public/getBlock.js +3 -1
  67. package/dist/esm/actions/public/getBlock.js.map +1 -1
  68. package/dist/esm/actions/public/getBlockNumber.js +2 -2
  69. package/dist/esm/actions/public/getBlockNumber.js.map +1 -1
  70. package/dist/esm/actions/public/getFilterChanges.js.map +1 -1
  71. package/dist/esm/actions/public/getFilterLogs.js +1 -1
  72. package/dist/esm/actions/public/getFilterLogs.js.map +1 -1
  73. package/dist/esm/actions/public/getLogs.js +16 -10
  74. package/dist/esm/actions/public/getLogs.js.map +1 -1
  75. package/dist/esm/actions/public/getTransaction.js +2 -1
  76. package/dist/esm/actions/public/getTransaction.js.map +1 -1
  77. package/dist/esm/actions/public/watchBlockNumber.js +1 -1
  78. package/dist/esm/actions/public/watchBlockNumber.js.map +1 -1
  79. package/dist/esm/actions/public/watchBlocks.js +2 -1
  80. package/dist/esm/actions/public/watchBlocks.js.map +1 -1
  81. package/dist/esm/actions/public/watchContractEvent.js.map +1 -1
  82. package/dist/esm/actions/public/watchEvent.js +4 -2
  83. package/dist/esm/actions/public/watchEvent.js.map +1 -1
  84. package/dist/esm/clients/createClient.js +2 -1
  85. package/dist/esm/clients/createClient.js.map +1 -1
  86. package/dist/esm/clients/createPublicClient.js.map +1 -1
  87. package/dist/esm/clients/createTestClient.js.map +1 -1
  88. package/dist/esm/clients/createWalletClient.js.map +1 -1
  89. package/dist/esm/clients/decorators/public.js.map +1 -1
  90. package/dist/esm/constants/number.js +97 -0
  91. package/dist/esm/constants/number.js.map +1 -0
  92. package/dist/esm/contract.js.map +1 -1
  93. package/dist/esm/errors/abi.js +8 -1
  94. package/dist/esm/errors/abi.js.map +1 -1
  95. package/dist/esm/errors/contract.js +56 -32
  96. package/dist/esm/errors/contract.js.map +1 -1
  97. package/dist/esm/errors/version.js +1 -1
  98. package/dist/esm/index.js +4 -1
  99. package/dist/esm/index.js.map +1 -1
  100. package/dist/esm/public.js.map +1 -1
  101. package/dist/esm/utils/formatters/block.js.map +1 -1
  102. package/dist/esm/utils/formatters/formatter.js +1 -0
  103. package/dist/esm/utils/formatters/formatter.js.map +1 -1
  104. package/dist/esm/utils/formatters/transaction.js.map +1 -1
  105. package/dist/esm/utils/promise/withCache.js +3 -3
  106. package/dist/esm/utils/promise/withCache.js.map +1 -1
  107. package/dist/esm/utils/signature/hexToSignature.js +18 -0
  108. package/dist/esm/utils/signature/hexToSignature.js.map +1 -0
  109. package/dist/esm/utils/signature/signatureToHex.js +21 -0
  110. package/dist/esm/utils/signature/signatureToHex.js.map +1 -0
  111. package/dist/types/accounts/index.d.ts +1 -1
  112. package/dist/types/accounts/index.d.ts.map +1 -1
  113. package/dist/types/actions/index.d.ts +5 -1
  114. package/dist/types/actions/index.d.ts.map +1 -1
  115. package/dist/types/actions/public/createContractEventFilter.d.ts +5 -5
  116. package/dist/types/actions/public/createContractEventFilter.d.ts.map +1 -1
  117. package/dist/types/actions/public/createEventFilter.d.ts +20 -7
  118. package/dist/types/actions/public/createEventFilter.d.ts.map +1 -1
  119. package/dist/types/actions/public/getBlock.d.ts +5 -5
  120. package/dist/types/actions/public/getBlock.d.ts.map +1 -1
  121. package/dist/types/actions/public/getBlockNumber.d.ts +4 -2
  122. package/dist/types/actions/public/getBlockNumber.d.ts.map +1 -1
  123. package/dist/types/actions/public/getFilterChanges.d.ts +5 -8
  124. package/dist/types/actions/public/getFilterChanges.d.ts.map +1 -1
  125. package/dist/types/actions/public/getFilterLogs.d.ts +5 -4
  126. package/dist/types/actions/public/getFilterLogs.d.ts.map +1 -1
  127. package/dist/types/actions/public/getLogs.d.ts +17 -6
  128. package/dist/types/actions/public/getLogs.d.ts.map +1 -1
  129. package/dist/types/actions/public/getTransaction.d.ts +4 -4
  130. package/dist/types/actions/public/getTransaction.d.ts.map +1 -1
  131. package/dist/types/actions/public/watchBlocks.d.ts +10 -10
  132. package/dist/types/actions/public/watchBlocks.d.ts.map +1 -1
  133. package/dist/types/actions/public/watchContractEvent.d.ts +3 -3
  134. package/dist/types/actions/public/watchContractEvent.d.ts.map +1 -1
  135. package/dist/types/actions/public/watchEvent.d.ts +19 -8
  136. package/dist/types/actions/public/watchEvent.d.ts.map +1 -1
  137. package/dist/types/chains/formatters/celo.d.ts +4 -0
  138. package/dist/types/chains/formatters/celo.d.ts.map +1 -1
  139. package/dist/types/chains/formatters/optimism.d.ts +5 -3
  140. package/dist/types/chains/formatters/optimism.d.ts.map +1 -1
  141. package/dist/types/chains/index.d.ts +44 -12
  142. package/dist/types/chains/index.d.ts.map +1 -1
  143. package/dist/types/clients/createClient.d.ts +7 -0
  144. package/dist/types/clients/createClient.d.ts.map +1 -1
  145. package/dist/types/clients/createPublicClient.d.ts +1 -1
  146. package/dist/types/clients/createPublicClient.d.ts.map +1 -1
  147. package/dist/types/clients/createTestClient.d.ts +1 -1
  148. package/dist/types/clients/createTestClient.d.ts.map +1 -1
  149. package/dist/types/clients/createWalletClient.d.ts +1 -1
  150. package/dist/types/clients/createWalletClient.d.ts.map +1 -1
  151. package/dist/types/clients/decorators/public.d.ts +10 -9
  152. package/dist/types/clients/decorators/public.d.ts.map +1 -1
  153. package/dist/types/constants/number.d.ts +97 -0
  154. package/dist/types/constants/number.d.ts.map +1 -0
  155. package/dist/types/contract.d.ts +5 -1
  156. package/dist/types/contract.d.ts.map +1 -1
  157. package/dist/types/errors/abi.d.ts +1 -0
  158. package/dist/types/errors/abi.d.ts.map +1 -1
  159. package/dist/types/errors/contract.d.ts +1 -0
  160. package/dist/types/errors/contract.d.ts.map +1 -1
  161. package/dist/types/errors/version.d.ts +1 -1
  162. package/dist/types/index.d.ts +10 -3
  163. package/dist/types/index.d.ts.map +1 -1
  164. package/dist/types/public.d.ts +5 -1
  165. package/dist/types/public.d.ts.map +1 -1
  166. package/dist/types/types/block.d.ts +7 -7
  167. package/dist/types/types/block.d.ts.map +1 -1
  168. package/dist/types/types/contract.d.ts +1 -1
  169. package/dist/types/types/contract.d.ts.map +1 -1
  170. package/dist/types/types/filter.d.ts +7 -3
  171. package/dist/types/types/filter.d.ts.map +1 -1
  172. package/dist/types/types/formatter.d.ts +3 -0
  173. package/dist/types/types/formatter.d.ts.map +1 -1
  174. package/dist/types/types/log.d.ts +8 -8
  175. package/dist/types/types/log.d.ts.map +1 -1
  176. package/dist/types/types/misc.d.ts.map +1 -1
  177. package/dist/types/types/rpc.d.ts +3 -3
  178. package/dist/types/types/rpc.d.ts.map +1 -1
  179. package/dist/types/types/transaction.d.ts +8 -8
  180. package/dist/types/types/transaction.d.ts.map +1 -1
  181. package/dist/types/utils/formatters/block.d.ts +13 -3
  182. package/dist/types/utils/formatters/block.d.ts.map +1 -1
  183. package/dist/types/utils/formatters/formatter.d.ts +1 -0
  184. package/dist/types/utils/formatters/formatter.d.ts.map +1 -1
  185. package/dist/types/utils/formatters/transaction.d.ts +10 -3
  186. package/dist/types/utils/formatters/transaction.d.ts.map +1 -1
  187. package/dist/types/utils/formatters/transactionReceipt.d.ts +1 -0
  188. package/dist/types/utils/formatters/transactionReceipt.d.ts.map +1 -1
  189. package/dist/types/utils/formatters/transactionRequest.d.ts +1 -0
  190. package/dist/types/utils/formatters/transactionRequest.d.ts.map +1 -1
  191. package/dist/types/utils/promise/withCache.d.ts +3 -3
  192. package/dist/types/utils/promise/withCache.d.ts.map +1 -1
  193. package/dist/types/utils/signature/hexToSignature.d.ts +13 -0
  194. package/dist/types/utils/signature/hexToSignature.d.ts.map +1 -0
  195. package/dist/types/utils/signature/signatureToHex.d.ts +17 -0
  196. package/dist/types/utils/signature/signatureToHex.d.ts.map +1 -0
  197. package/package.json +1 -1
  198. package/src/accounts/index.ts +1 -1
  199. package/src/accounts/utils/signMessage.ts +1 -1
  200. package/src/accounts/utils/signTypedData.ts +1 -1
  201. package/src/actions/index.ts +6 -2
  202. package/src/actions/public/createContractEventFilter.ts +30 -7
  203. package/src/actions/public/createEventFilter.ts +78 -24
  204. package/src/actions/public/getBlock.ts +18 -8
  205. package/src/actions/public/getBlockNumber.ts +5 -3
  206. package/src/actions/public/getFilterChanges.ts +46 -10
  207. package/src/actions/public/getFilterLogs.ts +26 -10
  208. package/src/actions/public/getLogs.ts +73 -18
  209. package/src/actions/public/getTransaction.ts +15 -8
  210. package/src/actions/public/watchBlockNumber.ts +1 -1
  211. package/src/actions/public/watchBlocks.ts +37 -18
  212. package/src/actions/public/watchContractEvent.ts +9 -9
  213. package/src/actions/public/watchEvent.ts +52 -19
  214. package/src/clients/createClient.ts +9 -0
  215. package/src/clients/createPublicClient.ts +7 -1
  216. package/src/clients/createTestClient.ts +7 -1
  217. package/src/clients/createWalletClient.ts +7 -1
  218. package/src/clients/decorators/public.ts +114 -25
  219. package/src/constants/number.ts +98 -0
  220. package/src/contract.ts +6 -2
  221. package/src/errors/abi.ts +5 -1
  222. package/src/errors/contract.ts +52 -31
  223. package/src/errors/version.ts +1 -1
  224. package/src/index.ts +108 -3
  225. package/src/public.ts +6 -2
  226. package/src/types/block.ts +25 -10
  227. package/src/types/contract.ts +7 -3
  228. package/src/types/filter.ts +32 -26
  229. package/src/types/formatter.ts +9 -0
  230. package/src/types/log.ts +13 -8
  231. package/src/types/misc.ts +3 -0
  232. package/src/types/rpc.ts +20 -6
  233. package/src/types/transaction.ts +22 -11
  234. package/src/utils/formatters/block.ts +31 -4
  235. package/src/utils/formatters/formatter.ts +1 -0
  236. package/src/utils/formatters/transaction.ts +25 -2
  237. package/src/utils/promise/withCache.ts +5 -5
  238. package/src/utils/signature/hexToSignature.ts +20 -0
  239. package/src/utils/signature/signatureToHex.ts +26 -0
  240. package/dist/cjs/accounts/utils/signatureToHex.js.map +0 -1
  241. package/dist/esm/accounts/utils/signatureToHex.js +0 -7
  242. package/dist/esm/accounts/utils/signatureToHex.js.map +0 -1
  243. package/dist/types/accounts/utils/signatureToHex.d.ts +0 -3
  244. package/dist/types/accounts/utils/signatureToHex.d.ts.map +0 -1
  245. package/src/accounts/utils/signatureToHex.ts +0 -12
  246. /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'
@@ -20,21 +20,21 @@ import {
20
20
  } from './createContractEventFilter.js'
21
21
  import { getBlockNumber } from './getBlockNumber.js'
22
22
  import { getFilterChanges } from './getFilterChanges.js'
23
- import { type GetLogsParameters, getLogs } from './getLogs.js'
23
+ import { getLogs } from './getLogs.js'
24
24
  import { uninstallFilter } from './uninstallFilter.js'
25
25
 
26
- export type OnLogsParameter<
26
+ export type WatchContractEventOnLogsParameter<
27
27
  TAbi extends Abi | readonly unknown[] = readonly unknown[],
28
28
  TEventName extends string = string,
29
29
  TStrict extends boolean | undefined = undefined,
30
30
  > = TAbi extends Abi
31
- ? Log<bigint, number, ExtractAbiEvent<TAbi, TEventName>, TStrict>[]
31
+ ? Log<bigint, number, false, ExtractAbiEvent<TAbi, TEventName>, TStrict>[]
32
32
  : Log[]
33
- export type OnLogsFn<
33
+ export type WatchContractEventOnLogsFn<
34
34
  TAbi extends Abi | readonly unknown[] = readonly unknown[],
35
35
  TEventName extends string = string,
36
36
  TStrict extends boolean | undefined = undefined,
37
- > = (logs: OnLogsParameter<TAbi, TEventName, TStrict>) => void
37
+ > = (logs: WatchContractEventOnLogsParameter<TAbi, TEventName, TStrict>) => void
38
38
 
39
39
  export type WatchContractEventParameters<
40
40
  TAbi extends Abi | readonly unknown[] = readonly unknown[],
@@ -53,7 +53,7 @@ export type WatchContractEventParameters<
53
53
  /** The callback to call when an error occurred when trying to get for a new block. */
54
54
  onError?: (error: Error) => void
55
55
  /** The callback to call when new event logs are received. */
56
- onLogs: OnLogsFn<TAbi, TEventName, TStrict>
56
+ onLogs: WatchContractEventOnLogsFn<TAbi, TEventName, TStrict>
57
57
  /** Polling frequency (in ms). Defaults to Client's pollingInterval config. */
58
58
  pollingInterval?: number
59
59
  /**
@@ -173,8 +173,8 @@ export function watchContractEvent<
173
173
  event: getAbiItem({
174
174
  abi,
175
175
  name: eventName,
176
- } as unknown as GetAbiItemParameters),
177
- } as unknown as GetLogsParameters)
176
+ } as unknown as GetAbiItemParameters) as AbiEvent,
177
+ })
178
178
  } else {
179
179
  logs = []
180
180
  }
@@ -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'
@@ -19,24 +19,38 @@ import {
19
19
  } from './createEventFilter.js'
20
20
  import { getBlockNumber } from './getBlockNumber.js'
21
21
  import { getFilterChanges } from './getFilterChanges.js'
22
- import { getLogs } from './getLogs.js'
22
+ import { type GetLogsParameters, getLogs } from './getLogs.js'
23
23
  import { uninstallFilter } from './uninstallFilter.js'
24
24
 
25
- export type OnLogsParameter<
25
+ export type WatchEventOnLogsParameter<
26
26
  TAbiEvent extends AbiEvent | undefined = undefined,
27
+ TAbiEvents extends
28
+ | readonly AbiEvent[]
29
+ | readonly unknown[]
30
+ | undefined = TAbiEvent extends AbiEvent ? [TAbiEvent] : undefined,
27
31
  TStrict extends boolean | undefined = undefined,
28
32
  TEventName extends string | undefined = MaybeAbiEventName<TAbiEvent>,
29
- > = Log<bigint, number, TAbiEvent, TStrict, [TAbiEvent], TEventName>[]
30
- export type OnLogsFn<
33
+ > = Log<bigint, number, false, TAbiEvent, TStrict, TAbiEvents, TEventName>[]
34
+ export type WatchEventOnLogsFn<
31
35
  TAbiEvent extends AbiEvent | undefined = undefined,
36
+ TAbiEvents extends
37
+ | readonly AbiEvent[]
38
+ | readonly unknown[]
39
+ | undefined = TAbiEvent extends AbiEvent ? [TAbiEvent] : undefined,
32
40
  TStrict extends boolean | undefined = undefined,
33
- TEventName extends string | undefined = MaybeAbiEventName<TAbiEvent>,
34
- > = (logs: OnLogsParameter<TAbiEvent, TStrict, TEventName>) => void
41
+ _EventName extends string | undefined = MaybeAbiEventName<TAbiEvent>,
42
+ > = (
43
+ logs: WatchEventOnLogsParameter<TAbiEvent, TAbiEvents, TStrict, _EventName>,
44
+ ) => void
35
45
 
36
46
  export type WatchEventParameters<
37
47
  TAbiEvent extends AbiEvent | undefined = undefined,
48
+ TAbiEvents extends
49
+ | readonly AbiEvent[]
50
+ | readonly unknown[]
51
+ | undefined = TAbiEvent extends AbiEvent ? [TAbiEvent] : undefined,
38
52
  TStrict extends boolean | undefined = undefined,
39
- TEventName extends string | undefined = MaybeAbiEventName<TAbiEvent>,
53
+ _EventName extends string | undefined = MaybeAbiEventName<TAbiEvent>,
40
54
  > = {
41
55
  /** The address of the contract. */
42
56
  address?: Address | Address[]
@@ -48,13 +62,24 @@ export type WatchEventParameters<
48
62
  /** The callback to call when an error occurred when trying to get for a new block. */
49
63
  onError?: (error: Error) => void
50
64
  /** The callback to call when new event logs are received. */
51
- onLogs: OnLogsFn<TAbiEvent, TStrict, TEventName>
65
+ onLogs: WatchEventOnLogsFn<TAbiEvent, TAbiEvents, TStrict, _EventName>
52
66
  /** Polling frequency (in ms). Defaults to Client's pollingInterval config. */
53
67
  pollingInterval?: number
54
68
  } & (
55
69
  | {
56
- event: TAbiEvent
57
- args?: MaybeExtractEventArgsFromAbi<[TAbiEvent], TEventName>
70
+ event: Narrow<TAbiEvent>
71
+ events?: never
72
+ args?: MaybeExtractEventArgsFromAbi<TAbiEvents, _EventName>
73
+ /**
74
+ * Whether or not the logs must match the indexed/non-indexed arguments on `event`.
75
+ * @default false
76
+ */
77
+ strict?: TStrict
78
+ }
79
+ | {
80
+ event?: never
81
+ events?: Narrow<TAbiEvents>
82
+ args?: never
58
83
  /**
59
84
  * Whether or not the logs must match the indexed/non-indexed arguments on `event`.
60
85
  * @default false
@@ -63,6 +88,7 @@ export type WatchEventParameters<
63
88
  }
64
89
  | {
65
90
  event?: never
91
+ events?: never
66
92
  args?: never
67
93
  strict?: never
68
94
  }
@@ -104,9 +130,13 @@ export type WatchEventReturnType = () => void
104
130
  */
105
131
  export function watchEvent<
106
132
  TChain extends Chain | undefined,
107
- TAbiEvent extends AbiEvent | undefined,
108
- TEventName extends string | undefined,
133
+ TAbiEvent extends AbiEvent | undefined = undefined,
134
+ TAbiEvents extends
135
+ | readonly AbiEvent[]
136
+ | readonly unknown[]
137
+ | undefined = TAbiEvent extends AbiEvent ? [TAbiEvent] : undefined,
109
138
  TStrict extends boolean | undefined = undefined,
139
+ _EventName extends string | undefined = undefined,
110
140
  >(
111
141
  client: Client<Transport, TChain>,
112
142
  {
@@ -114,11 +144,12 @@ export function watchEvent<
114
144
  args,
115
145
  batch = true,
116
146
  event,
147
+ events,
117
148
  onError,
118
149
  onLogs,
119
150
  pollingInterval = client.pollingInterval,
120
151
  strict: strict_,
121
- }: WatchEventParameters<TAbiEvent, TStrict>,
152
+ }: WatchEventParameters<TAbiEvent, TAbiEvents, TStrict>,
122
153
  ): WatchEventReturnType {
123
154
  const observerId = stringify([
124
155
  'watchEvent',
@@ -133,7 +164,7 @@ export function watchEvent<
133
164
 
134
165
  return observe(observerId, { onLogs, onError }, (emit) => {
135
166
  let previousBlockNumber: bigint
136
- let filter: Filter<'event', [TAbiEvent], TEventName, any>
167
+ let filter: Filter<'event', TAbiEvents, _EventName, any>
137
168
  let initialized = false
138
169
 
139
170
  const unwatch = poll(
@@ -144,11 +175,12 @@ export function watchEvent<
144
175
  address,
145
176
  args,
146
177
  event: event!,
178
+ events,
147
179
  strict,
148
180
  } as unknown as CreateEventFilterParameters)) as unknown as Filter<
149
181
  'event',
150
- [TAbiEvent],
151
- TEventName
182
+ TAbiEvents,
183
+ _EventName
152
184
  >
153
185
  } catch {}
154
186
  initialized = true
@@ -173,10 +205,11 @@ export function watchEvent<
173
205
  logs = await getLogs(client, {
174
206
  address,
175
207
  args,
208
+ event: event!,
209
+ events,
176
210
  fromBlock: previousBlockNumber + 1n,
177
211
  toBlock: blockNumber,
178
- event: event!,
179
- })
212
+ } as unknown as GetLogsParameters)
180
213
  } else {
181
214
  logs = []
182
215
  }
@@ -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