CAKE-278 | fixed calculating estimated amount when receive amount is entered; fixed applyTemplate() method in the exchange_page.dart and reset() method in the exchange_view_model.dart

This commit is contained in:
OleksandrSobol 2021-02-19 10:37:30 +02:00
parent 6e102c4969
commit fb6e7de166
3 changed files with 36 additions and 20 deletions
lib
exchange/changenow
src/screens/exchange
view_model/exchange

View file

@ -173,8 +173,32 @@ class ChangeNowExchangeProvider extends ExchangeProvider {
double amount,
bool isFixedRateMode,
bool isReceiveAmount}) async {
final url = isFixedRateMode
? apiUri +
if (isReceiveAmount && isFixedRateMode) {
final url = apiUri + _marketInfoUriSufix + _fixedRateUriSufix + apiKey;
final response = await get(url);
final responseJSON = json.decode(response.body) as List<dynamic>;
var rate = 0.0;
var fee = 0.0;
for (var elem in responseJSON) {
final elemFrom = elem["from"] as String;
final elemTo = elem["to"] as String;
if ((elemFrom == to.toString().toLowerCase()) &&
(elemTo == from.toString().toLowerCase())) {
rate = elem["rate"] as double;
fee = elem["minerFee"] as double;
break;
}
}
final estimatedAmount = (amount == 0.0)||(rate == 0.0) ? 0.0
: (amount + fee)/rate;
return estimatedAmount;
} else {
final url = isFixedRateMode
? apiUri +
_exchangeAmountUriSufix +
_fixedRateUriSufix +
amount.toString() +
@ -183,17 +207,18 @@ class ChangeNowExchangeProvider extends ExchangeProvider {
'_' +
to.toString() +
'?api_key=' + apiKey
: apiUri +
: apiUri +
_exchangeAmountUriSufix +
amount.toString() +
'/' +
from.toString() +
'_' +
to.toString();
final response = await get(url);
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
final estimatedAmount = responseJSON['estimatedAmount'] as double;
final response = await get(url);
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
final estimatedAmount = responseJSON['estimatedAmount'] as double;
return estimatedAmount;
return estimatedAmount;
}
}
}

View file

@ -480,17 +480,9 @@ class ExchangePage extends BasePage {
currency: CryptoCurrency.fromString(template.receiveCurrency));
switch (template.provider) {
case 'XMR.TO':
exchangeViewModel.changeProvider(
provider: exchangeViewModel.providerList[0]);
break;
case 'ChangeNOW':
exchangeViewModel.changeProvider(
provider: exchangeViewModel.providerList[1]);
break;
case 'MorphToken':
exchangeViewModel.changeProvider(
provider: exchangeViewModel.providerList[2]);
provider: exchangeViewModel.providerList[0]);
break;
}
@ -498,6 +490,7 @@ class ExchangePage extends BasePage {
exchangeViewModel.depositAddress = template.depositAddress;
exchangeViewModel.receiveAddress = template.receiveAddress;
exchangeViewModel.isReceiveAmountEntered = false;
exchangeViewModel.isFixedRateMode = false;
}
void _setReactions(
@ -670,12 +663,11 @@ class ExchangePage extends BasePage {
builder: (BuildContext context) {
return AlertWithTwoActions(
alertTitle: S.of(context).exchange,
alertContent: 'You will be able to enter receive amount when fixed rate is checked. Do you want to check fixed rate?', //FIXME
alertContent: 'You will be able to enter receive amount when fixed rate mode is checked. Do you want to switch to fixed rate mode?', //FIXME
leftButtonText: S.of(context).cancel,
rightButtonText: S.of(context).ok,
actionLeftButton: () {
FocusScope.of(context).unfocus();
receiveAmountController.text = '';
Navigator.of(context).pop();
},
actionRightButton: () {

View file

@ -301,11 +301,10 @@ abstract class ExchangeViewModelBase with Store {
@action
void reset() {
_initialPairBasedOnWallet();
isReceiveAmountEntered = false;
depositAmount = '';
receiveAmount = '';
depositCurrency = CryptoCurrency.xmr;
receiveCurrency = CryptoCurrency.btc;
depositAddress = depositCurrency == wallet.currency ? wallet.address : '';
receiveAddress = receiveCurrency == wallet.currency ? wallet.address : '';
isDepositAddressEnabled = !(depositCurrency == wallet.currency);