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

Commit

Permalink
Fix reward for redeem (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
matsakiv authored Feb 4, 2021
1 parent bdedf84 commit c12ad4a
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 176 deletions.
2 changes: 1 addition & 1 deletion Atomex.Client.Core
Submodule Atomex.Client.Core updated 53 files
+0 −4 Atomex.Client.Core/Common/ClientType.cs
+37 −10 Atomex.Client.Core/Core/Currency.cs
+45 −36 Atomex.Client.Core/Currencies/Bitcoin.cs
+23 −8 Atomex.Client.Core/Currencies/BitcoinBasedCurrency.cs
+3 −0 Atomex.Client.Core/Currencies/Currencies.cs
+104 −91 Atomex.Client.Core/Currencies/Ethereum.cs
+74 −54 Atomex.Client.Core/Currencies/EthereumTokens/ERC20.cs
+40 −31 Atomex.Client.Core/Currencies/Litecoin.cs
+112 −110 Atomex.Client.Core/Currencies/Tezos.cs
+124 −105 Atomex.Client.Core/Currencies/TezosTokens/FA12.cs
+87 −79 Atomex.Client.Core/Currencies/TezosTokens/FA2.cs
+73 −65 Atomex.Client.Core/Currencies/TezosTokens/NYX.cs
+1 −0 Atomex.Client.Core/MarketData/Abstract/ICurrencyQuotesProvider.cs
+1 −0 Atomex.Client.Core/MarketData/Abstract/QuotesProvider.cs
+5 −0 Atomex.Client.Core/MarketData/Bitfinex/BitfinexQuotesProvider.cs
+5 −0 Atomex.Client.Core/MarketData/Bitfinex/BitfinexQuotesProviderV1.cs
+5 −0 Atomex.Client.Core/MarketData/Bitfinex/BitfinexWebSocketQuotesProvider.cs
+0 −119 Atomex.Client.Core/MarketData/Poloniex/PoloniexQuotesProvider.cs
+15 −8 Atomex.Client.Core/MarketData/Quote.cs
+2 −0 Atomex.Client.Core/Subsystems/Abstract/IAtomexClient.cs
+87 −48 Atomex.Client.Core/Subsystems/WebSocketAtomexClient.cs
+69 −37 Atomex.Client.Core/Swaps/Abstract/CurrencySwap.cs
+8 −5 Atomex.Client.Core/Swaps/Abstract/ICurrencySwap.cs
+8 −2 Atomex.Client.Core/Swaps/Abstract/ISwapManager.cs
+55 −21 Atomex.Client.Core/Swaps/BitcoinBased/BitcoinBasedSwap.cs
+11 −5 Atomex.Client.Core/Swaps/BitcoinBased/Helpers/BitcoinBasedSwapInitiatedHelper.cs
+12 −6 Atomex.Client.Core/Swaps/BitcoinBased/Helpers/BitcoinBasedSwapSpentHelper.cs
+85 −61 Atomex.Client.Core/Swaps/Ethereum/ERC20/ERC20Swap.cs
+8 −4 Atomex.Client.Core/Swaps/Ethereum/ERC20/Helpers/ERC20SwapInitiatedHelper.cs
+18 −7 Atomex.Client.Core/Swaps/Ethereum/ERC20/Helpers/ERC20SwapRedeemedHelper.cs
+54 −37 Atomex.Client.Core/Swaps/Ethereum/EthereumSwap.cs
+16 −7 Atomex.Client.Core/Swaps/Ethereum/Helpers/EthereumSwapInitiatedHelper.cs
+18 −7 Atomex.Client.Core/Swaps/Ethereum/Helpers/EthereumSwapRedeemedHelper.cs
+82 −0 Atomex.Client.Core/Swaps/Helpers/RewardForRedeemHelper.cs
+207 −101 Atomex.Client.Core/Swaps/SwapManager.cs
+86 −72 Atomex.Client.Core/Swaps/Tezos/FA12/FA12Swap.cs
+2 −2 Atomex.Client.Core/Swaps/Tezos/FA12/Helpers/FA12SwapInitiatedHelper.cs
+17 −6 Atomex.Client.Core/Swaps/Tezos/FA12/Helpers/FA12SwapRedeemedHelper.cs
+84 −63 Atomex.Client.Core/Swaps/Tezos/FA2/FA2Swap.cs
+11 −5 Atomex.Client.Core/Swaps/Tezos/FA2/Helpers/FA2SwapInitiatedHelper.cs
+18 −7 Atomex.Client.Core/Swaps/Tezos/FA2/Helpers/FA2SwapRedeemedHelper.cs
+16 −6 Atomex.Client.Core/Swaps/Tezos/Helpers/TezosSwapInitiatedHelper.cs
+15 −6 Atomex.Client.Core/Swaps/Tezos/Helpers/TezosSwapRedeemedHelper.cs
+14 −5 Atomex.Client.Core/Swaps/Tezos/NYX/Helpers/NYXSwapInitiatedHelper.cs
+17 −6 Atomex.Client.Core/Swaps/Tezos/NYX/Helpers/NYXSwapRedeemedHelper.cs
+83 −63 Atomex.Client.Core/Swaps/Tezos/NYX/NYXSwap.cs
+65 −55 Atomex.Client.Core/Swaps/Tezos/TezosSwap.cs
+22 −8 Atomex.Client.Core/ViewModels/Helpers.cs
+1 −5 Atomex.Client.Core/Wallet/Abstract/IAccount.cs
+3 −13 Atomex.Client.Core/Wallet/Account.cs
+3 −2 Atomex.Client.Core/Wallet/BitcoinBased/BitcoinBasedAccount.cs
+5 −1 Atomex.Client.Core/Web/WebSocketClient.cs
+95 −19 Atomex.Client.Core/currencies.json
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,10 @@ private async Task<Error> ConvertAsync()
if (Amount > 0 && !fromWallets.Any())
return new Error(Errors.SwapError, Resources.CvInsufficientFunds);

var symbol = App.Account.Symbols.SymbolByCurrencies(FromCurrency, ToCurrency);
var symbol = App.SymbolsProvider
.GetSymbols(App.Account.Network)
.SymbolByCurrencies(FromCurrency, ToCurrency);

var baseCurrency = App.Account.Currencies.GetByName(symbol.Base);
var side = symbol.OrderSideForBuyCurrency(ToCurrency);
var terminal = App.Terminal;
Expand Down
163 changes: 100 additions & 63 deletions Atomex.Client.Wpf/ViewModels/ConversionViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using Atomex.Client.Wpf.Properties;
using Atomex.Client.Wpf.ViewModels.Abstract;
using Atomex.Client.Wpf.ViewModels.CurrencyViewModels;
using Atomex.Swaps.Helpers;

namespace Atomex.Client.Wpf.ViewModels
{
Expand All @@ -37,7 +38,8 @@ private ISymbols Symbols
if (Env.IsInDesignerMode())
return DesignTime.Symbols;
#endif
return App.Account.Symbols;
return App.SymbolsProvider
.GetSymbols(App.Account.Network);
}
}

Expand Down Expand Up @@ -130,8 +132,12 @@ public Currency ToCurrency
if (!Env.IsInDesignerMode())
{
#endif
OnQuotesUpdatedEventHandler(App.Terminal, null);
OnBaseQuotesUpdatedEventHandler(App.QuotesProvider, EventArgs.Empty);
_ = Task.Run(async () =>
{
await UpdateRedeemAndRewardFeesAsync();
OnQuotesUpdatedEventHandler(App.Terminal, null);
OnBaseQuotesUpdatedEventHandler(App.QuotesProvider, EventArgs.Empty);
});
#if DEBUG
}
#endif
Expand Down Expand Up @@ -188,8 +194,6 @@ public CurrencyViewModel ToCurrencyViewModel

PriceFormat = $"F{quoteCurrency.Digits}";
}

UpdateRedeemAndRewardFeesAsync();
}
}

Expand Down Expand Up @@ -380,13 +384,7 @@ public decimal EstimatedTotalNetworkFeeInBase
public decimal RewardForRedeem
{
get => _rewardForRedeem;
set
{
_rewardForRedeem = value;
OnPropertyChanged(nameof(RewardForRedeem));

HasRewardForRedeem = _rewardForRedeem != 0;
}
set { _rewardForRedeem = value; OnPropertyChanged(nameof(RewardForRedeem)); }
}

private decimal _rewardForRedeemInBase;
Expand Down Expand Up @@ -481,14 +479,15 @@ protected virtual async Task UpdateAmountAsync(decimal value)
{
IsAmountUpdating = true;

// esitmate max payment amount and max fee
// estimate max payment amount and max fee
var swapParams = await Atomex.ViewModels.Helpers
.EstimateSwapPaymentParamsAsync(
amount: value,
fromCurrency: FromCurrency,
toCurrency: ToCurrency,
account: App.Account,
atomexClient: App.Terminal);
atomexClient: App.Terminal,
symbolsProvider: App.SymbolsProvider);

IsCriticalWarning = false;

Expand Down Expand Up @@ -516,7 +515,7 @@ protected virtual async Task UpdateAmountAsync(decimal value)
OnPropertyChanged(nameof(EstimatedPaymentFee));
OnPropertyChanged(nameof(EstimatedMakerNetworkFee));

UpdateRedeemAndRewardFeesAsync();
await UpdateRedeemAndRewardFeesAsync();

#if DEBUG
if (!Env.IsInDesignerMode())
Expand Down Expand Up @@ -550,23 +549,37 @@ private void UpdateTargetAmountInBase(ICurrencyQuotesProvider provider)
TargetAmountInBase = _targetAmount * (quote?.Bid ?? 0m);
}

protected async void UpdateRedeemAndRewardFeesAsync()
protected async Task UpdateRedeemAndRewardFeesAsync()
{
#if DEBUG
if (!Env.IsInDesignerMode())
{
if (Env.IsInDesignerMode())
return;
#endif
var walletAddress = await App.Account
.GetRedeemAddressAsync(ToCurrency.FeeCurrencyName);
var walletAddress = await App.Account
.GetRedeemAddressAsync(ToCurrency.Name);

EstimatedRedeemFee = await ToCurrency.GetRedeemFeeAsync(walletAddress);
_estimatedRedeemFee = await ToCurrency
.GetRedeemFeeAsync(walletAddress);

RewardForRedeem = walletAddress.AvailableBalance() < EstimatedRedeemFee && !(ToCurrency is BitcoinBasedCurrency)
? await ToCurrency.GetRewardForRedeemAsync()
: 0;
#if DEBUG
_rewardForRedeem = await RewardForRedeemHelper
.EstimateAsync(
account: App.Account,
quotesProvider: App.QuotesProvider,
feeCurrencyQuotesProvider: symbol => App.Terminal?.GetOrderBook(symbol)?.TopOfBook(),
walletAddress: walletAddress);

_hasRewardForRedeem = _rewardForRedeem != 0;

if (Application.Current.Dispatcher != null)
{
await Application.Current.Dispatcher.InvokeAsync(() =>
{
OnPropertyChanged(nameof(EstimatedRedeemFee));
OnPropertyChanged(nameof(RewardForRedeem));
OnPropertyChanged(nameof(HasRewardForRedeem));

}, DispatcherPriority.Background);
}
#endif
}

private void OnTerminalChangedEventHandler(object sender, TerminalChangedEventArgs args)
Expand All @@ -585,61 +598,82 @@ private void OnTerminalChangedEventHandler(object sender, TerminalChangedEventAr
.ToList();

FromCurrencies = _currencyViewModels.ToList();

_toCurrency = terminal.Account.Currencies.GetByName("LTC");
FromCurrency = terminal.Account.Currencies.GetByName("BTC");
ToCurrency = terminal.Account.Currencies.GetByName("LTC");

OnSwapEventHandler(this, null);
}

protected void OnBaseQuotesUpdatedEventHandler(object sender, EventArgs args)
protected async void OnBaseQuotesUpdatedEventHandler(object sender, EventArgs args)
{
if (!(sender is ICurrencyQuotesProvider provider))
return;

if (CurrencyCode == null || TargetCurrencyCode == null || BaseCurrencyCode == null)
if (_currencyCode == null || _targetCurrencyCode == null || _baseCurrencyCode == null)
return;

var fromCurrencyPrice = provider.GetQuote(CurrencyCode, BaseCurrencyCode)?.Bid ?? 0m;
AmountInBase = _amount * fromCurrencyPrice;
var fromCurrencyPrice = provider.GetQuote(_currencyCode, _baseCurrencyCode)?.Bid ?? 0m;
_amountInBase = _amount * fromCurrencyPrice;

var fromCurrencyFeePrice = provider.GetQuote(FromCurrency.FeeCurrencyName, _baseCurrencyCode)?.Bid ?? 0m;
_estimatedPaymentFeeInBase = _estimatedPaymentFee * fromCurrencyFeePrice;

var fromCurrencyFeePrice = provider.GetQuote(FromCurrency.FeeCurrencyName, BaseCurrencyCode)?.Bid ?? 0m;
EstimatedPaymentFeeInBase = _estimatedPaymentFee * fromCurrencyFeePrice;
var toCurrencyFeePrice = provider.GetQuote(ToCurrency.FeeCurrencyName, _baseCurrencyCode)?.Bid ?? 0m;
_estimatedRedeemFeeInBase = _estimatedRedeemFee * toCurrencyFeePrice;

var toCurrencyFeePrice = provider.GetQuote(ToCurrency.FeeCurrencyName, BaseCurrencyCode)?.Bid ?? 0m;
EstimatedRedeemFeeInBase = _estimatedRedeemFee * toCurrencyFeePrice;
var toCurrencyPrice = provider.GetQuote(TargetCurrencyCode, _baseCurrencyCode)?.Bid ?? 0m;
_rewardForRedeemInBase = _rewardForRedeem * toCurrencyPrice;

EstimatedMakerNetworkFeeInBase = _estimatedMakerNetworkFee * fromCurrencyPrice;
_estimatedMakerNetworkFeeInBase = _estimatedMakerNetworkFee * fromCurrencyPrice;

EstimatedTotalNetworkFeeInBase =
EstimatedPaymentFeeInBase +
EstimatedRedeemFeeInBase +
EstimatedMakerNetworkFeeInBase;
_estimatedTotalNetworkFeeInBase =
_estimatedPaymentFeeInBase +
(!_hasRewardForRedeem ? _estimatedRedeemFeeInBase : 0) +
_estimatedMakerNetworkFeeInBase +
(_hasRewardForRedeem ? _rewardForRedeemInBase : 0);

if (AmountInBase != 0 && EstimatedTotalNetworkFeeInBase / AmountInBase > 0.3m)
if (_amountInBase != 0 && _estimatedTotalNetworkFeeInBase / _amountInBase > 0.3m)
{
IsCriticalWarning = true;
Warning = string.Format(
_isCriticalWarning = true;
_warning = string.Format(
CultureInfo.InvariantCulture,
Resources.CvTooHighNetworkFee,
FormattableString.Invariant($"{EstimatedTotalNetworkFeeInBase:$0.00}"),
FormattableString.Invariant($"{EstimatedTotalNetworkFeeInBase / AmountInBase:0.00%}"));
FormattableString.Invariant($"{_estimatedTotalNetworkFeeInBase:$0.00}"),
FormattableString.Invariant($"{_estimatedTotalNetworkFeeInBase / _amountInBase:0.00%}"));
}
else if (AmountInBase != 0 && EstimatedTotalNetworkFeeInBase / AmountInBase > 0.1m)
else if (_amountInBase != 0 && _estimatedTotalNetworkFeeInBase / _amountInBase > 0.1m)
{
IsCriticalWarning = false;
Warning = string.Format(
_isCriticalWarning = false;
_warning = string.Format(
CultureInfo.InvariantCulture,
Resources.CvSufficientNetworkFee,
FormattableString.Invariant($"{EstimatedTotalNetworkFeeInBase:$0.00}"),
FormattableString.Invariant($"{EstimatedTotalNetworkFeeInBase / AmountInBase:0.00%}"));
FormattableString.Invariant($"{_estimatedTotalNetworkFeeInBase:$0.00}"),
FormattableString.Invariant($"{_estimatedTotalNetworkFeeInBase / _amountInBase:0.00%}"));
}

CanConvert = AmountInBase == 0 || EstimatedTotalNetworkFeeInBase / AmountInBase <= 0.75m;
_canConvert = _amountInBase == 0 || _estimatedTotalNetworkFeeInBase / _amountInBase <= 0.75m;

var toCurrencyPrice = provider.GetQuote(TargetCurrencyCode, BaseCurrencyCode)?.Bid ?? 0m;
RewardForRedeemInBase = _rewardForRedeem * toCurrencyPrice;
if (Application.Current.Dispatcher != null)
{
await Application.Current.Dispatcher.InvokeAsync(() =>
{
OnPropertyChanged(nameof(AmountInBase));
OnPropertyChanged(nameof(EstimatedPaymentFeeInBase));
OnPropertyChanged(nameof(EstimatedRedeemFeeInBase));
OnPropertyChanged(nameof(RewardForRedeemInBase));

OnPropertyChanged(nameof(EstimatedMakerNetworkFeeInBase));
OnPropertyChanged(nameof(EstimatedTotalNetworkFeeInBase));

OnPropertyChanged(nameof(IsCriticalWarning));
OnPropertyChanged(nameof(Warning));
OnPropertyChanged(nameof(CanConvert));

UpdateTargetAmountInBase(provider);

UpdateTargetAmountInBase(provider);
}, DispatcherPriority.Background);
}
}

protected async void OnQuotesUpdatedEventHandler(object sender, MarketDataEventArgs args)
Expand All @@ -651,7 +685,8 @@ protected async void OnQuotesUpdatedEventHandler(object sender, MarketDataEventA
fromCurrency: FromCurrency,
toCurrency: ToCurrency,
account: App.Account,
atomexClient: App.Terminal);
atomexClient: App.Terminal,
symbolsProvider: App.SymbolsProvider);

if (swapPriceEstimation == null)
return;
Expand All @@ -674,6 +709,7 @@ await Application.Current.Dispatcher.InvokeAsync(() =>
OnPropertyChanged(nameof(TargetAmount));

UpdateTargetAmountInBase(App.QuotesProvider);

}, DispatcherPriority.Background);
}
}
Expand Down Expand Up @@ -701,6 +737,7 @@ await Application.Current.Dispatcher.InvokeAsync(() =>
.CompareTo(s1.Time.ToUniversalTime()));

Swaps = new ObservableCollection<SwapViewModel>(swapViewModels);

}, DispatcherPriority.Background);
}
}
Expand Down Expand Up @@ -825,19 +862,19 @@ private void DesignerMode()
{
SwapViewModelFactory.CreateSwapViewModel(new Swap
{
Symbol = "LTC/BTC",
Price = 0.0000888m,
Qty = 0.001000m,
Side = Side.Buy,
Symbol = "LTC/BTC",
Price = 0.0000888m,
Qty = 0.001000m,
Side = Side.Buy,
TimeStamp = DateTime.UtcNow
},
DesignTime.Currencies),
SwapViewModelFactory.CreateSwapViewModel(new Swap
{
Symbol = "LTC/BTC",
Price = 0.0100808m,
Qty = 0.0043000m,
Side = Side.Sell,
Symbol = "LTC/BTC",
Price = 0.0100808m,
Qty = 0.0043000m,
Side = Side.Sell,
TimeStamp = DateTime.UtcNow
},
DesignTime.Currencies)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ public override async void Next()
wallet: Wallet,
password: Password,
currenciesProvider: App.CurrenciesProvider,
symbolsProvider: App.SymbolsProvider,
clientType: ClientType.Wpf);

Password = null;
Expand Down
1 change: 0 additions & 1 deletion Atomex.Client.Wpf/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,6 @@ private void InactivityHandler(object sender, EventArgs args)
pathToAccount: pathToAccount,
password: password,
currenciesProvider: AtomexApp.CurrenciesProvider,
symbolsProvider: AtomexApp.SymbolsProvider,
clientType: ClientType.Wpf);
});

Expand Down
13 changes: 9 additions & 4 deletions Atomex.Client.Wpf/ViewModels/MyWalletsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public MyWalletsViewModel(
}

private ICommand _selectWalletCommand;
public ICommand SelectWalletCommand => _selectWalletCommand ?? new RelayCommand<WalletInfo>(info =>
public ICommand SelectWalletCommand => _selectWalletCommand ??= new RelayCommand<WalletInfo>(info =>
{
IAccount account = null;

Expand All @@ -48,13 +48,18 @@ public MyWalletsViewModel(
pathToAccount: info.Path,
password: password,
currenciesProvider: AtomexApp.CurrenciesProvider,
symbolsProvider: AtomexApp.SymbolsProvider,
clientType: ClientType.Wpf);
});

unlockViewModel.Unlocked += (sender, args) =>
{
AtomexApp.UseTerminal(new WebSocketAtomexClient(App.Configuration, account), restart: true);
var atomexClient = new WebSocketAtomexClient(
configuration: App.Configuration,
account: account,
symbolsProvider: AtomexApp.SymbolsProvider,
quotesProvider: AtomexApp.QuotesProvider);

AtomexApp.UseTerminal(atomexClient, restart: true);

DialogViewer.HideDialog(Dialogs.MyWallets);
DialogViewer.HideDialog(Dialogs.Start);
Expand All @@ -76,4 +81,4 @@ private void DesignerMode()
};
}
}
}
}
1 change: 0 additions & 1 deletion Atomex.Client.Wpf/ViewModels/SettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ private void Apply()
pathToAccount: _account.Wallet.PathToWallet,
password: password,
currenciesProvider: AtomexApp.CurrenciesProvider,
symbolsProvider: AtomexApp.SymbolsProvider,
clientType: ClientType.Wpf);

var pathToUserSettings =
Expand Down
Loading

0 comments on commit c12ad4a

Please sign in to comment.