From 5ea443235d9343c78ba5d6cc827bd5ccbca3c884 Mon Sep 17 00:00:00 2001 From: Omar Hatem Date: Fri, 3 Jan 2025 08:53:15 +0200 Subject: [PATCH] Disable mweb coins for exchanges (#1919) * Disable mweb coins for exchanges * Take into consideration to not use non-mweb coins in ALL calculations --- lib/bitcoin/cw_bitcoin.dart | 4 +- .../exchange/exchange_view_model.dart | 42 +++++++++++-------- lib/view_model/send/send_view_model.dart | 13 ++++-- tool/configure.dart | 4 +- 4 files changed, 40 insertions(+), 23 deletions(-) diff --git a/lib/bitcoin/cw_bitcoin.dart b/lib/bitcoin/cw_bitcoin.dart index d098ea0e4..bf9ec0c4d 100644 --- a/lib/bitcoin/cw_bitcoin.dart +++ b/lib/bitcoin/cw_bitcoin.dart @@ -149,7 +149,8 @@ class CWBitcoin extends Bitcoin { } @override - Future estimateFakeSendAllTxAmount(Object wallet, TransactionPriority priority) async { + Future estimateFakeSendAllTxAmount(Object wallet, TransactionPriority priority, + {UnspentCoinType coinTypeToSpendFrom = UnspentCoinType.any}) async { try { final sk = ECPrivate.random(); final electrumWallet = wallet as ElectrumWallet; @@ -173,6 +174,7 @@ class CWBitcoin extends Bitcoin { ? priority as LitecoinTransactionPriority : priority as BitcoinTransactionPriority, ), + coinTypeToSpendFrom: coinTypeToSpendFrom, ); return estimatedTx.amount; diff --git a/lib/view_model/exchange/exchange_view_model.dart b/lib/view_model/exchange/exchange_view_model.dart index ee8a88b6b..63e1db6bc 100644 --- a/lib/view_model/exchange/exchange_view_model.dart +++ b/lib/view_model/exchange/exchange_view_model.dart @@ -9,6 +9,7 @@ import 'package:cake_wallet/exchange/provider/stealth_ex_exchange_provider.dart' import 'package:cw_core/crypto_currency.dart'; import 'package:cw_core/sync_status.dart'; import 'package:cw_core/transaction_priority.dart'; +import 'package:cw_core/unspent_coin_type.dart'; import 'package:cw_core/utils/print_verbose.dart'; import 'package:cw_core/wallet_type.dart'; import 'package:hive/hive.dart'; @@ -122,7 +123,8 @@ abstract class ExchangeViewModelBase extends WalletChangeListenerViewModel with depositAmount = ''; receiveAmount = ''; receiveAddress = ''; - depositAddress = depositCurrency == wallet.currency ? wallet.walletAddresses.addressForExchange : ''; + depositAddress = + depositCurrency == wallet.currency ? wallet.walletAddresses.addressForExchange : ''; provider = providersForCurrentPair().first; final initialProvider = provider; provider!.checkIsAvailable().then((bool isAvailable) { @@ -157,8 +159,7 @@ abstract class ExchangeViewModelBase extends WalletChangeListenerViewModel with wallet.type == WalletType.bitcoinCash; bool get hideAddressAfterExchange => - wallet.type == WalletType.monero || - wallet.type == WalletType.wownero; + wallet.type == WalletType.monero || wallet.type == WalletType.wownero; bool _useTorOnly; final Box trades; @@ -167,17 +168,17 @@ abstract class ExchangeViewModelBase extends WalletChangeListenerViewModel with final SharedPreferences sharedPreferences; List get _allProviders => [ - ChangeNowExchangeProvider(settingsStore: _settingsStore), - SideShiftExchangeProvider(), - SimpleSwapExchangeProvider(), - ThorChainExchangeProvider(tradesStore: trades), - if (FeatureFlag.isExolixEnabled) ExolixExchangeProvider(), - QuantexExchangeProvider(), - LetsExchangeExchangeProvider(), - StealthExExchangeProvider(), - TrocadorExchangeProvider( - useTorOnly: _useTorOnly, providerStates: _settingsStore.trocadorProviderStates), - ]; + ChangeNowExchangeProvider(settingsStore: _settingsStore), + SideShiftExchangeProvider(), + SimpleSwapExchangeProvider(), + ThorChainExchangeProvider(tradesStore: trades), + if (FeatureFlag.isExolixEnabled) ExolixExchangeProvider(), + QuantexExchangeProvider(), + LetsExchangeExchangeProvider(), + StealthExExchangeProvider(), + TrocadorExchangeProvider( + useTorOnly: _useTorOnly, providerStates: _settingsStore.trocadorProviderStates), + ]; @observable ExchangeProvider? provider; @@ -613,8 +614,10 @@ abstract class ExchangeViewModelBase extends WalletChangeListenerViewModel with isReceiveAmountEntered = false; depositAmount = ''; receiveAmount = ''; - depositAddress = depositCurrency == wallet.currency ? wallet.walletAddresses.addressForExchange : ''; - receiveAddress = receiveCurrency == wallet.currency ? wallet.walletAddresses.addressForExchange : ''; + depositAddress = + depositCurrency == wallet.currency ? wallet.walletAddresses.addressForExchange : ''; + receiveAddress = + receiveCurrency == wallet.currency ? wallet.walletAddresses.addressForExchange : ''; isDepositAddressEnabled = !(depositCurrency == wallet.currency); isFixedRateMode = false; _onPairChange(); @@ -640,7 +643,12 @@ abstract class ExchangeViewModelBase extends WalletChangeListenerViewModel with wallet.type == WalletType.bitcoinCash) { final priority = _settingsStore.priority[wallet.type]!; - final amount = await bitcoin!.estimateFakeSendAllTxAmount(wallet, priority); + final amount = await bitcoin!.estimateFakeSendAllTxAmount( + wallet, + priority, + coinTypeToSpendFrom: + wallet.type == WalletType.litecoin ? UnspentCoinType.nonMweb : UnspentCoinType.any, + ); changeDepositAmount(amount: bitcoin!.formatterBitcoinAmountToString(amount: amount)); } diff --git a/lib/view_model/send/send_view_model.dart b/lib/view_model/send/send_view_model.dart index 78bc867db..cafe89cb1 100644 --- a/lib/view_model/send/send_view_model.dart +++ b/lib/view_model/send/send_view_model.dart @@ -394,7 +394,7 @@ abstract class SendViewModelBase extends WalletChangeListenerViewModel with Stor if (wallet.isHardwareWallet) state = IsAwaitingDeviceResponseState(); - pendingTransaction = await wallet.createTransaction(_credentials()); + pendingTransaction = await wallet.createTransaction(_credentials(provider)); if (provider is ThorChainExchangeProvider) { final outputCount = pendingTransaction?.outputCount ?? 0; @@ -522,7 +522,7 @@ abstract class SendViewModelBase extends WalletChangeListenerViewModel with Stor void setTransactionPriority(TransactionPriority priority) => _settingsStore.priority[wallet.type] = priority; - Object _credentials() { + Object _credentials([ExchangeProvider? provider]) { final priority = _settingsStore.priority[wallet.type]; if (priority == null && @@ -535,7 +535,6 @@ abstract class SendViewModelBase extends WalletChangeListenerViewModel with Stor switch (wallet.type) { case WalletType.bitcoin: - case WalletType.litecoin: case WalletType.bitcoinCash: return bitcoin!.createBitcoinTransactionCredentials( outputs, @@ -543,6 +542,14 @@ abstract class SendViewModelBase extends WalletChangeListenerViewModel with Stor feeRate: customBitcoinFeeRate, coinTypeToSpendFrom: coinTypeToSpendFrom, ); + case WalletType.litecoin: + return bitcoin!.createBitcoinTransactionCredentials( + outputs, + priority: priority!, + feeRate: customBitcoinFeeRate, + // if it's an exchange flow then disable sending from mweb coins + coinTypeToSpendFrom: provider != null ? UnspentCoinType.nonMweb : coinTypeToSpendFrom, + ); case WalletType.monero: return monero! diff --git a/tool/configure.dart b/tool/configure.dart index 6abd73d9e..c08ef3a34 100644 --- a/tool/configure.dart +++ b/tool/configure.dart @@ -109,7 +109,6 @@ import 'package:cw_bitcoin/electrum.dart'; import 'package:cw_bitcoin/electrum_transaction_info.dart'; import 'package:cw_bitcoin/pending_bitcoin_transaction.dart'; import 'package:cw_bitcoin/bitcoin_receive_page_option.dart'; -import 'package:cw_bitcoin/bitcoin_wallet.dart'; import 'package:cw_bitcoin/electrum_wallet.dart'; import 'package:cw_bitcoin/bitcoin_unspent.dart'; import 'package:cw_bitcoin/bitcoin_mnemonic.dart'; @@ -173,7 +172,8 @@ abstract class Bitcoin { List getSilentPaymentAddresses(Object wallet); List getSilentPaymentReceivedAddresses(Object wallet); - Future estimateFakeSendAllTxAmount(Object wallet, TransactionPriority priority); + Future estimateFakeSendAllTxAmount(Object wallet, TransactionPriority priority, + {UnspentCoinType coinTypeToSpendFrom = UnspentCoinType.any}); List getSubAddresses(Object wallet); String formatterBitcoinAmountToString({required int amount});