@@ -74,7 +74,7 @@ import {
74
74
buildInternalCacheKey ,
75
75
getCachedValue ,
76
76
makeCacheGetterAndSetter ,
77
- redisCache ,
77
+ providerRedisCache ,
78
78
} from "./_cache" ;
79
79
import {
80
80
MissingParamError ,
@@ -1261,6 +1261,23 @@ export const getProvider = (
1261
1261
return providerCache [ cacheKey ] ;
1262
1262
} ;
1263
1263
1264
+ // For most chains, we can cache immediately.
1265
+ const DEFAULT_CACHE_BLOCK_DISTANCE = 0 ;
1266
+
1267
+ // For chains that can reorg (mainnet and polygon), establish a buffer beyond which reorgs are rare.
1268
+ const CUSTOM_CACHE_BLOCK_DISTANCE : Record < number , number > = {
1269
+ 1 : 2 ,
1270
+ 137 : 10 ,
1271
+ } ;
1272
+
1273
+ function getCacheBlockDistance ( chainId : number ) {
1274
+ const cacheBlockDistance = CUSTOM_CACHE_BLOCK_DISTANCE [ chainId ] ;
1275
+ if ( ! cacheBlockDistance ) {
1276
+ return DEFAULT_CACHE_BLOCK_DISTANCE ;
1277
+ }
1278
+ return cacheBlockDistance ;
1279
+ }
1280
+
1264
1281
/**
1265
1282
* Resolves a provider from the `rpc-providers.json` configuration file.
1266
1283
*/
@@ -1290,9 +1307,10 @@ function getProviderFromConfigJson(
1290
1307
3 , // retries
1291
1308
0.5 , // delay
1292
1309
5 , // max. concurrency
1293
- "RPC_PROVIDER" , // cache namespace
1310
+ `RPC_PROVIDER_ ${ process . env . RPC_CACHE_NAMESPACE } ` , // cache namespace
1294
1311
0 , // disable RPC calls logging
1295
- redisCache
1312
+ providerRedisCache ,
1313
+ getCacheBlockDistance ( chainId )
1296
1314
) ;
1297
1315
}
1298
1316
@@ -1301,9 +1319,10 @@ function getProviderFromConfigJson(
1301
1319
chainId ,
1302
1320
3 , // max. concurrency used in `SpeedProvider`
1303
1321
5 , // max. concurrency used in `RateLimitedProvider`
1304
- "RPC_PROVIDER" , // cache namespace
1322
+ `RPC_PROVIDER_ ${ process . env . RPC_CACHE_NAMESPACE } ` , // cache namespace
1305
1323
0 , // disable RPC calls logging
1306
- redisCache
1324
+ providerRedisCache ,
1325
+ getCacheBlockDistance ( chainId )
1307
1326
) ;
1308
1327
}
1309
1328
0 commit comments