cake_wallet/lib/view_model/exchange/exchange_trade_view_model.dart

155 lines
5.2 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'package:cake_wallet/exchange/sideshift/sideshift_exchange_provider.dart';
import 'package:cake_wallet/exchange/simpleswap/simpleswap_exchange_provider.dart';
2023-02-06 19:20:43 +00:00
import 'package:cake_wallet/exchange/trocador/trocador_exchange_provider.dart';
2021-12-24 12:37:24 +00:00
import 'package:cw_core/wallet_base.dart';
import 'package:cw_core/crypto_currency.dart';
2020-09-21 11:50:26 +00:00
import 'package:cake_wallet/exchange/changenow/changenow_exchange_provider.dart';
import 'package:cake_wallet/exchange/exchange_provider.dart';
import 'package:cake_wallet/exchange/exchange_provider_description.dart';
import 'package:cake_wallet/exchange/morphtoken/morphtoken_exchange_provider.dart';
import 'package:cake_wallet/exchange/trade.dart';
import 'package:cake_wallet/exchange/xmrto/xmrto_exchange_provider.dart';
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';
import 'package:hive/hive.dart';
import 'package:mobx/mobx.dart';
import 'package:cake_wallet/src/screens/exchange_trade/exchange_trade_item.dart';
import 'package:cake_wallet/generated/i18n.dart';
part 'exchange_trade_view_model.g.dart';
2023-01-16 15:31:31 +00:00
class ExchangeTradeViewModel = ExchangeTradeViewModelBase with _$ExchangeTradeViewModel;
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!,
isSendable = tradesStore.trade!.from == wallet.currency ||
tradesStore.trade!.provider == ExchangeProviderDescription.xmrto,
items = ObservableList<ExchangeTradeItem>() {
switch (trade.provider) {
case ExchangeProviderDescription.xmrto:
_provider = XMRTOExchangeProvider();
break;
case ExchangeProviderDescription.changeNow:
_provider = ChangeNowExchangeProvider();
break;
case ExchangeProviderDescription.morphToken:
_provider = MorphTokenExchangeProvider(trades: trades);
break;
case ExchangeProviderDescription.sideShift:
_provider = SideShiftExchangeProvider();
break;
case ExchangeProviderDescription.simpleSwap:
_provider = SimpleSwapExchangeProvider();
break;
2023-02-06 19:20:43 +00:00
case ExchangeProviderDescription.trocador:
_provider = TrocadorExchangeProvider();
break;
}
_updateItems();
_updateTrade();
timer = Timer.periodic(Duration(seconds: 20), (_) async => _updateTrade());
}
final WalletBase wallet;
final Box<Trade> trades;
final TradesStore tradesStore;
2020-10-30 16:32:21 +00:00
final SendViewModel sendViewModel;
@observable
Trade trade;
@observable
bool isSendable;
@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
: '';
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
@observable
ObservableList<ExchangeTradeItem> items;
2022-10-12 17:09:57 +00:00
ExchangeProvider? _provider;
2022-10-12 17:09:57 +00:00
Timer? timer;
2020-10-30 16:32:21 +00:00
@action
Future confirmSending() async {
if (!isSendable) {
return;
}
sendViewModel.clearOutputs();
final output = sendViewModel.outputs.first;
2022-10-12 17:09:57 +00:00
output.address = trade.inputAddress ?? '';
output.setCryptoAmount(trade.amount);
2020-10-30 16:32:21 +00:00
await sendViewModel.createTransaction();
}
@action
Future<void> _updateTrade() async {
try {
2022-10-12 17:09:57 +00:00
final updatedTrade = await _provider!.findTradeById(id: trade.id);
if (updatedTrade.createdAt == null && trade.createdAt != null) {
updatedTrade.createdAt = trade.createdAt;
}
trade = updatedTrade;
_updateItems();
} catch (e) {
print(e.toString());
}
}
void _updateItems() {
final tagFrom = tradesStore.trade!.from.tag != null ? '${tradesStore.trade!.from.tag}' + ' ' : '';
final tagTo = tradesStore.trade!.to.tag != null ? '${tradesStore.trade!.to.tag}' + ' ' : '';
2022-10-12 17:09:57 +00:00
items.clear();
items.add(ExchangeTradeItem(
title: "${trade.provider.title} ${S.current.id}", data: '${trade.id}', isCopied: true));
if (trade.extraId != null) {
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;
2023-01-16 15:31:31 +00:00
items.add(ExchangeTradeItem(title: title, data: '${trade.extraId}', isCopied: false));
}
items.addAll([
ExchangeTradeItem(title: S.current.amount, data: '${trade.amount}', isCopied: true),
ExchangeTradeItem(
title: S.current.send_to_this_address('${tradesStore.trade!.from}', tagFrom) + ':',
2022-10-12 17:09:57 +00:00
data: trade.inputAddress ?? '',
isCopied: true),
ExchangeTradeItem(
title: S.current.arrive_in_this_address('${tradesStore.trade!.to}', tagTo) + ':',
data: trade.payoutAddress ?? '',
isCopied: true),
]);
}
2020-09-15 20:35:49 +00:00
}