Skip to content

Commit 2ba1683

Browse files
[SDK] fix: Update contract ABI caching strategy (#6356)
1 parent 39a8afd commit 2ba1683

File tree

3 files changed

+38
-31
lines changed

3 files changed

+38
-31
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: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { type Abi, formatAbi, parseAbi } from "abitype";
22
import { download } from "../../storage/download.js";
33
import { getClientFetch } from "../../utils/fetch.js";
4+
import { withCache } from "../../utils/promise/withCache.js";
45
import { type ThirdwebContract, getContract } from "../contract.js";
56

6-
const ABI_RESOLUTION_CACHE = new WeakMap<ThirdwebContract<Abi>, Promise<Abi>>();
7-
87
/**
98
* Resolves the ABI (Application Binary Interface) for a given contract.
109
* If the ABI is already cached, it returns the cached value.
@@ -32,31 +31,34 @@ export function resolveContractAbi<abi extends Abi>(
3231
contract: ThirdwebContract<abi>,
3332
contractApiBaseUrl = "https://contract.thirdweb.com/abi",
3433
): Promise<abi> {
35-
if (ABI_RESOLUTION_CACHE.has(contract)) {
36-
return ABI_RESOLUTION_CACHE.get(contract) as Promise<abi>;
37-
}
38-
39-
const prom = (async () => {
40-
// if the contract already HAS a user defined we always use that!
41-
if (contract.abi) {
42-
return contract.abi as abi;
43-
}
34+
return withCache(
35+
async () => {
36+
// if the contract already HAS a user defined we always use that!
37+
if (contract.abi) {
38+
return contract.abi as abi;
39+
}
4440

45-
// for local chains, we need to resolve the composite abi from bytecode
46-
if (contract.chain.id === 31337 || contract.chain.id === 1337) {
47-
return await resolveCompositeAbi(contract as ThirdwebContract);
48-
}
41+
// for local chains, we need to resolve the composite abi from bytecode
42+
if (contract.chain.id === 31337 || contract.chain.id === 1337) {
43+
return (await resolveCompositeAbi(contract as ThirdwebContract)) as abi;
44+
}
4945

50-
// try to get it from the api
51-
try {
52-
return await resolveAbiFromContractApi(contract, contractApiBaseUrl);
53-
} catch {
54-
// if that fails, try to resolve it from the bytecode
55-
return await resolveCompositeAbi(contract as ThirdwebContract);
56-
}
57-
})();
58-
ABI_RESOLUTION_CACHE.set(contract, prom);
59-
return prom as Promise<abi>;
46+
// try to get it from the api
47+
try {
48+
return (await resolveAbiFromContractApi(
49+
contract,
50+
contractApiBaseUrl,
51+
)) as abi;
52+
} catch {
53+
// if that fails, try to resolve it from the bytecode
54+
return (await resolveCompositeAbi(contract as ThirdwebContract)) as abi;
55+
}
56+
},
57+
{
58+
cacheKey: `${contract.chain.id}-${contract.address}`,
59+
cacheTime: 1000 * 60 * 60 * 1, // 1 hour
60+
},
61+
);
6062
}
6163

6264
/**

0 commit comments

Comments
 (0)