Skip to content

Commit 36b4c2c

Browse files
authored
Concurrent Queue RPC (#75)
1 parent 9a56157 commit 36b4c2c

File tree

3 files changed

+87
-159
lines changed

3 files changed

+87
-159
lines changed

Thirdweb.Console/Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929

3030
#region Contract Interaction
3131

32-
// var contract = await ThirdwebContract.Create(client: client, address: "0x81ebd23aA79bCcF5AaFb9c9c5B0Db4223c39102e", chain: 421614);
33-
// var readResult = await contract.Read<string>("name");
34-
// Console.WriteLine($"Contract read result: {readResult}");
32+
var contract = await ThirdwebContract.Create(client: client, address: "0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d", chain: 1);
33+
var nfts = await contract.ERC721_GetAllNFTs();
34+
Console.WriteLine($"NFTs: {JsonConvert.SerializeObject(nfts, Formatting.Indented)}");
3535

3636
#endregion
3737

Thirdweb/Thirdweb.Extensions/ThirdwebExtensions.cs

Lines changed: 41 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,26 +1188,21 @@ public static async Task<NFT> ERC721_GetNFT(this ThirdwebContract contract, BigI
11881188
/// </summary>
11891189
/// <param name="contract">The contract to interact with.</param>
11901190
/// <param name="startTokenId">The starting token ID (inclusive). Defaults to 0 if not specified.</param>
1191-
/// <param name="count">The number of tokens to retrieve. Defaults to the total supply if not specified.</param>
1191+
/// <param name="count">The number of tokens to retrieve. Defaults to 100 if not specified.</param>
11921192
/// <returns>A task representing the asynchronous operation, with a list of NFT results containing the token details.</returns>
11931193
/// <exception cref="ArgumentNullException">Thrown when the contract is null.</exception>
1194-
public static async Task<List<NFT>> ERC721_GetAllNFTs(this ThirdwebContract contract, BigInteger? startTokenId = null, BigInteger? count = null)
1194+
public static async Task<List<NFT>> ERC721_GetAllNFTs(this ThirdwebContract contract, int startTokenId = 0, int count = 100)
11951195
{
11961196
if (contract == null)
11971197
{
11981198
throw new ArgumentNullException(nameof(contract));
11991199
}
12001200

1201-
if (startTokenId == null)
1202-
{
1203-
startTokenId = 0;
1204-
}
1205-
12061201
var totalSupply = await contract.ERC721_TotalSupply().ConfigureAwait(false);
1207-
count = count == null ? totalSupply - startTokenId : Math.Min((int)count, (int)(totalSupply - startTokenId));
1202+
count = Math.Min(count, (int)(totalSupply - startTokenId));
12081203

12091204
var nftTasks = new List<Task<NFT>>();
1210-
for (var i = startTokenId.Value; i < startTokenId.Value + count.Value; i++)
1205+
for (var i = startTokenId; i < startTokenId + count; i++)
12111206
{
12121207
nftTasks.Add(contract.ERC721_GetNFT(i));
12131208
}
@@ -1221,13 +1216,13 @@ public static async Task<List<NFT>> ERC721_GetAllNFTs(this ThirdwebContract cont
12211216
/// </summary>
12221217
/// <param name="contract">The contract to interact with.</param>
12231218
/// <param name="owner">The address of the owner.</param>
1224-
/// <param name="startTokenId">The starting token ID (inclusive). Defaults to null if not specified.</param>
1225-
/// <param name="count">The number of tokens to retrieve. Defaults to null if not specified.</param>
1219+
/// <param name="startTokenId">The starting token ID (inclusive). Defaults to 0 if not specified.</param>
1220+
/// <param name="count">The number of tokens to retrieve. Defaults to 100 if not specified.</param>
12261221
/// <returns>A task representing the asynchronous operation, with a list of NFT results containing the token details.</returns>
12271222
/// <remarks>ERC721AQueryable and ERC721Enumerable are supported.</remarks>
12281223
/// <exception cref="ArgumentNullException">Thrown when the contract is null.</exception>
12291224
/// <exception cref="ArgumentException">Thrown when the owner address is null or empty.</exception>
1230-
public static async Task<List<NFT>> ERC721_GetOwnedNFTs(this ThirdwebContract contract, string owner, BigInteger? startTokenId = null, BigInteger? count = null)
1225+
public static async Task<List<NFT>> ERC721_GetOwnedNFTs(this ThirdwebContract contract, string owner, int startTokenId = 0, int count = 100)
12311226
{
12321227
if (contract == null)
12331228
{
@@ -1239,105 +1234,51 @@ public static async Task<List<NFT>> ERC721_GetOwnedNFTs(this ThirdwebContract co
12391234
throw new ArgumentException("Owner must be provided");
12401235
}
12411236

1242-
if (startTokenId == null)
1243-
{
1244-
startTokenId = 0;
1245-
}
1237+
var totalSupply = await contract.ERC721_TotalSupply().ConfigureAwait(false);
1238+
count = Math.Min(count, (int)(totalSupply - startTokenId));
12461239

1247-
// Paginated
1248-
if (count != null)
1240+
// ERC721AQueryable
1241+
try
12491242
{
1250-
var totalSupply = await contract.ERC721_TotalSupply().ConfigureAwait(false);
1251-
count = Math.Min((int)count, (int)(totalSupply - startTokenId));
1252-
1253-
// ERC721AQueryable
1254-
try
1243+
var tokensOfOwnerIn = await contract.ERC721A_TokensOfOwnerIn(owner, startTokenId, startTokenId + count).ConfigureAwait(false);
1244+
var ownedNftTasks = new List<Task<NFT>>();
1245+
foreach (var tokenId in tokensOfOwnerIn)
12551246
{
1256-
var tokensOfOwnerIn = await contract.ERC721A_TokensOfOwnerIn(owner, startTokenId.Value, startTokenId.Value + count.Value).ConfigureAwait(false);
1257-
var ownedNftTasks = new List<Task<NFT>>();
1258-
foreach (var tokenId in tokensOfOwnerIn)
1259-
{
1260-
ownedNftTasks.Add(contract.ERC721_GetNFT(tokenId));
1261-
}
1262-
var ownedNfts = await Task.WhenAll(ownedNftTasks).ConfigureAwait(false);
1263-
return ownedNfts.ToList();
1264-
}
1265-
catch
1266-
{
1267-
// ERC721Enumerable
1268-
try
1269-
{
1270-
var balanceOfOwner = await contract.ERC721_BalanceOf(owner).ConfigureAwait(false);
1271-
var ownedNftTasks = new List<Task<NFT>>();
1272-
for (var i = 0; i < balanceOfOwner; i++)
1273-
{
1274-
var tokenOfOwnerByIndex = await contract.ERC721_TokenOfOwnerByIndex(owner, i).ConfigureAwait(false);
1275-
if (tokenOfOwnerByIndex >= startTokenId && tokenOfOwnerByIndex < startTokenId + count)
1276-
{
1277-
ownedNftTasks.Add(contract.ERC721_GetNFT(tokenOfOwnerByIndex));
1278-
}
1279-
}
1280-
var ownedNfts = await Task.WhenAll(ownedNftTasks).ConfigureAwait(false);
1281-
return ownedNfts.ToList();
1282-
}
1283-
catch (Exception)
1284-
{
1285-
var allNfts = await contract.ERC721_GetAllNFTs(startTokenId, count).ConfigureAwait(false);
1286-
var ownedNfts = new List<NFT>();
1287-
foreach (var nft in allNfts)
1288-
{
1289-
if (nft.Owner == owner)
1290-
{
1291-
ownedNfts.Add(nft);
1292-
}
1293-
}
1294-
return ownedNfts.ToList();
1295-
}
1247+
ownedNftTasks.Add(contract.ERC721_GetNFT(tokenId));
12961248
}
1249+
var ownedNfts = await Task.WhenAll(ownedNftTasks).ConfigureAwait(false);
1250+
return ownedNfts.ToList();
12971251
}
1298-
// All
1299-
else
1252+
catch
13001253
{
1254+
// ERC721Enumerable
13011255
try
13021256
{
1303-
// ERC721AQueryable
1304-
var tokensOfOwner = await contract.ERC721A_TokensOfOwner(owner).ConfigureAwait(false);
1257+
var balanceOfOwner = await contract.ERC721_BalanceOf(owner).ConfigureAwait(false);
13051258
var ownedNftTasks = new List<Task<NFT>>();
1306-
foreach (var tokenId in tokensOfOwner)
1259+
for (var i = 0; i < balanceOfOwner; i++)
13071260
{
1308-
ownedNftTasks.Add(contract.ERC721_GetNFT(tokenId));
1261+
var tokenOfOwnerByIndex = await contract.ERC721_TokenOfOwnerByIndex(owner, i).ConfigureAwait(false);
1262+
if (tokenOfOwnerByIndex >= startTokenId && tokenOfOwnerByIndex < startTokenId + count)
1263+
{
1264+
ownedNftTasks.Add(contract.ERC721_GetNFT(tokenOfOwnerByIndex));
1265+
}
13091266
}
13101267
var ownedNfts = await Task.WhenAll(ownedNftTasks).ConfigureAwait(false);
13111268
return ownedNfts.ToList();
13121269
}
1313-
catch
1270+
catch (Exception)
13141271
{
1315-
try
1272+
var allNfts = await contract.ERC721_GetAllNFTs(startTokenId, count).ConfigureAwait(false);
1273+
var ownedNfts = new List<NFT>();
1274+
foreach (var nft in allNfts)
13161275
{
1317-
// ERC721Enumerable
1318-
var balanceOfOwner = await contract.ERC721_BalanceOf(owner).ConfigureAwait(false);
1319-
var ownedNftTasks = new List<Task<NFT>>();
1320-
for (var i = 0; i < balanceOfOwner; i++)
1276+
if (nft.Owner == owner)
13211277
{
1322-
var tokenOfOwnerByIndex = await contract.ERC721_TokenOfOwnerByIndex(owner, i).ConfigureAwait(false);
1323-
ownedNftTasks.Add(contract.ERC721_GetNFT(tokenOfOwnerByIndex));
1278+
ownedNfts.Add(nft);
13241279
}
1325-
var ownedNfts = await Task.WhenAll(ownedNftTasks).ConfigureAwait(false);
1326-
return ownedNfts.ToList();
1327-
}
1328-
catch (Exception)
1329-
{
1330-
var allNfts = await contract.ERC721_GetAllNFTs().ConfigureAwait(false);
1331-
var ownedNfts = new List<NFT>();
1332-
foreach (var nft in allNfts)
1333-
{
1334-
if (nft.Owner == owner)
1335-
{
1336-
ownedNfts.Add(nft);
1337-
}
1338-
}
1339-
return ownedNfts.ToList();
13401280
}
1281+
return ownedNfts.ToList();
13411282
}
13421283
}
13431284
}
@@ -1384,21 +1325,16 @@ public static async Task<NFT> ERC1155_GetNFT(this ThirdwebContract contract, Big
13841325
/// </summary>
13851326
/// <param name="contract">The contract to interact with.</param>
13861327
/// <param name="startTokenId">The starting token ID (inclusive). Defaults to 0 if not specified.</param>
1387-
/// <param name="count">The number of tokens to retrieve. Defaults to the total supply if not specified.</param>
1328+
/// <param name="count">The number of tokens to retrieve. Defaults to the 100 if not specified.</param>
13881329
/// <returns>A task representing the asynchronous operation, with a list of NFT results containing the token details.</returns>
13891330
/// <exception cref="ArgumentNullException">Thrown when the contract is null.</exception>
1390-
public static async Task<List<NFT>> ERC1155_GetAllNFTs(this ThirdwebContract contract, BigInteger? startTokenId = null, BigInteger? count = null)
1331+
public static async Task<List<NFT>> ERC1155_GetAllNFTs(this ThirdwebContract contract, int startTokenId = 0, int count = 100)
13911332
{
13921333
if (contract == null)
13931334
{
13941335
throw new ArgumentNullException(nameof(contract));
13951336
}
13961337

1397-
if (startTokenId == null)
1398-
{
1399-
startTokenId = 0;
1400-
}
1401-
14021338
BigInteger totalSupply;
14031339
try
14041340
{
@@ -1409,10 +1345,10 @@ public static async Task<List<NFT>> ERC1155_GetAllNFTs(this ThirdwebContract con
14091345
{
14101346
totalSupply = int.MaxValue;
14111347
}
1412-
count = count == null ? totalSupply - startTokenId : Math.Min((int)count, (int)(totalSupply - startTokenId));
1348+
count = Math.Min(count, (int)(totalSupply - startTokenId));
14131349

14141350
var nftTasks = new List<Task<NFT>>();
1415-
for (var i = startTokenId.Value; i < startTokenId.Value + count.Value; i++)
1351+
for (var i = startTokenId; i < startTokenId + count; i++)
14161352
{
14171353
nftTasks.Add(contract.ERC1155_GetNFT(i));
14181354
}
@@ -1427,11 +1363,11 @@ public static async Task<List<NFT>> ERC1155_GetAllNFTs(this ThirdwebContract con
14271363
/// <param name="contract">The contract to interact with.</param>
14281364
/// <param name="owner">The address of the owner.</param>
14291365
/// <param name="startTokenId">The starting token ID (inclusive). Defaults to 0 if not specified.</param>
1430-
/// <param name="count">The number of tokens to retrieve. Defaults to total supply if not specified.</param>
1366+
/// <param name="count">The number of tokens to retrieve. Defaults to 100 if not specified.</param>
14311367
/// <returns>A task representing the asynchronous operation, with a list of NFT results containing the token details.</returns>
14321368
/// <exception cref="ArgumentNullException">Thrown when the contract is null.</exception>
14331369
/// <exception cref="ArgumentException">Thrown when the owner address is null or empty.</exception>
1434-
public static async Task<List<NFT>> ERC1155_GetOwnedNFTs(this ThirdwebContract contract, string owner, BigInteger? startTokenId = null, BigInteger? count = null)
1370+
public static async Task<List<NFT>> ERC1155_GetOwnedNFTs(this ThirdwebContract contract, string owner, int startTokenId = 0, int count = 100)
14351371
{
14361372
if (contract == null)
14371373
{
@@ -1443,11 +1379,6 @@ public static async Task<List<NFT>> ERC1155_GetOwnedNFTs(this ThirdwebContract c
14431379
throw new ArgumentException("Owner must be provided");
14441380
}
14451381

1446-
if (startTokenId == null)
1447-
{
1448-
startTokenId = 0;
1449-
}
1450-
14511382
BigInteger totalSupply;
14521383
try
14531384
{
@@ -1458,12 +1389,12 @@ public static async Task<List<NFT>> ERC1155_GetOwnedNFTs(this ThirdwebContract c
14581389
{
14591390
totalSupply = int.MaxValue;
14601391
}
1461-
count = count == null ? totalSupply - startTokenId : Math.Min((int)count, (int)(totalSupply - startTokenId));
1392+
count = Math.Min(count, (int)(totalSupply - startTokenId));
14621393

14631394
var ownerArray = new List<string>();
14641395
var tokenIds = new List<BigInteger>();
14651396

1466-
for (var i = startTokenId.Value; i < startTokenId.Value + count.Value; i++)
1397+
for (var i = startTokenId; i < startTokenId + count; i++)
14671398
{
14681399
ownerArray.Add(owner);
14691400
tokenIds.Add(i);

0 commit comments

Comments
 (0)