Skip to content

Commit f38b262

Browse files
[SDK] Refactor ERC721 getNFTs to handle token ID offsets correctly (#6768)
1 parent 5b35a62 commit f38b262

File tree

3 files changed

+40
-22
lines changed

3 files changed

+40
-22
lines changed

packages/thirdweb/src/extensions/erc1155/read/getOwnedNFTs.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ async function getOwnedNFTsFromRPC(
9797
const ownedBalances = await getOwnedTokenIds(options);
9898

9999
const nfts = await Promise.all(
100-
ownedBalances.map((ob) => getNFT({ ...options, tokenId: ob.tokenId })),
100+
ownedBalances.map((ob) =>
101+
getNFT({ ...options, tokenId: ob.tokenId, useIndexer: false }),
102+
),
101103
);
102104

103105
return nfts.map((nft, index) => ({

packages/thirdweb/src/extensions/erc721/read/getNFTs.ts

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ async function getNFTsFromInsight(
104104
): Promise<NFT[]> {
105105
const { contract, start, count = Number(DEFAULT_QUERY_ALL_COUNT) } = options;
106106

107-
const [result, supply] = await Promise.all([
107+
const [result, supplyInfo] = await Promise.all([
108108
getContractNFTs({
109109
client: contract.client,
110110
chains: [contract.chain],
@@ -115,13 +115,21 @@ async function getNFTsFromInsight(
115115
page: start ? Math.floor(start / count) : undefined,
116116
},
117117
}),
118-
totalSupply(options),
118+
getSupplyInfo(options).catch(() => ({
119+
maxSupply: 0,
120+
startTokenId: 0,
121+
})),
119122
]);
120123

121124
const currentOffset = start ?? 0;
122125
const expectedResultLength = Math.min(
123126
count,
124-
Math.max(0, Number(supply) - currentOffset),
127+
Math.max(
128+
0,
129+
Number(supplyInfo.maxSupply) -
130+
Number(supplyInfo.startTokenId) -
131+
currentOffset,
132+
),
125133
);
126134
if (result.length < expectedResultLength) {
127135
// fresh contracts might be delayed in indexing, so we fallback to RPC
@@ -134,6 +142,27 @@ async function getNFTsFromInsight(
134142
async function getNFTsFromRPC(
135143
options: BaseTransactionOptions<GetNFTsParams>,
136144
): Promise<NFT[]> {
145+
const { startTokenId, maxSupply } = await getSupplyInfo(options);
146+
const start = BigInt(options.start ?? 0) + startTokenId;
147+
const count = BigInt(options.count ?? DEFAULT_QUERY_ALL_COUNT);
148+
const maxId = min(maxSupply, start + count);
149+
const promises: ReturnType<typeof getNFT>[] = [];
150+
151+
for (let i = start; i < maxId; i++) {
152+
promises.push(
153+
getNFT({
154+
...options,
155+
tokenId: i,
156+
includeOwner: options.includeOwners ?? false,
157+
useIndexer: false,
158+
}),
159+
);
160+
}
161+
162+
return await Promise.all(promises);
163+
}
164+
165+
async function getSupplyInfo(options: BaseTransactionOptions<GetNFTsParams>) {
137166
const [startTokenId_, maxSupply] = await Promise.allSettled([
138167
startTokenId(options),
139168
nextTokenIdToMint(options),
@@ -158,23 +187,9 @@ async function getNFTsFromRPC(
158187
}
159188
return [startTokenId__, maxSupply_] as const;
160189
});
161-
const start = BigInt(options.start ?? 0) + startTokenId_;
162-
const count = BigInt(options.count ?? DEFAULT_QUERY_ALL_COUNT);
163-
164-
const maxId = min(maxSupply + startTokenId_, start + count);
165-
166-
const promises: ReturnType<typeof getNFT>[] = [];
167190

168-
for (let i = start; i < maxId; i++) {
169-
promises.push(
170-
getNFT({
171-
...options,
172-
tokenId: i,
173-
includeOwner: options.includeOwners ?? false,
174-
useIndexer: false,
175-
}),
176-
);
177-
}
178-
179-
return await Promise.all(promises);
191+
return {
192+
startTokenId: startTokenId_,
193+
maxSupply,
194+
};
180195
}

packages/thirdweb/src/extensions/erc721/read/getOwnedNFTs.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ async function getOwnedNFTsFromRPC(
5454
getNFT({
5555
contract: options.contract,
5656
tokenId,
57+
useIndexer: false,
5758
}).then((nft) => ({
5859
...nft,
5960
// add the owner to the NFT since we know it

0 commit comments

Comments
 (0)