CAKE-278 | applied fixed-rate flow to changenow_exchange_provider.dart, exchange_view_model.dart and exchange_page.dart; added isFixedRateMode to fetchLimits(), createTrade() and calculateAmount() methods in the exchange_provider.dart

This commit is contained in:
OleksandrSobol 2021-02-18 21:08:52 +02:00
parent 317ef8f3e4
commit 6e102c4969
6 changed files with 79 additions and 26 deletions

View file

@ -29,6 +29,8 @@ class ChangeNowExchangeProvider extends ExchangeProvider {
static const _exchangeAmountUriSufix = '/exchange-amount/'; static const _exchangeAmountUriSufix = '/exchange-amount/';
static const _transactionsUriSufix = '/transactions/'; static const _transactionsUriSufix = '/transactions/';
static const _minAmountUriSufix = '/min-amount/'; static const _minAmountUriSufix = '/min-amount/';
static const _marketInfoUriSufix = '/market-info/';
static const _fixedRateUriSufix = 'fixed-rate/';
@override @override
String get title => 'ChangeNOW'; String get title => 'ChangeNOW';
@ -44,19 +46,43 @@ class ChangeNowExchangeProvider extends ExchangeProvider {
Future<bool> checkIsAvailable() async => true; Future<bool> checkIsAvailable() async => true;
@override @override
Future<Limits> fetchLimits({CryptoCurrency from, CryptoCurrency to}) async { Future<Limits> fetchLimits({CryptoCurrency from, CryptoCurrency to,
bool isFixedRateMode}) async {
final symbol = from.toString() + '_' + to.toString(); final symbol = from.toString() + '_' + to.toString();
final url = apiUri + _minAmountUriSufix + symbol; final url = isFixedRateMode
? apiUri + _marketInfoUriSufix + _fixedRateUriSufix + apiKey
: apiUri + _minAmountUriSufix + symbol;
final response = await get(url); final response = await get(url);
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
final min = responseJSON['minAmount'] as double;
return Limits(min: min, max: null); if (isFixedRateMode) {
final responseJSON = json.decode(response.body) as List<dynamic>;
for (var elem in responseJSON) {
final elemFrom = elem["from"] as String;
final elemTo = elem["to"] as String;
if ((elemFrom == from.toString().toLowerCase()) &&
(elemTo == to.toString().toLowerCase())) {
final min = elem["min"] as double;
final max = elem["max"] as double;
return Limits(min: min, max: max);
}
}
return Limits(min: 0, max: 0);
} else {
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
final min = responseJSON['minAmount'] as double;
return Limits(min: min, max: null);
}
} }
@override @override
Future<Trade> createTrade({TradeRequest request}) async { Future<Trade> createTrade({TradeRequest request, bool isFixedRateMode}) async {
const url = apiUri + _transactionsUriSufix + apiKey; final url = isFixedRateMode
? apiUri + _transactionsUriSufix + _fixedRateUriSufix + apiKey
: apiUri + _transactionsUriSufix + apiKey;
final _request = request as ChangeNowRequest; final _request = request as ChangeNowRequest;
final body = { final body = {
'from': _request.from.toString(), 'from': _request.from.toString(),
@ -145,14 +171,25 @@ class ChangeNowExchangeProvider extends ExchangeProvider {
{CryptoCurrency from, {CryptoCurrency from,
CryptoCurrency to, CryptoCurrency to,
double amount, double amount,
bool isFixedRateMode,
bool isReceiveAmount}) async { bool isReceiveAmount}) async {
final url = apiUri + final url = isFixedRateMode
_exchangeAmountUriSufix + ? apiUri +
amount.toString() + _exchangeAmountUriSufix +
'/' + _fixedRateUriSufix +
from.toString() + amount.toString() +
'_' + '/' +
to.toString(); from.toString() +
'_' +
to.toString() +
'?api_key=' + apiKey
: apiUri +
_exchangeAmountUriSufix +
amount.toString() +
'/' +
from.toString() +
'_' +
to.toString();
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>;
final estimatedAmount = responseJSON['estimatedAmount'] as double; final estimatedAmount = responseJSON['estimatedAmount'] as double;

View file

@ -17,10 +17,11 @@ abstract class ExchangeProvider {
@override @override
String toString() => title; String toString() => title;
Future<Limits> fetchLimits({CryptoCurrency from, CryptoCurrency to}); Future<Limits> fetchLimits(
Future<Trade> createTrade({TradeRequest request}); {CryptoCurrency from, CryptoCurrency to, bool isFixedRateMode});
Future<Trade> createTrade({TradeRequest request, bool isFixedRateMode});
Future<Trade> findTradeById({@required String id}); Future<Trade> findTradeById({@required String id});
Future<double> calculateAmount( Future<double> calculateAmount({CryptoCurrency from, CryptoCurrency to,
{CryptoCurrency from, CryptoCurrency to, double amount, bool isReceiveAmount}); double amount, bool isFixedRateMode, bool isReceiveAmount});
Future<bool> checkIsAvailable(); Future<bool> checkIsAvailable();
} }

View file

@ -71,7 +71,7 @@ class MorphTokenExchangeProvider extends ExchangeProvider {
Future<bool> checkIsAvailable() async => true; Future<bool> checkIsAvailable() async => true;
@override @override
Future<Limits> fetchLimits({CryptoCurrency from, CryptoCurrency to}) async { Future<Limits> fetchLimits({CryptoCurrency from, CryptoCurrency to, bool isFixedRateMode}) async {
final url = apiUri + _limitsURISuffix; final url = apiUri + _limitsURISuffix;
final headers = {'Content-type': 'application/json'}; final headers = {'Content-type': 'application/json'};
final body = json.encode({ final body = json.encode({
@ -100,7 +100,7 @@ class MorphTokenExchangeProvider extends ExchangeProvider {
} }
@override @override
Future<Trade> createTrade({TradeRequest request}) async { Future<Trade> createTrade({TradeRequest request, bool isFixedRateMode}) async {
const url = apiUri + _morphURISuffix; const url = apiUri + _morphURISuffix;
final _request = request as MorphTokenRequest; final _request = request as MorphTokenRequest;
final body = { final body = {
@ -191,7 +191,7 @@ class MorphTokenExchangeProvider extends ExchangeProvider {
@override @override
Future<double> calculateAmount( Future<double> calculateAmount(
{CryptoCurrency from, CryptoCurrency to, double amount, {CryptoCurrency from, CryptoCurrency to, double amount, bool isFixedRateMode,
bool isReceiveAmount}) async { bool isReceiveAmount}) async {
final url = apiUri + _ratesURISuffix; final url = apiUri + _ratesURISuffix;
final response = await get(url); final response = await get(url);

View file

@ -58,7 +58,7 @@ class XMRTOExchangeProvider extends ExchangeProvider {
} }
@override @override
Future<Limits> fetchLimits({CryptoCurrency from, CryptoCurrency to}) async { Future<Limits> fetchLimits({CryptoCurrency from, CryptoCurrency to, bool isFixedRateMode}) async {
final url = originalApiUri + _orderParameterUriSuffix; final url = originalApiUri + _orderParameterUriSuffix;
final response = await get(url); final response = await get(url);
final correction = 0.001; final correction = 0.001;
@ -91,7 +91,7 @@ class XMRTOExchangeProvider extends ExchangeProvider {
} }
@override @override
Future<Trade> createTrade({TradeRequest request}) async { Future<Trade> createTrade({TradeRequest request, bool isFixedRateMode}) async {
final _request = request as XMRTOTradeRequest; final _request = request as XMRTOTradeRequest;
final url = originalApiUri + _orderCreateUriSuffix; final url = originalApiUri + _orderCreateUriSuffix;
final _amount = final _amount =
@ -188,6 +188,7 @@ class XMRTOExchangeProvider extends ExchangeProvider {
{CryptoCurrency from, {CryptoCurrency from,
CryptoCurrency to, CryptoCurrency to,
double amount, double amount,
bool isFixedRateMode,
bool isReceiveAmount}) async { bool isReceiveAmount}) async {
if (from != CryptoCurrency.xmr && to != CryptoCurrency.btc) { if (from != CryptoCurrency.xmr && to != CryptoCurrency.btc) {
return 0; return 0;

View file

@ -693,7 +693,14 @@ class ExchangePage extends BasePage {
exchangeViewModel.isReceiveAmountEntered) && !isFixedRateMode) { exchangeViewModel.isReceiveAmountEntered) && !isFixedRateMode) {
FocusScope.of(context).unfocus(); FocusScope.of(context).unfocus();
receiveAmountController.text = ''; receiveAmountController.text = '';
} else {
exchangeViewModel.changeDepositAmount(
amount: depositAmountController.text);
} }
checkBoxKey.currentState
.changeValue(exchangeViewModel.isFixedRateMode);
exchangeViewModel.loadLimits();
}); });
_isReactionsSet = true; _isReactionsSet = true;

View file

@ -137,12 +137,14 @@ abstract class ExchangeViewModelBase with Store {
depositAmount = ''; depositAmount = '';
receiveAmount = ''; receiveAmount = '';
isReceiveAmountEditable = provider is ChangeNowExchangeProvider; isReceiveAmountEditable = provider is ChangeNowExchangeProvider;
isFixedRateMode = false;
loadLimits(); loadLimits();
} }
@action @action
void changeDepositCurrency({CryptoCurrency currency}) { void changeDepositCurrency({CryptoCurrency currency}) {
depositCurrency = currency; depositCurrency = currency;
isFixedRateMode = false;
_onPairChange(); _onPairChange();
isDepositAddressEnabled = !(depositCurrency == wallet.currency); isDepositAddressEnabled = !(depositCurrency == wallet.currency);
isReceiveAddressEnabled = !(receiveCurrency == wallet.currency); isReceiveAddressEnabled = !(receiveCurrency == wallet.currency);
@ -151,6 +153,7 @@ abstract class ExchangeViewModelBase with Store {
@action @action
void changeReceiveCurrency({CryptoCurrency currency}) { void changeReceiveCurrency({CryptoCurrency currency}) {
receiveCurrency = currency; receiveCurrency = currency;
isFixedRateMode = false;
_onPairChange(); _onPairChange();
isDepositAddressEnabled = !(depositCurrency == wallet.currency); isDepositAddressEnabled = !(depositCurrency == wallet.currency);
isReceiveAddressEnabled = !(receiveCurrency == wallet.currency); isReceiveAddressEnabled = !(receiveCurrency == wallet.currency);
@ -173,6 +176,7 @@ abstract class ExchangeViewModelBase with Store {
from: receiveCurrency, from: receiveCurrency,
to: depositCurrency, to: depositCurrency,
amount: _amount, amount: _amount,
isFixedRateMode: isFixedRateMode,
isReceiveAmount: true) isReceiveAmount: true)
.then((amount) => _cryptoNumberFormat .then((amount) => _cryptoNumberFormat
.format(amount) .format(amount)
@ -197,6 +201,7 @@ abstract class ExchangeViewModelBase with Store {
from: depositCurrency, from: depositCurrency,
to: receiveCurrency, to: receiveCurrency,
amount: _amount, amount: _amount,
isFixedRateMode: isFixedRateMode,
isReceiveAmount: false) isReceiveAmount: false)
.then((amount) => _cryptoNumberFormat .then((amount) => _cryptoNumberFormat
.format(amount) .format(amount)
@ -210,8 +215,8 @@ abstract class ExchangeViewModelBase with Store {
limitsState = LimitsIsLoading(); limitsState = LimitsIsLoading();
try { try {
limits = await provider.fetchLimits( limits = await provider.fetchLimits(from: depositCurrency,
from: depositCurrency, to: receiveCurrency); to: receiveCurrency, isFixedRateMode: isFixedRateMode);
limitsState = LimitsLoadedSuccessfully(limits: limits); limitsState = LimitsLoadedSuccessfully(limits: limits);
} catch (e) { } catch (e) {
limitsState = LimitsLoadedFailure(error: e.toString()); limitsState = LimitsLoadedFailure(error: e.toString());
@ -275,7 +280,8 @@ abstract class ExchangeViewModelBase with Store {
} else { } else {
try { try {
tradeState = TradeIsCreating(); tradeState = TradeIsCreating();
final trade = await provider.createTrade(request: request); final trade = await provider.createTrade(request: request,
isFixedRateMode: isFixedRateMode);
trade.walletId = wallet.id; trade.walletId = wallet.id;
tradesStore.setTrade(trade); tradesStore.setTrade(trade);
await trades.add(trade); await trades.add(trade);
@ -304,6 +310,7 @@ abstract class ExchangeViewModelBase with Store {
receiveAddress = receiveCurrency == wallet.currency ? wallet.address : ''; receiveAddress = receiveCurrency == wallet.currency ? wallet.address : '';
isDepositAddressEnabled = !(depositCurrency == wallet.currency); isDepositAddressEnabled = !(depositCurrency == wallet.currency);
isReceiveAddressEnabled = !(receiveCurrency == wallet.currency); isReceiveAddressEnabled = !(receiveCurrency == wallet.currency);
isFixedRateMode = false;
_onPairChange(); _onPairChange();
} }