2020-08-26 17:31:23 +00:00
|
|
|
import 'dart:async';
|
2023-10-25 20:58:25 +00:00
|
|
|
|
2020-09-21 11:50:26 +00:00
|
|
|
import 'package:cake_wallet/exchange/exchange_provider_description.dart';
|
2023-10-25 20:58:25 +00:00
|
|
|
import 'package:cake_wallet/exchange/provider/changenow_exchange_provider.dart';
|
|
|
|
import 'package:cake_wallet/exchange/provider/exchange_provider.dart';
|
|
|
|
import 'package:cake_wallet/exchange/provider/exolix_exchange_provider.dart';
|
|
|
|
import 'package:cake_wallet/exchange/provider/sideshift_exchange_provider.dart';
|
|
|
|
import 'package:cake_wallet/exchange/provider/simpleswap_exchange_provider.dart';
|
2024-03-28 12:41:11 +00:00
|
|
|
import 'package:cake_wallet/exchange/provider/thorchain_exchange.provider.dart';
|
2023-10-25 20:58:25 +00:00
|
|
|
import 'package:cake_wallet/exchange/provider/trocador_exchange_provider.dart';
|
2020-09-21 11:50:26 +00:00
|
|
|
import 'package:cake_wallet/exchange/trade.dart';
|
2023-10-25 20:58:25 +00:00
|
|
|
import 'package:cake_wallet/generated/i18n.dart';
|
|
|
|
import 'package:cake_wallet/src/screens/exchange_trade/exchange_trade_item.dart';
|
2020-08-26 17:31:23 +00:00
|
|
|
import 'package:cake_wallet/store/dashboard/trades_store.dart';
|
2020-10-30 16:32:21 +00:00
|
|
|
import 'package:cake_wallet/view_model/send/send_view_model.dart';
|
2023-10-25 20:58:25 +00:00
|
|
|
import 'package:cw_core/crypto_currency.dart';
|
|
|
|
import 'package:cw_core/wallet_base.dart';
|
2020-08-26 17:31:23 +00:00
|
|
|
import 'package:hive/hive.dart';
|
|
|
|
import 'package:mobx/mobx.dart';
|
|
|
|
|
|
|
|
part 'exchange_trade_view_model.g.dart';
|
|
|
|
|
2023-01-16 15:31:31 +00:00
|
|
|
class ExchangeTradeViewModel = ExchangeTradeViewModelBase with _$ExchangeTradeViewModel;
|
2020-08-26 17:31:23 +00:00
|
|
|
|
|
|
|
abstract class ExchangeTradeViewModelBase with Store {
|
2020-10-30 16:32:21 +00:00
|
|
|
ExchangeTradeViewModelBase(
|
2022-10-12 17:09:57 +00:00
|
|
|
{required this.wallet,
|
2023-01-16 15:31:31 +00:00
|
|
|
required this.trades,
|
|
|
|
required this.tradesStore,
|
|
|
|
required this.sendViewModel})
|
|
|
|
: trade = tradesStore.trade!,
|
2023-12-02 02:26:43 +00:00
|
|
|
isSendable = _checkIfCanSend(tradesStore, wallet),
|
2023-01-16 15:31:31 +00:00
|
|
|
items = ObservableList<ExchangeTradeItem>() {
|
2020-08-26 17:31:23 +00:00
|
|
|
switch (trade.provider) {
|
|
|
|
case ExchangeProviderDescription.changeNow:
|
2023-07-08 02:30:05 +00:00
|
|
|
_provider =
|
|
|
|
ChangeNowExchangeProvider(settingsStore: sendViewModel.balanceViewModel.settingsStore);
|
2020-08-26 17:31:23 +00:00
|
|
|
break;
|
2022-08-31 15:34:07 +00:00
|
|
|
case ExchangeProviderDescription.sideShift:
|
|
|
|
_provider = SideShiftExchangeProvider();
|
|
|
|
break;
|
|
|
|
case ExchangeProviderDescription.simpleSwap:
|
|
|
|
_provider = SimpleSwapExchangeProvider();
|
|
|
|
break;
|
2023-06-09 11:46:00 +00:00
|
|
|
case ExchangeProviderDescription.trocador:
|
2023-02-06 19:20:43 +00:00
|
|
|
_provider = TrocadorExchangeProvider();
|
|
|
|
break;
|
2023-10-12 00:20:19 +00:00
|
|
|
case ExchangeProviderDescription.exolix:
|
|
|
|
_provider = ExolixExchangeProvider();
|
|
|
|
break;
|
2024-03-28 12:41:11 +00:00
|
|
|
case ExchangeProviderDescription.thorChain:
|
|
|
|
_provider = ThorChainExchangeProvider(tradesStore: trades);
|
|
|
|
break;
|
2020-08-26 17:31:23 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 12:42:53 +00:00
|
|
|
_updateItems();
|
2023-10-25 20:58:25 +00:00
|
|
|
|
|
|
|
if (_provider != null) {
|
|
|
|
_updateTrade();
|
|
|
|
timer = Timer.periodic(Duration(seconds: 20), (_) async => _updateTrade());
|
|
|
|
}
|
2020-08-26 17:31:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
final WalletBase wallet;
|
|
|
|
final Box<Trade> trades;
|
|
|
|
final TradesStore tradesStore;
|
2020-10-30 16:32:21 +00:00
|
|
|
final SendViewModel sendViewModel;
|
2020-08-26 17:31:23 +00:00
|
|
|
|
|
|
|
@observable
|
|
|
|
Trade trade;
|
|
|
|
|
|
|
|
@observable
|
|
|
|
bool isSendable;
|
|
|
|
|
2021-02-24 17:45:30 +00:00
|
|
|
@computed
|
|
|
|
String get extraInfo => trade.from == CryptoCurrency.xlm
|
2023-01-16 15:31:31 +00:00
|
|
|
? '\n\n' + S.current.xlm_extra_info
|
|
|
|
: trade.from == CryptoCurrency.xrp
|
|
|
|
? '\n\n' + S.current.xrp_extra_info
|
|
|
|
: '';
|
2021-02-24 17:45:30 +00:00
|
|
|
|
2022-11-22 17:33:24 +00:00
|
|
|
@computed
|
2023-01-16 15:31:31 +00:00
|
|
|
String get pendingTransactionFiatAmountValueFormatted => sendViewModel.isFiatDisabled
|
|
|
|
? ''
|
|
|
|
: sendViewModel.pendingTransactionFiatAmount + ' ' + sendViewModel.fiat.title;
|
2022-11-22 17:33:24 +00:00
|
|
|
|
|
|
|
@computed
|
2023-01-16 15:31:31 +00:00
|
|
|
String get pendingTransactionFeeFiatAmountFormatted => sendViewModel.isFiatDisabled
|
|
|
|
? ''
|
|
|
|
: sendViewModel.pendingTransactionFeeFiatAmount + ' ' + sendViewModel.fiat.title;
|
2022-11-22 17:33:24 +00:00
|
|
|
|
2020-08-27 19:42:28 +00:00
|
|
|
@observable
|
2020-08-28 20:04:48 +00:00
|
|
|
ObservableList<ExchangeTradeItem> items;
|
2020-08-27 19:42:28 +00:00
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
ExchangeProvider? _provider;
|
2020-08-26 17:31:23 +00:00
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
Timer? timer;
|
2020-08-26 17:31:23 +00:00
|
|
|
|
2020-10-30 16:32:21 +00:00
|
|
|
@action
|
2023-10-25 20:58:25 +00:00
|
|
|
Future<void> confirmSending() async {
|
|
|
|
if (!isSendable) return;
|
2020-10-30 16:32:21 +00:00
|
|
|
|
2021-08-10 14:52:35 +00:00
|
|
|
sendViewModel.clearOutputs();
|
|
|
|
final output = sendViewModel.outputs.first;
|
2022-10-12 17:09:57 +00:00
|
|
|
output.address = trade.inputAddress ?? '';
|
2021-08-10 14:52:35 +00:00
|
|
|
output.setCryptoAmount(trade.amount);
|
2024-03-28 12:41:11 +00:00
|
|
|
if (_provider is ThorChainExchangeProvider) output.memo = trade.memo;
|
2024-03-29 18:51:34 +00:00
|
|
|
if (trade.isSendAll == true) output.sendAll = true;
|
2023-08-08 01:02:09 +00:00
|
|
|
sendViewModel.selectedCryptoCurrency = trade.from;
|
2024-03-28 12:41:11 +00:00
|
|
|
final pendingTransaction = await sendViewModel.createTransaction(provider: _provider);
|
|
|
|
if (_provider is ThorChainExchangeProvider) {
|
|
|
|
trade.id = pendingTransaction?.id ?? '';
|
|
|
|
trades.add(trade);
|
|
|
|
}
|
2020-10-30 16:32:21 +00:00
|
|
|
}
|
|
|
|
|
2020-08-26 17:31:23 +00:00
|
|
|
@action
|
|
|
|
Future<void> _updateTrade() async {
|
|
|
|
try {
|
2024-03-29 18:51:34 +00:00
|
|
|
final agreedAmount = tradesStore.trade!.amount;
|
|
|
|
final isSendAll = tradesStore.trade!.isSendAll;
|
2022-10-12 17:09:57 +00:00
|
|
|
final updatedTrade = await _provider!.findTradeById(id: trade.id);
|
2020-08-26 17:31:23 +00:00
|
|
|
|
2023-10-25 20:58:25 +00:00
|
|
|
if (updatedTrade.createdAt == null && trade.createdAt != null)
|
2020-08-26 17:31:23 +00:00
|
|
|
updatedTrade.createdAt = trade.createdAt;
|
|
|
|
|
2023-10-25 20:58:25 +00:00
|
|
|
if (updatedTrade.amount.isEmpty) updatedTrade.amount = trade.amount;
|
2023-06-09 11:46:00 +00:00
|
|
|
|
2020-08-26 17:31:23 +00:00
|
|
|
trade = updatedTrade;
|
2024-03-29 18:51:34 +00:00
|
|
|
trade.amount = agreedAmount;
|
|
|
|
trade.isSendAll = isSendAll;
|
2020-12-18 12:42:53 +00:00
|
|
|
|
|
|
|
_updateItems();
|
2020-08-26 17:31:23 +00:00
|
|
|
} catch (e) {
|
|
|
|
print(e.toString());
|
|
|
|
}
|
|
|
|
}
|
2020-12-18 12:42:53 +00:00
|
|
|
|
|
|
|
void _updateItems() {
|
2023-06-09 11:46:00 +00:00
|
|
|
final tagFrom =
|
|
|
|
tradesStore.trade!.from.tag != null ? '${tradesStore.trade!.from.tag}' + ' ' : '';
|
2023-05-10 13:58:31 +00:00
|
|
|
final tagTo = tradesStore.trade!.to.tag != null ? '${tradesStore.trade!.to.tag}' + ' ' : '';
|
2022-10-12 17:09:57 +00:00
|
|
|
items.clear();
|
2024-03-28 12:41:11 +00:00
|
|
|
|
2024-03-29 18:51:34 +00:00
|
|
|
if (trade.provider != ExchangeProviderDescription.thorChain)
|
|
|
|
items.add(ExchangeTradeItem(
|
|
|
|
title: "${trade.provider.title} ${S.current.id}", data: '${trade.id}', isCopied: true));
|
2021-01-11 17:14:53 +00:00
|
|
|
|
|
|
|
if (trade.extraId != null) {
|
2021-01-13 09:47:56 +00:00
|
|
|
final title = trade.from == CryptoCurrency.xrp
|
|
|
|
? S.current.destination_tag
|
|
|
|
: trade.from == CryptoCurrency.xlm
|
2023-01-16 15:31:31 +00:00
|
|
|
? S.current.memo
|
|
|
|
: S.current.extra_id;
|
2021-01-13 09:47:56 +00:00
|
|
|
|
2023-01-16 15:31:31 +00:00
|
|
|
items.add(ExchangeTradeItem(title: title, data: '${trade.extraId}', isCopied: false));
|
2021-01-11 17:14:53 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 12:42:53 +00:00
|
|
|
items.addAll([
|
2023-04-16 13:27:33 +00:00
|
|
|
ExchangeTradeItem(title: S.current.amount, data: '${trade.amount}', isCopied: true),
|
2020-12-18 12:42:53 +00:00
|
|
|
ExchangeTradeItem(
|
2023-05-10 13:58:31 +00:00
|
|
|
title: S.current.send_to_this_address('${tradesStore.trade!.from}', tagFrom) + ':',
|
2022-10-12 17:09:57 +00:00
|
|
|
data: trade.inputAddress ?? '',
|
2020-12-18 12:42:53 +00:00
|
|
|
isCopied: true),
|
2023-01-16 15:27:49 +00:00
|
|
|
ExchangeTradeItem(
|
2023-05-10 13:58:31 +00:00
|
|
|
title: S.current.arrive_in_this_address('${tradesStore.trade!.to}', tagTo) + ':',
|
2023-01-16 15:27:49 +00:00
|
|
|
data: trade.payoutAddress ?? '',
|
|
|
|
isCopied: true),
|
2020-12-18 12:42:53 +00:00
|
|
|
]);
|
|
|
|
}
|
2023-12-02 02:26:43 +00:00
|
|
|
|
|
|
|
static bool _checkIfCanSend(TradesStore tradesStore, WalletBase wallet) {
|
|
|
|
bool _isEthToken() =>
|
|
|
|
wallet.currency == CryptoCurrency.eth &&
|
|
|
|
tradesStore.trade!.from.tag == CryptoCurrency.eth.title;
|
|
|
|
|
|
|
|
bool _isPolygonToken() =>
|
|
|
|
wallet.currency == CryptoCurrency.maticpoly &&
|
|
|
|
tradesStore.trade!.from.tag == CryptoCurrency.maticpoly.tag;
|
|
|
|
|
2024-03-04 17:32:10 +00:00
|
|
|
bool _isSplToken() =>
|
|
|
|
wallet.currency == CryptoCurrency.sol &&
|
|
|
|
tradesStore.trade!.from.tag == CryptoCurrency.sol.title;
|
|
|
|
|
2023-12-02 02:26:43 +00:00
|
|
|
return tradesStore.trade!.from == wallet.currency ||
|
|
|
|
tradesStore.trade!.provider == ExchangeProviderDescription.xmrto ||
|
|
|
|
_isEthToken() ||
|
2024-03-04 17:32:10 +00:00
|
|
|
_isPolygonToken() ||
|
|
|
|
_isSplToken();
|
2023-12-02 02:26:43 +00:00
|
|
|
}
|
2020-09-15 20:35:49 +00:00
|
|
|
}
|