CAKE-43 | added the ability to enter an amount for xmr.to exchange in both currencies xmr and btc; added isReceiveAmountEntered to ExchangeViewModel; added receiveAmount and isBTCRequest to XMRTOTradeRequest; added isReceiveAmount to calculateAmount method of ExchangeProvider; fixed XMRTOExchangeProvider

This commit is contained in:
OleksandrSobol 2020-09-25 22:55:41 +03:00
parent dcbaf01118
commit bc85058ad8
8 changed files with 50 additions and 20 deletions

View file

@ -146,7 +146,8 @@ class ChangeNowExchangeProvider extends ExchangeProvider {
@override
Future<double> calculateAmount(
{CryptoCurrency from, CryptoCurrency to, double amount}) async {
{CryptoCurrency from, CryptoCurrency to, double amount,
bool isReceiveAmount}) async {
final url = apiUri +
_exchangeAmountUriSufix +
amount.toString() +

View file

@ -20,5 +20,5 @@ abstract class ExchangeProvider {
Future<Trade> createTrade({TradeRequest request});
Future<Trade> findTradeById({@required String id});
Future<double> calculateAmount(
{CryptoCurrency from, CryptoCurrency to, double amount});
{CryptoCurrency from, CryptoCurrency to, double amount, bool isReceiveAmount});
}

View file

@ -186,7 +186,8 @@ class MorphTokenExchangeProvider extends ExchangeProvider {
@override
Future<double> calculateAmount(
{CryptoCurrency from, CryptoCurrency to, double amount}) async {
{CryptoCurrency from, CryptoCurrency to, double amount,
bool isReceiveAmount}) async {
final url = apiUri + _ratesURISuffix;
final response = await get(url);
final responseJSON = json.decode(response.body) as Map<String, dynamic>;

View file

@ -21,8 +21,8 @@ class XMRTOExchangeProvider extends ExchangeProvider {
]);
static const userAgent = 'CakeWallet/XMR iOS';
static const originalApiUri = 'https://xmr.to/api/v2/xmr2btc';
static const proxyApiUri = 'https://xmrproxy.net/api/v2/xmr2btc';
static const originalApiUri = 'https://xmr.to/api/v3/xmr2btc';
static const proxyApiUri = 'https://xmrproxy.net/api/v3/xmr2btc';
static const _orderParameterUriSufix = '/order_parameter_query';
static const _orderStatusUriSufix = '/order_status_query/';
static const _orderCreateUriSufix = '/order_create/';
@ -61,9 +61,9 @@ class XMRTOExchangeProvider extends ExchangeProvider {
}
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
double min = responseJSON['lower_limit'] as double;
double max = responseJSON['upper_limit'] as double;
final price = responseJSON['price'] as double;
double min = double.parse(responseJSON['lower_limit'] as String);
double max = double.parse(responseJSON['upper_limit'] as String);
final price = double.parse(responseJSON['price'] as String);
if (price > 0) {
try {
@ -88,7 +88,12 @@ class XMRTOExchangeProvider extends ExchangeProvider {
final _request = request as XMRTOTradeRequest;
final url = await getApiUri() + _orderCreateUriSufix;
final body = {
'xmr_amount': _request.amount,
'amount': _request.isBTCRequest
? _request.receiveAmount
: _request.amount,
'amount_currency': _request.isBTCRequest
? _request.to.toString()
: _request.from.toString(),
'btc_dest_address': _request.address
};
final response = await post(url,
@ -165,7 +170,8 @@ class XMRTOExchangeProvider extends ExchangeProvider {
@override
Future<double> calculateAmount(
{CryptoCurrency from, CryptoCurrency to, double amount}) async {
{CryptoCurrency from, CryptoCurrency to, double amount,
bool isReceiveAmount}) async {
if (from != CryptoCurrency.xmr && to != CryptoCurrency.btc) {
return 0;
}
@ -174,7 +180,9 @@ class XMRTOExchangeProvider extends ExchangeProvider {
_rate = await _fetchRates();
}
final double result = _rate * amount;
final double result = isReceiveAmount
? _rate == 0 ? 0 : amount / _rate
: _rate * amount;
return double.parse(result.toStringAsFixed(12));
}
@ -185,7 +193,7 @@ class XMRTOExchangeProvider extends ExchangeProvider {
final response =
await get(url, headers: {'Content-Type': 'application/json'});
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
final price = responseJSON['price'] as double;
final price = double.parse(responseJSON['price'] as String);
return price;
} catch (e) {

View file

@ -7,12 +7,16 @@ class XMRTOTradeRequest extends TradeRequest {
{@required this.from,
@required this.to,
@required this.amount,
@required this.receiveAmount,
@required this.address,
@required this.refundAddress});
@required this.refundAddress,
@required this.isBTCRequest});
final CryptoCurrency from;
final CryptoCurrency to;
final String amount;
final String receiveAmount;
final String address;
final String refundAddress;
final bool isBTCRequest;
}

View file

@ -114,9 +114,9 @@ class DashboardPage extends BasePage {
alignment: Alignment.centerLeft,
),
ActionButton(
image: sendImage,
title: S.of(context).send,
route: Routes.send,
image: exchangeImage,
title: S.of(context).exchange,
route: Routes.exchange,
alignment: Alignment.centerLeft,
),
],

View file

@ -1,4 +1,5 @@
import 'dart:ui';
import 'package:cake_wallet/exchange/exchange_provider.dart';
import 'package:cake_wallet/exchange/exchange_template.dart';
import 'package:cake_wallet/src/screens/base_page.dart';
import 'package:cake_wallet/src/widgets/alert_with_two_actions.dart';
@ -181,7 +182,7 @@ class ExchangePage extends BasePage {
exchangeViewModel.wallet.currency
? exchangeViewModel.wallet.address
: exchangeViewModel.receiveAddress,
initialIsAmountEditable: false,
initialIsAmountEditable: exchangeViewModel.provider is XMRTOExchangeProvider ? true : false,
initialIsAddressEditable:
exchangeViewModel.isReceiveAddressEnabled,
isAmountEstimated: true,
@ -487,6 +488,12 @@ class ExchangePage extends BasePage {
receiveKey.currentState.isAddressEditable(isEditable: isEnabled);
});
reaction((_) => exchangeViewModel.provider, (ExchangeProvider provider) {
provider is XMRTOExchangeProvider
? receiveKey.currentState.isAmountEditable(isEditable: true)
: receiveKey.currentState.isAmountEditable(isEditable: false);
});
// FIXME: FIXME
// reaction((_) => exchangeViewModel.tradeState, (ExchangeTradeState state) {
@ -540,6 +547,7 @@ class ExchangePage extends BasePage {
if (depositAmountController.text != exchangeViewModel.depositAmount) {
exchangeViewModel.changeDepositAmount(
amount: depositAmountController.text);
exchangeViewModel.isReceiveAmountEntered = false;
}
});
@ -550,6 +558,7 @@ class ExchangePage extends BasePage {
if (receiveAmountController.text != exchangeViewModel.receiveAmount) {
exchangeViewModel.changeReceiveAmount(
amount: receiveAmountController.text);
exchangeViewModel.isReceiveAmountEntered = true;
}
});

View file

@ -48,6 +48,7 @@ abstract class ExchangeViewModelBase with Store {
tradeState = ExchangeTradeStateInitial();
_cryptoNumberFormat = NumberFormat()..maximumFractionDigits = 12;
provider = providersForCurrentPair().first;
isReceiveAmountEntered = false;
loadLimits();
}
@ -92,6 +93,8 @@ abstract class ExchangeViewModelBase with Store {
@observable
bool isReceiveAddressEnabled;
bool isReceiveAmountEntered;
Limits limits;
NumberFormat _cryptoNumberFormat;
@ -138,7 +141,8 @@ abstract class ExchangeViewModelBase with Store {
provider
.calculateAmount(
from: depositCurrency, to: receiveCurrency, amount: _amount)
from: depositCurrency, to: receiveCurrency, amount: _amount,
isReceiveAmount: true)
.then((amount) => _cryptoNumberFormat
.format(amount)
.toString()
@ -159,7 +163,8 @@ abstract class ExchangeViewModelBase with Store {
final _amount = double.parse(amount);
provider
.calculateAmount(
from: depositCurrency, to: receiveCurrency, amount: _amount)
from: depositCurrency, to: receiveCurrency, amount: _amount,
isReceiveAmount: false)
.then((amount) => _cryptoNumberFormat
.format(amount)
.toString()
@ -191,8 +196,10 @@ abstract class ExchangeViewModelBase with Store {
from: depositCurrency,
to: receiveCurrency,
amount: depositAmount,
receiveAmount: receiveAmount,
address: receiveAddress,
refundAddress: depositAddress);
refundAddress: depositAddress,
isBTCRequest: isReceiveAmountEntered);
amount = depositAmount;
currency = depositCurrency;
}