Merge pull request #109 from cake-tech/CAKE-43-xmr_to-amount-entering

Cake 43 xmr to amount entering
This commit is contained in:
M 2020-10-01 18:38:34 +03:00
commit f5707e6610
7 changed files with 49 additions and 17 deletions

View file

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

View file

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

View file

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

View file

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

View file

@ -1,4 +1,5 @@
import 'dart:ui'; import 'dart:ui';
import 'package:cake_wallet/exchange/exchange_provider.dart';
import 'package:cake_wallet/core/execution_state.dart'; import 'package:cake_wallet/core/execution_state.dart';
import 'package:cake_wallet/exchange/exchange_template.dart'; import 'package:cake_wallet/exchange/exchange_template.dart';
import 'package:cake_wallet/src/screens/base_page.dart'; import 'package:cake_wallet/src/screens/base_page.dart';
@ -207,7 +208,9 @@ class ExchangePage extends BasePage {
exchangeViewModel.wallet.currency exchangeViewModel.wallet.currency
? exchangeViewModel.wallet.address ? exchangeViewModel.wallet.address
: exchangeViewModel.receiveAddress, : exchangeViewModel.receiveAddress,
initialIsAmountEditable: false, initialIsAmountEditable:
exchangeViewModel.provider is
XMRTOExchangeProvider ? true : false,
initialIsAddressEditable: initialIsAddressEditable:
exchangeViewModel exchangeViewModel
.isReceiveAddressEnabled, .isReceiveAddressEnabled,
@ -520,6 +523,12 @@ class ExchangePage extends BasePage {
receiveKey.currentState.isAddressEditable(isEditable: isEnabled); 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 // FIXME: FIXME
// reaction((_) => exchangeViewModel.tradeState, (ExchangeTradeState state) { // reaction((_) => exchangeViewModel.tradeState, (ExchangeTradeState state) {
@ -573,6 +582,7 @@ class ExchangePage extends BasePage {
if (depositAmountController.text != exchangeViewModel.depositAmount) { if (depositAmountController.text != exchangeViewModel.depositAmount) {
exchangeViewModel.changeDepositAmount( exchangeViewModel.changeDepositAmount(
amount: depositAmountController.text); amount: depositAmountController.text);
exchangeViewModel.isReceiveAmountEntered = false;
} }
}); });
@ -583,6 +593,7 @@ class ExchangePage extends BasePage {
if (receiveAmountController.text != exchangeViewModel.receiveAmount) { if (receiveAmountController.text != exchangeViewModel.receiveAmount) {
exchangeViewModel.changeReceiveAmount( exchangeViewModel.changeReceiveAmount(
amount: receiveAmountController.text); amount: receiveAmountController.text);
exchangeViewModel.isReceiveAmountEntered = true;
} }
}); });

View file

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