Delegate returning rate to each provider to handle its logic internally

This commit is contained in:
OmarHatem 2022-12-07 21:09:15 +02:00
parent 8ff8cf38b8
commit 0da48f66fe
4 changed files with 11 additions and 13 deletions

View file

@ -113,8 +113,8 @@ class ChangeNowExchangeProvider extends ExchangeProvider {
// since we schedule to calculate the rate every 5 seconds we need to ensure that // since we schedule to calculate the rate every 5 seconds we need to ensure that
// we have the latest rate id with the given inputs before creating the trade // we have the latest rate id with the given inputs before creating the trade
await calculateAmount( await calculateAmount(
from: _request.to, from: _request.from,
to: _request.from, to: _request.to,
amount: double.tryParse(_request.toAmount) ?? 0, amount: double.tryParse(_request.toAmount) ?? 0,
isFixedRateMode: true, isFixedRateMode: true,
isReceiveAmount: true, isReceiveAmount: true,
@ -222,10 +222,10 @@ class ChangeNowExchangeProvider extends ExchangeProvider {
final type = isReverse ? 'reverse' : 'direct'; final type = isReverse ? 'reverse' : 'direct';
final flow = getFlow(isFixedRateMode); final flow = getFlow(isFixedRateMode);
final params = <String, String>{ final params = <String, String>{
'fromCurrency': isReverse ? normalizeCryptoCurrency(to) : normalizeCryptoCurrency(from), 'fromCurrency': normalizeCryptoCurrency(from),
'toCurrency': isReverse ? normalizeCryptoCurrency(from) : normalizeCryptoCurrency(to), 'toCurrency': normalizeCryptoCurrency(to),
'fromNetwork': isReverse ? networkFor(to) : networkFor(from), 'fromNetwork': networkFor(from),
'toNetwork': isReverse ? networkFor(from) : networkFor(to), 'toNetwork': networkFor(to),
'type': type, 'type': type,
'flow': flow}; 'flow': flow};
@ -246,7 +246,7 @@ class ChangeNowExchangeProvider extends ExchangeProvider {
_lastUsedRateId = rateId; _lastUsedRateId = rateId;
} }
return isReverse ? fromAmount : toAmount; return isReverse ? (amount / fromAmount) : (toAmount / amount);
} catch(e) { } catch(e) {
print(e.toString()); print(e.toString());
return 0.0; return 0.0;

View file

@ -79,9 +79,7 @@ class SideShiftExchangeProvider extends ExchangeProvider {
if (amount > max) return 0.00; if (amount > max) return 0.00;
final estimatedAmount = rate * amount; return rate;
return estimatedAmount;
} catch (_) { } catch (_) {
return 0.00; return 0.00;
} }

View file

@ -60,7 +60,7 @@ class SimpleSwapExchangeProvider extends ExchangeProvider {
if (response.body == "null") return 0.00; if (response.body == "null") return 0.00;
final data = json.decode(response.body) as String; final data = json.decode(response.body) as String;
return double.parse(data); return double.parse(data) / amount;
} catch (_) { } catch (_) {
return 0.00; return 0.00;
} }

View file

@ -316,7 +316,7 @@ abstract class ExchangeViewModelBase with Store {
to: receiveCurrency, to: receiveCurrency,
amount: amount, amount: amount,
isFixedRateMode: isFixedRateMode, isFixedRateMode: isFixedRateMode,
isReceiveAmount: false)) isReceiveAmount: isFixedRateMode))
); );
_sortedAvailableProviders.clear(); _sortedAvailableProviders.clear();
@ -324,7 +324,7 @@ abstract class ExchangeViewModelBase with Store {
for (int i=0;i<result.length;i++) { for (int i=0;i<result.length;i++) {
if (result[i] != 0) { if (result[i] != 0) {
/// add this provider as its valid for this trade /// add this provider as its valid for this trade
_sortedAvailableProviders[result[i] / amount] = _tradeAvailableProviders[i]; _sortedAvailableProviders[result[i]] = _tradeAvailableProviders[i];
} }
} }
if (_sortedAvailableProviders.isNotEmpty) { if (_sortedAvailableProviders.isNotEmpty) {