viem 2.57.15 → 2.57.16

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 (111) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/_cjs/errors/version.js +1 -1
  3. package/_cjs/tempo/Gateway.js +195 -0
  4. package/_cjs/tempo/Gateway.js.map +1 -0
  5. package/_cjs/tempo/GatewayDecorator.js +168 -0
  6. package/_cjs/tempo/GatewayDecorator.js.map +1 -0
  7. package/_cjs/tempo/actions/ai.js +69 -0
  8. package/_cjs/tempo/actions/ai.js.map +1 -0
  9. package/_cjs/tempo/actions/bucket.js +85 -0
  10. package/_cjs/tempo/actions/bucket.js.map +1 -0
  11. package/_cjs/tempo/actions/compute.js +165 -0
  12. package/_cjs/tempo/actions/compute.js.map +1 -0
  13. package/_cjs/tempo/actions/gateway.js +8 -0
  14. package/_cjs/tempo/actions/gateway.js.map +1 -0
  15. package/_cjs/tempo/actions/index.js +6 -1
  16. package/_cjs/tempo/actions/index.js.map +1 -1
  17. package/_cjs/tempo/actions/kv.js +44 -0
  18. package/_cjs/tempo/actions/kv.js.map +1 -0
  19. package/_cjs/tempo/index.js +4 -1
  20. package/_cjs/tempo/index.js.map +1 -1
  21. package/_cjs/tempo/internal/ndjson.js +43 -0
  22. package/_cjs/tempo/internal/ndjson.js.map +1 -0
  23. package/_cjs/utils/buildRequest.js +4 -0
  24. package/_cjs/utils/buildRequest.js.map +1 -1
  25. package/_cjs/utils/hash/toSignatureHash.js +10 -1
  26. package/_cjs/utils/hash/toSignatureHash.js.map +1 -1
  27. package/_cjs/utils/rpc/http.js +6 -3
  28. package/_cjs/utils/rpc/http.js.map +1 -1
  29. package/_cjs/utils/rpc/socket.js +20 -9
  30. package/_cjs/utils/rpc/socket.js.map +1 -1
  31. package/_cjs/zksync/actions/finalizeWithdrawal.js +4 -2
  32. package/_cjs/zksync/actions/finalizeWithdrawal.js.map +1 -1
  33. package/_cjs/zksync/actions/isWithdrawalFinalized.js +4 -5
  34. package/_cjs/zksync/actions/isWithdrawalFinalized.js.map +1 -1
  35. package/_esm/errors/version.js +1 -1
  36. package/_esm/tempo/Gateway.js +294 -0
  37. package/_esm/tempo/Gateway.js.map +1 -0
  38. package/_esm/tempo/GatewayDecorator.js +207 -0
  39. package/_esm/tempo/GatewayDecorator.js.map +1 -0
  40. package/_esm/tempo/actions/ai.js +132 -0
  41. package/_esm/tempo/actions/ai.js.map +1 -0
  42. package/_esm/tempo/actions/bucket.js +181 -0
  43. package/_esm/tempo/actions/bucket.js.map +1 -0
  44. package/_esm/tempo/actions/compute.js +424 -0
  45. package/_esm/tempo/actions/compute.js.map +1 -0
  46. package/_esm/tempo/actions/gateway.js +22 -0
  47. package/_esm/tempo/actions/gateway.js.map +1 -0
  48. package/_esm/tempo/actions/index.js +10 -0
  49. package/_esm/tempo/actions/index.js.map +1 -1
  50. package/_esm/tempo/actions/kv.js +124 -0
  51. package/_esm/tempo/actions/kv.js.map +1 -0
  52. package/_esm/tempo/index.js +9 -0
  53. package/_esm/tempo/index.js.map +1 -1
  54. package/_esm/tempo/internal/ndjson.js +45 -0
  55. package/_esm/tempo/internal/ndjson.js.map +1 -0
  56. package/_esm/utils/buildRequest.js +6 -0
  57. package/_esm/utils/buildRequest.js.map +1 -1
  58. package/_esm/utils/hash/toSignatureHash.js +11 -1
  59. package/_esm/utils/hash/toSignatureHash.js.map +1 -1
  60. package/_esm/utils/rpc/http.js +11 -3
  61. package/_esm/utils/rpc/http.js.map +1 -1
  62. package/_esm/utils/rpc/socket.js +24 -9
  63. package/_esm/utils/rpc/socket.js.map +1 -1
  64. package/_esm/zksync/actions/finalizeWithdrawal.js +5 -2
  65. package/_esm/zksync/actions/finalizeWithdrawal.js.map +1 -1
  66. package/_esm/zksync/actions/isWithdrawalFinalized.js +5 -5
  67. package/_esm/zksync/actions/isWithdrawalFinalized.js.map +1 -1
  68. package/_types/errors/version.d.ts +1 -1
  69. package/_types/tempo/Gateway.d.ts +337 -0
  70. package/_types/tempo/Gateway.d.ts.map +1 -0
  71. package/_types/tempo/GatewayDecorator.d.ts +635 -0
  72. package/_types/tempo/GatewayDecorator.d.ts.map +1 -0
  73. package/_types/tempo/actions/ai.d.ts +145 -0
  74. package/_types/tempo/actions/ai.d.ts.map +1 -0
  75. package/_types/tempo/actions/bucket.d.ts +210 -0
  76. package/_types/tempo/actions/bucket.d.ts.map +1 -0
  77. package/_types/tempo/actions/compute.d.ts +628 -0
  78. package/_types/tempo/actions/compute.d.ts.map +1 -0
  79. package/_types/tempo/actions/gateway.d.ts +25 -0
  80. package/_types/tempo/actions/gateway.d.ts.map +1 -0
  81. package/_types/tempo/actions/index.d.ts +10 -0
  82. package/_types/tempo/actions/index.d.ts.map +1 -1
  83. package/_types/tempo/actions/kv.d.ts +167 -0
  84. package/_types/tempo/actions/kv.d.ts.map +1 -0
  85. package/_types/tempo/index.d.ts +11 -0
  86. package/_types/tempo/index.d.ts.map +1 -1
  87. package/_types/tempo/internal/ndjson.d.ts +9 -0
  88. package/_types/tempo/internal/ndjson.d.ts.map +1 -0
  89. package/_types/utils/buildRequest.d.ts.map +1 -1
  90. package/_types/utils/hash/toSignatureHash.d.ts +1 -1
  91. package/_types/utils/hash/toSignatureHash.d.ts.map +1 -1
  92. package/_types/utils/rpc/http.d.ts.map +1 -1
  93. package/_types/utils/rpc/socket.d.ts.map +1 -1
  94. package/errors/version.ts +1 -1
  95. package/package.json +5 -1
  96. package/tempo/Gateway.ts +477 -0
  97. package/tempo/GatewayDecorator.ts +909 -0
  98. package/tempo/actions/ai.ts +250 -0
  99. package/tempo/actions/bucket.ts +324 -0
  100. package/tempo/actions/compute.ts +894 -0
  101. package/tempo/actions/gateway.ts +33 -0
  102. package/tempo/actions/index.ts +10 -0
  103. package/tempo/actions/kv.ts +229 -0
  104. package/tempo/index.ts +12 -0
  105. package/tempo/internal/ndjson.ts +51 -0
  106. package/utils/buildRequest.ts +4 -0
  107. package/utils/hash/toSignatureHash.ts +12 -2
  108. package/utils/rpc/http.ts +11 -3
  109. package/utils/rpc/socket.ts +28 -15
  110. package/zksync/actions/finalizeWithdrawal.ts +5 -2
  111. package/zksync/actions/isWithdrawalFinalized.ts +5 -5
@@ -0,0 +1,33 @@
1
+ import * as Gateway from '../Gateway.js'
2
+
3
+ /**
4
+ * Reads the Gateway's free discovery document: chain, currencies, prices,
5
+ * limits, and models.
6
+ *
7
+ * @experimental
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * import { Actions, Gateway } from 'viem/tempo'
12
+ *
13
+ * const capabilities = await Actions.gateway.getCapabilities(Gateway.from())
14
+ * ```
15
+ *
16
+ * @param gateway - Gateway connection.
17
+ * @param parameters - Parameters.
18
+ * @returns The Gateway capabilities.
19
+ */
20
+ export async function getCapabilities(
21
+ gateway: Gateway.Gateway,
22
+ parameters: getCapabilities.Parameters = {},
23
+ ): Promise<getCapabilities.ReturnValue> {
24
+ return Gateway.getCapabilities(gateway, parameters)
25
+ }
26
+
27
+ export declare namespace getCapabilities {
28
+ type Parameters = Gateway.getCapabilities.Options
29
+
30
+ type ReturnValue = Gateway.Capabilities
31
+
32
+ type ErrorType = Gateway.request.ErrorType
33
+ }
@@ -1,12 +1,22 @@
1
1
  // biome-ignore lint/performance/noBarrelFile: _
2
2
  export * as accessKey from './accessKey.js'
3
3
  export * as accounts from './accounts.js'
4
+ /** @experimental */
5
+ export * as ai from './ai.js'
4
6
  export * as amm from './amm.js'
7
+ /** @experimental */
8
+ export * as bucket from './bucket.js'
5
9
  export * as channel from './channel.js'
10
+ /** @experimental */
11
+ export * as compute from './compute.js'
6
12
  export * as dex from './dex.js'
7
13
  export * as earn from './earn.js'
8
14
  export * as faucet from './faucet.js'
9
15
  export * as fee from './fee.js'
16
+ /** @experimental */
17
+ export * as gateway from './gateway.js'
18
+ /** @experimental */
19
+ export * as kv from './kv.js'
10
20
  export * as nonce from './nonce.js'
11
21
  export * as policy from './policy.js'
12
22
  export * as propAmm from './propAmm.js'
@@ -0,0 +1,229 @@
1
+ import * as Gateway from '../Gateway.js'
2
+
3
+ /**
4
+ * Entry in a Gateway key-value store.
5
+ *
6
+ * @experimental
7
+ */
8
+ export type Entry = {
9
+ /** Expiry as a Unix timestamp in milliseconds. */
10
+ expiresAt: number
11
+ /** Key. */
12
+ key: string
13
+ /** JSON value. Validate it before use. */
14
+ value: unknown
15
+ /** Version, incremented on every write. */
16
+ version: number
17
+ }
18
+
19
+ /**
20
+ * Deletes a key from the app's key-value store. Paid per request.
21
+ *
22
+ * @experimental
23
+ *
24
+ * @example
25
+ * ```ts
26
+ * import { Actions, Gateway } from 'viem/tempo'
27
+ *
28
+ * const gateway = Gateway.from({ fetch: mppx.fetch })
29
+ *
30
+ * const { deleted } = await Actions.kv.delete(gateway, { key: 'counter' })
31
+ * ```
32
+ *
33
+ * @param gateway - Gateway connection.
34
+ * @param parameters - Parameters.
35
+ * @returns Whether the key existed.
36
+ */
37
+ // biome-ignore lint/correctness/noUnusedVariables: exported as delete
38
+ async function delete_(
39
+ gateway: Gateway.Gateway,
40
+ parameters: delete_.Parameters,
41
+ ): Promise<delete_.ReturnValue> {
42
+ const { key, signal } = parameters
43
+ const response = await Gateway.request(gateway, {
44
+ body: JSON.stringify({ key }),
45
+ path: '/kv/delete',
46
+ signal,
47
+ })
48
+ return (await response.json()) as delete_.ReturnValue
49
+ }
50
+
51
+ declare namespace delete_ {
52
+ type Parameters = {
53
+ /** Key. */
54
+ key: string
55
+ /** Signal to abort the request. */
56
+ signal?: AbortSignal | undefined
57
+ }
58
+
59
+ type ReturnValue = {
60
+ /** Whether the key existed. */
61
+ deleted: boolean
62
+ }
63
+
64
+ type ErrorType = Gateway.request.ErrorType
65
+ }
66
+
67
+ export { delete_ as delete }
68
+
69
+ /**
70
+ * Reads a key from the app's key-value store. Returns `null` when the key does
71
+ * not exist or expired. Paid per request.
72
+ *
73
+ * @experimental
74
+ *
75
+ * @example
76
+ * ```ts
77
+ * import { Actions, Gateway } from 'viem/tempo'
78
+ *
79
+ * const gateway = Gateway.from({ fetch: mppx.fetch })
80
+ *
81
+ * const entry = await Actions.kv.get(gateway, { key: 'counter' })
82
+ * ```
83
+ *
84
+ * @param gateway - Gateway connection.
85
+ * @param parameters - Parameters.
86
+ * @returns The entry, or `null`.
87
+ */
88
+ export async function get(
89
+ gateway: Gateway.Gateway,
90
+ parameters: get.Parameters,
91
+ ): Promise<get.ReturnValue> {
92
+ const { key, signal } = parameters
93
+ const response = await Gateway.request(gateway, {
94
+ body: JSON.stringify({ key }),
95
+ path: '/kv/get',
96
+ signal,
97
+ })
98
+ return (await response.json()) as get.ReturnValue
99
+ }
100
+
101
+ export declare namespace get {
102
+ type Parameters = {
103
+ /** Key. */
104
+ key: string
105
+ /** Signal to abort the request. */
106
+ signal?: AbortSignal | undefined
107
+ }
108
+
109
+ type ReturnValue = Entry | null
110
+
111
+ type ErrorType = Gateway.request.ErrorType
112
+ }
113
+
114
+ /**
115
+ * Lists entries in the app's key-value store by key prefix, in key order. Paid
116
+ * per request.
117
+ *
118
+ * @experimental
119
+ *
120
+ * @example
121
+ * ```ts
122
+ * import { Actions, Gateway } from 'viem/tempo'
123
+ *
124
+ * const gateway = Gateway.from({ fetch: mppx.fetch })
125
+ *
126
+ * const { cursor, entries } = await Actions.kv.list(gateway, {
127
+ * prefix: 'settings/',
128
+ * })
129
+ * ```
130
+ *
131
+ * @param gateway - Gateway connection.
132
+ * @param parameters - Parameters.
133
+ * @returns A page of entries and the next cursor.
134
+ */
135
+ export async function list(
136
+ gateway: Gateway.Gateway,
137
+ parameters: list.Parameters = {},
138
+ ): Promise<list.ReturnValue> {
139
+ const { cursor, limit, prefix, signal } = parameters
140
+ const response = await Gateway.request(gateway, {
141
+ body: JSON.stringify({ cursor, limit, prefix }),
142
+ path: '/kv/list',
143
+ signal,
144
+ })
145
+ return (await response.json()) as list.ReturnValue
146
+ }
147
+
148
+ export declare namespace list {
149
+ type Parameters = {
150
+ /** Cursor from a previous page. */
151
+ cursor?: string | undefined
152
+ /** Maximum entries per page. */
153
+ limit?: number | undefined
154
+ /** Key prefix. */
155
+ prefix?: string | undefined
156
+ /** Signal to abort the request. */
157
+ signal?: AbortSignal | undefined
158
+ }
159
+
160
+ type ReturnValue = {
161
+ /** Cursor of the next page, or `null` on the last page. */
162
+ cursor: string | null
163
+ /** Entries on this page. */
164
+ entries: readonly Entry[]
165
+ }
166
+
167
+ type ErrorType = Gateway.request.ErrorType
168
+ }
169
+
170
+ /**
171
+ * Writes a JSON value to the app's key-value store. Paid per request.
172
+ *
173
+ * Pass `ifVersion` to write conditionally: `null` creates the key only if it
174
+ * does not exist, and a version replaces the value only if it is still that
175
+ * version. A failed condition throws `Gateway.VersionConflictError`.
176
+ *
177
+ * @experimental
178
+ *
179
+ * @example
180
+ * ```ts
181
+ * import { Actions, Gateway } from 'viem/tempo'
182
+ *
183
+ * const gateway = Gateway.from({ fetch: mppx.fetch })
184
+ *
185
+ * const entry = await Actions.kv.set(gateway, {
186
+ * ifVersion: null,
187
+ * key: 'counter',
188
+ * value: { count: 1 },
189
+ * })
190
+ * ```
191
+ *
192
+ * @param gateway - Gateway connection.
193
+ * @param parameters - Parameters.
194
+ * @returns The written entry.
195
+ */
196
+ export async function set(
197
+ gateway: Gateway.Gateway,
198
+ parameters: set.Parameters,
199
+ ): Promise<set.ReturnValue> {
200
+ const { ifVersion, key, signal, ttl, value } = parameters
201
+ const response = await Gateway.request(gateway, {
202
+ body: JSON.stringify({ ifVersion, key, ttl, value }),
203
+ path: '/kv/set',
204
+ signal,
205
+ })
206
+ return (await response.json()) as set.ReturnValue
207
+ }
208
+
209
+ export declare namespace set {
210
+ type Parameters = {
211
+ /**
212
+ * Write condition: `null` creates the key only if it does not exist; a
213
+ * version replaces the value only if it is still that version.
214
+ */
215
+ ifVersion?: number | null | undefined
216
+ /** Key. */
217
+ key: string
218
+ /** Signal to abort the request. */
219
+ signal?: AbortSignal | undefined
220
+ /** Time to live in seconds. Without it, the entry is kept for the Gateway's longest retention (`limits.retentionSeconds`). */
221
+ ttl?: number | undefined
222
+ /** JSON-serializable value. */
223
+ value: unknown
224
+ }
225
+
226
+ type ReturnValue = Entry
227
+
228
+ type ErrorType = Gateway.request.ErrorType
229
+ }
package/tempo/index.ts CHANGED
@@ -72,6 +72,18 @@ export * as ExecutionError from './ExecutionError.js'
72
72
  export * as Expiry from './Expiry.js'
73
73
  export * from './errors.js'
74
74
  export * as Formatters from './Formatters.js'
75
+ /**
76
+ * Wallet-paid inference, object storage, sandboxed compute, and key-value state
77
+ * on a Tempo Gateway.
78
+ * @experimental
79
+ */
80
+ export * as Gateway from './Gateway.js'
81
+ export {
82
+ /** @experimental */
83
+ type Decorator as GatewayActions,
84
+ /** @experimental */
85
+ decorator as gatewayActions,
86
+ } from './GatewayDecorator.js'
75
87
  export * as Hardfork from './Hardfork.js'
76
88
  /** Client-side storage for pending key authorizations, backed by a `Store`. */
77
89
  export * as KeyAuthorizationManager from './KeyAuthorizationManager.js'
@@ -0,0 +1,51 @@
1
+ import * as Gateway from '../Gateway.js'
2
+
3
+ /**
4
+ * Reads a Gateway NDJSON stream, one event per line. An `error` event throws a
5
+ * {@link Gateway.RequestError}. Stopping early cancels the response body, so
6
+ * the Gateway stops the upstream work.
7
+ */
8
+ export async function* read<event extends { type: string }>(
9
+ response: Response,
10
+ ): AsyncGenerator<event> {
11
+ if (!response.body) return
12
+
13
+ const reader = response.body.getReader()
14
+ const decoder = new TextDecoder()
15
+ let buffer = ''
16
+ let done = false
17
+
18
+ try {
19
+ for (;;) {
20
+ const result = await reader.read()
21
+ done = result.done
22
+ const value = result.value
23
+ buffer += decoder.decode(value, { stream: !done })
24
+
25
+ const lines = buffer.split('\n')
26
+ buffer = done ? '' : (lines.pop() ?? '')
27
+
28
+ for (const line of lines) {
29
+ if (!line.trim()) continue
30
+
31
+ const event = JSON.parse(line) as event & {
32
+ code?: string
33
+ detail?: string
34
+ }
35
+ if (event.type === 'error')
36
+ throw new Gateway.RequestError({
37
+ code: event.code ?? 'upstream_failed',
38
+ detail: event.detail ?? '',
39
+ status: 502,
40
+ url: response.url,
41
+ })
42
+ yield event
43
+ }
44
+
45
+ if (done) return
46
+ }
47
+ } finally {
48
+ if (!done) await reader.cancel().catch(() => {})
49
+ reader.releaseLock()
50
+ }
51
+ }
@@ -310,6 +310,10 @@ export function shouldRetry(error: Error) {
310
310
  // reached - reduce calls per second or upgrade your account at
311
311
  // quicknode.com" }`.
312
312
  if (error.code === -32007) return true
313
+ // The node does not serve the method, so a retry gets the same refusal
314
+ // whatever the HTTP status (e.g. a 403 refusing an unserved method).
315
+ if (error.code === MethodNotFoundRpcError.code) return false
316
+ if (error.code === MethodNotSupportedRpcError.code) return false
313
317
  // A JSON-RPC error from a non-2xx response also retries by its HTTP status.
314
318
  if (!status) return false
315
319
  }
@@ -1,9 +1,14 @@
1
- import type { AbiEvent, AbiFunction } from 'abitype'
1
+ import { type AbiEvent, type AbiFunction, formatAbiItem } from 'abitype'
2
2
 
3
3
  import type { ErrorType } from '../../errors/utils.js'
4
+ import type { Hex } from '../../types/misc.js'
5
+ import { LruMap } from '../lru.js'
4
6
  import { type HashSignatureErrorType, hashSignature } from './hashSignature.js'
5
7
  import { type ToSignatureErrorType, toSignature } from './toSignature.js'
6
8
 
9
+ /** Signature hashes keyed by their definition, since hashing is costly. */
10
+ const signatureHashCache = /*#__PURE__*/ new LruMap<Hex>(8192)
11
+
7
12
  export type ToSignatureHashErrorType =
8
13
  | HashSignatureErrorType
9
14
  | ToSignatureErrorType
@@ -13,5 +18,10 @@ export type ToSignatureHashErrorType =
13
18
  * Returns the hash (of the function/event/error signature) for a given function, event, or error definition.
14
19
  */
15
20
  export function toSignatureHash(fn: string | AbiFunction | AbiEvent) {
16
- return hashSignature(toSignature(fn))
21
+ const definition = typeof fn === 'string' ? fn : formatAbiItem(fn)
22
+ const cached = signatureHashCache.get(definition)
23
+ if (cached) return cached
24
+ const hash = hashSignature(toSignature(definition))
25
+ signatureHashCache.set(definition, hash)
26
+ return hash
17
27
  }
package/utils/rpc/http.ts CHANGED
@@ -87,6 +87,12 @@ export type HttpRequestErrorType =
87
87
 
88
88
  const defaultMaxResponseBodySize = 10_485_760
89
89
 
90
+ /**
91
+ * Rejection of a request that timed out. Replaced by a `TimeoutError`, which
92
+ * serializes the request body, only once a request actually times out.
93
+ */
94
+ const timedOut = /*#__PURE__*/ new Error('timed out')
95
+
90
96
  export type HttpRpcClient = {
91
97
  request<body extends RpcRequest | RpcRequest[]>(
92
98
  params: HttpRequestParameters<body>,
@@ -151,8 +157,9 @@ export function getHttpRpcClient(
151
157
  method: method || 'POST',
152
158
  signal,
153
159
  }
154
- const request = new Request(url, init)
155
- const args = (await onRequest?.(request, init)) ?? {
160
+ // Only the hook needs a `Request`, which is costly to construct.
161
+ const args = (onRequest &&
162
+ (await onRequest(new Request(url, init), init))) ?? {
156
163
  ...init,
157
164
  url,
158
165
  }
@@ -171,7 +178,7 @@ export function getHttpRpcClient(
171
178
  }
172
179
  },
173
180
  {
174
- errorInstance: new TimeoutError({ body, url }),
181
+ errorInstance: timedOut,
175
182
  timeout,
176
183
  signal: true,
177
184
  },
@@ -227,6 +234,7 @@ export function getHttpRpcClient(
227
234
  return body.map(() => error)
228
235
  } catch (err) {
229
236
  if (signal_?.aborted) throw getAbortError(signal_)
237
+ if (err === timedOut) throw new TimeoutError({ body, url })
230
238
  if (isAbortError(err)) throw err
231
239
  if (err instanceof HttpRequestError) throw err
232
240
  if (err instanceof ResponseBodyTooLargeError) throw err
@@ -94,6 +94,12 @@ export const socketClientCache = /*#__PURE__*/ new Map<
94
94
  SocketRpcClient<Socket<{}>>
95
95
  >()
96
96
 
97
+ /**
98
+ * Rejection of a request that timed out. Replaced by a `TimeoutError`, which
99
+ * serializes the request body, only once a request actually times out.
100
+ */
101
+ const timedOut = /*#__PURE__*/ new Error('timed out')
102
+
97
103
  export async function getSocketRpcClient<socket extends {}>(
98
104
  parameters: GetSocketRpcClientParameters<socket>,
99
105
  ): Promise<SocketRpcClient<socket>> {
@@ -289,22 +295,29 @@ export async function getSocketRpcClient<socket extends {}>(
289
295
  onError?.(error as Error)
290
296
  }
291
297
  },
292
- requestAsync({ body, timeout = 10_000 }) {
298
+ async requestAsync({ body, timeout = 10_000 }) {
293
299
  const id = body.id ?? idCache.take()
294
- return withTimeout(
295
- () =>
296
- new Promise<RpcResponse>((onResponse, onError) =>
297
- this.request({
298
- body: { ...body, id },
299
- onError,
300
- onResponse,
301
- }),
302
- ),
303
- {
304
- errorInstance: new TimeoutError({ body, url }),
305
- timeout,
306
- },
307
- ).finally(() => requests.delete(id))
300
+ try {
301
+ return await withTimeout(
302
+ () =>
303
+ new Promise<RpcResponse>((onResponse, onError) =>
304
+ this.request({
305
+ body: { ...body, id },
306
+ onError,
307
+ onResponse,
308
+ }),
309
+ ),
310
+ {
311
+ errorInstance: timedOut,
312
+ timeout,
313
+ },
314
+ )
315
+ } catch (error) {
316
+ if (error === timedOut) throw new TimeoutError({ body, url })
317
+ throw error
318
+ } finally {
319
+ requests.delete(id)
320
+ }
308
321
  },
309
322
  requests,
310
323
  subscriptions,
@@ -188,8 +188,11 @@ async function getFinalizeWithdrawalParams<
188
188
  parameters: { hash: Hex; index: number },
189
189
  ) {
190
190
  const { hash } = parameters
191
- const { log, l1BatchTxId } = await getWithdrawalLog(client, parameters)
192
- const { l2ToL1LogIndex } = await getWithdrawalL2ToL1Log(client, parameters)
191
+ // Both read the same receipt, which concurrent requests fetch once.
192
+ const [{ log, l1BatchTxId }, { l2ToL1LogIndex }] = await Promise.all([
193
+ getWithdrawalLog(client, parameters),
194
+ getWithdrawalL2ToL1Log(client, parameters),
195
+ ])
193
196
  const sender = slice(log.topics[1]!, 12) as Address
194
197
  const proof = await getLogProof(client, {
195
198
  txHash: hash,
@@ -78,11 +78,11 @@ export async function isWithdrawalFinalized<
78
78
 
79
79
  if (!l2Client.chain) throw new ChainNotFoundError()
80
80
 
81
- const { log } = await getWithdrawalLog(l2Client, { hash, index })
82
- const { l2ToL1LogIndex } = await getWithdrawalL2ToL1Log(l2Client, {
83
- hash,
84
- index,
85
- })
81
+ // Both read the same receipt, which concurrent requests fetch once.
82
+ const [{ log }, { l2ToL1LogIndex }] = await Promise.all([
83
+ getWithdrawalLog(l2Client, { hash, index }),
84
+ getWithdrawalL2ToL1Log(l2Client, { hash, index }),
85
+ ])
86
86
 
87
87
  // `getLogProof` is called not to get proof but
88
88
  // to get the index of the corresponding L2->L1 log,