diff --git a/lib/exchange/changenow/changenow_exchange_provider.dart b/lib/exchange/changenow/changenow_exchange_provider.dart index 8f984359f..a852bc298 100644 --- a/lib/exchange/changenow/changenow_exchange_provider.dart +++ b/lib/exchange/changenow/changenow_exchange_provider.dart @@ -29,6 +29,8 @@ class ChangeNowExchangeProvider extends ExchangeProvider { static const _exchangeAmountUriSufix = '/exchange-amount/'; static const _transactionsUriSufix = '/transactions/'; static const _minAmountUriSufix = '/min-amount/'; + static const _marketInfoUriSufix = '/market-info/'; + static const _fixedRateUriSufix = 'fixed-rate/'; @override String get title => 'ChangeNOW'; @@ -44,19 +46,43 @@ class ChangeNowExchangeProvider extends ExchangeProvider { Future checkIsAvailable() async => true; @override - Future fetchLimits({CryptoCurrency from, CryptoCurrency to}) async { + Future fetchLimits({CryptoCurrency from, CryptoCurrency to, + bool isFixedRateMode}) async { final symbol = from.toString() + '_' + to.toString(); - final url = apiUri + _minAmountUriSufix + symbol; + final url = isFixedRateMode + ? apiUri + _marketInfoUriSufix + _fixedRateUriSufix + apiKey + : apiUri + _minAmountUriSufix + symbol; final response = await get(url); - final responseJSON = json.decode(response.body) as Map; - final min = responseJSON['minAmount'] as double; - return Limits(min: min, max: null); + if (isFixedRateMode) { + final responseJSON = json.decode(response.body) as List; + + for (var elem in responseJSON) { + final elemFrom = elem["from"] as String; + final elemTo = elem["to"] as String; + + if ((elemFrom == from.toString().toLowerCase()) && + (elemTo == to.toString().toLowerCase())) { + final min = elem["min"] as double; + final max = elem["max"] as double; + + return Limits(min: min, max: max); + } + } + return Limits(min: 0, max: 0); + } else { + final responseJSON = json.decode(response.body) as Map; + final min = responseJSON['minAmount'] as double; + + return Limits(min: min, max: null); + } } @override - Future createTrade({TradeRequest request}) async { - const url = apiUri + _transactionsUriSufix + apiKey; + Future createTrade({TradeRequest request, bool isFixedRateMode}) async { + final url = isFixedRateMode + ? apiUri + _transactionsUriSufix + _fixedRateUriSufix + apiKey + : apiUri + _transactionsUriSufix + apiKey; final _request = request as ChangeNowRequest; final body = { 'from': _request.from.toString(), @@ -145,14 +171,25 @@ class ChangeNowExchangeProvider extends ExchangeProvider { {CryptoCurrency from, CryptoCurrency to, double amount, + bool isFixedRateMode, bool isReceiveAmount}) async { - final url = apiUri + - _exchangeAmountUriSufix + - amount.toString() + - '/' + - from.toString() + - '_' + - to.toString(); + final url = isFixedRateMode + ? apiUri + + _exchangeAmountUriSufix + + _fixedRateUriSufix + + amount.toString() + + '/' + + from.toString() + + '_' + + to.toString() + + '?api_key=' + apiKey + : apiUri + + _exchangeAmountUriSufix + + amount.toString() + + '/' + + from.toString() + + '_' + + to.toString(); final response = await get(url); final responseJSON = json.decode(response.body) as Map; final estimatedAmount = responseJSON['estimatedAmount'] as double; diff --git a/lib/exchange/exchange_provider.dart b/lib/exchange/exchange_provider.dart index 276177dac..616715022 100644 --- a/lib/exchange/exchange_provider.dart +++ b/lib/exchange/exchange_provider.dart @@ -17,10 +17,11 @@ abstract class ExchangeProvider { @override String toString() => title; - Future fetchLimits({CryptoCurrency from, CryptoCurrency to}); - Future createTrade({TradeRequest request}); + Future fetchLimits( + {CryptoCurrency from, CryptoCurrency to, bool isFixedRateMode}); + Future createTrade({TradeRequest request, bool isFixedRateMode}); Future findTradeById({@required String id}); - Future calculateAmount( - {CryptoCurrency from, CryptoCurrency to, double amount, bool isReceiveAmount}); + Future calculateAmount({CryptoCurrency from, CryptoCurrency to, + double amount, bool isFixedRateMode, bool isReceiveAmount}); Future checkIsAvailable(); } diff --git a/lib/exchange/morphtoken/morphtoken_exchange_provider.dart b/lib/exchange/morphtoken/morphtoken_exchange_provider.dart index b90eb84e9..ed8010f41 100644 --- a/lib/exchange/morphtoken/morphtoken_exchange_provider.dart +++ b/lib/exchange/morphtoken/morphtoken_exchange_provider.dart @@ -71,7 +71,7 @@ class MorphTokenExchangeProvider extends ExchangeProvider { Future checkIsAvailable() async => true; @override - Future fetchLimits({CryptoCurrency from, CryptoCurrency to}) async { + Future fetchLimits({CryptoCurrency from, CryptoCurrency to, bool isFixedRateMode}) async { final url = apiUri + _limitsURISuffix; final headers = {'Content-type': 'application/json'}; final body = json.encode({ @@ -100,7 +100,7 @@ class MorphTokenExchangeProvider extends ExchangeProvider { } @override - Future createTrade({TradeRequest request}) async { + Future createTrade({TradeRequest request, bool isFixedRateMode}) async { const url = apiUri + _morphURISuffix; final _request = request as MorphTokenRequest; final body = { @@ -191,7 +191,7 @@ class MorphTokenExchangeProvider extends ExchangeProvider { @override Future calculateAmount( - {CryptoCurrency from, CryptoCurrency to, double amount, + {CryptoCurrency from, CryptoCurrency to, double amount, bool isFixedRateMode, bool isReceiveAmount}) async { final url = apiUri + _ratesURISuffix; final response = await get(url); diff --git a/lib/exchange/xmrto/xmrto_exchange_provider.dart b/lib/exchange/xmrto/xmrto_exchange_provider.dart index cc63ff1a3..91365ee40 100644 --- a/lib/exchange/xmrto/xmrto_exchange_provider.dart +++ b/lib/exchange/xmrto/xmrto_exchange_provider.dart @@ -58,7 +58,7 @@ class XMRTOExchangeProvider extends ExchangeProvider { } @override - Future fetchLimits({CryptoCurrency from, CryptoCurrency to}) async { + Future fetchLimits({CryptoCurrency from, CryptoCurrency to, bool isFixedRateMode}) async { final url = originalApiUri + _orderParameterUriSuffix; final response = await get(url); final correction = 0.001; @@ -91,7 +91,7 @@ class XMRTOExchangeProvider extends ExchangeProvider { } @override - Future createTrade({TradeRequest request}) async { + Future createTrade({TradeRequest request, bool isFixedRateMode}) async { final _request = request as XMRTOTradeRequest; final url = originalApiUri + _orderCreateUriSuffix; final _amount = @@ -188,6 +188,7 @@ class XMRTOExchangeProvider extends ExchangeProvider { {CryptoCurrency from, CryptoCurrency to, double amount, + bool isFixedRateMode, bool isReceiveAmount}) async { if (from != CryptoCurrency.xmr && to != CryptoCurrency.btc) { return 0; diff --git a/lib/src/screens/exchange/exchange_page.dart b/lib/src/screens/exchange/exchange_page.dart index 20b7a842f..03cc866e6 100644 --- a/lib/src/screens/exchange/exchange_page.dart +++ b/lib/src/screens/exchange/exchange_page.dart @@ -693,7 +693,14 @@ class ExchangePage extends BasePage { exchangeViewModel.isReceiveAmountEntered) && !isFixedRateMode) { FocusScope.of(context).unfocus(); receiveAmountController.text = ''; + } else { + exchangeViewModel.changeDepositAmount( + amount: depositAmountController.text); } + + checkBoxKey.currentState + .changeValue(exchangeViewModel.isFixedRateMode); + exchangeViewModel.loadLimits(); }); _isReactionsSet = true; diff --git a/lib/view_model/exchange/exchange_view_model.dart b/lib/view_model/exchange/exchange_view_model.dart index 506df787b..f38180230 100644 --- a/lib/view_model/exchange/exchange_view_model.dart +++ b/lib/view_model/exchange/exchange_view_model.dart @@ -137,12 +137,14 @@ abstract class ExchangeViewModelBase with Store { depositAmount = ''; receiveAmount = ''; isReceiveAmountEditable = provider is ChangeNowExchangeProvider; + isFixedRateMode = false; loadLimits(); } @action void changeDepositCurrency({CryptoCurrency currency}) { depositCurrency = currency; + isFixedRateMode = false; _onPairChange(); isDepositAddressEnabled = !(depositCurrency == wallet.currency); isReceiveAddressEnabled = !(receiveCurrency == wallet.currency); @@ -151,6 +153,7 @@ abstract class ExchangeViewModelBase with Store { @action void changeReceiveCurrency({CryptoCurrency currency}) { receiveCurrency = currency; + isFixedRateMode = false; _onPairChange(); isDepositAddressEnabled = !(depositCurrency == wallet.currency); isReceiveAddressEnabled = !(receiveCurrency == wallet.currency); @@ -173,6 +176,7 @@ abstract class ExchangeViewModelBase with Store { from: receiveCurrency, to: depositCurrency, amount: _amount, + isFixedRateMode: isFixedRateMode, isReceiveAmount: true) .then((amount) => _cryptoNumberFormat .format(amount) @@ -197,6 +201,7 @@ abstract class ExchangeViewModelBase with Store { from: depositCurrency, to: receiveCurrency, amount: _amount, + isFixedRateMode: isFixedRateMode, isReceiveAmount: false) .then((amount) => _cryptoNumberFormat .format(amount) @@ -210,8 +215,8 @@ abstract class ExchangeViewModelBase with Store { limitsState = LimitsIsLoading(); try { - limits = await provider.fetchLimits( - from: depositCurrency, to: receiveCurrency); + limits = await provider.fetchLimits(from: depositCurrency, + to: receiveCurrency, isFixedRateMode: isFixedRateMode); limitsState = LimitsLoadedSuccessfully(limits: limits); } catch (e) { limitsState = LimitsLoadedFailure(error: e.toString()); @@ -275,7 +280,8 @@ abstract class ExchangeViewModelBase with Store { } else { try { tradeState = TradeIsCreating(); - final trade = await provider.createTrade(request: request); + final trade = await provider.createTrade(request: request, + isFixedRateMode: isFixedRateMode); trade.walletId = wallet.id; tradesStore.setTrade(trade); await trades.add(trade); @@ -304,6 +310,7 @@ abstract class ExchangeViewModelBase with Store { receiveAddress = receiveCurrency == wallet.currency ? wallet.address : ''; isDepositAddressEnabled = !(depositCurrency == wallet.currency); isReceiveAddressEnabled = !(receiveCurrency == wallet.currency); + isFixedRateMode = false; _onPairChange(); }