Skip to content

Commit

Permalink
Fix AmountInBase overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
matsakiv committed Feb 16, 2022
1 parent 513d7cf commit 79a7e7e
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 16 deletions.
13 changes: 11 additions & 2 deletions Common/DecimalExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,18 @@ public static decimal TruncateDecimal(this decimal value, int precision)
var decimalPart = value - integralPart;

return integralPart + Math.Truncate(step * decimalPart) / step;
}

//var tmp = Math.Truncate(step * value);
//return tmp / step;
public static decimal SafeMultiply(this decimal arg1, decimal arg2, decimal overflowValue = 0)
{
try
{
return arg1 * arg2;
}
catch
{
return overflowValue;
}
}
}
}
3 changes: 2 additions & 1 deletion ViewModels/ConversionViewModels/ConversionViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,8 @@ private static decimal TryGetAmountInBase(
return defaultAmountInBase;

var quote = provider.GetQuote(currency, baseCurrency);
return amount * (quote?.Bid ?? 0m);

return amount.SafeMultiply(quote?.Bid ?? 0m);
}

private void UpdateFromAmountInBase() => FromViewModel.AmountInBase = TryGetAmountInBase(
Expand Down
7 changes: 4 additions & 3 deletions ViewModels/CurrencyViewModels/CurrencyViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Atomex.MarketData.Abstract;
using Atomex.Wallet;
using Atomex.Wallet.Abstract;
using Atomex.Client.Desktop.Common;

namespace Atomex.Client.Desktop.ViewModels.CurrencyViewModels
{
Expand Down Expand Up @@ -134,9 +135,9 @@ private void UpdateQuotesInBaseCurrency(ICurrencyQuotesProvider quotesProvider)
{
var quote = quotesProvider.GetQuote(CurrencyCode, BaseCurrencyCode);

TotalAmountInBase = TotalAmount * (quote?.Bid ?? 0m);
AvailableAmountInBase = AvailableAmount * (quote?.Bid ?? 0m);
UnconfirmedAmountInBase = UnconfirmedAmount * (quote?.Bid ?? 0m);
TotalAmountInBase = TotalAmount.SafeMultiply(quote?.Bid ?? 0m);
AvailableAmountInBase = AvailableAmount.SafeMultiply(quote?.Bid ?? 0m);
UnconfirmedAmountInBase = UnconfirmedAmount.SafeMultiply(quote?.Bid ?? 0m);

AmountUpdated?.Invoke(this, EventArgs.Empty);
}
Expand Down
4 changes: 2 additions & 2 deletions ViewModels/DelegateViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -563,13 +563,13 @@ private void SubscribeToServices()

private void OnQuotesUpdatedEventHandler(object sender, EventArgs args)
{
if (!(sender is ICurrencyQuotesProvider quotesProvider))
if (sender is not ICurrencyQuotesProvider quotesProvider)
return;

var quote = quotesProvider.GetQuote(FeeCurrencyCode, BaseCurrencyCode);

if (quote != null)
FeeInBase = Fee * quote.Bid;
FeeInBase = Fee.SafeMultiply(quote.Bid);
}

private void DesignerMode()
Expand Down
5 changes: 3 additions & 2 deletions ViewModels/SendViewModels/Erc20SendViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Serilog;

using Atomex.Blockchain.Abstract;
using Atomex.Client.Desktop.Common;
using Atomex.Client.Desktop.Properties;
using Atomex.Core;
using Atomex.MarketData.Abstract;
Expand Down Expand Up @@ -160,8 +161,8 @@ protected override void OnQuotesUpdatedEventHandler(object sender, EventArgs arg
var quote = quotesProvider.GetQuote(CurrencyCode, BaseCurrencyCode);
var ethQuote = quotesProvider.GetQuote(Currency.FeeCurrencyName, BaseCurrencyCode);

AmountInBase = Amount * (quote?.Bid ?? 0m);
FeeInBase = FeeAmount * (ethQuote?.Bid ?? 0m);
AmountInBase = Amount.SafeMultiply(quote?.Bid ?? 0m);
FeeInBase = FeeAmount.SafeMultiply(ethQuote?.Bid ?? 0m);
}

protected override Task<Error> Send(CancellationToken cancellationToken = default)
Expand Down
5 changes: 3 additions & 2 deletions ViewModels/SendViewModels/EthereumSendViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Serilog;

using Atomex.Blockchain.Abstract;
using Atomex.Client.Desktop.Common;
using Atomex.Client.Desktop.Properties;
using Atomex.Core;
using Atomex.EthereumTokens;
Expand Down Expand Up @@ -377,8 +378,8 @@ protected override void OnQuotesUpdatedEventHandler(object sender, EventArgs arg

var quote = quotesProvider.GetQuote(CurrencyCode, BaseCurrencyCode);

AmountInBase = Amount * (quote?.Bid ?? 0m);
FeeInBase = FeeAmount * (quote?.Bid ?? 0m);
AmountInBase = Amount.SafeMultiply(quote?.Bid ?? 0m);
FeeInBase = FeeAmount.SafeMultiply(quote?.Bid ?? 0m);
}

protected override Task<Error> Send(CancellationToken cancellationToken = default)
Expand Down
5 changes: 3 additions & 2 deletions ViewModels/SendViewModels/Fa12SendViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Serilog;

using Atomex.Blockchain.Abstract;
using Atomex.Client.Desktop.Common;
using Atomex.Client.Desktop.Properties;
using Atomex.Core;
using Atomex.MarketData.Abstract;
Expand Down Expand Up @@ -216,8 +217,8 @@ protected override void OnQuotesUpdatedEventHandler(object sender, EventArgs arg
var quote = quotesProvider.GetQuote(CurrencyCode, BaseCurrencyCode);
var xtzQuote = quotesProvider.GetQuote("XTZ", BaseCurrencyCode);

AmountInBase = Amount * (quote?.Bid ?? 0m);
FeeInBase = Fee * (xtzQuote?.Bid ?? 0m);
AmountInBase = Amount.SafeMultiply(quote?.Bid ?? 0m);
FeeInBase = Fee.SafeMultiply(xtzQuote?.Bid ?? 0m);
}

protected override async Task<Error> Send(CancellationToken cancellationToken = default)
Expand Down
4 changes: 2 additions & 2 deletions ViewModels/SendViewModels/TezosTokensSendViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -614,11 +614,11 @@ protected void OnQuotesUpdatedEventHandler(object sender, EventArgs args)
return;

AmountInBase = !string.IsNullOrEmpty(CurrencyCode)
? Amount * (quotesProvider.GetQuote(CurrencyCode, BaseCurrencyCode)?.Bid ?? 0m)
? Amount.SafeMultiply(quotesProvider.GetQuote(CurrencyCode, BaseCurrencyCode)?.Bid ?? 0m)
: 0;

FeeInBase = !string.IsNullOrEmpty(FeeCurrencyCode)
? Fee * (quotesProvider.GetQuote(FeeCurrencyCode, BaseCurrencyCode)?.Bid ?? 0m)
? Fee.SafeMultiply(quotesProvider.GetQuote(FeeCurrencyCode, BaseCurrencyCode)?.Bid ?? 0m)
: 0;
}

Expand Down

0 comments on commit 79a7e7e

Please sign in to comment.