Skip to content

Commit 7bf236d

Browse files
committed
WIP
Signed-off-by: Matt Rice <[email protected]>
1 parent 31669cb commit 7bf236d

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

api/_cache.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,20 @@ export class RedisCache implements interfaces.CachingMechanismInterface {
8888

8989
export const redisCache = new RedisCache();
9090

91+
// For some reason, the upstash client seems to strip the quote characters from the value.
92+
// This is a workaround to add the quote characters back to the value so the provider responses won't fail to parse.
93+
export class ProviderRedisCache extends RedisCache {
94+
async get<T>(key: string): Promise<T | null> {
95+
const value = await super.get<T>(key);
96+
if (typeof value === "string") {
97+
return ('"' + value + '"') as T;
98+
}
99+
return value;
100+
}
101+
}
102+
103+
export const providerRedisCache = new ProviderRedisCache();
104+
91105
export function buildCacheKey(
92106
prefix: string,
93107
...args: (string | number)[]

api/_utils.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ import {
7474
buildInternalCacheKey,
7575
getCachedValue,
7676
makeCacheGetterAndSetter,
77-
redisCache,
77+
providerRedisCache,
7878
} from "./_cache";
7979
import {
8080
MissingParamError,
@@ -1261,6 +1261,23 @@ export const getProvider = (
12611261
return providerCache[cacheKey];
12621262
};
12631263

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+
12641281
/**
12651282
* Resolves a provider from the `rpc-providers.json` configuration file.
12661283
*/
@@ -1290,9 +1307,10 @@ function getProviderFromConfigJson(
12901307
3, // retries
12911308
0.5, // delay
12921309
5, // max. concurrency
1293-
"RPC_PROVIDER", // cache namespace
1310+
`RPC_PROVIDER_${process.env.RPC_CACHE_NAMESPACE}`, // cache namespace
12941311
0, // disable RPC calls logging
1295-
redisCache
1312+
providerRedisCache,
1313+
getCacheBlockDistance(chainId)
12961314
);
12971315
}
12981316

@@ -1301,9 +1319,10 @@ function getProviderFromConfigJson(
13011319
chainId,
13021320
3, // max. concurrency used in `SpeedProvider`
13031321
5, // max. concurrency used in `RateLimitedProvider`
1304-
"RPC_PROVIDER", // cache namespace
1322+
`RPC_PROVIDER_${process.env.RPC_CACHE_NAMESPACE}`, // cache namespace
13051323
0, // disable RPC calls logging
1306-
redisCache
1324+
providerRedisCache,
1325+
getCacheBlockDistance(chainId)
13071326
);
13081327
}
13091328

0 commit comments

Comments
 (0)