Skip to content

Commit 10aa67e

Browse files
committed
handle approvals option for buying and bidding
1 parent c94665b commit 10aa67e

File tree

1 file changed

+57
-2
lines changed

1 file changed

+57
-2
lines changed

Thirdweb/Thirdweb.Extensions/ThirdwebMarketplaceExtensions.cs

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ BigInteger pricePerTokenInCurrency
214214
/// <param name="quantity">The quantity of NFTs to buy.</param>
215215
/// <param name="currency">The currency to use for the purchase.</param>
216216
/// <param name="expectedTotalPrice">The expected total price to pay.</param>
217+
/// <param name="handleApprovals">Whether to handle token approvals automatically.</param>
217218
/// <returns>A task that represents the transaction receipt of the purchase.</returns>
218219
public static async Task<ThirdwebTransactionReceipt> Marketplace_DirectListings_BuyFromListing(
219220
this ThirdwebContract contract,
@@ -222,7 +223,8 @@ public static async Task<ThirdwebTransactionReceipt> Marketplace_DirectListings_
222223
string buyFor,
223224
BigInteger quantity,
224225
string currency,
225-
BigInteger expectedTotalPrice
226+
BigInteger expectedTotalPrice,
227+
bool handleApprovals = false
226228
)
227229
{
228230
if (contract == null)
@@ -241,6 +243,29 @@ BigInteger expectedTotalPrice
241243
{
242244
value = expectedTotalPrice;
243245
}
246+
else if (handleApprovals)
247+
{
248+
var tokenContractAddress = currency;
249+
250+
var prepTasks = new List<Task>();
251+
252+
var tokenContractTask = ThirdwebContract.Create(contract.Client, tokenContractAddress, contract.Chain);
253+
prepTasks.Add(tokenContractTask);
254+
255+
var walletAddressTask = wallet.GetAddress();
256+
prepTasks.Add(walletAddressTask);
257+
258+
await Task.WhenAll(prepTasks);
259+
260+
var tokenContract = tokenContractTask.Result;
261+
var walletAddress = walletAddressTask.Result;
262+
263+
var allowance = await tokenContract.ERC20_Allowance(walletAddress, contract.Address);
264+
if (allowance < expectedTotalPrice)
265+
{
266+
_ = await tokenContract.ERC20_Approve(wallet, contract.Address, expectedTotalPrice);
267+
}
268+
}
244269

245270
return await contract.Write(wallet, "buyFromListing", value, listingId, buyFor, quantity, currency, expectedTotalPrice);
246271
}
@@ -470,8 +495,15 @@ public static async Task<ThirdwebTransactionReceipt> Marketplace_EnglishAuctions
470495
/// <param name="wallet">The wallet used for the transaction.</param>
471496
/// <param name="auctionId">The ID of the auction to bid in.</param>
472497
/// <param name="bidAmount">The bid amount to place.</param>
498+
/// <param name="handleApprovals">Whether to handle token approvals automatically.</param>
473499
/// <returns>A task that represents the transaction receipt of the placed bid.</returns>
474-
public static async Task<ThirdwebTransactionReceipt> Marketplace_EnglishAuctions_BidInAuction(this ThirdwebContract contract, IThirdwebWallet wallet, BigInteger auctionId, BigInteger bidAmount)
500+
public static async Task<ThirdwebTransactionReceipt> Marketplace_EnglishAuctions_BidInAuction(
501+
this ThirdwebContract contract,
502+
IThirdwebWallet wallet,
503+
BigInteger auctionId,
504+
BigInteger bidAmount,
505+
bool handleApprovals = false
506+
)
475507
{
476508
if (contract == null)
477509
{
@@ -490,6 +522,29 @@ public static async Task<ThirdwebTransactionReceipt> Marketplace_EnglishAuctions
490522
{
491523
value = bidAmount;
492524
}
525+
else if (handleApprovals)
526+
{
527+
var tokenContractAddress = auctionDetails.Currency;
528+
529+
var prepTasks = new List<Task>();
530+
531+
var tokenContractTask = ThirdwebContract.Create(contract.Client, tokenContractAddress, contract.Chain);
532+
prepTasks.Add(tokenContractTask);
533+
534+
var walletAddressTask = wallet.GetAddress();
535+
prepTasks.Add(walletAddressTask);
536+
537+
await Task.WhenAll(prepTasks);
538+
539+
var tokenContract = tokenContractTask.Result;
540+
var walletAddress = walletAddressTask.Result;
541+
542+
var allowance = await tokenContract.ERC20_Allowance(walletAddress, contract.Address);
543+
if (allowance < bidAmount)
544+
{
545+
_ = await tokenContract.ERC20_Approve(wallet, contract.Address, bidAmount);
546+
}
547+
}
493548

494549
return await contract.Write(wallet, "bidInAuction", value, auctionId, bidAmount);
495550
}

0 commit comments

Comments
 (0)