CAKE-278 | applied fixed-rate flow to changenow_exchange_provider.dart, exchange_view_model.dart and exchange_page.dart; added isFixedRateMode to fetchLimits(), createTrade() and calculateAmount() methods in the exchange_provider.dart
OleksandrSobol committed
Feb 18, 2021 at 21:08 UTC
6e102c49696a4ff50f21bc0594c0cbf6c0cf3143
6 files changed
+79
-26
lib/exchange/changenow/changenow_exchange_provider.dart
+51
-14
@@ -29,6 +29,8 @@ class ChangeNowExchangeProvider extends ExchangeProvider {
29
static const _exchangeAmountUriSufix = '/exchange-amount/';
30
static const _transactionsUriSufix = '/transactions/';
31
static const _minAmountUriSufix = '/min-amount/';
32
+ static const _marketInfoUriSufix = '/market-info/';
33
+ static const _fixedRateUriSufix = 'fixed-rate/';
34
35
@override
36
String get title => 'ChangeNOW';
@@ -44,19 +46,43 @@ class ChangeNowExchangeProvider extends ExchangeProvider {
46
Future<bool> checkIsAvailable() async => true;
47
48
@override
47
- Future<Limits> fetchLimits({CryptoCurrency from, CryptoCurrency to}) async {
49
+ Future<Limits> fetchLimits({CryptoCurrency from, CryptoCurrency to,
50
+ bool isFixedRateMode}) async {
51
final symbol = from.toString() + '_' + to.toString();
49
- final url = apiUri + _minAmountUriSufix + symbol;
52
+ final url = isFixedRateMode
53
+ ? apiUri + _marketInfoUriSufix + _fixedRateUriSufix + apiKey
54
+ : apiUri + _minAmountUriSufix + symbol;
55
final response = await get(url);
51
- final responseJSON = json.decode(response.body) as Map<String, dynamic>;
52
- final min = responseJSON['minAmount'] as double;
56
54
- return Limits(min: min, max: null);
57
+ if (isFixedRateMode) {
58
+ final responseJSON = json.decode(response.body) as List<dynamic>;
59
+
60
+ for (var elem in responseJSON) {
61
+ final elemFrom = elem["from"] as String;
62
+ final elemTo = elem["to"] as String;
63
+
64
+ if ((elemFrom == from.toString().toLowerCase()) &&
65
+ (elemTo == to.toString().toLowerCase())) {
66
+ final min = elem["min"] as double;
67
+ final max = elem["max"] as double;
68
+
69
+ return Limits(min: min, max: max);
70
+ }
71
+ }
72
+ return Limits(min: 0, max: 0);
73
+ } else {
74
+ final responseJSON = json.decode(response.body) as Map<String, dynamic>;
75
+ final min = responseJSON['minAmount'] as double;
76
+
77
+ return Limits(min: min, max: null);
78
+ }
79
}
80
81
@override
58
- Future<Trade> createTrade({TradeRequest request}) async {
59
- const url = apiUri + _transactionsUriSufix + apiKey;
82
+ Future<Trade> createTrade({TradeRequest request, bool isFixedRateMode}) async {
83
+ final url = isFixedRateMode
84
+ ? apiUri + _transactionsUriSufix + _fixedRateUriSufix + apiKey
85
+ : apiUri + _transactionsUriSufix + apiKey;
86
final _request = request as ChangeNowRequest;
87
final body = {
88
'from': _request.from.toString(),
@@ -145,14 +171,25 @@ class ChangeNowExchangeProvider extends ExchangeProvider {
171
{CryptoCurrency from,
172
CryptoCurrency to,
173
double amount,
174
+ bool isFixedRateMode,
175
bool isReceiveAmount}) async {
149
- final url = apiUri +
150
- _exchangeAmountUriSufix +
151
- amount.toString() +
152
- '/' +
153
- from.toString() +
154
- '_' +
155
- to.toString();
176
+ final url = isFixedRateMode
177
+ ? apiUri +
178
+ _exchangeAmountUriSufix +
179
+ _fixedRateUriSufix +
180
+ amount.toString() +
181
+ '/' +
182
+ from.toString() +
183
+ '_' +
184
+ to.toString() +
185
+ '?api_key=' + apiKey
186
+ : apiUri +
187
+ _exchangeAmountUriSufix +
188
+ amount.toString() +
189
+ '/' +
190
+ from.toString() +
191
+ '_' +
192
+ to.toString();
193
final response = await get(url);
194
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
195
final estimatedAmount = responseJSON['estimatedAmount'] as double;
lib/exchange/exchange_provider.dart
+5
-4
@@ -17,10 +17,11 @@ abstract class ExchangeProvider {
17
@override
18
String toString() => title;
19
20
- Future<Limits> fetchLimits({CryptoCurrency from, CryptoCurrency to});
21
- Future<Trade> createTrade({TradeRequest request});
20
+ Future<Limits> fetchLimits(
21
+ {CryptoCurrency from, CryptoCurrency to, bool isFixedRateMode});
22
+ Future<Trade> createTrade({TradeRequest request, bool isFixedRateMode});
23
Future<Trade> findTradeById({@required String id});
23
- Future<double> calculateAmount(
24
- {CryptoCurrency from, CryptoCurrency to, double amount, bool isReceiveAmount});
24
+ Future<double> calculateAmount({CryptoCurrency from, CryptoCurrency to,
25
+ double amount, bool isFixedRateMode, bool isReceiveAmount});
26
Future<bool> checkIsAvailable();
27
}
lib/exchange/morphtoken/morphtoken_exchange_provider.dart
+3
-3
@@ -71,7 +71,7 @@ class MorphTokenExchangeProvider extends ExchangeProvider {
71
Future<bool> checkIsAvailable() async => true;
72
73
@override
74
- Future<Limits> fetchLimits({CryptoCurrency from, CryptoCurrency to}) async {
74
+ Future<Limits> fetchLimits({CryptoCurrency from, CryptoCurrency to, bool isFixedRateMode}) async {
75
final url = apiUri + _limitsURISuffix;
76
final headers = {'Content-type': 'application/json'};
77
final body = json.encode({
@@ -100,7 +100,7 @@ class MorphTokenExchangeProvider extends ExchangeProvider {
100
}
101
102
@override
103
- Future<Trade> createTrade({TradeRequest request}) async {
103
+ Future<Trade> createTrade({TradeRequest request, bool isFixedRateMode}) async {
104
const url = apiUri + _morphURISuffix;
105
final _request = request as MorphTokenRequest;
106
final body = {
@@ -191,7 +191,7 @@ class MorphTokenExchangeProvider extends ExchangeProvider {
191
192
@override
193
Future<double> calculateAmount(
194
- {CryptoCurrency from, CryptoCurrency to, double amount,
194
+ {CryptoCurrency from, CryptoCurrency to, double amount, bool isFixedRateMode,
195
bool isReceiveAmount}) async {
196
final url = apiUri + _ratesURISuffix;
197
final response = await get(url);
lib/exchange/xmrto/xmrto_exchange_provider.dart
+3
-2
@@ -58,7 +58,7 @@ class XMRTOExchangeProvider extends ExchangeProvider {
58
}
59
60
@override
61
- Future<Limits> fetchLimits({CryptoCurrency from, CryptoCurrency to}) async {
61
+ Future<Limits> fetchLimits({CryptoCurrency from, CryptoCurrency to, bool isFixedRateMode}) async {
62
final url = originalApiUri + _orderParameterUriSuffix;
63
final response = await get(url);
64
final correction = 0.001;
@@ -91,7 +91,7 @@ class XMRTOExchangeProvider extends ExchangeProvider {
91
}
92
93
@override
94
- Future<Trade> createTrade({TradeRequest request}) async {
94
+ Future<Trade> createTrade({TradeRequest request, bool isFixedRateMode}) async {
95
final _request = request as XMRTOTradeRequest;
96
final url = originalApiUri + _orderCreateUriSuffix;
97
final _amount =
@@ -188,6 +188,7 @@ class XMRTOExchangeProvider extends ExchangeProvider {
188
{CryptoCurrency from,
189
CryptoCurrency to,
190
double amount,
191
+ bool isFixedRateMode,
192
bool isReceiveAmount}) async {
193
if (from != CryptoCurrency.xmr && to != CryptoCurrency.btc) {
194
return 0;
lib/src/screens/exchange/exchange_page.dart
+7
@@ -693,7 +693,14 @@ class ExchangePage extends BasePage {
693
exchangeViewModel.isReceiveAmountEntered) && !isFixedRateMode) {
694
FocusScope.of(context).unfocus();
695
receiveAmountController.text = '';
696
+ } else {
697
+ exchangeViewModel.changeDepositAmount(
698
+ amount: depositAmountController.text);
699
}
700
+
701
+ checkBoxKey.currentState
702
+ .changeValue(exchangeViewModel.isFixedRateMode);
703
+ exchangeViewModel.loadLimits();
704
});
705
706
_isReactionsSet = true;
lib/view_model/exchange/exchange_view_model.dart
+10
-3
@@ -137,12 +137,14 @@ abstract class ExchangeViewModelBase with Store {
137
depositAmount = '';
138
receiveAmount = '';
139
isReceiveAmountEditable = provider is ChangeNowExchangeProvider;
140
+ isFixedRateMode = false;
141
loadLimits();
142
}
143
144
@action
145
void changeDepositCurrency({CryptoCurrency currency}) {
146
depositCurrency = currency;
147
+ isFixedRateMode = false;
148
_onPairChange();
149
isDepositAddressEnabled = !(depositCurrency == wallet.currency);
150
isReceiveAddressEnabled = !(receiveCurrency == wallet.currency);
@@ -151,6 +153,7 @@ abstract class ExchangeViewModelBase with Store {
153
@action
154
void changeReceiveCurrency({CryptoCurrency currency}) {
155
receiveCurrency = currency;
156
+ isFixedRateMode = false;
157
_onPairChange();
158
isDepositAddressEnabled = !(depositCurrency == wallet.currency);
159
isReceiveAddressEnabled = !(receiveCurrency == wallet.currency);
@@ -173,6 +176,7 @@ abstract class ExchangeViewModelBase with Store {
176
from: receiveCurrency,
177
to: depositCurrency,
178
amount: _amount,
179
+ isFixedRateMode: isFixedRateMode,
180
isReceiveAmount: true)
181
.then((amount) => _cryptoNumberFormat
182
.format(amount)
@@ -197,6 +201,7 @@ abstract class ExchangeViewModelBase with Store {
201
from: depositCurrency,
202
to: receiveCurrency,
203
amount: _amount,
204
+ isFixedRateMode: isFixedRateMode,
205
isReceiveAmount: false)
206
.then((amount) => _cryptoNumberFormat
207
.format(amount)
@@ -210,8 +215,8 @@ abstract class ExchangeViewModelBase with Store {
215
limitsState = LimitsIsLoading();
216
217
try {
213
- limits = await provider.fetchLimits(
214
- from: depositCurrency, to: receiveCurrency);
218
+ limits = await provider.fetchLimits(from: depositCurrency,
219
+ to: receiveCurrency, isFixedRateMode: isFixedRateMode);
220
limitsState = LimitsLoadedSuccessfully(limits: limits);
221
} catch (e) {
222
limitsState = LimitsLoadedFailure(error: e.toString());
@@ -275,7 +280,8 @@ abstract class ExchangeViewModelBase with Store {
280
} else {
281
try {
282
tradeState = TradeIsCreating();
278
- final trade = await provider.createTrade(request: request);
283
+ final trade = await provider.createTrade(request: request,
284
+ isFixedRateMode: isFixedRateMode);
285
trade.walletId = wallet.id;
286
tradesStore.setTrade(trade);
287
await trades.add(trade);
@@ -304,6 +310,7 @@ abstract class ExchangeViewModelBase with Store {
310
receiveAddress = receiveCurrency == wallet.currency ? wallet.address : '';
311
isDepositAddressEnabled = !(depositCurrency == wallet.currency);
312
isReceiveAddressEnabled = !(receiveCurrency == wallet.currency);
313
+ isFixedRateMode = false;
314
_onPairChange();
315
}
316