CW-997 coin control enhancements / send ALL fixes (#2114)

* computed sending balance value

* files that didnt get added before

* monero + move unspent calc to view model

* working

* remove old code

* Update lib/view_model/send/send_view_model.dart

---------

Co-authored-by: Omar Hatem <omarh.ismail1@gmail.com>
This commit is contained in:
Matthew Fosse
2025-03-30 16:52:46 +02:00
committed by GitHub
co-authored by Omar Hatem
parent 7831b421b1
commit 40a0989956
8 changed files with 106 additions and 12 deletions
+38 -2
View File
@@ -225,6 +225,42 @@ abstract class SendViewModelBase extends WalletChangeListenerViewModel with Stor
return wallet.balance[selectedCryptoCurrency]!.formattedFullAvailableBalance;
}
@action
Future<void> updateSendingBalance() async {
// force the sendingBalance to recompute since unspent coins aren't observable
// or at least mobx can't detect the changes
final currentType = coinTypeToSpendFrom;
if (currentType == UnspentCoinType.any) {
coinTypeToSpendFrom = UnspentCoinType.nonMweb;
} else if (currentType == UnspentCoinType.nonMweb) {
coinTypeToSpendFrom = UnspentCoinType.any;
} else if (currentType == UnspentCoinType.mweb) {
coinTypeToSpendFrom = UnspentCoinType.nonMweb;
}
// set it back to the original value:
coinTypeToSpendFrom = currentType;
}
@computed
String get sendingBalance {
// only for electrum, monero, wownero, decred wallets atm:
switch (wallet.type) {
case WalletType.bitcoin:
case WalletType.litecoin:
case WalletType.bitcoinCash:
case WalletType.monero:
case WalletType.wownero:
case WalletType.decred:
return wallet.formatCryptoAmount(
unspentCoinsListViewModel.getSendingBalance(coinTypeToSpendFrom).toString());
default:
return balance;
}
}
@computed
bool get isFiatDisabled => balanceViewModel.isFiatDisabled;
@@ -502,14 +538,14 @@ abstract class SendViewModelBase extends WalletChangeListenerViewModel with Stor
return bitcoin!.createBitcoinTransactionCredentials(
outputs,
priority: priority!,
feeRate:feesViewModel. customBitcoinFeeRate,
feeRate: feesViewModel.customBitcoinFeeRate,
coinTypeToSpendFrom: coinTypeToSpendFrom,
);
case WalletType.litecoin:
return bitcoin!.createBitcoinTransactionCredentials(
outputs,
priority: priority!,
feeRate:feesViewModel. customBitcoinFeeRate,
feeRate: feesViewModel.customBitcoinFeeRate,
// if it's an exchange flow then disable sending from mweb coins
coinTypeToSpendFrom: provider != null ? UnspentCoinType.nonMweb : coinTypeToSpendFrom,
);
@@ -131,6 +131,36 @@ abstract class UnspentCoinsListViewModelBase with Store {
}
}
List<Unspent> _getSpecificUnspents(UnspentCoinType overrideCoinTypeToSpendFrom) {
switch (wallet.type) {
case WalletType.monero:
return monero!.getUnspents(wallet);
case WalletType.wownero:
return wownero!.getUnspents(wallet);
case WalletType.bitcoin:
case WalletType.litecoin:
case WalletType.bitcoinCash:
return bitcoin!.getUnspents(wallet, coinTypeToSpendFrom: overrideCoinTypeToSpendFrom);
case WalletType.decred:
return decred!.getUnspents(wallet);
default:
return List.empty();
}
}
@action
int getSendingBalance(UnspentCoinType overrideCoinTypeToSpendFrom) {
// return items.where((element) => element.isSending).fold(0, (previousValue, element) => previousValue + element.value);
// go through all unspent coins and add up the value minus frozen and non sending:
int total = 0;
for (final item in _getSpecificUnspents(overrideCoinTypeToSpendFrom)) {
if (item.isFrozen || !item.isSending) continue;
total += item.value;
}
return total;
}
@action
void _updateUnspentCoinsInfo() {
items.clear();