-
-
Notifications
You must be signed in to change notification settings - Fork 944
/
Copy pathbuildRequest.ts
239 lines (235 loc) · 8.07 KB
/
buildRequest.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import { BaseError } from '../errors/base.js'
import {
HttpRequestError,
type HttpRequestErrorType,
type RpcRequestErrorType,
type TimeoutErrorType,
type WebSocketRequestErrorType,
} from '../errors/request.js'
import {
ChainDisconnectedError,
type ChainDisconnectedErrorType,
InternalRpcError,
type InternalRpcErrorType,
InvalidInputRpcError,
type InvalidInputRpcErrorType,
InvalidParamsRpcError,
type InvalidParamsRpcErrorType,
InvalidRequestRpcError,
type InvalidRequestRpcErrorType,
JsonRpcVersionUnsupportedError,
type JsonRpcVersionUnsupportedErrorType,
LimitExceededRpcError,
type LimitExceededRpcErrorType,
MethodNotFoundRpcError,
type MethodNotFoundRpcErrorType,
MethodNotSupportedRpcError,
type MethodNotSupportedRpcErrorType,
ParseRpcError,
type ParseRpcErrorType,
ProviderDisconnectedError,
type ProviderDisconnectedErrorType,
type ProviderRpcErrorCode,
ResourceNotFoundRpcError,
type ResourceNotFoundRpcErrorType,
ResourceUnavailableRpcError,
type ResourceUnavailableRpcErrorType,
type RpcError,
type RpcErrorCode,
type RpcErrorType,
SwitchChainError,
type SwitchChainErrorType,
TransactionRejectedRpcError,
type TransactionRejectedRpcErrorType,
UnauthorizedProviderError,
type UnauthorizedProviderErrorType,
UnknownRpcError,
type UnknownRpcErrorType,
UnsupportedProviderMethodError,
type UnsupportedProviderMethodErrorType,
UserRejectedRequestError,
type UserRejectedRequestErrorType,
} from '../errors/rpc.js'
import type { ErrorType } from '../errors/utils.js'
import type {
EIP1193RequestFn,
EIP1193RequestOptions,
} from '../types/eip1193.js'
import { stringToHex } from './encoding/toHex.js'
import { keccak256 } from './hash/keccak256.js'
import type { CreateBatchSchedulerErrorType } from './promise/createBatchScheduler.js'
import { withDedupe } from './promise/withDedupe.js'
import { type WithRetryErrorType, withRetry } from './promise/withRetry.js'
import type { GetSocketRpcClientErrorType } from './rpc/socket.js'
import { stringify } from './stringify.js'
export type RequestErrorType =
| ChainDisconnectedErrorType
| CreateBatchSchedulerErrorType
| HttpRequestErrorType
| InternalRpcErrorType
| InvalidInputRpcErrorType
| InvalidParamsRpcErrorType
| InvalidRequestRpcErrorType
| GetSocketRpcClientErrorType
| JsonRpcVersionUnsupportedErrorType
| LimitExceededRpcErrorType
| MethodNotFoundRpcErrorType
| MethodNotSupportedRpcErrorType
| ParseRpcErrorType
| ProviderDisconnectedErrorType
| ResourceNotFoundRpcErrorType
| ResourceUnavailableRpcErrorType
| RpcErrorType
| RpcRequestErrorType
| SwitchChainErrorType
| TimeoutErrorType
| TransactionRejectedRpcErrorType
| UnauthorizedProviderErrorType
| UnknownRpcErrorType
| UnsupportedProviderMethodErrorType
| UserRejectedRequestErrorType
| WebSocketRequestErrorType
| WithRetryErrorType
| ErrorType
export function buildRequest<request extends (args: any) => Promise<any>>(
request: request,
options: EIP1193RequestOptions = {},
): EIP1193RequestFn {
return async (args, overrideOptions = {}) => {
const {
dedupe = false,
retryDelay = 150,
retryCount = 3,
uid,
} = {
...options,
...overrideOptions,
}
const requestId = dedupe
? keccak256(stringToHex(`${uid}.${stringify(args)}`))
: undefined
return withDedupe(
() =>
withRetry(
async () => {
try {
return await request(args)
} catch (err_) {
const err = err_ as unknown as RpcError<
RpcErrorCode | ProviderRpcErrorCode
>
switch (err.code) {
// -32700
case ParseRpcError.code:
throw new ParseRpcError(err)
// -32600
case InvalidRequestRpcError.code:
throw new InvalidRequestRpcError(err)
// -32601
case MethodNotFoundRpcError.code:
throw new MethodNotFoundRpcError(err, { method: args.method })
// -32602
case InvalidParamsRpcError.code:
throw new InvalidParamsRpcError(err)
// -32603
case InternalRpcError.code:
throw new InternalRpcError(err)
// -32000
case InvalidInputRpcError.code:
throw new InvalidInputRpcError(err)
// -32001
case ResourceNotFoundRpcError.code:
throw new ResourceNotFoundRpcError(err)
// -32002
case ResourceUnavailableRpcError.code:
throw new ResourceUnavailableRpcError(err)
// -32003
case TransactionRejectedRpcError.code:
throw new TransactionRejectedRpcError(err)
// -32004
case MethodNotSupportedRpcError.code:
throw new MethodNotSupportedRpcError(err, {
method: args.method,
})
// -32005
case LimitExceededRpcError.code:
throw new LimitExceededRpcError(err)
// -32006
case JsonRpcVersionUnsupportedError.code:
throw new JsonRpcVersionUnsupportedError(err)
// 4001
case UserRejectedRequestError.code:
throw new UserRejectedRequestError(err)
// 4100
case UnauthorizedProviderError.code:
throw new UnauthorizedProviderError(err)
// 4200
case UnsupportedProviderMethodError.code:
throw new UnsupportedProviderMethodError(err)
// 4900
case ProviderDisconnectedError.code:
throw new ProviderDisconnectedError(err)
// 4901
case ChainDisconnectedError.code:
throw new ChainDisconnectedError(err)
// 4902
case SwitchChainError.code:
throw new SwitchChainError(err)
// CAIP-25: User Rejected Error
// https://docs.walletconnect.com/2.0/specs/clients/sign/error-codes#rejected-caip-25
case 5000:
throw new UserRejectedRequestError(err)
default:
if (err_ instanceof BaseError) throw err_
throw new UnknownRpcError(err as Error)
}
}
},
{
delay: ({ count, error }) => {
// If we find a Retry-After header, let's retry after the given time.
if (error && error instanceof HttpRequestError) {
const retryAfter = error?.headers?.get('Retry-After')
if (retryAfter?.match(/\d/))
return Number.parseInt(retryAfter) * 1000
}
// Otherwise, let's retry with an exponential backoff.
return ~~(1 << count) * retryDelay
},
retryCount,
shouldRetry: ({ error }) => shouldRetry(error),
},
),
{ enabled: dedupe, id: requestId },
)
}
}
/** @internal */
export function shouldRetry(error: Error) {
if ('code' in error && typeof error.code === 'number') {
if (error.code === -1) return true // Unknown error
if (error.code === LimitExceededRpcError.code) return true
if (error.code === InternalRpcError.code) return true
return false
}
if (error instanceof HttpRequestError && error.status) {
// Forbidden
if (error.status === 403) return true
// Request Timeout
if (error.status === 408) return true
// Request Entity Too Large
if (error.status === 413) return true
// Too Many Requests
if (error.status === 429) return true
// Internal Server Error
if (error.status === 500) return true
// Bad Gateway
if (error.status === 502) return true
// Service Unavailable
if (error.status === 503) return true
// Gateway Timeout
if (error.status === 504) return true
return false
}
return true
}