Skip to content

Commit 4606540

Browse files
[thirdweb] fix: Update contract ABI caching strategy
1 parent 3f6d525 commit 4606540

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

.changeset/thick-bees-deny.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Change caching strategy for contract ABI

packages/thirdweb/src/contract/actions/resolve-abi.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { type Abi, formatAbi, parseAbi } from "abitype";
22
import { download } from "../../storage/download.js";
3+
import { LruMap } from "../../utils/caching/lru.js";
34
import { getClientFetch } from "../../utils/fetch.js";
45
import { type ThirdwebContract, getContract } from "../contract.js";
56

6-
const ABI_RESOLUTION_CACHE = new WeakMap<ThirdwebContract<Abi>, Promise<Abi>>();
7+
const ABI_RESOLUTION_CACHE = new LruMap<Promise<Abi>>(1000);
78

89
/**
910
* Resolves the ABI (Application Binary Interface) for a given contract.
@@ -32,8 +33,9 @@ export function resolveContractAbi<abi extends Abi>(
3233
contract: ThirdwebContract<abi>,
3334
contractApiBaseUrl = "https://contract.thirdweb.com/abi",
3435
): Promise<abi> {
35-
if (ABI_RESOLUTION_CACHE.has(contract)) {
36-
return ABI_RESOLUTION_CACHE.get(contract) as Promise<abi>;
36+
const key = `${contract.chain.id}-${contract.address}`;
37+
if (ABI_RESOLUTION_CACHE.has(key)) {
38+
return ABI_RESOLUTION_CACHE.get(key) as Promise<abi>;
3739
}
3840

3941
const prom = (async () => {
@@ -55,7 +57,7 @@ export function resolveContractAbi<abi extends Abi>(
5557
return await resolveCompositeAbi(contract as ThirdwebContract);
5658
}
5759
})();
58-
ABI_RESOLUTION_CACHE.set(contract, prom);
60+
ABI_RESOLUTION_CACHE.set(key, prom);
5961
return prom as Promise<abi>;
6062
}
6163

0 commit comments

Comments
 (0)