From 79a7e7e9d4e0a11b4965ca99422ab333af1ea65f Mon Sep 17 00:00:00 2001 From: Igor Matsak Date: Wed, 16 Feb 2022 18:58:53 +0300 Subject: [PATCH] Fix AmountInBase overflow --- Common/DecimalExtensions.cs | 13 +++++++++++-- .../ConversionViewModels/ConversionViewModel.cs | 3 ++- ViewModels/CurrencyViewModels/CurrencyViewModel.cs | 7 ++++--- ViewModels/DelegateViewModel.cs | 4 ++-- ViewModels/SendViewModels/Erc20SendViewModel.cs | 5 +++-- ViewModels/SendViewModels/EthereumSendViewModel.cs | 5 +++-- ViewModels/SendViewModels/Fa12SendViewModel.cs | 5 +++-- .../SendViewModels/TezosTokensSendViewModel.cs | 4 ++-- 8 files changed, 30 insertions(+), 16 deletions(-) diff --git a/Common/DecimalExtensions.cs b/Common/DecimalExtensions.cs index b8279ede..b843ffef 100644 --- a/Common/DecimalExtensions.cs +++ b/Common/DecimalExtensions.cs @@ -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; + } } } } \ No newline at end of file diff --git a/ViewModels/ConversionViewModels/ConversionViewModel.cs b/ViewModels/ConversionViewModels/ConversionViewModel.cs index ed3328bf..4d174f84 100644 --- a/ViewModels/ConversionViewModels/ConversionViewModel.cs +++ b/ViewModels/ConversionViewModels/ConversionViewModel.cs @@ -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( diff --git a/ViewModels/CurrencyViewModels/CurrencyViewModel.cs b/ViewModels/CurrencyViewModels/CurrencyViewModel.cs index 71345be3..68eac8f5 100644 --- a/ViewModels/CurrencyViewModels/CurrencyViewModel.cs +++ b/ViewModels/CurrencyViewModels/CurrencyViewModel.cs @@ -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 { @@ -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); } diff --git a/ViewModels/DelegateViewModel.cs b/ViewModels/DelegateViewModel.cs index c6f0fc2c..a7065403 100644 --- a/ViewModels/DelegateViewModel.cs +++ b/ViewModels/DelegateViewModel.cs @@ -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() diff --git a/ViewModels/SendViewModels/Erc20SendViewModel.cs b/ViewModels/SendViewModels/Erc20SendViewModel.cs index 5f6e02bf..02098387 100644 --- a/ViewModels/SendViewModels/Erc20SendViewModel.cs +++ b/ViewModels/SendViewModels/Erc20SendViewModel.cs @@ -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; @@ -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 Send(CancellationToken cancellationToken = default) diff --git a/ViewModels/SendViewModels/EthereumSendViewModel.cs b/ViewModels/SendViewModels/EthereumSendViewModel.cs index 66cd87a4..f598a8b2 100644 --- a/ViewModels/SendViewModels/EthereumSendViewModel.cs +++ b/ViewModels/SendViewModels/EthereumSendViewModel.cs @@ -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; @@ -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 Send(CancellationToken cancellationToken = default) diff --git a/ViewModels/SendViewModels/Fa12SendViewModel.cs b/ViewModels/SendViewModels/Fa12SendViewModel.cs index 946b31a1..00777a70 100644 --- a/ViewModels/SendViewModels/Fa12SendViewModel.cs +++ b/ViewModels/SendViewModels/Fa12SendViewModel.cs @@ -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; @@ -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 Send(CancellationToken cancellationToken = default) diff --git a/ViewModels/SendViewModels/TezosTokensSendViewModel.cs b/ViewModels/SendViewModels/TezosTokensSendViewModel.cs index 7b5b48de..c9183644 100644 --- a/ViewModels/SendViewModels/TezosTokensSendViewModel.cs +++ b/ViewModels/SendViewModels/TezosTokensSendViewModel.cs @@ -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; }