- Add whether this provide supports fixed rate or not

- Fix Change now fixed rate exchange due to wrong rate id
This commit is contained in:
OmarHatem 2022-12-06 19:23:46 +02:00
parent e65bd87f06
commit f14c01303f
7 changed files with 31 additions and 13 deletions

View file

@ -11,7 +11,6 @@ import 'package:cake_wallet/exchange/trade_request.dart';
import 'package:cake_wallet/exchange/trade_state.dart';
import 'package:cake_wallet/exchange/changenow/changenow_request.dart';
import 'package:cake_wallet/exchange/exchange_provider_description.dart';
import 'package:cake_wallet/exchange/trade_not_created_exeption.dart';
class ChangeNowExchangeProvider extends ExchangeProvider {
ChangeNowExchangeProvider()
@ -21,8 +20,7 @@ class ChangeNowExchangeProvider extends ExchangeProvider {
.where((i) => i != CryptoCurrency.xhv)
.map((i) => CryptoCurrency.all
.where((i) => i != CryptoCurrency.xhv)
.map((k) => ExchangePair(from: i, to: k, reverse: true))
.where((c) => c != null))
.map((k) => ExchangePair(from: i, to: k, reverse: true)))
.expand((i) => i)
.toList());
@ -43,6 +41,9 @@ class ChangeNowExchangeProvider extends ExchangeProvider {
@override
bool get isEnabled => true;
@override
bool get supportsFixedRate => true;
@override
ExchangeProviderDescription get description =>
ExchangeProviderDescription.changeNow;
@ -109,6 +110,15 @@ class ChangeNowExchangeProvider extends ExchangeProvider {
};
if (isFixedRateMode) {
// 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
await calculateAmount(
from: _request.to,
to: _request.from,
amount: double.tryParse(_request.toAmount) ?? 0,
isFixedRateMode: true,
isReceiveAmount: true,
);
body['rateId'] = _lastUsedRateId;
}
@ -180,9 +190,7 @@ class ChangeNowExchangeProvider extends ExchangeProvider {
final extraId = responseJSON['payinExtraId'] as String;
final outputTransaction = responseJSON['payoutHash'] as String;
final expiredAtRaw = responseJSON['validUntil'] as String;
final expiredAt = expiredAtRaw != null
? DateTime.parse(expiredAtRaw).toLocal()
: null;
final expiredAt = DateTime.tryParse(expiredAtRaw)?.toLocal();
return Trade(
id: id,

View file

@ -1,4 +1,3 @@
import 'package:flutter/foundation.dart';
import 'package:cw_core/crypto_currency.dart';
import 'package:cake_wallet/exchange/trade_request.dart';
import 'package:cake_wallet/exchange/exchange_pair.dart';
@ -14,6 +13,7 @@ abstract class ExchangeProvider {
ExchangeProviderDescription get description;
bool get isAvailable;
bool get isEnabled;
bool get supportsFixedRate;
@override
String toString() => title;

View file

@ -66,6 +66,9 @@ class MorphTokenExchangeProvider extends ExchangeProvider {
@override
bool get isEnabled => true;
@override
bool get supportsFixedRate => false;
@override
ExchangeProviderDescription get description =>
ExchangeProviderDescription.morphToken;

View file

@ -12,7 +12,6 @@ import 'package:cw_core/crypto_currency.dart';
import 'package:cake_wallet/exchange/trade_request.dart';
import 'package:cake_wallet/exchange/trade.dart';
import 'package:cake_wallet/exchange/limits.dart';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart';
class SideShiftExchangeProvider extends ExchangeProvider {
@ -48,8 +47,7 @@ class SideShiftExchangeProvider extends ExchangeProvider {
return supportedCurrencies
.map((i) => supportedCurrencies
.map((k) => ExchangePair(from: i, to: k, reverse: true))
.where((c) => c != null))
.map((k) => ExchangePair(from: i, to: k, reverse: true)))
.expand((i) => i)
.toList();
}
@ -257,8 +255,7 @@ class SideShiftExchangeProvider extends ExchangeProvider {
state = TradeState.deserialize(raw: status ?? 'created');
final expiredAtRaw = responseJSON['expiresAtISO'] as String;
final expiredAt =
expiredAtRaw != null ? DateTime.parse(expiredAtRaw).toLocal() : null;
final expiredAt = DateTime.tryParse(expiredAtRaw)?.toLocal();
return Trade(
id: id,
@ -278,6 +275,9 @@ class SideShiftExchangeProvider extends ExchangeProvider {
@override
bool get isEnabled => true;
@override
bool get supportsFixedRate => true;
@override
String get title => 'SideShift';

View file

@ -209,6 +209,9 @@ class SimpleSwapExchangeProvider extends ExchangeProvider {
@override
bool get isEnabled => true;
@override
bool get supportsFixedRate => false;
@override
String get title => 'SimpleSwap';

View file

@ -48,6 +48,9 @@ class XMRTOExchangeProvider extends ExchangeProvider {
@override
bool get isEnabled => true;
@override
bool get supportsFixedRate => false;
@override
ExchangeProviderDescription get description =>
ExchangeProviderDescription.xmrto;

View file

@ -306,10 +306,11 @@ abstract class ExchangeViewModelBase with Store {
}
Future<void> _calculateBestRate() async {
final amount = double.tryParse(depositAmount) ?? 1;
final amount = double.tryParse(isFixedRateMode ? receiveAmount : depositAmount) ?? 1;
final result = await Future.wait<double>(
_tradeAvailableProviders
.where((element) => element.supportsFixedRate)
.map((element) => element.calculateAmount(
from: depositCurrency,
to: receiveCurrency,