Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

Commit

Permalink
Swap amount field logic and swap currencies button (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
matsakiv authored Feb 17, 2021
1 parent 252b488 commit d230549
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 57 deletions.
2 changes: 1 addition & 1 deletion Atomex.Client.Wpf.Installer/Product.wxs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<?define Name = "Atomex Client" ?>
<?define Manufacturer = "Atomex.me" ?>
<?define Description = "Multicurrency HD wallet with built-in hybrid exchange based on atomic swap." ?>
<?define Version = "1.0.69" ?>
<?define Version = "1.0.70" ?>
<?define UpgradeCode = "DB7FCF8D-E0C6-4C99-A6B1-3FB6D703F97E" ?>
<?define ExeName = "Atomex.Client.Wpf.exe" ?>

Expand Down
15 changes: 12 additions & 3 deletions Atomex.Client.Wpf/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions Atomex.Client.Wpf/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@
<data name="CvWarning" xml:space="preserve">
<value>Warning</value>
</data>
<data name="CvWrongAmount" xml:space="preserve">
<value>Amount to convert must be greater than zero.</value>
<data name="CvBigAmount" xml:space="preserve">
<value>Amount is greater than the available. Please use the Max button to get the maximum available value.</value>
</data>
<data name="CvNoLiquidity" xml:space="preserve">
<value>Not enough liquidity to convert a specified amount.</value>
Expand Down Expand Up @@ -296,4 +296,7 @@ NOTE: Do not sign out or close the application until the swap is completed, othe
<data name="CvTooHighNetworkFee" xml:space="preserve">
<value>Too high network fee for this amount ({0} is {1})!</value>
</data>
<data name="CvZeroAmount" xml:space="preserve">
<value>Amount must be greater than zero.</value>
</data>
</root>
13 changes: 13 additions & 0 deletions Atomex.Client.Wpf/Styles/AmountTextBoxStyle.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,17 @@
</Setter.Value>
</Setter>
</Style>

<Style x:Key="ConversionAmountTextBox"
BasedOn="{StaticResource AmountTextBox}"
TargetType="{x:Type TextBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsAmountValid}" Value="True">
<Setter Property="Foreground" Value="{DynamicResource DefaultForegroundBrush}"/>
</DataTrigger>
<DataTrigger Binding="{Binding IsAmountValid}" Value="False">
<Setter Property="Foreground" Value="{DynamicResource InvalidAmountForegroundBrush}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
1 change: 1 addition & 0 deletions Atomex.Client.Wpf/Themes/blue.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@
<SolidColorBrush x:Key="NoLiquidityForegroundBrush" Color="#e25b5b"/>
<SolidColorBrush x:Key="MarketVolumeForegroundBrush" Color="AliceBlue"/>
<SolidColorBrush x:Key="ConversionArrowBrush" Color="LightGray"/>
<SolidColorBrush x:Key="InvalidAmountForegroundBrush" Color="#e25b5b"/>

<SolidColorBrush x:Key="SwapStateCanceledBackgroundBrush" Color="{DynamicResource UnconfirmedColor}"/>
<SolidColorBrush x:Key="SwapStateCompletedBackgroundBrush" Color="{DynamicResource ConfirmedColor}"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private async Task<Error> ConvertAsync()
.ToList();

if (Amount == 0)
return new Error(Errors.SwapError, Resources.CvWrongAmount);
return new Error(Errors.SwapError, Resources.CvZeroAmount);

if (Amount > 0 && !fromWallets.Any())
return new Error(Errors.SwapError, Resources.CvInsufficientFunds);
Expand Down
92 changes: 77 additions & 15 deletions Atomex.Client.Wpf/ViewModels/ConversionViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ public virtual Currency FromCurrency
FromCurrencyViewModel = _currencyViewModels
.First(c => c.Currency.Name == _fromCurrency.Name);

Amount = 0;
_amount = 0;
_ = UpdateAmountAsync(_amount, updateUi: true);
}
}

Expand Down Expand Up @@ -233,10 +234,27 @@ public string BaseCurrencyFormat
}

protected decimal _amount;
public decimal Amount

public string AmountString
{
get => _amount;
set { _ = UpdateAmountAsync(value); }
get => _amount.ToString(CurrencyFormat, CultureInfo.InvariantCulture);
set
{
if (!decimal.TryParse(value, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var amount))
return;

_amount = amount.TruncateByFormat(CurrencyFormat);

if (_amount > long.MaxValue)
_amount = long.MaxValue;

_ = UpdateAmountAsync(_amount, updateUi: false);
}
}

public string FromAmountString
{
get => _amount.ToString(CurrencyFormat, CultureInfo.InvariantCulture);
}

private decimal _amountInBase;
Expand All @@ -253,6 +271,13 @@ public bool IsAmountUpdating
set { _isAmountUpdating = value; OnPropertyChanged(nameof(IsAmountUpdating)); }
}

private bool _isAmountValid = true;
public bool IsAmountValid
{
get => _isAmountValid;
set { _isAmountValid = value; OnPropertyChanged(nameof(IsAmountValid)); }
}

private decimal _targetAmount;
public decimal TargetAmount
{
Expand Down Expand Up @@ -458,9 +483,35 @@ public ConversionViewModel(
public ICommand ConvertCommand => _convertCommand ??= new Command(OnConvertClick);

private ICommand _maxAmountCommand;
public ICommand MaxAmountCommand => _maxAmountCommand ??= new Command(() =>
public ICommand MaxAmountCommand => _maxAmountCommand ??= new Command(async () =>
{
try
{
var swapParams = await Atomex.ViewModels.Helpers
.EstimateSwapPaymentParamsAsync(
amount: EstimatedMaxAmount,
fromCurrency: FromCurrency,
toCurrency: ToCurrency,
account: App.Account,
atomexClient: App.Terminal,
symbolsProvider: App.SymbolsProvider);

_amount = Math.Min(swapParams.Amount, EstimatedMaxAmount);
_ = UpdateAmountAsync(_amount, updateUi: true);
}
catch (Exception e)
{
Log.Error(e, "Max amount command error.");
}
});


private ICommand _swapCurrenciesCommand;
public ICommand SwapCurrenciesCommand => _swapCurrenciesCommand ??= new Command(() =>
{
Amount = EstimatedMaxAmount;
var temp = _toCurrency;
_toCurrency = _fromCurrency;
FromCurrency = temp;
});

private void SubscribeToServices()
Expand All @@ -471,7 +522,7 @@ private void SubscribeToServices()
App.QuotesProvider.QuotesUpdated += OnBaseQuotesUpdatedEventHandler;
}

protected virtual async Task UpdateAmountAsync(decimal value)
protected virtual async Task UpdateAmountAsync(decimal value, bool updateUi = false)
{
Warning = string.Empty;

Expand Down Expand Up @@ -505,15 +556,19 @@ protected virtual async Task UpdateAmountAsync(decimal value)
Warning = string.Empty;
}

_amount = swapParams.Amount;
_estimatedPaymentFee = swapParams.PaymentFee;
_estimatedMakerNetworkFee = swapParams.MakerNetworkFee;

OnPropertyChanged(nameof(CurrencyFormat));
OnPropertyChanged(nameof(TargetCurrencyFormat));
OnPropertyChanged(nameof(Amount));
OnPropertyChanged(nameof(EstimatedPaymentFee));
OnPropertyChanged(nameof(EstimatedMakerNetworkFee));
OnPropertyChanged(nameof(FromAmountString));

IsAmountValid = _amount <= swapParams.Amount.TruncateByFormat(CurrencyFormat);

if (updateUi)
OnPropertyChanged(nameof(AmountString));

await UpdateRedeemAndRewardFeesAsync();

Expand Down Expand Up @@ -681,7 +736,7 @@ protected async void OnQuotesUpdatedEventHandler(object sender, MarketDataEventA
try
{
var swapPriceEstimation = await Atomex.ViewModels.Helpers.EstimateSwapPriceAsync(
amount: Amount,
amount: _amount,
fromCurrency: FromCurrency,
toCurrency: ToCurrency,
account: App.Account,
Expand Down Expand Up @@ -749,9 +804,15 @@ await Application.Current.Dispatcher.InvokeAsync(() =>

private void OnConvertClick()
{
if (Amount == 0)
if (_amount == 0)
{
DialogViewer.ShowMessage(Resources.CvWarning, Resources.CvZeroAmount);
return;
}

if (!IsAmountValid)
{
DialogViewer.ShowMessage(Resources.CvWarning, Resources.CvWrongAmount);
DialogViewer.ShowMessage(Resources.CvWarning, Resources.CvBigAmount);
return;
}

Expand All @@ -777,7 +838,7 @@ private void OnConvertClick()
var side = symbol.OrderSideForBuyCurrency(ToCurrency);
var price = EstimatedPrice;
var baseCurrency = Currencies.GetByName(symbol.Base);
var qty = AmountHelper.AmountToQty(side, Amount, price, baseCurrency.DigitsMultiplier);
var qty = AmountHelper.AmountToQty(side, _amount, price, baseCurrency.DigitsMultiplier);

if (qty < symbol.MinimumQty)
{
Expand Down Expand Up @@ -808,7 +869,7 @@ private void OnConvertClick()
TargetFeeCurrencyCode = TargetFeeCurrencyCode,
TargetFeeCurrencyFormat = TargetFeeCurrencyFormat,

Amount = Amount,
Amount = _amount,
AmountInBase = AmountInBase,
TargetAmount = TargetAmount,
TargetAmountInBase = TargetAmountInBase,
Expand Down Expand Up @@ -836,7 +897,8 @@ private void OnConvertClick()

private void OnSuccessConvertion(object sender, EventArgs e)
{
Amount = _amount; // recalculate amount
_amount = Math.Min(_amount, EstimatedMaxAmount); // recalculate amount
_ = UpdateAmountAsync(_amount, updateUi: true);
}

private void DesignerMode()
Expand Down
Loading

0 comments on commit d230549

Please sign in to comment.