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/sour-kiwis-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Better fallbacks for live NFT data cache miss
2 changes: 2 additions & 0 deletions apps/dashboard/src/@/constants/thirdweb.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
import {
THIRDWEB_BUNDLER_DOMAIN,
THIRDWEB_INAPP_WALLET_DOMAIN,
THIRDWEB_INSIGHT_API_DOMAIN,
THIRDWEB_PAY_DOMAIN,
THIRDWEB_RPC_DOMAIN,
THIRDWEB_SOCIAL_API_DOMAIN,
Expand All @@ -32,6 +33,7 @@ export function getThirdwebClient(jwt?: string) {
storage: THIRDWEB_STORAGE_DOMAIN,
social: THIRDWEB_SOCIAL_API_DOMAIN,
bundler: THIRDWEB_BUNDLER_DOMAIN,
insight: THIRDWEB_INSIGHT_API_DOMAIN,
});
}

Expand Down
3 changes: 3 additions & 0 deletions apps/dashboard/src/constants/urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ export const THIRDWEB_SOCIAL_API_DOMAIN =

export const THIRDWEB_BUNDLER_DOMAIN =
process.env.NEXT_PUBLIC_BUNDLER_URL || "bundler.thirdweb-dev.com";

export const THIRDWEB_INSIGHT_API_DOMAIN =
process.env.NEXT_PUBLIC_INSIGHT_API_URL || "insight.thirdweb-dev.com";
19 changes: 2 additions & 17 deletions packages/thirdweb/src/extensions/erc1155/read/getNFT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,15 @@ export async function getNFT(
async function getNFTFromInsight(
options: BaseTransactionOptions<GetNFTParams>,
): Promise<NFT> {
const tokenId = options.tokenId;
const nft = await getNFTInsight({
client: options.contract.client,
chain: options.contract.chain,
contractAddress: options.contract.address,
tokenId: options.tokenId,
});
if (!nft) {
return parseNFT(
{
id: tokenId,
type: "ERC1155",
uri: "",
},
{
tokenId: options.tokenId,
tokenUri: "",
type: "ERC1155",
owner: null,
supply: 0n,
tokenAddress: options.contract.address,
chainId: options.contract.chain.id,
},
);
// fresh contracts might be delayed in indexing, so we fallback to RPC
return getNFTFromRPC(options);
}
return nft;
}
Expand Down
31 changes: 22 additions & 9 deletions packages/thirdweb/src/extensions/erc1155/read/getNFTs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,29 @@
): Promise<NFT[]> {
const { contract, start, count = Number(DEFAULT_QUERY_ALL_COUNT) } = options;

const result = await getContractNFTs({
client: contract.client,
chains: [contract.chain],
contractAddress: contract.address,
queryOptions: {
limit: count,
page: start ? Math.floor(start / count) : undefined,
},
});
const [result, supply] = await Promise.all([
getContractNFTs({
client: contract.client,
chains: [contract.chain],
contractAddress: contract.address,
queryOptions: {
limit: count,
page: start ? Math.floor(start / count) : undefined,
},
}),
nextTokenIdToMint(options).catch(() => maxUint256),
]);

const currentOffset = start ?? 0;

Check warning on line 81 in packages/thirdweb/src/extensions/erc1155/read/getNFTs.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/extensions/erc1155/read/getNFTs.ts#L81

Added line #L81 was not covered by tests
const expectedResultLength = Math.min(
count,
Math.max(0, Number(supply) - currentOffset),
);
if (result.length < expectedResultLength) {
// fresh contracts might be delayed in indexing, so we fallback to RPC
return getNFTsFromRPC(options);
}
return result;

Check warning on line 90 in packages/thirdweb/src/extensions/erc1155/read/getNFTs.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/extensions/erc1155/read/getNFTs.ts#L88-L90

Added lines #L88 - L90 were not covered by tests
}

async function getNFTsFromRPC(
Expand All @@ -95,6 +107,7 @@
getNFT({
...options,
tokenId: i,
useIndexer: false,
}),
);
}
Expand Down
18 changes: 2 additions & 16 deletions packages/thirdweb/src/extensions/erc721/read/getNFT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ export async function getNFT(
async function getNFTFromInsight(
options: BaseTransactionOptions<GetNFTParams>,
): Promise<NFT> {
const tokenId = options.tokenId;
const nft = await getNFTInsight({
client: options.contract.client,
chain: options.contract.chain,
Expand All @@ -87,21 +86,8 @@ async function getNFTFromInsight(
includeOwners: options.includeOwner,
});
if (!nft) {
return parseNFT(
{
id: tokenId,
type: "ERC721",
uri: "",
},
{
tokenId,
tokenUri: "",
type: "ERC721",
owner: null,
tokenAddress: options.contract.address,
chainId: options.contract.chain.id,
},
);
// fresh contracts might be delayed in indexing, so we fallback to RPC
return getNFTFromRPC(options);
}
return nft;
}
Expand Down
34 changes: 24 additions & 10 deletions packages/thirdweb/src/extensions/erc721/read/getNFTs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,29 @@
): Promise<NFT[]> {
const { contract, start, count = Number(DEFAULT_QUERY_ALL_COUNT) } = options;

const result = await getContractNFTs({
client: contract.client,
chains: [contract.chain],
contractAddress: contract.address,
includeOwners: options.includeOwners ?? false,
queryOptions: {
limit: count,
page: start ? Math.floor(start / count) : undefined,
},
});
const [result, supply] = await Promise.all([
getContractNFTs({
client: contract.client,
chains: [contract.chain],
contractAddress: contract.address,
includeOwners: options.includeOwners ?? false,
queryOptions: {
limit: count,
page: start ? Math.floor(start / count) : undefined,
},
}),
totalSupply(options),
]);

const currentOffset = start ?? 0;
const expectedResultLength = Math.min(
count,
Math.max(0, Number(supply) - currentOffset),
);
if (result.length < expectedResultLength) {
// fresh contracts might be delayed in indexing, so we fallback to RPC
return getNFTsFromRPC(options);
}

Check warning on line 129 in packages/thirdweb/src/extensions/erc721/read/getNFTs.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/extensions/erc721/read/getNFTs.ts#L128-L129

Added lines #L128 - L129 were not covered by tests

return result;
}
Expand Down Expand Up @@ -158,6 +171,7 @@
...options,
tokenId: i,
includeOwner: options.includeOwners ?? false,
useIndexer: false,
}),
);
}
Expand Down
Loading