File tree Expand file tree Collapse file tree 2 files changed +38
-1
lines changed Expand file tree Collapse file tree 2 files changed +38
-1
lines changed Original file line number Diff line number Diff line change
1
+ import { sleep } from 'bun' ;
2
+ import type { PublicClient } from 'viem' ;
3
+
4
+ async function retryOnRateLimit < T > (
5
+ fn : ( ) => Promise < T > ,
6
+ maxRetries = 5 ,
7
+ initialDelay = 1000 ,
8
+ ) : Promise < T > {
9
+ let retries = 0 ;
10
+ while ( retries < maxRetries ) {
11
+ try {
12
+ return await fn ( ) ;
13
+ } catch ( error ) {
14
+ if ( error instanceof Error && error . message . includes ( '429' ) && retries < maxRetries ) {
15
+ const delay = initialDelay * 2 ** retries ;
16
+ await sleep ( delay ) ;
17
+ retries ++ ;
18
+ } else {
19
+ throw error ;
20
+ }
21
+ }
22
+ }
23
+ throw new Error ( 'Max retries reached' ) ;
24
+ }
25
+
26
+ export function createRetryClient ( client : PublicClient ) : PublicClient {
27
+ return new Proxy ( client , {
28
+ get ( target , prop , receiver ) {
29
+ const originalMethod = Reflect . get ( target , prop , receiver ) ;
30
+ if ( typeof originalMethod === 'function' ) {
31
+ return ( ...args : unknown [ ] ) => retryOnRateLimit ( ( ) => originalMethod . apply ( target , args ) ) ;
32
+ }
33
+ return originalMethod ;
34
+ } ,
35
+ } ) ;
36
+ }
Original file line number Diff line number Diff line change 7
7
} from './checks/evm-stack-addresses' ;
8
8
import { checkOpcodes } from './checks/opcodes' ;
9
9
import { checkPrecompiles } from './checks/precompiles' ;
10
+ import { createRetryClient } from './checks/utils' ;
10
11
import type { Metadata } from './types' ;
11
12
import { join } from 'node:path' ;
12
13
@@ -77,7 +78,7 @@ function initClient(rpcUrls: string[]) {
77
78
// Websocket seems to hang and script doesn't exit, so we only use HTTP.
78
79
const https = rpcUrls . filter ( ( url ) => url . startsWith ( 'https' ) ) . map ( ( url ) => http ( url ) ) ;
79
80
const transport = fallback ( [ ...https ] ) ;
80
- return createPublicClient ( { transport } ) ;
81
+ return createRetryClient ( createPublicClient ( { transport } ) ) ;
81
82
}
82
83
83
84
async function getMetadata ( chainId : number ) : Promise < Metadata > {
You can’t perform that action at this time.
0 commit comments