Skip to content

Commit 7234d99

Browse files
committed
cache eth_call and getChainID
1 parent cee60f0 commit 7234d99

File tree

1 file changed

+35
-15
lines changed

1 file changed

+35
-15
lines changed

packages/messenger-widget/src/hooks/mainnetprovider/cache/providerCache.ts

+35-15
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,65 @@
11
/* eslint-disable no-console */
2+
import { sha256 } from '@dm3-org/dm3-lib-shared';
23
import { ethers } from 'ethers';
4+
import { keccak256 } from 'viem';
35

46
export const getCachedProvider = (
57
provider: ethers.providers.JsonRpcProvider,
68
) => {
79
const cache = new Map<string, any>();
10+
11+
const unique = new Set();
812
const cacheHandler: ProxyHandler<ethers.providers.JsonRpcProvider> = {
913
get: (target, fnSig, receiver) => {
1014
if (fnSig === 'send') {
1115
return async (method: string, ...args: any[]) => {
1216
//Simpy caching chainId safes 10 sek of initial load time
1317
if (method === 'eth_chainId') {
18+
console.log(method);
1419
const key = `${fnSig}-${method}`;
1520
if (cache.has(key)) {
21+
console.log('cache hit ', key);
1622
return cache.get(key);
1723
}
24+
25+
console.log(
26+
'chain id cache miss ',
27+
sha256(method + fnSig),
28+
);
1829
//@ts-ignore
1930
const result = await target[fnSig](method);
2031
cache.set(key, result);
2132
return result;
2233
}
2334

35+
if (method === 'eth_call') {
36+
const [[{ data, to }]] = args;
37+
const key = `${fnSig}-${method}-${sha256(to)}-${sha256(
38+
data,
39+
)}`;
40+
if (cache.has(key)) {
41+
console.log('eth_call cache hit ');
42+
return cache.get(key);
43+
}
44+
45+
console.log(
46+
'ethcall cache miss ',
47+
sha256(method + fnSig),
48+
);
49+
50+
//@ts-ignore
51+
const result = await target[fnSig](method, ...args);
52+
cache.set(key, result);
53+
return result;
54+
}
55+
56+
console.log('send cache miss ', sha256(method + fnSig));
57+
2458
//@ts-ignore
2559
return target[fnSig](method, ...args);
2660
};
2761
}
28-
//TODO figure out how to cache inner call
29-
/* if (fnSig === 'getResolver') {
30-
return async (address: string) => {
31-
const key = `${fnSig}-${address}`;
32-
if (cache.has(key)) {
33-
console.log('cache hit ', key);
34-
return cache.get(key);
35-
}
36-
//@ts-ignore
37-
const result = await target[fnSig](address);
38-
//@ts-ignore
39-
cache.set(key, result);
40-
return result;
41-
};
42-
} */
62+
4363
//@ts-ignore
4464
return target[fnSig];
4565
},

0 commit comments

Comments
 (0)