viem 2.57.13 → 2.57.15

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 (304) hide show
  1. package/CHANGELOG.md +60 -0
  2. package/_cjs/account-abstraction/utils/formatters/userOperationReceipt.js +2 -0
  3. package/_cjs/account-abstraction/utils/formatters/userOperationReceipt.js.map +1 -1
  4. package/_cjs/actions/public/estimateGas.js +5 -2
  5. package/_cjs/actions/public/estimateGas.js.map +1 -1
  6. package/_cjs/actions/public/fillTransaction.js +2 -0
  7. package/_cjs/actions/public/fillTransaction.js.map +1 -1
  8. package/_cjs/actions/public/getFilterChanges.js +1 -0
  9. package/_cjs/actions/public/getFilterChanges.js.map +1 -1
  10. package/_cjs/actions/public/getFilterLogs.js +1 -0
  11. package/_cjs/actions/public/getFilterLogs.js.map +1 -1
  12. package/_cjs/actions/public/watchBlockNumber.js +3 -1
  13. package/_cjs/actions/public/watchBlockNumber.js.map +1 -1
  14. package/_cjs/actions/public/watchBlocks.js +20 -8
  15. package/_cjs/actions/public/watchBlocks.js.map +1 -1
  16. package/_cjs/actions/public/watchContractEvent.js +10 -3
  17. package/_cjs/actions/public/watchContractEvent.js.map +1 -1
  18. package/_cjs/actions/public/watchEvent.js +5 -0
  19. package/_cjs/actions/public/watchEvent.js.map +1 -1
  20. package/_cjs/actions/wallet/prepareTransactionRequest.js +3 -2
  21. package/_cjs/actions/wallet/prepareTransactionRequest.js.map +1 -1
  22. package/_cjs/actions/wallet/sendTransaction.js +4 -2
  23. package/_cjs/actions/wallet/sendTransaction.js.map +1 -1
  24. package/_cjs/actions/wallet/sendTransactionSync.js +48 -7
  25. package/_cjs/actions/wallet/sendTransactionSync.js.map +1 -1
  26. package/_cjs/chains/definitions/hashKeyChain.js +4 -4
  27. package/_cjs/chains/definitions/hashKeyChain.js.map +1 -1
  28. package/_cjs/chains/definitions/hashkeyChainTestnet.js +4 -4
  29. package/_cjs/chains/definitions/hashkeyChainTestnet.js.map +1 -1
  30. package/_cjs/chains/definitions/turkchain.js +26 -0
  31. package/_cjs/chains/definitions/turkchain.js.map +1 -0
  32. package/_cjs/chains/definitions/zksync.js +1 -1
  33. package/_cjs/chains/definitions/zksync.js.map +1 -1
  34. package/_cjs/chains/index.js +4 -2
  35. package/_cjs/chains/index.js.map +1 -1
  36. package/_cjs/clients/createClient.js.map +1 -1
  37. package/_cjs/clients/transports/fallback.js +5 -0
  38. package/_cjs/clients/transports/fallback.js.map +1 -1
  39. package/_cjs/errors/node.js +10 -4
  40. package/_cjs/errors/node.js.map +1 -1
  41. package/_cjs/errors/typedData.js +4 -2
  42. package/_cjs/errors/typedData.js.map +1 -1
  43. package/_cjs/errors/version.js +1 -1
  44. package/_cjs/index.js +5 -4
  45. package/_cjs/index.js.map +1 -1
  46. package/_cjs/tempo/Account.js +85 -12
  47. package/_cjs/tempo/Account.js.map +1 -1
  48. package/_cjs/tempo/Formatters.js +4 -1
  49. package/_cjs/tempo/Formatters.js.map +1 -1
  50. package/_cjs/tempo/Relay.js +5 -0
  51. package/_cjs/tempo/Relay.js.map +1 -1
  52. package/_cjs/tempo/Transaction.js +2 -1
  53. package/_cjs/tempo/Transaction.js.map +1 -1
  54. package/_cjs/tempo/chainConfig.js +10 -13
  55. package/_cjs/tempo/chainConfig.js.map +1 -1
  56. package/_cjs/tempo/internal/relay/autoSwap.js +200 -0
  57. package/_cjs/tempo/internal/relay/autoSwap.js.map +1 -0
  58. package/_cjs/tempo/internal/relay/error.js +5 -1
  59. package/_cjs/tempo/internal/relay/error.js.map +1 -1
  60. package/_cjs/tempo/internal/relay/feePayer.js +18 -5
  61. package/_cjs/tempo/internal/relay/feePayer.js.map +1 -1
  62. package/_cjs/tempo/internal/relay/feeToken.js +14 -1
  63. package/_cjs/tempo/internal/relay/feeToken.js.map +1 -1
  64. package/_cjs/tempo/internal/relay/simulate.js +57 -15
  65. package/_cjs/tempo/internal/relay/simulate.js.map +1 -1
  66. package/_cjs/utils/abi/decodeEventLog.js +57 -7
  67. package/_cjs/utils/abi/decodeEventLog.js.map +1 -1
  68. package/_cjs/utils/abi/getAbiItem.js +27 -6
  69. package/_cjs/utils/abi/getAbiItem.js.map +1 -1
  70. package/_cjs/utils/abi/parseEventLogs.js +27 -4
  71. package/_cjs/utils/abi/parseEventLogs.js.map +1 -1
  72. package/_cjs/utils/authorization/recoverAuthorizationAddress.js +9 -2
  73. package/_cjs/utils/authorization/recoverAuthorizationAddress.js.map +1 -1
  74. package/_cjs/utils/authorization/verifyAuthorization.js +14 -4
  75. package/_cjs/utils/authorization/verifyAuthorization.js.map +1 -1
  76. package/_cjs/utils/ccip.js +134 -28
  77. package/_cjs/utils/ccip.js.map +1 -1
  78. package/_cjs/utils/ccipTunnel.js +5 -1
  79. package/_cjs/utils/ccipTunnel.js.map +1 -1
  80. package/_cjs/utils/ens/localBatchGatewayRequest.js +55 -5
  81. package/_cjs/utils/ens/localBatchGatewayRequest.js.map +1 -1
  82. package/_cjs/utils/internal/http.js +79 -0
  83. package/_cjs/utils/internal/http.js.map +1 -0
  84. package/_cjs/utils/nonceManager.js.map +1 -1
  85. package/_cjs/utils/rpc/http.js +3 -73
  86. package/_cjs/utils/rpc/http.js.map +1 -1
  87. package/_cjs/utils/transaction/assertRequest.js +2 -2
  88. package/_cjs/utils/transaction/assertRequest.js.map +1 -1
  89. package/_cjs/utils/transaction/assertTransaction.js +2 -2
  90. package/_cjs/utils/transaction/assertTransaction.js.map +1 -1
  91. package/_cjs/utils/transaction/parseTransaction.js +21 -7
  92. package/_cjs/utils/transaction/parseTransaction.js.map +1 -1
  93. package/_cjs/utils/typedData.js +19 -6
  94. package/_cjs/utils/typedData.js.map +1 -1
  95. package/_esm/account-abstraction/utils/formatters/userOperationReceipt.js +2 -0
  96. package/_esm/account-abstraction/utils/formatters/userOperationReceipt.js.map +1 -1
  97. package/_esm/actions/public/estimateGas.js +10 -5
  98. package/_esm/actions/public/estimateGas.js.map +1 -1
  99. package/_esm/actions/public/fillTransaction.js +2 -0
  100. package/_esm/actions/public/fillTransaction.js.map +1 -1
  101. package/_esm/actions/public/getFilterChanges.js +1 -0
  102. package/_esm/actions/public/getFilterChanges.js.map +1 -1
  103. package/_esm/actions/public/getFilterLogs.js +1 -0
  104. package/_esm/actions/public/getFilterLogs.js.map +1 -1
  105. package/_esm/actions/public/watchBlockNumber.js +5 -1
  106. package/_esm/actions/public/watchBlockNumber.js.map +1 -1
  107. package/_esm/actions/public/watchBlocks.js +28 -8
  108. package/_esm/actions/public/watchBlocks.js.map +1 -1
  109. package/_esm/actions/public/watchContractEvent.js +15 -5
  110. package/_esm/actions/public/watchContractEvent.js.map +1 -1
  111. package/_esm/actions/public/watchEvent.js +11 -2
  112. package/_esm/actions/public/watchEvent.js.map +1 -1
  113. package/_esm/actions/wallet/prepareTransactionRequest.js +3 -2
  114. package/_esm/actions/wallet/prepareTransactionRequest.js.map +1 -1
  115. package/_esm/actions/wallet/sendTransaction.js +6 -2
  116. package/_esm/actions/wallet/sendTransaction.js.map +1 -1
  117. package/_esm/actions/wallet/sendTransactionSync.js +59 -8
  118. package/_esm/actions/wallet/sendTransactionSync.js.map +1 -1
  119. package/_esm/chains/definitions/hashKeyChain.js +4 -4
  120. package/_esm/chains/definitions/hashKeyChain.js.map +1 -1
  121. package/_esm/chains/definitions/hashkeyChainTestnet.js +4 -4
  122. package/_esm/chains/definitions/hashkeyChainTestnet.js.map +1 -1
  123. package/_esm/chains/definitions/turkchain.js +23 -0
  124. package/_esm/chains/definitions/turkchain.js.map +1 -0
  125. package/_esm/chains/definitions/zksync.js +1 -1
  126. package/_esm/chains/definitions/zksync.js.map +1 -1
  127. package/_esm/chains/index.js +1 -0
  128. package/_esm/chains/index.js.map +1 -1
  129. package/_esm/clients/createClient.js.map +1 -1
  130. package/_esm/clients/transports/fallback.js +8 -0
  131. package/_esm/clients/transports/fallback.js.map +1 -1
  132. package/_esm/errors/node.js +10 -4
  133. package/_esm/errors/node.js.map +1 -1
  134. package/_esm/errors/typedData.js +5 -2
  135. package/_esm/errors/typedData.js.map +1 -1
  136. package/_esm/errors/version.js +1 -1
  137. package/_esm/index.js +1 -1
  138. package/_esm/index.js.map +1 -1
  139. package/_esm/tempo/Account.js +122 -11
  140. package/_esm/tempo/Account.js.map +1 -1
  141. package/_esm/tempo/Formatters.js +5 -2
  142. package/_esm/tempo/Formatters.js.map +1 -1
  143. package/_esm/tempo/Relay.js +20 -0
  144. package/_esm/tempo/Relay.js.map +1 -1
  145. package/_esm/tempo/Transaction.js +2 -0
  146. package/_esm/tempo/Transaction.js.map +1 -1
  147. package/_esm/tempo/chainConfig.js +20 -14
  148. package/_esm/tempo/chainConfig.js.map +1 -1
  149. package/_esm/tempo/internal/relay/autoSwap.js +218 -0
  150. package/_esm/tempo/internal/relay/autoSwap.js.map +1 -0
  151. package/_esm/tempo/internal/relay/error.js +6 -2
  152. package/_esm/tempo/internal/relay/error.js.map +1 -1
  153. package/_esm/tempo/internal/relay/feePayer.js +24 -7
  154. package/_esm/tempo/internal/relay/feePayer.js.map +1 -1
  155. package/_esm/tempo/internal/relay/feeToken.js +17 -3
  156. package/_esm/tempo/internal/relay/feeToken.js.map +1 -1
  157. package/_esm/tempo/internal/relay/simulate.js +68 -17
  158. package/_esm/tempo/internal/relay/simulate.js.map +1 -1
  159. package/_esm/utils/abi/decodeEventLog.js +69 -7
  160. package/_esm/utils/abi/decodeEventLog.js.map +1 -1
  161. package/_esm/utils/abi/getAbiItem.js +34 -6
  162. package/_esm/utils/abi/getAbiItem.js.map +1 -1
  163. package/_esm/utils/abi/parseEventLogs.js +43 -10
  164. package/_esm/utils/abi/parseEventLogs.js.map +1 -1
  165. package/_esm/utils/authorization/recoverAuthorizationAddress.js +11 -2
  166. package/_esm/utils/authorization/recoverAuthorizationAddress.js.map +1 -1
  167. package/_esm/utils/authorization/verifyAuthorization.js +15 -4
  168. package/_esm/utils/authorization/verifyAuthorization.js.map +1 -1
  169. package/_esm/utils/ccip.js +163 -31
  170. package/_esm/utils/ccip.js.map +1 -1
  171. package/_esm/utils/ccipTunnel.js +5 -1
  172. package/_esm/utils/ccipTunnel.js.map +1 -1
  173. package/_esm/utils/ens/localBatchGatewayRequest.js +69 -4
  174. package/_esm/utils/ens/localBatchGatewayRequest.js.map +1 -1
  175. package/_esm/utils/internal/http.js +83 -0
  176. package/_esm/utils/internal/http.js.map +1 -0
  177. package/_esm/utils/nonceManager.js.map +1 -1
  178. package/_esm/utils/rpc/http.js +1 -75
  179. package/_esm/utils/rpc/http.js.map +1 -1
  180. package/_esm/utils/transaction/assertRequest.js +2 -2
  181. package/_esm/utils/transaction/assertRequest.js.map +1 -1
  182. package/_esm/utils/transaction/assertTransaction.js +2 -2
  183. package/_esm/utils/transaction/assertTransaction.js.map +1 -1
  184. package/_esm/utils/transaction/parseTransaction.js +24 -7
  185. package/_esm/utils/transaction/parseTransaction.js.map +1 -1
  186. package/_esm/utils/typedData.js +25 -8
  187. package/_esm/utils/typedData.js.map +1 -1
  188. package/_types/account-abstraction/utils/formatters/userOperationReceipt.d.ts.map +1 -1
  189. package/_types/actions/public/estimateGas.d.ts.map +1 -1
  190. package/_types/actions/public/fillTransaction.d.ts.map +1 -1
  191. package/_types/actions/public/getFilterChanges.d.ts.map +1 -1
  192. package/_types/actions/public/getFilterLogs.d.ts.map +1 -1
  193. package/_types/actions/public/watchBlockNumber.d.ts +6 -2
  194. package/_types/actions/public/watchBlockNumber.d.ts.map +1 -1
  195. package/_types/actions/public/watchBlocks.d.ts +7 -2
  196. package/_types/actions/public/watchBlocks.d.ts.map +1 -1
  197. package/_types/actions/public/watchContractEvent.d.ts.map +1 -1
  198. package/_types/actions/public/watchEvent.d.ts.map +1 -1
  199. package/_types/actions/wallet/prepareTransactionRequest.d.ts.map +1 -1
  200. package/_types/actions/wallet/sendTransaction.d.ts.map +1 -1
  201. package/_types/actions/wallet/sendTransactionSync.d.ts.map +1 -1
  202. package/_types/chains/definitions/hashKeyChain.d.ts +4 -4
  203. package/_types/chains/definitions/hashkeyChainTestnet.d.ts +4 -4
  204. package/_types/chains/definitions/turkchain.d.ts +53 -0
  205. package/_types/chains/definitions/turkchain.d.ts.map +1 -0
  206. package/_types/chains/definitions/zksync.d.ts +1 -1
  207. package/_types/chains/index.d.ts +1 -0
  208. package/_types/chains/index.d.ts.map +1 -1
  209. package/_types/clients/createClient.d.ts +6 -0
  210. package/_types/clients/createClient.d.ts.map +1 -1
  211. package/_types/clients/transports/fallback.d.ts.map +1 -1
  212. package/_types/errors/node.d.ts.map +1 -1
  213. package/_types/errors/typedData.d.ts.map +1 -1
  214. package/_types/errors/version.d.ts +1 -1
  215. package/_types/index.d.ts +1 -1
  216. package/_types/index.d.ts.map +1 -1
  217. package/_types/tempo/Account.d.ts +78 -5
  218. package/_types/tempo/Account.d.ts.map +1 -1
  219. package/_types/tempo/Capabilities.d.ts +32 -2
  220. package/_types/tempo/Capabilities.d.ts.map +1 -1
  221. package/_types/tempo/Relay.d.ts +27 -1
  222. package/_types/tempo/Relay.d.ts.map +1 -1
  223. package/_types/tempo/Transaction.d.ts +2 -0
  224. package/_types/tempo/Transaction.d.ts.map +1 -1
  225. package/_types/tempo/actions/accessKey.d.ts +2 -2
  226. package/_types/tempo/chainConfig.d.ts.map +1 -1
  227. package/_types/tempo/internal/relay/autoSwap.d.ts +3 -0
  228. package/_types/tempo/internal/relay/autoSwap.d.ts.map +1 -0
  229. package/_types/tempo/internal/relay/error.d.ts.map +1 -1
  230. package/_types/tempo/internal/relay/feePayer.d.ts.map +1 -1
  231. package/_types/tempo/internal/relay/feeToken.d.ts.map +1 -1
  232. package/_types/tempo/internal/relay/simulate.d.ts +4 -0
  233. package/_types/tempo/internal/relay/simulate.d.ts.map +1 -1
  234. package/_types/tempo/internal/relay/utils.d.ts +8 -8
  235. package/_types/utils/abi/decodeEventLog.d.ts +13 -1
  236. package/_types/utils/abi/decodeEventLog.d.ts.map +1 -1
  237. package/_types/utils/abi/getAbiItem.d.ts.map +1 -1
  238. package/_types/utils/abi/parseEventLogs.d.ts.map +1 -1
  239. package/_types/utils/authorization/recoverAuthorizationAddress.d.ts +2 -1
  240. package/_types/utils/authorization/recoverAuthorizationAddress.d.ts.map +1 -1
  241. package/_types/utils/authorization/verifyAuthorization.d.ts.map +1 -1
  242. package/_types/utils/ccip.d.ts +24 -4
  243. package/_types/utils/ccip.d.ts.map +1 -1
  244. package/_types/utils/ccipTunnel.d.ts.map +1 -1
  245. package/_types/utils/ens/localBatchGatewayRequest.d.ts +26 -0
  246. package/_types/utils/ens/localBatchGatewayRequest.d.ts.map +1 -1
  247. package/_types/utils/internal/http.d.ts +16 -0
  248. package/_types/utils/internal/http.d.ts.map +1 -0
  249. package/_types/utils/nonceManager.d.ts +15 -1
  250. package/_types/utils/nonceManager.d.ts.map +1 -1
  251. package/_types/utils/rpc/http.d.ts.map +1 -1
  252. package/_types/utils/transaction/parseTransaction.d.ts.map +1 -1
  253. package/_types/utils/typedData.d.ts.map +1 -1
  254. package/account-abstraction/utils/formatters/userOperationReceipt.ts +1 -0
  255. package/actions/public/estimateGas.ts +12 -5
  256. package/actions/public/fillTransaction.ts +2 -0
  257. package/actions/public/getFilterChanges.ts +1 -0
  258. package/actions/public/getFilterLogs.ts +1 -0
  259. package/actions/public/watchBlockNumber.ts +11 -2
  260. package/actions/public/watchBlocks.ts +46 -14
  261. package/actions/public/watchContractEvent.ts +22 -4
  262. package/actions/public/watchEvent.ts +17 -1
  263. package/actions/wallet/prepareTransactionRequest.ts +6 -2
  264. package/actions/wallet/sendTransaction.ts +9 -3
  265. package/actions/wallet/sendTransactionSync.ts +74 -12
  266. package/chains/definitions/hashKeyChain.ts +4 -4
  267. package/chains/definitions/hashkeyChainTestnet.ts +4 -4
  268. package/chains/definitions/turkchain.ts +23 -0
  269. package/chains/definitions/zksync.ts +1 -1
  270. package/chains/index.ts +1 -0
  271. package/clients/createClient.ts +6 -0
  272. package/clients/transports/fallback.ts +11 -0
  273. package/errors/node.ts +11 -5
  274. package/errors/typedData.ts +5 -2
  275. package/errors/version.ts +1 -1
  276. package/index.ts +2 -0
  277. package/package.json +1 -1
  278. package/tempo/Account.ts +203 -17
  279. package/tempo/Capabilities.ts +34 -2
  280. package/tempo/Formatters.ts +5 -5
  281. package/tempo/Relay.ts +32 -1
  282. package/tempo/Transaction.ts +3 -0
  283. package/tempo/actions/accessKey.ts +2 -2
  284. package/tempo/chainConfig.ts +22 -14
  285. package/tempo/internal/relay/autoSwap.ts +263 -0
  286. package/tempo/internal/relay/error.ts +7 -2
  287. package/tempo/internal/relay/feePayer.ts +30 -7
  288. package/tempo/internal/relay/feeToken.ts +21 -3
  289. package/tempo/internal/relay/simulate.ts +106 -19
  290. package/utils/abi/decodeEventLog.ts +97 -10
  291. package/utils/abi/getAbiItem.ts +35 -5
  292. package/utils/abi/parseEventLogs.ts +48 -11
  293. package/utils/authorization/recoverAuthorizationAddress.ts +18 -2
  294. package/utils/authorization/verifyAuthorization.ts +12 -7
  295. package/utils/ccip.ts +199 -33
  296. package/utils/ccipTunnel.ts +5 -1
  297. package/utils/ens/localBatchGatewayRequest.ts +82 -4
  298. package/utils/internal/http.ts +92 -0
  299. package/utils/nonceManager.ts +16 -1
  300. package/utils/rpc/http.ts +1 -83
  301. package/utils/transaction/assertRequest.ts +2 -2
  302. package/utils/transaction/assertTransaction.ts +2 -2
  303. package/utils/transaction/parseTransaction.ts +32 -7
  304. package/utils/typedData.ts +31 -8
package/utils/ccip.ts CHANGED
@@ -1,6 +1,10 @@
1
1
  import type { Abi, Address } from 'abitype'
2
2
 
3
- import { type CallParameters, call } from '../actions/public/call.js'
3
+ import {
4
+ type CallParameters,
5
+ call,
6
+ getRevertErrorData,
7
+ } from '../actions/public/call.js'
4
8
  import type { Client } from '../clients/createClient.js'
5
9
  import type { Transport } from '../clients/transports/createTransport.js'
6
10
  import type { BaseError } from '../errors/base.js'
@@ -14,6 +18,10 @@ import {
14
18
  import {
15
19
  HttpRequestError,
16
20
  type HttpRequestErrorType,
21
+ ResponseBodyTooLargeError,
22
+ type ResponseBodyTooLargeErrorType,
23
+ TimeoutError,
24
+ type TimeoutErrorType,
17
25
  } from '../errors/request.js'
18
26
  import { type ErrorType, getAbortError, isAbortError } from '../errors/utils.js'
19
27
  import type { Chain } from '../types/chain.js'
@@ -25,11 +33,20 @@ import { isAddressEqual } from './address/isAddressEqual.js'
25
33
  import { concat } from './data/concat.js'
26
34
  import { isHex } from './data/isHex.js'
27
35
  import {
36
+ type CcipReadBudget,
37
+ ccipReadLimit,
38
+ createCcipReadBudget,
28
39
  localBatchGatewayRequest,
29
40
  localBatchGatewayUrl,
30
41
  } from './ens/localBatchGatewayRequest.js'
42
+ import { anySignal, readResponseBody } from './internal/http.js'
43
+ import { withTimeout } from './promise/withTimeout.js'
31
44
  import { stringify } from './stringify.js'
32
45
 
46
+ const ccipTimeout = 10_000
47
+ // Same ceiling as the HTTP transport.
48
+ const maxCcipResponseBytes = 10_485_760
49
+
33
50
  export const offchainLookupSignature = '0x556f1830'
34
51
  export const offchainLookupAbiItem = {
35
52
  name: 'OffchainLookup',
@@ -60,16 +77,29 @@ export const offchainLookupAbiItem = {
60
77
 
61
78
  export type OffchainLookupErrorType = OffchainLookupErrorType_ | ErrorType
62
79
 
80
+ /**
81
+ * Performs an EIP-3668 CCIP-Read lookup for a reverted `OffchainLookup` call.
82
+ *
83
+ * Follows at most 4 nested callbacks. Every gateway request of the lookup,
84
+ * including nested local batches, draws from one query and concurrency budget.
85
+ * Each gateway request uses the client transport timeout.
86
+ */
63
87
  export async function offchainLookup<chain extends Chain | undefined>(
64
88
  client: Client<Transport, chain>,
65
89
  {
66
90
  blockNumber,
67
91
  blockTag,
92
+ budget = createCcipReadBudget(),
68
93
  data,
94
+ depth = 0,
69
95
  requestOptions,
70
96
  to,
71
97
  }: Pick<CallParameters, 'blockNumber' | 'blockTag' | 'requestOptions'> & {
98
+ /** Budget shared by the callbacks of one lookup. */
99
+ budget?: CcipReadBudget | undefined
72
100
  data: Hex
101
+ /** Callbacks already followed in this lookup. */
102
+ depth?: number | undefined
73
103
  to: Address
74
104
  },
75
105
  ): Promise<Hex> {
@@ -84,34 +114,82 @@ export async function offchainLookup<chain extends Chain | undefined>(
84
114
  ccipRead && typeof ccipRead?.request === 'function'
85
115
  ? ccipRead.request
86
116
  : ccipRequest
117
+ const timeout = getTransportTimeout(client.transport)
87
118
 
88
119
  try {
120
+ if (depth >= ccipReadLimit.depth)
121
+ throw new HttpRequestError({
122
+ details: `CCIP Read exceeded the maximum callback depth (${ccipReadLimit.depth}).`,
123
+ url: urls[0] ?? '',
124
+ })
125
+
89
126
  if (!isAddressEqual(to, sender))
90
127
  throw new OffchainLookupSenderMismatchError({ sender, to })
91
128
 
92
- const result = urls.includes(localBatchGatewayUrl)
93
- ? await localBatchGatewayRequest({
94
- data: callData,
129
+ const result = await (async () => {
130
+ if (urls.includes(localBatchGatewayUrl))
131
+ return localBatchGatewayRequest({
132
+ budget,
95
133
  ccipRequest: (parameters) =>
96
- ccipRequest_({ ...parameters, requestOptions }),
134
+ ccipRequest_({
135
+ ...parameters,
136
+ requestOptions,
137
+ timeout: parameters.timeout ?? timeout,
138
+ }),
139
+ data: callData,
140
+ })
141
+
142
+ if (budget.queries <= 0)
143
+ throw new HttpRequestError({
144
+ details: `CCIP Read exceeded the maximum number of queries (${ccipReadLimit.queries}).`,
145
+ url: urls[0] ?? '',
97
146
  })
98
- : await ccipRequest_({ data: callData, requestOptions, sender, urls })
99
-
100
- const { data: data_ } = await call(client, {
101
- blockNumber,
102
- blockTag,
103
- data: concat([
104
- callbackSelector,
105
- encodeAbiParameters(
106
- [{ type: 'bytes' }, { type: 'bytes' }],
107
- [result, extraData],
108
- ),
109
- ]),
110
- requestOptions,
111
- to,
112
- } as CallParameters)
113
-
114
- return data_!
147
+ // Each URL is a gateway request, so each one draws from the budget.
148
+ const urls_ = urls.slice(0, budget.queries)
149
+ budget.queries -= Math.max(1, urls_.length)
150
+ return budget.run(() =>
151
+ ccipRequest_({
152
+ data: callData,
153
+ requestOptions,
154
+ sender,
155
+ timeout,
156
+ urls: urls_,
157
+ }),
158
+ )
159
+ })()
160
+
161
+ const callbackData = concat([
162
+ callbackSelector,
163
+ encodeAbiParameters(
164
+ [{ type: 'bytes' }, { type: 'bytes' }],
165
+ [result, extraData],
166
+ ),
167
+ ])
168
+
169
+ // The callback runs without CCIP-Read so a nested `OffchainLookup` comes
170
+ // back here with the lookup's depth and budget.
171
+ try {
172
+ const { data: data_ } = await call({ ...client, ccipRead: false }, {
173
+ blockNumber,
174
+ blockTag,
175
+ data: callbackData,
176
+ requestOptions,
177
+ to,
178
+ } as CallParameters)
179
+ return data_!
180
+ } catch (err) {
181
+ const revertData = getRevertErrorData(err)
182
+ if (revertData?.slice(0, 10) !== offchainLookupSignature) throw err
183
+ return await offchainLookup(client, {
184
+ blockNumber,
185
+ blockTag,
186
+ budget,
187
+ data: revertData,
188
+ depth: depth + 1,
189
+ requestOptions,
190
+ to,
191
+ })
192
+ }
115
193
  } catch (err) {
116
194
  if (requestOptions?.signal?.aborted)
117
195
  throw getAbortError(requestOptions.signal)
@@ -132,6 +210,8 @@ export type CcipRequestParameters = {
132
210
  data: Hex
133
211
  requestOptions?: EIP1193RequestOptions | undefined
134
212
  sender: Address
213
+ /** The timeout (in ms) for each gateway request. Default: 10_000 */
214
+ timeout?: number | undefined
135
215
  urls: readonly string[]
136
216
  }
137
217
 
@@ -140,34 +220,72 @@ export type CcipRequestReturnType = Hex
140
220
  export type CcipRequestErrorType =
141
221
  | HttpRequestErrorType
142
222
  | OffchainLookupResponseMalformedErrorType
223
+ | ResponseBodyTooLargeErrorType
224
+ | TimeoutErrorType
143
225
  | ErrorType
144
226
 
227
+ /**
228
+ * Requests a CCIP-Read gateway result.
229
+ *
230
+ * Tries each HTTP(S) URL in order. Each request, including the response body,
231
+ * times out after `timeout` (default 10 seconds) and reads at most 10 MiB.
232
+ */
145
233
  export async function ccipRequest({
146
234
  data,
147
235
  requestOptions,
148
236
  sender,
237
+ timeout = ccipTimeout,
149
238
  urls,
150
239
  }: CcipRequestParameters): Promise<CcipRequestReturnType> {
151
240
  let error = new Error('An unknown error occurred.')
152
241
 
153
- for (let i = 0; i < urls.length; i++) {
242
+ for (const url of urls) {
154
243
  if (requestOptions?.signal?.aborted)
155
244
  throw getAbortError(requestOptions.signal)
156
245
 
157
- const url = urls[i]
246
+ const requestUrl = url
247
+ .replace('{sender}', sender.toLowerCase())
248
+ .replace('{data}', data)
158
249
  const method = url.includes('{data}') ? 'GET' : 'POST'
159
250
  const body = method === 'POST' ? { data, sender } : undefined
160
251
  const headers: HeadersInit =
161
252
  method === 'POST' ? { 'Content-Type': 'application/json' } : {}
162
253
 
163
254
  try {
164
- const response = await fetch(
165
- url.replace('{sender}', sender.toLowerCase()).replace('{data}', data),
255
+ assertCcipUrl(requestUrl)
256
+
257
+ const { response, responseBody } = await withTimeout(
258
+ async ({ signal: timeoutSignal }) => {
259
+ // Abort on whichever fires first so that a `signal` does not
260
+ // disable the timeout.
261
+ const { signal, cleanup } = anySignal([
262
+ requestOptions?.signal,
263
+ timeout > 0 ? timeoutSignal : null,
264
+ ])
265
+ try {
266
+ const response = await fetch(requestUrl, {
267
+ body: JSON.stringify(body),
268
+ headers,
269
+ method,
270
+ ...(signal ? { signal } : {}),
271
+ })
272
+ // Read the body within the timeout too, so a response that stalls
273
+ // after its headers cannot hang the request.
274
+ const responseBody = await readResponseBody(response, {
275
+ maxResponseBodySize: maxCcipResponseBytes,
276
+ })
277
+ return { response, responseBody }
278
+ } finally {
279
+ cleanup()
280
+ }
281
+ },
166
282
  {
167
- body: JSON.stringify(body),
168
- headers,
169
- method,
170
- ...(requestOptions?.signal ? { signal: requestOptions.signal } : {}),
283
+ errorInstance: new TimeoutError({
284
+ body: body ?? { data, sender },
285
+ url,
286
+ }),
287
+ signal: true,
288
+ timeout,
171
289
  },
172
290
  )
173
291
 
@@ -175,9 +293,9 @@ export async function ccipRequest({
175
293
  if (
176
294
  response.headers.get('Content-Type')?.startsWith('application/json')
177
295
  ) {
178
- result = (await response.json()).data
296
+ result = JSON.parse(responseBody).data
179
297
  } else {
180
- result = (await response.text()) as any
298
+ result = responseBody
181
299
  }
182
300
 
183
301
  if (!response.ok) {
@@ -206,10 +324,18 @@ export async function ccipRequest({
206
324
  if (requestOptions?.signal?.aborted)
207
325
  throw getAbortError(requestOptions.signal)
208
326
  if (isAbortError(err)) throw err
327
+ if (
328
+ err instanceof HttpRequestError ||
329
+ err instanceof TimeoutError ||
330
+ err instanceof ResponseBodyTooLargeError
331
+ ) {
332
+ error = err
333
+ continue
334
+ }
209
335
 
210
336
  error = new HttpRequestError({
211
337
  body,
212
- details: (err as Error).message,
338
+ cause: err as Error,
213
339
  url,
214
340
  })
215
341
  }
@@ -217,3 +343,43 @@ export async function ccipRequest({
217
343
 
218
344
  throw error
219
345
  }
346
+
347
+ function assertCcipUrl(url: string) {
348
+ const parsed = (() => {
349
+ try {
350
+ return new URL(url)
351
+ } catch {
352
+ throw new HttpRequestError({
353
+ details: 'CCIP Read URL is invalid.',
354
+ url,
355
+ })
356
+ }
357
+ })()
358
+
359
+ if (parsed.protocol === 'http:' || parsed.protocol === 'https:') return
360
+ throw new HttpRequestError({
361
+ details: `CCIP Read URL scheme "${parsed.protocol.slice(0, -1)}" is not allowed.`,
362
+ url,
363
+ })
364
+ }
365
+
366
+ type TimeoutTransport = {
367
+ config?: { timeout?: number | undefined } | undefined
368
+ timeout?: number | undefined
369
+ transports?: readonly TimeoutTransport[] | undefined
370
+ value?: { transports?: readonly TimeoutTransport[] | undefined } | undefined
371
+ }
372
+
373
+ /**
374
+ * Returns the transport timeout. A wrapper that sets none (such as
375
+ * `fallback`) uses the longest timeout of its transports, since any of them
376
+ * may have served the call.
377
+ */
378
+ function getTransportTimeout(transport: TimeoutTransport): number | undefined {
379
+ const timeout = transport.timeout ?? transport.config?.timeout
380
+ if (timeout !== undefined) return timeout
381
+ const timeouts = (transport.transports ?? transport.value?.transports ?? [])
382
+ .map(getTransportTimeout)
383
+ .filter((timeout) => timeout !== undefined)
384
+ return timeouts.length ? Math.max(...timeouts) : undefined
385
+ }
@@ -17,11 +17,13 @@ export function ccipReadTunnel({
17
17
  ccipRequest = ccipRequest_,
18
18
  }: CcipReadTunnelParameters): { request: typeof ccipRequest_ } {
19
19
  return {
20
- async request({ data, sender, urls }) {
20
+ async request({ data, requestOptions, sender, timeout, urls }) {
21
21
  if (urls.includes(localBatchGatewayUrl)) {
22
22
  return ccipRequest({
23
23
  data,
24
+ requestOptions,
24
25
  sender,
26
+ timeout,
25
27
  urls: batchGateways,
26
28
  })
27
29
  } else {
@@ -34,7 +36,9 @@ export function ccipReadTunnel({
34
36
  functionName: 'query',
35
37
  args: [[{ sender, data, urls }]],
36
38
  }),
39
+ requestOptions,
37
40
  sender,
41
+ timeout,
38
42
  urls: batchGateways,
39
43
  }),
40
44
  })
@@ -1,5 +1,6 @@
1
1
  import { batchGatewayAbi } from '../../constants/abis.js'
2
2
  import { solidityError } from '../../constants/solidity.js'
3
+ import { HttpRequestError } from '../../errors/request.js'
3
4
  import type { Hex } from '../../types/misc.js'
4
5
  import { decodeFunctionData } from '../abi/decodeFunctionData.js'
5
6
  import { encodeErrorResult } from '../abi/encodeErrorResult.js'
@@ -12,13 +13,67 @@ import type {
12
13
 
13
14
  export const localBatchGatewayUrl = 'x-batch-gateway:true'
14
15
 
16
+ /**
17
+ * Caps for one CCIP-Read lookup.
18
+ *
19
+ * - `concurrency`: gateway requests in flight at once, across nested batches.
20
+ * - `depth`: nested callbacks, and separately, nested local batches.
21
+ * - `queries`: gateway requests in total, across callbacks and nested batches.
22
+ * Each contract-chosen URL counts once, and a query with none counts once.
23
+ * Gateways a custom `ccipRead.request` adds (such as `ccipReadTunnel`'s
24
+ * `batchGateways`) are the caller's own and are not counted separately.
25
+ */
26
+ export const ccipReadLimit = {
27
+ concurrency: 16,
28
+ depth: 4,
29
+ queries: 256,
30
+ } as const
31
+
32
+ /** Query and concurrency budget shared by every request of one lookup. */
33
+ export type CcipReadBudget = {
34
+ /** Gateway requests still allowed. */
35
+ queries: number
36
+ /** Runs a gateway request once a concurrency slot is free. */
37
+ run: <result>(fn: () => Promise<result>) => Promise<result>
38
+ }
39
+
40
+ /** Creates the budget for one CCIP-Read lookup. */
41
+ export function createCcipReadBudget(): CcipReadBudget {
42
+ let active = 0
43
+ const queue: (() => void)[] = []
44
+ return {
45
+ queries: ccipReadLimit.queries,
46
+ async run(fn) {
47
+ if (active >= ccipReadLimit.concurrency)
48
+ await new Promise<void>((resolve) => queue.push(resolve))
49
+ else active++
50
+ try {
51
+ return await fn()
52
+ } finally {
53
+ // Hand the slot straight to the next waiter so a new caller cannot
54
+ // take it in between.
55
+ const next = queue.shift()
56
+ if (next) next()
57
+ else active--
58
+ }
59
+ },
60
+ }
61
+ }
62
+
15
63
  export async function localBatchGatewayRequest(parameters: {
16
64
  data: Hex
17
65
  ccipRequest: (
18
66
  parameters: CcipRequestParameters,
19
67
  ) => Promise<CcipRequestReturnType>
68
+ budget?: CcipReadBudget | undefined
69
+ depth?: number | undefined
20
70
  }): Promise<Hex> {
21
- const { data, ccipRequest } = parameters
71
+ const {
72
+ data,
73
+ ccipRequest,
74
+ budget = createCcipReadBudget(),
75
+ depth = 0,
76
+ } = parameters
22
77
 
23
78
  const {
24
79
  args: [queries],
@@ -29,9 +84,32 @@ export async function localBatchGatewayRequest(parameters: {
29
84
  await Promise.all(
30
85
  queries.map(async (query, i) => {
31
86
  try {
32
- responses[i] = query.urls.includes(localBatchGatewayUrl)
33
- ? await localBatchGatewayRequest({ data: query.data, ccipRequest })
34
- : await ccipRequest(query)
87
+ const nested = query.urls.includes(localBatchGatewayUrl)
88
+ if (nested && depth + 1 >= ccipReadLimit.depth)
89
+ throw new HttpRequestError({
90
+ details: `CCIP Read exceeded the maximum batch depth (${ccipReadLimit.depth}).`,
91
+ url: localBatchGatewayUrl,
92
+ })
93
+ if (nested)
94
+ responses[i] = await localBatchGatewayRequest({
95
+ data: query.data,
96
+ ccipRequest,
97
+ budget,
98
+ depth: depth + 1,
99
+ })
100
+ else {
101
+ if (budget.queries <= 0)
102
+ throw new HttpRequestError({
103
+ details: `CCIP Read exceeded the maximum number of queries (${ccipReadLimit.queries}).`,
104
+ url: query.urls[0] ?? '',
105
+ })
106
+ // Each URL is a gateway request, so each one draws from the budget.
107
+ // A query with no URLs still costs one, since a custom request (such
108
+ // as `ccipReadTunnel`) may send it elsewhere.
109
+ const urls = query.urls.slice(0, budget.queries)
110
+ budget.queries -= Math.max(1, urls.length)
111
+ responses[i] = await budget.run(() => ccipRequest({ ...query, urls }))
112
+ }
35
113
  failures[i] = false
36
114
  } catch (err) {
37
115
  failures[i] = true
@@ -0,0 +1,92 @@
1
+ import { ResponseBodyTooLargeError } from '../../errors/request.js'
2
+
3
+ /**
4
+ * Returns a signal that aborts as soon as any of `signals` aborts, and a
5
+ * `cleanup` function that detaches the listeners added to `signals`.
6
+ */
7
+ export function anySignal(
8
+ signals: readonly (AbortSignal | null | undefined)[],
9
+ ) {
10
+ const signals_ = signals.filter((signal) => !!signal)
11
+ if (signals_.length < 2)
12
+ return { signal: signals_[0] ?? null, cleanup: () => {} }
13
+
14
+ const controller = new AbortController()
15
+ const onAbort = () => {
16
+ cleanup()
17
+ controller.abort(signals_.find((signal) => signal.aborted)?.reason)
18
+ }
19
+ const cleanup = () => {
20
+ for (const signal of signals_) signal.removeEventListener('abort', onAbort)
21
+ }
22
+
23
+ for (const signal of signals_) {
24
+ if (signal.aborted) {
25
+ cleanup()
26
+ controller.abort(signal.reason)
27
+ break
28
+ }
29
+ signal.addEventListener('abort', onAbort)
30
+ }
31
+
32
+ return { signal: controller.signal, cleanup }
33
+ }
34
+
35
+ /**
36
+ * Reads a response body as text, throwing once it exceeds
37
+ * `maxResponseBodySize` bytes (`false` reads it whole).
38
+ */
39
+ export async function readResponseBody(
40
+ response: Response,
41
+ { maxResponseBodySize }: { maxResponseBodySize: number | false },
42
+ ): Promise<string> {
43
+ if (maxResponseBodySize === false) return response.text()
44
+
45
+ const contentLength = response.headers.get('Content-Length')
46
+ if (contentLength) {
47
+ const size = Number(contentLength)
48
+ if (size > maxResponseBodySize) {
49
+ await response.body?.cancel()
50
+ throw new ResponseBodyTooLargeError({
51
+ maxSize: maxResponseBodySize,
52
+ size,
53
+ })
54
+ }
55
+ }
56
+
57
+ if (!response.body) {
58
+ const body = await response.text()
59
+ const size = new TextEncoder().encode(body).length
60
+ if (size > maxResponseBodySize)
61
+ throw new ResponseBodyTooLargeError({
62
+ maxSize: maxResponseBodySize,
63
+ size,
64
+ })
65
+ return body
66
+ }
67
+
68
+ const reader = response.body.getReader()
69
+ const decoder = new TextDecoder()
70
+ let body = ''
71
+ let size = 0
72
+
73
+ try {
74
+ while (true) {
75
+ const { done, value } = await reader.read()
76
+ if (done) break
77
+ size += value.byteLength
78
+ if (size > maxResponseBodySize) {
79
+ await reader.cancel()
80
+ throw new ResponseBodyTooLargeError({
81
+ maxSize: maxResponseBodySize,
82
+ size,
83
+ })
84
+ }
85
+ body += decoder.decode(value, { stream: true })
86
+ }
87
+ body += decoder.decode()
88
+ return body
89
+ } finally {
90
+ reader.releaseLock()
91
+ }
92
+ }
@@ -14,6 +14,21 @@ type FunctionParameters = {
14
14
  chainId: number
15
15
  }
16
16
 
17
+ type ResetParameters = FunctionParameters & {
18
+ /**
19
+ * The nonce that `consume` returned for the send being reset.
20
+ *
21
+ * Set when a send fails after `consume` returned a nonce, and omitted
22
+ * when `consume` threw or `reset` is called outside of a send. Managers
23
+ * that track a nonce per send can use it to release that nonce only.
24
+ *
25
+ * A failed send does not prove the nonce is unused: the node may have
26
+ * accepted the transaction before the request failed (e.g. a timeout).
27
+ * Check the account's pending nonce before reusing a released nonce.
28
+ */
29
+ nonce?: number | undefined
30
+ }
31
+
17
32
  export type NonceManager = {
18
33
  /** Get and increment a nonce. */
19
34
  consume: (
@@ -24,7 +39,7 @@ export type NonceManager = {
24
39
  /** Get a nonce. */
25
40
  get: (chainId: FunctionParameters & { client: Client }) => Promise<number>
26
41
  /** Reset a nonce. */
27
- reset: (chainId: FunctionParameters) => void
42
+ reset: (chainId: ResetParameters) => void
28
43
  }
29
44
 
30
45
  /**
package/utils/rpc/http.ts CHANGED
@@ -15,6 +15,7 @@ import {
15
15
  } from '../../errors/utils.js'
16
16
  import type { RpcRequest, RpcResponse } from '../../types/rpc.js'
17
17
  import type { MaybePromise } from '../../types/utils.js'
18
+ import { anySignal, readResponseBody } from '../internal/http.js'
18
19
  import {
19
20
  type WithTimeoutErrorType,
20
21
  withTimeout,
@@ -241,89 +242,6 @@ export function getHttpRpcClient(
241
242
  }
242
243
  }
243
244
 
244
- /**
245
- * Returns a signal that aborts as soon as any of `signals` aborts, and a
246
- * `cleanup` function that detaches the listeners added to `signals`.
247
- */
248
- function anySignal(signals: readonly (AbortSignal | null | undefined)[]) {
249
- const signals_ = signals.filter((signal) => !!signal)
250
- if (signals_.length < 2)
251
- return { signal: signals_[0] ?? null, cleanup: () => {} }
252
-
253
- const controller = new AbortController()
254
- const onAbort = () => {
255
- cleanup()
256
- controller.abort(signals_.find((signal) => signal.aborted)?.reason)
257
- }
258
- const cleanup = () => {
259
- for (const signal of signals_) signal.removeEventListener('abort', onAbort)
260
- }
261
-
262
- for (const signal of signals_) {
263
- if (signal.aborted) {
264
- cleanup()
265
- controller.abort(signal.reason)
266
- break
267
- }
268
- signal.addEventListener('abort', onAbort)
269
- }
270
-
271
- return { signal: controller.signal, cleanup }
272
- }
273
-
274
- async function readResponseBody(
275
- response: Response,
276
- { maxResponseBodySize }: { maxResponseBodySize: number | false },
277
- ): Promise<string> {
278
- if (maxResponseBodySize === false) return response.text()
279
-
280
- const contentLength = response.headers.get('Content-Length')
281
- if (contentLength) {
282
- const size = Number(contentLength)
283
- if (size > maxResponseBodySize)
284
- throw new ResponseBodyTooLargeError({
285
- maxSize: maxResponseBodySize,
286
- size,
287
- })
288
- }
289
-
290
- if (!response.body) {
291
- const body = await response.text()
292
- const size = new TextEncoder().encode(body).length
293
- if (size > maxResponseBodySize)
294
- throw new ResponseBodyTooLargeError({
295
- maxSize: maxResponseBodySize,
296
- size,
297
- })
298
- return body
299
- }
300
-
301
- const reader = response.body.getReader()
302
- const decoder = new TextDecoder()
303
- let body = ''
304
- let size = 0
305
-
306
- try {
307
- while (true) {
308
- const { done, value } = await reader.read()
309
- if (done) break
310
- size += value.byteLength
311
- if (size > maxResponseBodySize) {
312
- await reader.cancel()
313
- throw new ResponseBodyTooLargeError({
314
- maxSize: maxResponseBodySize,
315
- size,
316
- })
317
- }
318
- body += decoder.decode(value, { stream: true })
319
- }
320
- body += decoder.decode()
321
- return body
322
- } finally {
323
- reader.releaseLock()
324
- }
325
- }
326
-
327
245
  function isRpcError(data: any) {
328
246
  return (
329
247
  typeof data?.error?.code === 'number' &&