Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Comment on lines +48 to +51
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Network requests should be wrapped with timeout and proper error handling, and the AbortSignal should be passed to the fetch calls. The withCache utility should also handle request cancellation properly.

Spotted by Graphite Reviewer (based on CI logs)

Is this helpful? React 👍 or 👎 to let us know.

} 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
Loading