Fix balance overflow when balance is too big

This commit is contained in:
Ivan Yaremenchuk 2022-09-30 06:09:39 -05:00
parent a45575bda2
commit 401e3fed76
6 changed files with 20 additions and 6 deletions

View File

@ -1,7 +1,9 @@
using System.Numerics;
namespace NftFaucet.Models;
public class Balance
{
public decimal Amount { get; set; }
public BigInteger Amount { get; set; }
public string Currency { get; set; }
}

View File

@ -50,6 +50,10 @@ else if (State == MintingState.CheckingNetwork)
{
@(Balance.Amount + " " + Balance.Currency)
}
else if (!string.IsNullOrEmpty(GetBalanceError))
{
@GetBalanceError
}
else
{
@:<unknown>

View File

@ -1,3 +1,4 @@
using System.Numerics;
using NftFaucet.Components;
using NftFaucet.Models;
using NftFaucet.Plugins.NetworkPlugins;
@ -15,6 +16,7 @@ public partial class MintDialog : BasicComponent
private string SourceAddress { get; set; }
private Balance Balance { get; set; }
private string TransactionHash { get; set; }
private string GetBalanceError { get; set; }
private string SendTransactionError { get; set; }
protected override Task OnInitializedAsync()
@ -89,9 +91,14 @@ public partial class MintDialog : BasicComponent
return task1.Result;
});
Balance = balanceResult.IsSuccess ? balanceResult.Value : null;
var amount = Math.Max(Balance?.Amount ?? 0, 0);
GetBalanceError = balanceResult.IsFailure ? balanceResult.Error : null;
var amount = Balance?.Amount ?? BigInteger.Zero;
if (amount < BigInteger.Zero)
{
amount = BigInteger.Zero;
}
if (amount != 0)
if (amount != BigInteger.Zero)
{
SendTransaction();
}

View File

@ -92,7 +92,7 @@ public class EthereumKeygenProvider : IProvider
var web3 = new Web3(network.PublicRpcUrl.OriginalString);
var hexBalance = await web3.Eth.GetBalance.SendRequestAsync(Key.Address);
var balance = (long) hexBalance.Value;
var balance = hexBalance.Value;
return new Balance
{
Amount = balance,

View File

@ -1,3 +1,4 @@
using System.Numerics;
using System.Text;
using CSharpFunctionalExtensions;
using NftFaucet.Components.CardList;
@ -111,7 +112,7 @@ public class SolanaKeygenProvider : IProvider
if (!balanceResult.WasSuccessful || balanceResult.Result == null)
return null;
var balance = (long) balanceResult.Result.Value;
var balance = new BigInteger(balanceResult.Result.Value);
return new Balance
{
Amount = balance,

View File

@ -137,7 +137,7 @@ public class MetamaskProvider : IProvider
if (!IsConfigured)
return null;
var balance = (long) await MetaMaskService.GetBalanceAsync();
var balance = await MetaMaskService.GetBalanceAsync();
return new Balance
{
Amount = balance,