Skip to content

Commit 2412a69

Browse files
authored
Handle null metadata in ERC721 and ERC1155 getNFT functions (#8312)
1 parent bfd3be7 commit 2412a69

File tree

4 files changed

+40
-28
lines changed

4 files changed

+40
-28
lines changed

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,23 @@ async function getNFTFromRPC(
8383
client: options.contract.client,
8484
tokenId: options.tokenId,
8585
tokenUri,
86-
}).catch(() => ({
87-
id: options.tokenId,
88-
type: "ERC1155",
89-
uri: tokenUri,
90-
})),
86+
})
87+
.then((metadata) => {
88+
// if the metadata is null-ish, return a default metadata object
89+
if (!metadata) {
90+
return {
91+
id: options.tokenId,
92+
type: "ERC1155",
93+
uri: tokenUri,
94+
};
95+
}
96+
return metadata;
97+
})
98+
.catch(() => ({
99+
id: options.tokenId,
100+
type: "ERC1155",
101+
uri: tokenUri,
102+
})),
91103
{
92104
chainId: options.contract.chain.id,
93105
owner: null,

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,12 @@ export async function getNFTs(
5252
const { useIndexer = true } = options;
5353
if (useIndexer) {
5454
try {
55-
return await getNFTsFromInsight(options).then((nfts) =>
56-
nfts.filter((nft) => nft?.id !== undefined && nft?.id !== null),
57-
);
55+
return await getNFTsFromInsight(options);
5856
} catch {
59-
return await getNFTsFromRPC(options).then((nfts) =>
60-
nfts.filter((nft) => nft?.id !== undefined && nft?.id !== null),
61-
);
57+
return await getNFTsFromRPC(options);
6258
}
6359
}
64-
return await getNFTsFromRPC(options).then((nfts) =>
65-
nfts.filter((nft) => nft?.id !== undefined && nft?.id !== null),
66-
);
60+
return await getNFTsFromRPC(options);
6761
}
6862

6963
async function getNFTsFromInsight(

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,23 @@ async function getNFTFromRPC(
139139
client: options.contract.client,
140140
tokenId,
141141
tokenUri: uri,
142-
}).catch(() => ({
143-
id: tokenId,
144-
type: "ERC721",
145-
uri,
146-
})),
142+
})
143+
.then((metadata) => {
144+
// if the metadata is null-ish, return a default metadata object
145+
if (!metadata) {
146+
return {
147+
id: tokenId,
148+
type: "ERC721",
149+
uri,
150+
};
151+
}
152+
return metadata;
153+
})
154+
.catch(() => ({
155+
id: tokenId,
156+
type: "ERC721",
157+
uri,
158+
})),
147159
{
148160
chainId: options.contract.chain.id,
149161
owner,

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,12 @@ export async function getNFTs(
7171
const { useIndexer = true } = options;
7272
if (useIndexer) {
7373
try {
74-
return await getNFTsFromInsight(options).then((nfts) =>
75-
nfts.filter((nft) => nft?.id !== undefined && nft?.id !== null),
76-
);
74+
return await getNFTsFromInsight(options);
7775
} catch {
78-
return await getNFTsFromRPC(options).then((nfts) =>
79-
nfts.filter((nft) => nft?.id !== undefined && nft?.id !== null),
80-
);
76+
return await getNFTsFromRPC(options);
8177
}
8278
}
83-
return await getNFTsFromRPC(options).then((nfts) =>
84-
nfts.filter((nft) => nft?.id !== undefined && nft?.id !== null),
85-
);
79+
return await getNFTsFromRPC(options);
8680
}
8781

8882
/**

0 commit comments

Comments
 (0)