CWA-199 | changed possibility to enter amount in the deposit card only (exchange page); changed request for XMRTOExchangeProvider in the createTrade() (exchange_store); fixed calculation limits for XMR in the fetchLimits(), fixed post body in the createTrade() (xmrto_exchange_provider); created limits_format
Oleksandr Sobol committed
Apr 13, 2020 at 19:05 UTC
ef750cb454604699b9fd7b0f9ebd16aa670e6fda
4 files changed
+43
-42
lib/src/domain/exchange/limits_format.dart
new
+7
@@ -0,0 +1,7 @@
1
+import 'package:intl/intl.dart';
2
+
3
+final amountFormat = NumberFormat()
4
+ ..maximumFractionDigits = 3
5
+ ..minimumFractionDigits = 1;
6
+
7
+double limitsFormat(double limit) => double.parse(amountFormat.format(limit));
\ No newline at end of file
lib/src/domain/exchange/xmrto/xmrto_exchange_provider.dart
+22
-5
@@ -12,6 +12,7 @@ import 'package:cake_wallet/src/domain/exchange/xmrto/xmrto_trade_request.dart';
12
import 'package:cake_wallet/src/domain/exchange/trade_not_created_exeption.dart';
13
import 'package:cake_wallet/src/domain/exchange/exchange_provider_description.dart';
14
import 'package:cake_wallet/src/domain/exchange/trade_not_found_exeption.dart';
15
+import 'package:cake_wallet/src/domain/exchange/limits_format.dart';
16
17
class XMRTOExchangeProvider extends ExchangeProvider {
18
XMRTOExchangeProvider()
@@ -54,14 +55,31 @@ class XMRTOExchangeProvider extends ExchangeProvider {
55
Future<Limits> fetchLimits({CryptoCurrency from, CryptoCurrency to}) async {
56
final url = await getApiUri() + _orderParameterUriSufix;
57
final response = await get(url);
58
+ final correction = 0.001;
59
60
if (response.statusCode != 200) {
61
return Limits(min: 0, max: 0);
62
}
63
64
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
63
- final min = responseJSON['lower_limit'] as double;
64
- final max = responseJSON['upper_limit'] as double;
65
+ double min = responseJSON['lower_limit'] as double;
66
+ double max = responseJSON['upper_limit'] as double;
67
+ final price = responseJSON['price'] as double;
68
+
69
+ if (price > 0) {
70
+ try {
71
+ min /= price;
72
+ min = limitsFormat(min) + correction;
73
+ max /= price;
74
+ max = limitsFormat(max);
75
+ } catch (e) {
76
+ min = 0;
77
+ max = 0;
78
+ }
79
+ } else {
80
+ min = 0;
81
+ max = 0;
82
+ }
83
84
return Limits(min: min, max: max);
85
}
@@ -71,7 +89,7 @@ class XMRTOExchangeProvider extends ExchangeProvider {
89
final _request = request as XMRTOTradeRequest;
90
final url = await getApiUri() + _orderCreateUriSufix;
91
final body = {
74
- 'btc_amount': _request.amount,
92
+ 'xmr_amount': _request.amount,
93
'btc_dest_address': _request.address
94
};
95
final response = await post(url,
@@ -168,8 +186,7 @@ class XMRTOExchangeProvider extends ExchangeProvider {
186
final response =
187
await get(url, headers: {'Content-Type': 'application/json'});
188
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
171
- final btcprice = responseJSON['price'] as double;
172
- final price = 1 / btcprice;
189
+ final price = responseJSON['price'] as double;
190
191
return price;
192
} catch (e) {
lib/src/screens/exchange/exchange_page.dart
+10
-33
@@ -168,12 +168,9 @@ class ExchangeFormState extends State<ExchangeForm> {
168
exchangeStore.depositCurrency == walletStore.type
169
? walletStore.address
170
: null,
171
- initialIsAmountEditable:
172
- !(exchangeStore.provider is XMRTOExchangeProvider),
173
- initialIsAddressEditable:
174
- !(exchangeStore.provider is XMRTOExchangeProvider),
175
- isAmountEstimated:
176
- exchangeStore.provider is XMRTOExchangeProvider,
171
+ initialIsAmountEditable: true,
172
+ initialIsAddressEditable: true,
173
+ isAmountEstimated: false,
174
currencies: CryptoCurrency.all,
175
onCurrencySelected: (currency) =>
176
exchangeStore.changeDepositCurrency(currency: currency),
@@ -209,12 +206,9 @@ class ExchangeFormState extends State<ExchangeForm> {
206
exchangeStore.receiveCurrency == walletStore.type
207
? walletStore.address
208
: null,
212
- initialIsAmountEditable:
213
- exchangeStore.provider is XMRTOExchangeProvider,
214
- initialIsAddressEditable:
215
- exchangeStore.provider is XMRTOExchangeProvider,
216
- isAmountEstimated: !(exchangeStore.provider
217
- is XMRTOExchangeProvider),
209
+ initialIsAmountEditable: false,
210
+ initialIsAddressEditable: true,
211
+ isAmountEstimated: true,
212
currencies: CryptoCurrency.all,
213
onCurrencySelected: (currency) => exchangeStore
214
.changeReceiveCurrency(currency: currency),
@@ -321,8 +315,7 @@ class ExchangeFormState extends State<ExchangeForm> {
315
final max = limitsState.limits.max != null
316
? limitsState.limits.max.toString()
317
: null;
324
- final key =
325
- store.provider is XMRTOExchangeProvider ? receiveKey : depositKey;
318
+ final key = depositKey;
319
key.currentState.changeLimits(min: min, max: max);
320
}
321
@@ -363,22 +356,12 @@ class ExchangeFormState extends State<ExchangeForm> {
356
});
357
358
reaction((_) => store.provider, (ExchangeProvider provider) {
366
- final isReversed = provider is XMRTOExchangeProvider;
367
-
368
- if (isReversed) {
369
- receiveKey.currentState.isAddressEditable(isEditable: true);
370
- receiveKey.currentState.isAmountEditable(isEditable: true);
371
- depositKey.currentState.isAddressEditable(isEditable: false);
372
- depositKey.currentState.isAmountEditable(isEditable: false);
373
- } else {
359
receiveKey.currentState.isAddressEditable(isEditable: true);
360
receiveKey.currentState.isAmountEditable(isEditable: false);
361
depositKey.currentState.isAddressEditable(isEditable: true);
362
depositKey.currentState.isAmountEditable(isEditable: true);
378
- }
363
380
- depositKey.currentState.changeIsAmountEstimated(isReversed);
381
- receiveKey.currentState.changeIsAmountEstimated(!isReversed);
364
+ receiveKey.currentState.changeIsAmountEstimated(true);
365
});
366
367
reaction((_) => store.tradeState, (ExchangeTradeState state) {
@@ -406,7 +389,6 @@ class ExchangeFormState extends State<ExchangeForm> {
389
});
390
391
reaction((_) => store.limitsState, (LimitsState state) {
409
- final isXMRTO = store.provider is XMRTOExchangeProvider;
392
String min;
393
String max;
394
@@ -425,13 +407,8 @@ class ExchangeFormState extends State<ExchangeForm> {
407
max = '...';
408
}
409
428
- final depositMin = isXMRTO ? null : min;
429
- final depositMax = isXMRTO ? null : max;
430
- final receiveMin = isXMRTO ? min : null;
431
- final receiveMax = isXMRTO ? max : null;
432
-
433
- depositKey.currentState.changeLimits(min: depositMin, max: depositMax);
434
- receiveKey.currentState.changeLimits(min: receiveMin, max: receiveMax);
410
+ depositKey.currentState.changeLimits(min: min, max: max);
411
+ receiveKey.currentState.changeLimits(min: null, max: null);
412
});
413
414
depositAddressController.addListener(
lib/src/stores/exchange/exchange_store.dart
+4
-4
@@ -157,13 +157,13 @@ abstract class ExchangeStoreBase with Store {
157
158
if (provider is XMRTOExchangeProvider) {
159
request = XMRTOTradeRequest(
160
- to: receiveCurrency,
160
from: depositCurrency,
162
- amount: receiveAmount,
161
+ to: receiveCurrency,
162
+ amount: depositAmount,
163
address: receiveAddress,
164
refundAddress: depositAddress);
165
- amount = receiveAmount;
166
- currency = receiveCurrency;
165
+ amount = depositAmount;
166
+ currency = depositCurrency;
167
}
168
169
if (provider is ChangeNowExchangeProvider) {