Skip to content

Commit ff672fe

Browse files
committed
[Pay] check allowance before asking to approve
1 parent 9193546 commit ff672fe

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

Assets/Thirdweb/Core/Scripts/Pay/ThirdwebPay.BuyWithCrypto.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Numerics;
12
using System.Threading.Tasks;
23

34
namespace Thirdweb.Pay
@@ -18,8 +19,16 @@ public static async Task<string> BuyWithCrypto(BuyWithCryptoQuoteResult buyWithC
1819
{
1920
ThirdwebDebug.Log("Approving ERC20...");
2021
var erc20ToApprove = sdk.GetContract(buyWithCryptoQuote.Approval.TokenAddress);
21-
var approvalRes = await erc20ToApprove.ERC20.SetAllowance(buyWithCryptoQuote.Approval.SpenderAddress, buyWithCryptoQuote.Approval.AmountWei.ToEth());
22-
ThirdwebDebug.Log($"Approval transaction receipt: {approvalRes}");
22+
var currentAllowance = await erc20ToApprove.ERC20.Allowance(buyWithCryptoQuote.Approval.SpenderAddress);
23+
if (BigInteger.Parse(currentAllowance.value) >= BigInteger.Parse(buyWithCryptoQuote.Approval.AmountWei))
24+
{
25+
ThirdwebDebug.Log("Already approved");
26+
}
27+
else
28+
{
29+
var approvalRes = await erc20ToApprove.ERC20.SetAllowance(buyWithCryptoQuote.Approval.SpenderAddress, buyWithCryptoQuote.Approval.AmountWei.ToEth());
30+
ThirdwebDebug.Log($"Approval transaction receipt: {approvalRes}");
31+
}
2332
}
2433

2534
ThirdwebDebug.Log("Sending swap transaction...");

0 commit comments

Comments
 (0)