Skip to content

Commit 6e5e53b

Browse files
committed
Improve NFT Metadata & Filters
1 parent 1fef151 commit 6e5e53b

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

Thirdweb/Thirdweb.Extensions/ThirdwebExtensions.Types.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public struct NFT
157157
public NFTMetadata Metadata { get; set; }
158158

159159
/// <summary>
160-
/// Gets or sets the owner address of the NFT.
160+
/// Gets or sets the owner address of the NFT. This is only applicable for ERC721 tokens.
161161
/// </summary>
162162
public string Owner { get; set; }
163163

@@ -172,7 +172,7 @@ public struct NFT
172172
public BigInteger? Supply { get; set; }
173173

174174
/// <summary>
175-
/// Gets or sets the quantity owned by the user.
175+
/// Gets or sets the quantity owned by the user. This is only applicable for ERC1155 tokens.
176176
/// </summary>
177177
public BigInteger? QuantityOwned { get; set; }
178178
}

Thirdweb/Thirdweb.Extensions/ThirdwebExtensions.cs

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,9 +1177,10 @@ public static async Task<BigInteger> ERC1155_TotalSupply(this ThirdwebContract c
11771177
/// </summary>
11781178
/// <param name="contract">The contract to interact with.</param>
11791179
/// <param name="tokenId">The ID of the token.</param>
1180+
/// <param name="fillOwner">A boolean indicating whether to fill the owner details. Defaults to true.</param>
11801181
/// <returns>A task representing the asynchronous operation, with an NFT result containing the token details.</returns>
11811182
/// <exception cref="ArgumentNullException">Thrown when the contract is null.</exception>
1182-
public static async Task<NFT> ERC721_GetNFT(this ThirdwebContract contract, BigInteger tokenId)
1183+
public static async Task<NFT> ERC721_GetNFT(this ThirdwebContract contract, BigInteger tokenId, bool fillOwner = true)
11831184
{
11841185
if (contract == null)
11851186
{
@@ -1198,14 +1199,17 @@ public static async Task<NFT> ERC721_GetNFT(this ThirdwebContract contract, BigI
11981199
}
11991200
metadata.Id = tokenId.ToString();
12001201

1201-
string owner;
1202-
try
1203-
{
1204-
owner = await contract.ERC721_OwnerOf(tokenId).ConfigureAwait(false);
1205-
}
1206-
catch (Exception)
1202+
var owner = Constants.ADDRESS_ZERO;
1203+
if (fillOwner)
12071204
{
1208-
owner = Constants.ADDRESS_ZERO;
1205+
try
1206+
{
1207+
owner = await contract.ERC721_OwnerOf(tokenId).ConfigureAwait(false);
1208+
}
1209+
catch (Exception)
1210+
{
1211+
owner = Constants.ADDRESS_ZERO;
1212+
}
12091213
}
12101214

12111215
return new NFT
@@ -1214,6 +1218,7 @@ public static async Task<NFT> ERC721_GetNFT(this ThirdwebContract contract, BigI
12141218
Owner = owner,
12151219
Type = NFTType.ERC721,
12161220
Supply = 1,
1221+
QuantityOwned = 1
12171222
};
12181223
}
12191224

@@ -1322,9 +1327,10 @@ public static async Task<List<NFT>> ERC721_GetOwnedNFTs(this ThirdwebContract co
13221327
/// </summary>
13231328
/// <param name="contract">The contract to interact with.</param>
13241329
/// <param name="tokenId">The ID of the token.</param>
1330+
/// <param name="fillSupply">A boolean indicating whether to fill the supply. Defaults to true if not specified.</param>
13251331
/// <returns>A task representing the asynchronous operation, with an NFT result containing the token details.</returns>
13261332
/// <exception cref="ArgumentNullException">Thrown when the contract is null.</exception>
1327-
public static async Task<NFT> ERC1155_GetNFT(this ThirdwebContract contract, BigInteger tokenId)
1333+
public static async Task<NFT> ERC1155_GetNFT(this ThirdwebContract contract, BigInteger tokenId, bool fillSupply = true)
13281334
{
13291335
if (contract == null)
13301336
{
@@ -1342,21 +1348,24 @@ public static async Task<NFT> ERC1155_GetNFT(this ThirdwebContract contract, Big
13421348
metadata = new NFTMetadata { Description = e.Message };
13431349
}
13441350
metadata.Id = tokenId.ToString();
1345-
var owner = string.Empty;
1346-
BigInteger supply;
1347-
try
1348-
{
1349-
supply = await contract.ERC1155_TotalSupply(tokenId).ConfigureAwait(false);
1350-
}
1351-
catch (Exception)
1351+
1352+
var supply = BigInteger.MinusOne;
1353+
if (fillSupply)
13521354
{
1353-
supply = BigInteger.MinusOne;
1355+
try
1356+
{
1357+
supply = await contract.ERC1155_TotalSupply(tokenId).ConfigureAwait(false);
1358+
}
1359+
catch (Exception)
1360+
{
1361+
supply = BigInteger.MinusOne;
1362+
}
13541363
}
13551364

13561365
return new NFT
13571366
{
13581367
Metadata = metadata,
1359-
Owner = owner,
1368+
Owner = "",
13601369
Type = NFTType.ERC1155,
13611370
Supply = supply,
13621371
};
@@ -1454,6 +1463,10 @@ public static async Task<List<NFT>> ERC1155_GetOwnedNFTs(this ThirdwebContract c
14541463
}
14551464

14561465
var ownerNfts = await Task.WhenAll(ownerNftTasks).ConfigureAwait(false);
1466+
for (var i = 0; i < ownerNfts.Length; i++)
1467+
{
1468+
ownerNfts[i].QuantityOwned = balanceOfBatch[i];
1469+
}
14571470
return ownerNfts.ToList();
14581471
}
14591472

0 commit comments

Comments
 (0)