2022-10-04 00:01:04 +00:00
|
|
|
import 'package:decimal/decimal.dart';
|
2023-02-05 20:32:39 +00:00
|
|
|
import 'package:flutter/foundation.dart';
|
2023-02-08 19:06:26 +00:00
|
|
|
import 'package:stackwallet/models/exchange/aggregate_currency.dart';
|
2022-10-04 00:01:04 +00:00
|
|
|
import 'package:stackwallet/models/exchange/response_objects/estimate.dart';
|
2023-02-08 20:06:58 +00:00
|
|
|
import 'package:stackwallet/services/exchange/change_now/change_now_exchange.dart';
|
2022-10-04 00:01:04 +00:00
|
|
|
import 'package:stackwallet/services/exchange/exchange.dart';
|
2023-02-08 20:06:58 +00:00
|
|
|
import 'package:stackwallet/services/exchange/majestic_bank/majestic_bank_exchange.dart';
|
2023-02-08 22:46:30 +00:00
|
|
|
import 'package:stackwallet/utilities/enums/exchange_rate_type_enum.dart';
|
2022-10-04 00:01:04 +00:00
|
|
|
import 'package:stackwallet/utilities/logger.dart';
|
|
|
|
|
|
|
|
class ExchangeFormState extends ChangeNotifier {
|
|
|
|
Exchange? _exchange;
|
2023-02-05 20:32:39 +00:00
|
|
|
Exchange get exchange => _exchange ??= Exchange.defaultExchange;
|
2022-10-04 00:01:04 +00:00
|
|
|
|
2023-02-06 16:27:24 +00:00
|
|
|
ExchangeRateType _exchangeRateType = ExchangeRateType.estimated;
|
|
|
|
ExchangeRateType get exchangeRateType => _exchangeRateType;
|
|
|
|
set exchangeRateType(ExchangeRateType exchangeRateType) {
|
|
|
|
_exchangeRateType = exchangeRateType;
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
2023-02-05 20:32:39 +00:00
|
|
|
Estimate? _estimate;
|
|
|
|
Estimate? get estimate => _estimate;
|
2022-10-04 00:01:04 +00:00
|
|
|
|
2023-02-05 20:32:39 +00:00
|
|
|
bool _reversed = false;
|
|
|
|
bool get reversed => _reversed;
|
|
|
|
set reversed(bool reversed) {
|
|
|
|
_reversed = reversed;
|
|
|
|
//
|
2022-10-04 00:01:04 +00:00
|
|
|
}
|
|
|
|
|
2023-02-05 20:32:39 +00:00
|
|
|
Decimal? _rate;
|
|
|
|
Decimal? get rate => _rate;
|
|
|
|
// set rate(Decimal? rate) {
|
|
|
|
// _rate = rate;
|
|
|
|
// //
|
|
|
|
// }
|
|
|
|
|
|
|
|
Decimal? _sendAmount;
|
|
|
|
Decimal? get sendAmount => _sendAmount;
|
|
|
|
// set sendAmount(Decimal? sendAmount) {
|
|
|
|
// _sendAmount = sendAmount;
|
|
|
|
// //
|
|
|
|
// }
|
|
|
|
|
|
|
|
Decimal? _receiveAmount;
|
|
|
|
Decimal? get receiveAmount => _receiveAmount;
|
|
|
|
set receiveAmount(Decimal? receiveAmount) {
|
|
|
|
_receiveAmount = receiveAmount;
|
|
|
|
//
|
2022-10-04 00:01:04 +00:00
|
|
|
}
|
|
|
|
|
2023-02-08 19:06:26 +00:00
|
|
|
AggregateCurrency? _sendCurrency;
|
|
|
|
AggregateCurrency? get sendCurrency => _sendCurrency;
|
2023-02-05 20:32:39 +00:00
|
|
|
// set sendCurrency(Currency? sendCurrency) {
|
|
|
|
// _sendCurrency = sendCurrency;
|
|
|
|
// //
|
|
|
|
// }
|
|
|
|
|
2023-02-08 19:06:26 +00:00
|
|
|
AggregateCurrency? _receiveCurrency;
|
|
|
|
AggregateCurrency? get receiveCurrency => _receiveCurrency;
|
2023-02-05 20:32:39 +00:00
|
|
|
// set receiveCurrency(Currency? receiveCurrency) {
|
|
|
|
// _receiveCurrency = receiveCurrency;
|
|
|
|
// //
|
|
|
|
// }
|
|
|
|
|
|
|
|
Decimal? _minSendAmount;
|
|
|
|
Decimal? get minSendAmount => _minSendAmount;
|
|
|
|
// set minSendAmount(Decimal? minSendAmount) {
|
|
|
|
// _minSendAmount = minSendAmount;
|
|
|
|
// //
|
|
|
|
// }
|
|
|
|
|
|
|
|
Decimal? _minReceiveAmount;
|
|
|
|
Decimal? get minReceiveAmount => _minReceiveAmount;
|
|
|
|
// set minReceiveAmount(Decimal? minReceiveAmount) {
|
|
|
|
// _minReceiveAmount = minReceiveAmount;
|
|
|
|
// //
|
|
|
|
// }
|
|
|
|
|
|
|
|
Decimal? _maxSendAmount;
|
|
|
|
Decimal? get maxSendAmount => _maxSendAmount;
|
|
|
|
// set maxSendAmount(Decimal? maxSendAmount) {
|
|
|
|
// _maxSendAmount = maxSendAmount;
|
|
|
|
// //
|
|
|
|
// }
|
|
|
|
|
|
|
|
Decimal? _maxReceiveAmount;
|
|
|
|
Decimal? get maxReceiveAmount => _maxReceiveAmount;
|
|
|
|
// set maxReceiveAmount(Decimal? maxReceiveAmount) {
|
|
|
|
// _maxReceiveAmount = maxReceiveAmount;
|
|
|
|
// //
|
|
|
|
// }
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
// computed properties
|
|
|
|
//============================================================================
|
|
|
|
|
|
|
|
String? get fromTicker => _sendCurrency?.ticker;
|
|
|
|
String? get toTicker => _receiveCurrency?.ticker;
|
|
|
|
|
|
|
|
String get fromAmountString => _sendAmount?.toStringAsFixed(8) ?? "";
|
|
|
|
String get toAmountString => _receiveAmount?.toStringAsFixed(8) ?? "";
|
2022-10-04 00:01:04 +00:00
|
|
|
|
2023-02-05 20:32:39 +00:00
|
|
|
bool get canExchange {
|
|
|
|
return sendCurrency != null &&
|
|
|
|
receiveCurrency != null &&
|
|
|
|
sendAmount != null &&
|
|
|
|
sendAmount! >= Decimal.zero &&
|
|
|
|
receiveAmount != null &&
|
|
|
|
rate != null &&
|
|
|
|
rate! >= Decimal.zero &&
|
2023-02-08 19:06:26 +00:00
|
|
|
sendCurrency!.forExchange(exchange.name) != null &&
|
|
|
|
receiveCurrency!.forExchange(exchange.name) != null &&
|
2023-02-05 20:32:39 +00:00
|
|
|
warning.isEmpty;
|
2022-10-04 00:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String get warning {
|
|
|
|
if (reversed) {
|
2023-02-05 20:32:39 +00:00
|
|
|
if (_receiveCurrency != null && _receiveAmount != null) {
|
|
|
|
if (_minReceiveAmount != null &&
|
|
|
|
_receiveAmount! < _minReceiveAmount! &&
|
|
|
|
_receiveAmount! > Decimal.zero) {
|
2023-02-06 18:29:00 +00:00
|
|
|
return "Min receive amount ${_minReceiveAmount!.toString()} ${_receiveCurrency!.ticker.toUpperCase()}";
|
2023-02-05 20:32:39 +00:00
|
|
|
} else if (_maxReceiveAmount != null &&
|
|
|
|
_receiveAmount! > _maxReceiveAmount!) {
|
2023-02-06 18:29:00 +00:00
|
|
|
return "Max receive amount ${_maxReceiveAmount!.toString()} ${_receiveCurrency!.ticker.toUpperCase()}";
|
2022-10-04 00:01:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2023-02-05 20:32:39 +00:00
|
|
|
if (_sendCurrency != null && _sendAmount != null) {
|
|
|
|
if (_minSendAmount != null &&
|
|
|
|
_sendAmount! < _minSendAmount! &&
|
|
|
|
_sendAmount! > Decimal.zero) {
|
2023-02-06 18:29:00 +00:00
|
|
|
return "Min send amount ${_minSendAmount!.toString()} ${_sendCurrency!.ticker.toUpperCase()}";
|
2023-02-05 20:32:39 +00:00
|
|
|
} else if (_maxSendAmount != null && _sendAmount! > _maxSendAmount!) {
|
2023-02-06 18:29:00 +00:00
|
|
|
return "Max send amount ${_maxSendAmount!.toString()} ${_sendCurrency!.ticker.toUpperCase()}";
|
2022-10-04 00:01:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2023-02-05 20:32:39 +00:00
|
|
|
//============================================================================
|
|
|
|
// public state updaters
|
|
|
|
//============================================================================
|
2022-10-04 00:01:04 +00:00
|
|
|
|
2023-02-05 23:50:40 +00:00
|
|
|
Future<void> updateExchange({
|
|
|
|
required Exchange exchange,
|
|
|
|
required bool shouldUpdateData,
|
|
|
|
required bool shouldNotifyListeners,
|
|
|
|
}) async {
|
|
|
|
_exchange = exchange;
|
|
|
|
if (shouldUpdateData) {
|
|
|
|
await _updateRangesAndEstimate(
|
|
|
|
shouldNotifyListeners: false,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shouldNotifyListeners) {
|
2023-02-06 15:44:23 +00:00
|
|
|
_notify();
|
2023-02-05 23:50:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-10 14:04:12 +00:00
|
|
|
void setCurrencies(AggregateCurrency? from, AggregateCurrency? to) {
|
2023-02-05 20:32:39 +00:00
|
|
|
_sendCurrency = from;
|
|
|
|
_receiveCurrency = to;
|
2022-10-04 00:01:04 +00:00
|
|
|
}
|
|
|
|
|
2023-02-05 20:32:39 +00:00
|
|
|
void reset({
|
|
|
|
required bool shouldNotifyListeners,
|
|
|
|
}) {
|
|
|
|
_exchange = null;
|
|
|
|
_reversed = false;
|
|
|
|
_rate = null;
|
|
|
|
_sendAmount = null;
|
|
|
|
_receiveAmount = null;
|
|
|
|
_sendCurrency = null;
|
|
|
|
_receiveCurrency = null;
|
|
|
|
_minSendAmount = null;
|
|
|
|
_minReceiveAmount = null;
|
|
|
|
_maxSendAmount = null;
|
|
|
|
_maxReceiveAmount = null;
|
2022-10-04 00:01:04 +00:00
|
|
|
|
|
|
|
if (shouldNotifyListeners) {
|
2023-02-06 15:44:23 +00:00
|
|
|
_notify();
|
2022-10-04 00:01:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-05 20:32:39 +00:00
|
|
|
Future<void> setSendAmountAndCalculateReceiveAmount(
|
|
|
|
Decimal? newSendAmount,
|
2022-10-04 00:01:04 +00:00
|
|
|
bool shouldNotifyListeners,
|
|
|
|
) async {
|
2023-02-05 20:32:39 +00:00
|
|
|
if (newSendAmount == null) {
|
|
|
|
// todo: check if this breaks things and stuff
|
|
|
|
_receiveAmount = null;
|
|
|
|
_sendAmount = null;
|
|
|
|
} else {
|
|
|
|
if (newSendAmount <= Decimal.zero) {
|
|
|
|
_receiveAmount = Decimal.zero;
|
|
|
|
}
|
2022-10-04 00:01:04 +00:00
|
|
|
|
2023-02-05 20:32:39 +00:00
|
|
|
_sendAmount = newSendAmount;
|
|
|
|
_reversed = false;
|
2022-10-04 00:01:04 +00:00
|
|
|
|
2023-02-05 20:32:39 +00:00
|
|
|
await _updateRangesAndEstimate(
|
|
|
|
shouldNotifyListeners: false,
|
|
|
|
);
|
|
|
|
}
|
2022-10-04 00:01:04 +00:00
|
|
|
|
|
|
|
if (shouldNotifyListeners) {
|
2023-02-06 15:44:23 +00:00
|
|
|
_notify();
|
2022-10-04 00:01:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-06 16:27:24 +00:00
|
|
|
Future<void> setReceivingAmountAndCalculateSendAmount(
|
2023-02-05 20:32:39 +00:00
|
|
|
Decimal? newReceiveAmount,
|
2022-10-04 00:01:04 +00:00
|
|
|
bool shouldNotifyListeners,
|
|
|
|
) async {
|
2023-02-05 20:32:39 +00:00
|
|
|
if (newReceiveAmount == null) {
|
|
|
|
// todo: check if this breaks things and stuff
|
|
|
|
_receiveAmount = null;
|
|
|
|
_sendAmount = null;
|
|
|
|
} else {
|
|
|
|
if (newReceiveAmount <= Decimal.zero) {
|
|
|
|
_sendAmount = Decimal.zero;
|
2022-10-04 00:01:04 +00:00
|
|
|
}
|
|
|
|
|
2023-02-05 20:32:39 +00:00
|
|
|
_receiveAmount = newReceiveAmount;
|
|
|
|
_reversed = true;
|
2022-10-04 00:01:04 +00:00
|
|
|
|
2023-02-05 20:32:39 +00:00
|
|
|
await _updateRangesAndEstimate(
|
2022-10-04 00:01:04 +00:00
|
|
|
shouldNotifyListeners: false,
|
|
|
|
);
|
2023-02-05 20:32:39 +00:00
|
|
|
}
|
2022-10-04 00:01:04 +00:00
|
|
|
|
2023-02-05 20:32:39 +00:00
|
|
|
if (shouldNotifyListeners) {
|
2023-02-06 15:44:23 +00:00
|
|
|
_notify();
|
2022-10-04 00:01:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-05 21:25:35 +00:00
|
|
|
Future<void> updateSendCurrency(
|
2023-02-08 19:06:26 +00:00
|
|
|
AggregateCurrency sendCurrency,
|
2023-02-05 20:32:39 +00:00
|
|
|
bool shouldNotifyListeners,
|
|
|
|
) async {
|
2022-10-04 00:01:04 +00:00
|
|
|
try {
|
2023-02-05 20:32:39 +00:00
|
|
|
_sendCurrency = sendCurrency;
|
2023-02-05 23:50:40 +00:00
|
|
|
_minSendAmount = null;
|
|
|
|
_maxSendAmount = null;
|
|
|
|
|
2023-02-05 20:32:39 +00:00
|
|
|
if (_receiveCurrency == null) {
|
|
|
|
_rate = null;
|
|
|
|
} else {
|
|
|
|
await _updateRangesAndEstimate(
|
|
|
|
shouldNotifyListeners: false,
|
|
|
|
);
|
2022-10-04 00:01:04 +00:00
|
|
|
}
|
2023-02-05 21:25:35 +00:00
|
|
|
if (shouldNotifyListeners) {
|
2023-02-06 15:44:23 +00:00
|
|
|
_notify();
|
2023-02-05 21:25:35 +00:00
|
|
|
}
|
2022-10-04 00:01:04 +00:00
|
|
|
} catch (e, s) {
|
|
|
|
Logging.instance.log("$e\n$s", level: LogLevel.Error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-05 21:25:35 +00:00
|
|
|
Future<void> updateReceivingCurrency(
|
2023-02-08 19:06:26 +00:00
|
|
|
AggregateCurrency receiveCurrency,
|
2022-10-04 00:01:04 +00:00
|
|
|
bool shouldNotifyListeners,
|
|
|
|
) async {
|
2023-02-05 20:32:39 +00:00
|
|
|
try {
|
|
|
|
_receiveCurrency = receiveCurrency;
|
2023-02-05 23:50:40 +00:00
|
|
|
_minReceiveAmount = null;
|
|
|
|
_maxReceiveAmount = null;
|
2023-02-05 20:32:39 +00:00
|
|
|
|
|
|
|
if (_sendCurrency == null) {
|
|
|
|
_rate = null;
|
|
|
|
} else {
|
|
|
|
await _updateRangesAndEstimate(
|
|
|
|
shouldNotifyListeners: false,
|
|
|
|
);
|
2022-10-04 00:01:04 +00:00
|
|
|
}
|
2023-02-05 21:25:35 +00:00
|
|
|
if (shouldNotifyListeners) {
|
2023-02-06 15:44:23 +00:00
|
|
|
_notify();
|
2023-02-05 21:25:35 +00:00
|
|
|
}
|
2023-02-05 20:32:39 +00:00
|
|
|
} catch (e, s) {
|
|
|
|
Logging.instance.log("$e\n$s", level: LogLevel.Error);
|
2022-10-04 00:01:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-05 20:32:39 +00:00
|
|
|
Future<void> swap({
|
|
|
|
required bool shouldNotifyListeners,
|
|
|
|
}) async {
|
|
|
|
final Decimal? temp = sendAmount;
|
|
|
|
_sendAmount = receiveAmount;
|
|
|
|
_receiveAmount = temp;
|
|
|
|
|
|
|
|
_minSendAmount = null;
|
|
|
|
_maxSendAmount = null;
|
|
|
|
_minReceiveAmount = null;
|
|
|
|
_maxReceiveAmount = null;
|
|
|
|
|
2023-02-08 19:06:26 +00:00
|
|
|
final AggregateCurrency? tmp = sendCurrency;
|
2023-02-05 20:32:39 +00:00
|
|
|
_sendCurrency = receiveCurrency;
|
|
|
|
_receiveCurrency = tmp;
|
|
|
|
|
|
|
|
await _updateRangesAndEstimate(
|
|
|
|
shouldNotifyListeners: false,
|
2022-10-20 15:03:16 +00:00
|
|
|
);
|
2023-02-06 15:44:23 +00:00
|
|
|
|
|
|
|
if (shouldNotifyListeners) {
|
|
|
|
_notify();
|
|
|
|
}
|
2022-10-04 00:01:04 +00:00
|
|
|
}
|
|
|
|
|
2023-02-06 21:50:18 +00:00
|
|
|
Future<void> refresh() => _updateRangesAndEstimate(
|
|
|
|
shouldNotifyListeners: true,
|
|
|
|
);
|
|
|
|
|
2023-02-05 20:32:39 +00:00
|
|
|
//============================================================================
|
|
|
|
// private state updaters
|
|
|
|
//============================================================================
|
|
|
|
|
|
|
|
Future<void> _updateRangesAndEstimate({
|
|
|
|
required bool shouldNotifyListeners,
|
|
|
|
}) async {
|
|
|
|
try {
|
2023-02-08 20:06:58 +00:00
|
|
|
switch (exchange.name) {
|
|
|
|
case ChangeNowExchange.exchangeName:
|
|
|
|
if (!_exchangeSupported(
|
|
|
|
exchangeName: exchange.name,
|
|
|
|
sendCurrency: sendCurrency,
|
|
|
|
receiveCurrency: receiveCurrency,
|
|
|
|
exchangeRateType: exchangeRateType,
|
|
|
|
)) {
|
|
|
|
_exchange = MajesticBankExchange.instance;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MajesticBankExchange.exchangeName:
|
|
|
|
if (!_exchangeSupported(
|
|
|
|
exchangeName: exchange.name,
|
|
|
|
sendCurrency: sendCurrency,
|
|
|
|
receiveCurrency: receiveCurrency,
|
|
|
|
exchangeRateType: exchangeRateType,
|
|
|
|
)) {
|
|
|
|
_exchange = ChangeNowExchange.instance;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-02-05 20:32:39 +00:00
|
|
|
await _updateRanges(shouldNotifyListeners: false);
|
|
|
|
await _updateEstimate(shouldNotifyListeners: false);
|
|
|
|
if (shouldNotifyListeners) {
|
2023-02-06 15:44:23 +00:00
|
|
|
_notify();
|
2023-02-05 20:32:39 +00:00
|
|
|
}
|
|
|
|
} catch (_) {
|
|
|
|
//
|
|
|
|
}
|
2022-10-04 00:01:04 +00:00
|
|
|
}
|
|
|
|
|
2023-02-05 20:32:39 +00:00
|
|
|
Future<void> _updateRanges({
|
|
|
|
required bool shouldNotifyListeners,
|
|
|
|
}) async {
|
2022-12-26 21:06:51 +00:00
|
|
|
// if (exchange?.name == SimpleSwapExchange.exchangeName) {
|
|
|
|
// reversed = false;
|
|
|
|
// }
|
2023-02-05 20:32:39 +00:00
|
|
|
final _send = sendCurrency;
|
|
|
|
final _receive = receiveCurrency;
|
|
|
|
if (_send == null || _receive == null) {
|
2022-10-04 00:01:04 +00:00
|
|
|
Logging.instance.log(
|
2023-02-05 20:32:39 +00:00
|
|
|
"Tried to $runtimeType.updateRanges where ( $_send || $_receive) for: $exchange",
|
2022-10-04 00:01:04 +00:00
|
|
|
level: LogLevel.Info,
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2023-02-05 20:32:39 +00:00
|
|
|
final response = await exchange.getRange(
|
|
|
|
_send.ticker,
|
|
|
|
_receive.ticker,
|
|
|
|
exchangeRateType == ExchangeRateType.fixed,
|
2022-10-04 00:01:04 +00:00
|
|
|
);
|
|
|
|
|
2023-02-05 20:32:39 +00:00
|
|
|
if (response.value == null) {
|
2022-10-04 00:01:04 +00:00
|
|
|
Logging.instance.log(
|
2022-10-04 19:10:44 +00:00
|
|
|
"Tried to $runtimeType.updateRanges for: $exchange where response: $response",
|
2022-10-04 00:01:04 +00:00
|
|
|
level: LogLevel.Info,
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2023-02-05 20:32:39 +00:00
|
|
|
final responseReversed = await exchange.getRange(
|
|
|
|
_receive.ticker,
|
|
|
|
_send.ticker,
|
|
|
|
exchangeRateType == ExchangeRateType.fixed,
|
|
|
|
);
|
2022-10-04 00:01:04 +00:00
|
|
|
|
2023-02-05 20:32:39 +00:00
|
|
|
if (responseReversed.value == null) {
|
|
|
|
Logging.instance.log(
|
|
|
|
"Tried to $runtimeType.updateRanges for: $exchange where response: $responseReversed",
|
|
|
|
level: LogLevel.Info,
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2022-10-04 00:01:04 +00:00
|
|
|
|
2023-02-05 20:32:39 +00:00
|
|
|
final range = response.value!;
|
|
|
|
final rangeReversed = responseReversed.value!;
|
|
|
|
|
|
|
|
_minSendAmount = range.min;
|
|
|
|
_maxSendAmount = range.max;
|
|
|
|
_minReceiveAmount = rangeReversed.min;
|
|
|
|
_maxReceiveAmount = rangeReversed.max;
|
2022-10-04 00:01:04 +00:00
|
|
|
|
2022-12-13 00:17:02 +00:00
|
|
|
//todo: check if print needed
|
|
|
|
// debugPrint(
|
|
|
|
// "updated range for: $exchange for $_fromTicker-$_toTicker: $range");
|
2022-10-04 00:01:04 +00:00
|
|
|
|
|
|
|
if (shouldNotifyListeners) {
|
2023-02-06 15:44:23 +00:00
|
|
|
_notify();
|
2022-10-04 00:01:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-05 20:32:39 +00:00
|
|
|
Future<void> _updateEstimate({
|
2022-10-04 00:01:04 +00:00
|
|
|
required bool shouldNotifyListeners,
|
|
|
|
}) async {
|
2022-12-26 21:06:51 +00:00
|
|
|
// if (exchange?.name == SimpleSwapExchange.exchangeName) {
|
|
|
|
// reversed = false;
|
|
|
|
// }
|
2023-02-05 20:32:39 +00:00
|
|
|
final amount = reversed ? receiveAmount : sendAmount;
|
|
|
|
if (sendCurrency == null ||
|
|
|
|
receiveCurrency == null ||
|
2022-10-04 00:01:04 +00:00
|
|
|
amount == null ||
|
|
|
|
amount <= Decimal.zero) {
|
|
|
|
Logging.instance.log(
|
2023-02-05 20:32:39 +00:00
|
|
|
"Tried to $runtimeType.updateEstimate for: $exchange where (from: $sendCurrency || to: $receiveCurrency || amount: $amount)",
|
2022-10-04 00:01:04 +00:00
|
|
|
level: LogLevel.Info,
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2023-02-05 20:32:39 +00:00
|
|
|
final response = await exchange.getEstimate(
|
|
|
|
sendCurrency!.ticker,
|
|
|
|
receiveCurrency!.ticker,
|
2022-10-04 00:01:04 +00:00
|
|
|
amount,
|
2023-02-05 20:32:39 +00:00
|
|
|
exchangeRateType == ExchangeRateType.fixed,
|
2022-10-04 00:01:04 +00:00
|
|
|
reversed,
|
|
|
|
);
|
|
|
|
|
2023-02-05 20:32:39 +00:00
|
|
|
if (response.value == null) {
|
2022-10-04 00:01:04 +00:00
|
|
|
Logging.instance.log(
|
2022-10-04 19:10:44 +00:00
|
|
|
"Tried to $runtimeType.updateEstimate for: $exchange where response: $response",
|
2022-10-04 00:01:04 +00:00
|
|
|
level: LogLevel.Info,
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-02-05 20:32:39 +00:00
|
|
|
_estimate = response.value!;
|
2022-10-04 00:01:04 +00:00
|
|
|
|
|
|
|
if (reversed) {
|
2023-02-05 20:32:39 +00:00
|
|
|
_sendAmount = _estimate!.estimatedAmount;
|
2022-10-04 00:01:04 +00:00
|
|
|
} else {
|
2023-02-05 20:32:39 +00:00
|
|
|
_receiveAmount = _estimate!.estimatedAmount;
|
2022-10-04 00:01:04 +00:00
|
|
|
}
|
|
|
|
|
2023-02-05 20:32:39 +00:00
|
|
|
_rate =
|
|
|
|
(receiveAmount! / sendAmount!).toDecimal(scaleOnInfinitePrecision: 12);
|
2022-10-04 00:01:04 +00:00
|
|
|
|
2022-12-13 00:17:02 +00:00
|
|
|
//todo: check if print needed
|
|
|
|
// debugPrint(
|
|
|
|
// "updated estimate for: $exchange for $fromTicker-$toTicker: $estimate");
|
2022-10-04 00:01:04 +00:00
|
|
|
|
|
|
|
if (shouldNotifyListeners) {
|
2023-02-06 15:44:23 +00:00
|
|
|
_notify();
|
2022-10-04 00:01:04 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-06 15:44:23 +00:00
|
|
|
|
|
|
|
//============================================================================
|
2023-02-06 18:29:00 +00:00
|
|
|
|
|
|
|
void _notify() {
|
|
|
|
debugPrint("ExFState NOTIFY: ${toString()}");
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2023-02-08 20:06:58 +00:00
|
|
|
bool _exchangeSupported({
|
|
|
|
required String exchangeName,
|
|
|
|
required AggregateCurrency? sendCurrency,
|
|
|
|
required AggregateCurrency? receiveCurrency,
|
|
|
|
required ExchangeRateType exchangeRateType,
|
|
|
|
}) {
|
|
|
|
final send = sendCurrency?.forExchange(exchangeName);
|
|
|
|
if (send == null) return false;
|
|
|
|
|
|
|
|
final rcv = receiveCurrency?.forExchange(exchangeName);
|
|
|
|
if (rcv == null) return false;
|
|
|
|
|
|
|
|
if (exchangeRateType == ExchangeRateType.fixed) {
|
|
|
|
return send.supportsFixedRate && rcv.supportsFixedRate;
|
|
|
|
} else {
|
|
|
|
return send.supportsEstimatedRate && rcv.supportsEstimatedRate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-06 15:44:23 +00:00
|
|
|
@override
|
|
|
|
String toString() {
|
|
|
|
return "{"
|
|
|
|
"\n\t exchange: $exchange,"
|
|
|
|
"\n\t exchangeRateType: $exchangeRateType,"
|
|
|
|
"\n\t sendCurrency: $sendCurrency,"
|
|
|
|
"\n\t receiveCurrency: $receiveCurrency,"
|
|
|
|
"\n\t rate: $rate,"
|
|
|
|
"\n\t reversed: $reversed,"
|
|
|
|
"\n\t sendAmount: $sendAmount,"
|
|
|
|
"\n\t receiveAmount: $receiveAmount,"
|
|
|
|
"\n\t estimate: $estimate,"
|
|
|
|
"\n\t minSendAmount: $minSendAmount,"
|
|
|
|
"\n\t maxSendAmount: $maxSendAmount,"
|
|
|
|
"\n\t minReceiveAmount: $minReceiveAmount,"
|
|
|
|
"\n\t maxReceiveAmount: $maxReceiveAmount,"
|
|
|
|
"\n\t canExchange: $canExchange,"
|
|
|
|
"\n\t warning: $warning,"
|
|
|
|
"\n}";
|
|
|
|
}
|
2022-10-04 00:01:04 +00:00
|
|
|
}
|