Skip to content

Commit b39e658

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

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
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

.github/workflows/CI.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ env:
2020

2121
jobs:
2222
optimize_ci:
23-
runs-on: ubuntu-latest
23+
runs-on: ubuntu-latest-8
2424
outputs:
2525
skip: ${{ steps.check_skip.outputs.skip }}
2626
steps:
@@ -34,7 +34,7 @@ jobs:
3434
build:
3535
needs: optimize_ci
3636
if: needs.optimize_ci.outputs.skip == 'false'
37-
runs-on: ubuntu-latest
37+
runs-on: ubuntu-latest-8
3838
name: Build Packages
3939
steps:
4040
- name: Check out the code
@@ -51,7 +51,7 @@ jobs:
5151
if: needs.optimize_ci.outputs.skip == 'false'
5252
timeout-minutes: 15
5353
name: Lint Packages
54-
runs-on: ubuntu-latest
54+
runs-on: ubuntu-latest-8
5555
steps:
5656
- name: Check out the code
5757
uses: actions/checkout@v4
@@ -71,7 +71,7 @@ jobs:
7171
if: needs.optimize_ci.outputs.skip == 'false'
7272
timeout-minutes: 15
7373
name: Unit Tests
74-
runs-on: ubuntu-latest
74+
runs-on: ubuntu-latest-8
7575
steps:
7676
- name: Check out the code
7777
uses: actions/checkout@v4
@@ -99,7 +99,7 @@ jobs:
9999
if: needs.optimize_ci.outputs.skip == 'false'
100100
timeout-minutes: 15
101101
name: E2E Tests
102-
runs-on: ubuntu-latest
102+
runs-on: ubuntu-latest-8
103103
strategy:
104104
matrix:
105105
package_manager: [npm, yarn, pnpm, bun]
@@ -169,7 +169,7 @@ jobs:
169169
if: github.event_name == 'pull_request' && needs.optimize_ci.outputs.skip == 'false'
170170
timeout-minutes: 15
171171
name: "Size"
172-
runs-on: ubuntu-latest
172+
runs-on: ubuntu-latest-8
173173
steps:
174174
- name: Check out the code
175175
uses: actions/checkout@v4

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>>(256);
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)