Skip to content

Commit

Permalink
[Pay] check allowance before asking to approve
Browse files Browse the repository at this point in the history
  • Loading branch information
0xFirekeeper committed Mar 28, 2024
1 parent 9193546 commit ff672fe
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions Assets/Thirdweb/Core/Scripts/Pay/ThirdwebPay.BuyWithCrypto.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Numerics;
using System.Threading.Tasks;

namespace Thirdweb.Pay
Expand All @@ -18,8 +19,16 @@ public static async Task<string> BuyWithCrypto(BuyWithCryptoQuoteResult buyWithC
{
ThirdwebDebug.Log("Approving ERC20...");
var erc20ToApprove = sdk.GetContract(buyWithCryptoQuote.Approval.TokenAddress);
var approvalRes = await erc20ToApprove.ERC20.SetAllowance(buyWithCryptoQuote.Approval.SpenderAddress, buyWithCryptoQuote.Approval.AmountWei.ToEth());
ThirdwebDebug.Log($"Approval transaction receipt: {approvalRes}");
var currentAllowance = await erc20ToApprove.ERC20.Allowance(buyWithCryptoQuote.Approval.SpenderAddress);
if (BigInteger.Parse(currentAllowance.value) >= BigInteger.Parse(buyWithCryptoQuote.Approval.AmountWei))
{
ThirdwebDebug.Log("Already approved");
}
else
{
var approvalRes = await erc20ToApprove.ERC20.SetAllowance(buyWithCryptoQuote.Approval.SpenderAddress, buyWithCryptoQuote.Approval.AmountWei.ToEth());
ThirdwebDebug.Log($"Approval transaction receipt: {approvalRes}");
}
}

ThirdwebDebug.Log("Sending swap transaction...");
Expand Down

0 comments on commit ff672fe

Please sign in to comment.