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

This commit is contained in:
Oleksandr Sobol 2020-04-13 19:05:27 +03:00
parent b85c332b24
commit ef750cb454
4 changed files with 43 additions and 42 deletions

View file

@ -0,0 +1,7 @@
import 'package:intl/intl.dart';
final amountFormat = NumberFormat()
..maximumFractionDigits = 3
..minimumFractionDigits = 1;
double limitsFormat(double limit) => double.parse(amountFormat.format(limit));

View file

@ -12,6 +12,7 @@ import 'package:cake_wallet/src/domain/exchange/xmrto/xmrto_trade_request.dart';
import 'package:cake_wallet/src/domain/exchange/trade_not_created_exeption.dart'; import 'package:cake_wallet/src/domain/exchange/trade_not_created_exeption.dart';
import 'package:cake_wallet/src/domain/exchange/exchange_provider_description.dart'; import 'package:cake_wallet/src/domain/exchange/exchange_provider_description.dart';
import 'package:cake_wallet/src/domain/exchange/trade_not_found_exeption.dart'; import 'package:cake_wallet/src/domain/exchange/trade_not_found_exeption.dart';
import 'package:cake_wallet/src/domain/exchange/limits_format.dart';
class XMRTOExchangeProvider extends ExchangeProvider { class XMRTOExchangeProvider extends ExchangeProvider {
XMRTOExchangeProvider() XMRTOExchangeProvider()
@ -54,14 +55,31 @@ class XMRTOExchangeProvider extends ExchangeProvider {
Future<Limits> fetchLimits({CryptoCurrency from, CryptoCurrency to}) async { Future<Limits> fetchLimits({CryptoCurrency from, CryptoCurrency to}) async {
final url = await getApiUri() + _orderParameterUriSufix; final url = await getApiUri() + _orderParameterUriSufix;
final response = await get(url); final response = await get(url);
final correction = 0.001;
if (response.statusCode != 200) { if (response.statusCode != 200) {
return Limits(min: 0, max: 0); return Limits(min: 0, max: 0);
} }
final responseJSON = json.decode(response.body) as Map<String, dynamic>; final responseJSON = json.decode(response.body) as Map<String, dynamic>;
final min = responseJSON['lower_limit'] as double; double min = responseJSON['lower_limit'] as double;
final max = responseJSON['upper_limit'] as double; double max = responseJSON['upper_limit'] as double;
final price = responseJSON['price'] as double;
if (price > 0) {
try {
min /= price;
min = limitsFormat(min) + correction;
max /= price;
max = limitsFormat(max);
} catch (e) {
min = 0;
max = 0;
}
} else {
min = 0;
max = 0;
}
return Limits(min: min, max: max); return Limits(min: min, max: max);
} }
@ -71,7 +89,7 @@ class XMRTOExchangeProvider extends ExchangeProvider {
final _request = request as XMRTOTradeRequest; final _request = request as XMRTOTradeRequest;
final url = await getApiUri() + _orderCreateUriSufix; final url = await getApiUri() + _orderCreateUriSufix;
final body = { final body = {
'btc_amount': _request.amount, 'xmr_amount': _request.amount,
'btc_dest_address': _request.address 'btc_dest_address': _request.address
}; };
final response = await post(url, final response = await post(url,
@ -168,8 +186,7 @@ class XMRTOExchangeProvider extends ExchangeProvider {
final response = final response =
await get(url, headers: {'Content-Type': 'application/json'}); await get(url, headers: {'Content-Type': 'application/json'});
final responseJSON = json.decode(response.body) as Map<String, dynamic>; final responseJSON = json.decode(response.body) as Map<String, dynamic>;
final btcprice = responseJSON['price'] as double; final price = responseJSON['price'] as double;
final price = 1 / btcprice;
return price; return price;
} catch (e) { } catch (e) {

View file

@ -168,12 +168,9 @@ class ExchangeFormState extends State<ExchangeForm> {
exchangeStore.depositCurrency == walletStore.type exchangeStore.depositCurrency == walletStore.type
? walletStore.address ? walletStore.address
: null, : null,
initialIsAmountEditable: initialIsAmountEditable: true,
!(exchangeStore.provider is XMRTOExchangeProvider), initialIsAddressEditable: true,
initialIsAddressEditable: isAmountEstimated: false,
!(exchangeStore.provider is XMRTOExchangeProvider),
isAmountEstimated:
exchangeStore.provider is XMRTOExchangeProvider,
currencies: CryptoCurrency.all, currencies: CryptoCurrency.all,
onCurrencySelected: (currency) => onCurrencySelected: (currency) =>
exchangeStore.changeDepositCurrency(currency: currency), exchangeStore.changeDepositCurrency(currency: currency),
@ -209,12 +206,9 @@ class ExchangeFormState extends State<ExchangeForm> {
exchangeStore.receiveCurrency == walletStore.type exchangeStore.receiveCurrency == walletStore.type
? walletStore.address ? walletStore.address
: null, : null,
initialIsAmountEditable: initialIsAmountEditable: false,
exchangeStore.provider is XMRTOExchangeProvider, initialIsAddressEditable: true,
initialIsAddressEditable: isAmountEstimated: true,
exchangeStore.provider is XMRTOExchangeProvider,
isAmountEstimated: !(exchangeStore.provider
is XMRTOExchangeProvider),
currencies: CryptoCurrency.all, currencies: CryptoCurrency.all,
onCurrencySelected: (currency) => exchangeStore onCurrencySelected: (currency) => exchangeStore
.changeReceiveCurrency(currency: currency), .changeReceiveCurrency(currency: currency),
@ -321,8 +315,7 @@ class ExchangeFormState extends State<ExchangeForm> {
final max = limitsState.limits.max != null final max = limitsState.limits.max != null
? limitsState.limits.max.toString() ? limitsState.limits.max.toString()
: null; : null;
final key = final key = depositKey;
store.provider is XMRTOExchangeProvider ? receiveKey : depositKey;
key.currentState.changeLimits(min: min, max: max); key.currentState.changeLimits(min: min, max: max);
} }
@ -363,22 +356,12 @@ class ExchangeFormState extends State<ExchangeForm> {
}); });
reaction((_) => store.provider, (ExchangeProvider provider) { reaction((_) => store.provider, (ExchangeProvider provider) {
final isReversed = provider is XMRTOExchangeProvider;
if (isReversed) {
receiveKey.currentState.isAddressEditable(isEditable: true);
receiveKey.currentState.isAmountEditable(isEditable: true);
depositKey.currentState.isAddressEditable(isEditable: false);
depositKey.currentState.isAmountEditable(isEditable: false);
} else {
receiveKey.currentState.isAddressEditable(isEditable: true); receiveKey.currentState.isAddressEditable(isEditable: true);
receiveKey.currentState.isAmountEditable(isEditable: false); receiveKey.currentState.isAmountEditable(isEditable: false);
depositKey.currentState.isAddressEditable(isEditable: true); depositKey.currentState.isAddressEditable(isEditable: true);
depositKey.currentState.isAmountEditable(isEditable: true); depositKey.currentState.isAmountEditable(isEditable: true);
}
depositKey.currentState.changeIsAmountEstimated(isReversed); receiveKey.currentState.changeIsAmountEstimated(true);
receiveKey.currentState.changeIsAmountEstimated(!isReversed);
}); });
reaction((_) => store.tradeState, (ExchangeTradeState state) { reaction((_) => store.tradeState, (ExchangeTradeState state) {
@ -406,7 +389,6 @@ class ExchangeFormState extends State<ExchangeForm> {
}); });
reaction((_) => store.limitsState, (LimitsState state) { reaction((_) => store.limitsState, (LimitsState state) {
final isXMRTO = store.provider is XMRTOExchangeProvider;
String min; String min;
String max; String max;
@ -425,13 +407,8 @@ class ExchangeFormState extends State<ExchangeForm> {
max = '...'; max = '...';
} }
final depositMin = isXMRTO ? null : min; depositKey.currentState.changeLimits(min: min, max: max);
final depositMax = isXMRTO ? null : max; receiveKey.currentState.changeLimits(min: null, max: null);
final receiveMin = isXMRTO ? min : null;
final receiveMax = isXMRTO ? max : null;
depositKey.currentState.changeLimits(min: depositMin, max: depositMax);
receiveKey.currentState.changeLimits(min: receiveMin, max: receiveMax);
}); });
depositAddressController.addListener( depositAddressController.addListener(

View file

@ -157,13 +157,13 @@ abstract class ExchangeStoreBase with Store {
if (provider is XMRTOExchangeProvider) { if (provider is XMRTOExchangeProvider) {
request = XMRTOTradeRequest( request = XMRTOTradeRequest(
to: receiveCurrency,
from: depositCurrency, from: depositCurrency,
amount: receiveAmount, to: receiveCurrency,
amount: depositAmount,
address: receiveAddress, address: receiveAddress,
refundAddress: depositAddress); refundAddress: depositAddress);
amount = receiveAmount; amount = depositAmount;
currency = receiveCurrency; currency = depositCurrency;
} }
if (provider is ChangeNowExchangeProvider) { if (provider is ChangeNowExchangeProvider) {