Skip to content

Commit

Permalink
[SDK] fix: Update contract ABI caching strategy (#6356)
Browse files Browse the repository at this point in the history
  • Loading branch information
joaquim-verges authored Feb 27, 2025
1 parent 39a8afd commit 2ba1683
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 31 deletions.
5 changes: 5 additions & 0 deletions .changeset/thick-bees-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Change caching strategy for contract ABI
12 changes: 6 additions & 6 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ env:

jobs:
optimize_ci:
runs-on: ubuntu-latest
runs-on: ubuntu-latest-8
outputs:
skip: ${{ steps.check_skip.outputs.skip }}
steps:
Expand All @@ -34,7 +34,7 @@ jobs:
build:
needs: optimize_ci
if: needs.optimize_ci.outputs.skip == 'false'
runs-on: ubuntu-latest
runs-on: ubuntu-latest-8
name: Build Packages
steps:
- name: Check out the code
Expand All @@ -51,7 +51,7 @@ jobs:
if: needs.optimize_ci.outputs.skip == 'false'
timeout-minutes: 15
name: Lint Packages
runs-on: ubuntu-latest
runs-on: ubuntu-latest-8
steps:
- name: Check out the code
uses: actions/checkout@v4
Expand All @@ -71,7 +71,7 @@ jobs:
if: needs.optimize_ci.outputs.skip == 'false'
timeout-minutes: 15
name: Unit Tests
runs-on: ubuntu-latest
runs-on: ubuntu-latest-8
steps:
- name: Check out the code
uses: actions/checkout@v4
Expand Down Expand Up @@ -99,7 +99,7 @@ jobs:
if: needs.optimize_ci.outputs.skip == 'false'
timeout-minutes: 15
name: E2E Tests
runs-on: ubuntu-latest
runs-on: ubuntu-latest-8
strategy:
matrix:
package_manager: [npm, yarn, pnpm, bun]
Expand Down Expand Up @@ -169,7 +169,7 @@ jobs:
if: github.event_name == 'pull_request' && needs.optimize_ci.outputs.skip == 'false'
timeout-minutes: 15
name: "Size"
runs-on: ubuntu-latest
runs-on: ubuntu-latest-8
steps:
- name: Check out the code
uses: actions/checkout@v4
Expand Down
52 changes: 27 additions & 25 deletions packages/thirdweb/src/contract/actions/resolve-abi.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { type Abi, formatAbi, parseAbi } from "abitype";
import { download } from "../../storage/download.js";
import { getClientFetch } from "../../utils/fetch.js";
import { withCache } from "../../utils/promise/withCache.js";
import { type ThirdwebContract, getContract } from "../contract.js";

const ABI_RESOLUTION_CACHE = new WeakMap<ThirdwebContract<Abi>, Promise<Abi>>();

/**
* Resolves the ABI (Application Binary Interface) for a given contract.
* If the ABI is already cached, it returns the cached value.
Expand Down Expand Up @@ -32,31 +31,34 @@ export function resolveContractAbi<abi extends Abi>(
contract: ThirdwebContract<abi>,
contractApiBaseUrl = "https://contract.thirdweb.com/abi",
): Promise<abi> {
if (ABI_RESOLUTION_CACHE.has(contract)) {
return ABI_RESOLUTION_CACHE.get(contract) as Promise<abi>;
}

const prom = (async () => {
// if the contract already HAS a user defined we always use that!
if (contract.abi) {
return contract.abi as abi;
}
return withCache(
async () => {
// if the contract already HAS a user defined we always use that!
if (contract.abi) {
return contract.abi as abi;
}

// for local chains, we need to resolve the composite abi from bytecode
if (contract.chain.id === 31337 || contract.chain.id === 1337) {
return await resolveCompositeAbi(contract as ThirdwebContract);
}
// for local chains, we need to resolve the composite abi from bytecode
if (contract.chain.id === 31337 || contract.chain.id === 1337) {
return (await resolveCompositeAbi(contract as ThirdwebContract)) as abi;
}

// try to get it from the api
try {
return await resolveAbiFromContractApi(contract, contractApiBaseUrl);
} catch {
// if that fails, try to resolve it from the bytecode
return await resolveCompositeAbi(contract as ThirdwebContract);
}
})();
ABI_RESOLUTION_CACHE.set(contract, prom);
return prom as Promise<abi>;
// try to get it from the api
try {
return (await resolveAbiFromContractApi(
contract,
contractApiBaseUrl,
)) as abi;
} catch {
// if that fails, try to resolve it from the bytecode
return (await resolveCompositeAbi(contract as ThirdwebContract)) as abi;
}
},
{
cacheKey: `${contract.chain.id}-${contract.address}`,
cacheTime: 1000 * 60 * 60 * 1, // 1 hour
},
);
}

/**
Expand Down

0 comments on commit 2ba1683

Please sign in to comment.