Skip to content

Commit b4eade9

Browse files
committed
Add filters to GetAll fns and tests
1 parent 9818158 commit b4eade9

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

Thirdweb.Tests/Thirdweb.Extensions/Thirdweb.Extensions.Tests.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,17 @@ public async Task GetNFT_721()
893893
Assert.True(nft.QuantityOwned == 1);
894894
}
895895

896+
[Fact(Timeout = 120000)]
897+
public async Task GetNFT_721_NoOwner()
898+
{
899+
var contract = await this.GetTokenERC721Contract();
900+
var nft = await contract.ERC721_GetNFT(0, false);
901+
Assert.Equal(Constants.ADDRESS_ZERO, nft.Owner);
902+
Assert.Equal(NFTType.ERC721, nft.Type);
903+
Assert.True(nft.Supply == 1);
904+
Assert.True(nft.QuantityOwned == 1);
905+
}
906+
896907
[Fact(Timeout = 120000)]
897908
public async Task GetAllNFTs_721()
898909
{
@@ -902,6 +913,16 @@ public async Task GetAllNFTs_721()
902913
Assert.NotEmpty(nfts);
903914
}
904915

916+
[Fact(Timeout = 120000)]
917+
public async Task GetAllNFTs_721_NoOwner()
918+
{
919+
var contract = await this.GetTokenERC721Contract();
920+
var nfts = await contract.ERC721_GetAllNFTs(fillOwner: false);
921+
Assert.NotNull(nfts);
922+
Assert.NotEmpty(nfts);
923+
Assert.True(nfts.All(nft => nft.Owner == Constants.ADDRESS_ZERO));
924+
}
925+
905926
[Fact(Timeout = 120000)]
906927
public async Task GetAllNFTs_721_ExceedTotalSupply()
907928
{
@@ -984,6 +1005,15 @@ public async Task GetNFT_1155()
9841005
Assert.True(nft.Supply >= 0);
9851006
}
9861007

1008+
[Fact(Timeout = 120000)]
1009+
public async Task GetNFT_1155_NoSupply()
1010+
{
1011+
var contract = await this.GetTokenERC1155Contract();
1012+
var nft = await contract.ERC1155_GetNFT(0, false);
1013+
Assert.Equal(NFTType.ERC1155, nft.Type);
1014+
Assert.True(nft.Supply == -1);
1015+
}
1016+
9871017
[Fact(Timeout = 120000)]
9881018
public async Task GetAllNFTs_1155()
9891019
{
@@ -993,6 +1023,16 @@ public async Task GetAllNFTs_1155()
9931023
Assert.NotEmpty(nfts);
9941024
}
9951025

1026+
[Fact(Timeout = 120000)]
1027+
public async Task GetAllNFTs_1155_NoSupply()
1028+
{
1029+
var contract = await this.GetTokenERC1155Contract();
1030+
var nfts = await contract.ERC1155_GetAllNFTs(fillSupply: false);
1031+
Assert.NotNull(nfts);
1032+
Assert.NotEmpty(nfts);
1033+
Assert.True(nfts.All(nft => nft.Supply == -1));
1034+
}
1035+
9961036
[Fact(Timeout = 120000)]
9971037
public async Task GetAllNFTs_1155_ExceedTotalSupply()
9981038
{

Thirdweb/Thirdweb.Extensions/ThirdwebExtensions.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,9 +1228,10 @@ public static async Task<NFT> ERC721_GetNFT(this ThirdwebContract contract, BigI
12281228
/// <param name="contract">The contract to interact with.</param>
12291229
/// <param name="startTokenId">The starting token ID (inclusive). Defaults to 0 if not specified.</param>
12301230
/// <param name="count">The number of tokens to retrieve. Defaults to 100 if not specified.</param>
1231+
/// <param name="fillOwner">A boolean indicating whether to fill the owner details. Defaults to true.</param>
12311232
/// <returns>A task representing the asynchronous operation, with a list of NFT results containing the token details.</returns>
12321233
/// <exception cref="ArgumentNullException">Thrown when the contract is null.</exception>
1233-
public static async Task<List<NFT>> ERC721_GetAllNFTs(this ThirdwebContract contract, int startTokenId = 0, int count = 100)
1234+
public static async Task<List<NFT>> ERC721_GetAllNFTs(this ThirdwebContract contract, int startTokenId = 0, int count = 100, bool fillOwner = true)
12341235
{
12351236
if (contract == null)
12361237
{
@@ -1243,7 +1244,7 @@ public static async Task<List<NFT>> ERC721_GetAllNFTs(this ThirdwebContract cont
12431244
var nftTasks = new List<Task<NFT>>();
12441245
for (var i = startTokenId; i < startTokenId + count; i++)
12451246
{
1246-
nftTasks.Add(contract.ERC721_GetNFT(i));
1247+
nftTasks.Add(contract.ERC721_GetNFT(i, fillOwner));
12471248
}
12481249

12491250
var allNfts = await Task.WhenAll(nftTasks).ConfigureAwait(false);
@@ -1377,31 +1378,32 @@ public static async Task<NFT> ERC1155_GetNFT(this ThirdwebContract contract, Big
13771378
/// <param name="contract">The contract to interact with.</param>
13781379
/// <param name="startTokenId">The starting token ID (inclusive). Defaults to 0 if not specified.</param>
13791380
/// <param name="count">The number of tokens to retrieve. Defaults to the 100 if not specified.</param>
1381+
/// <param name="fillSupply">A boolean indicating whether to fill the supply. Defaults to true if not specified.</param>
13801382
/// <returns>A task representing the asynchronous operation, with a list of NFT results containing the token details.</returns>
13811383
/// <exception cref="ArgumentNullException">Thrown when the contract is null.</exception>
1382-
public static async Task<List<NFT>> ERC1155_GetAllNFTs(this ThirdwebContract contract, int startTokenId = 0, int count = 100)
1384+
public static async Task<List<NFT>> ERC1155_GetAllNFTs(this ThirdwebContract contract, int startTokenId = 0, int count = 100, bool fillSupply = true)
13831385
{
13841386
if (contract == null)
13851387
{
13861388
throw new ArgumentNullException(nameof(contract));
13871389
}
13881390

1389-
BigInteger totalSupply;
1391+
BigInteger totalCount;
13901392
try
13911393
{
13921394
// Not part of IERC1155 so we fallback just in case
1393-
totalSupply = await contract.ERC1155_TotalSupply().ConfigureAwait(false);
1395+
totalCount = await contract.ERC1155_TotalSupply().ConfigureAwait(false);
13941396
}
13951397
catch
13961398
{
1397-
totalSupply = int.MaxValue;
1399+
totalCount = int.MaxValue;
13981400
}
1399-
count = Math.Min(count, (int)(totalSupply - startTokenId));
1401+
count = Math.Min(count, (int)(totalCount - startTokenId));
14001402

14011403
var nftTasks = new List<Task<NFT>>();
14021404
for (var i = startTokenId; i < startTokenId + count; i++)
14031405
{
1404-
nftTasks.Add(contract.ERC1155_GetNFT(i));
1406+
nftTasks.Add(contract.ERC1155_GetNFT(i, fillSupply));
14051407
}
14061408

14071409
var allNfts = await Task.WhenAll(nftTasks).ConfigureAwait(false);

0 commit comments

Comments
 (0)