Skip to content

Commit cabb8fc

Browse files
committed
GUI: 'getAvailableBalance', use the cached balance if the user did not select UTXO manually.
No need to walk through the entire wallet's tx map. Used for 'walletModel::prepareTransaction' and 'useAvailable' flow in sendcoinsdialog.
1 parent 0a830f4 commit cabb8fc

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

src/qt/sendcoinsdialog.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include <key_io.h>
2424
#include <node/ui_interface.h>
2525
#include <policy/fees.h>
26-
#include <txmempool.h>
2726
#include <validation.h>
2827
#include <wallet/coincontrol.h>
2928
#include <wallet/fees.h>
@@ -795,7 +794,7 @@ void SendCoinsDialog::useAvailableBalance(SendCoinsEntry* entry)
795794
m_coin_control->fAllowWatchOnly = model->wallet().privateKeysDisabled() && !model->wallet().hasExternalSigner();
796795

797796
// Calculate available amount to send.
798-
CAmount amount = model->wallet().getAvailableBalance(*m_coin_control);
797+
CAmount amount = model->getAvailableBalance(m_coin_control.get());
799798
for (int i = 0; i < ui->entries->count(); ++i) {
800799
SendCoinsEntry* e = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
801800
if (e && !e->isHidden() && e != entry) {

src/qt/walletmodel.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,9 @@ WalletModel::SendCoinsReturn WalletModel::prepareTransaction(WalletModelTransact
209209
return DuplicateAddress;
210210
}
211211

212-
CAmount nBalance = m_wallet->getAvailableBalance(coinControl);
212+
// If no coin was manually selected, use the cached balance
213+
// Future: can merge this call with 'createTransaction'.
214+
CAmount nBalance = getAvailableBalance(&coinControl);
213215

214216
if(total > nBalance)
215217
{
@@ -614,3 +616,7 @@ uint256 WalletModel::getLastBlockProcessed() const
614616
{
615617
return m_client_model ? m_client_model->getBestBlockHash() : uint256{};
616618
}
619+
620+
CAmount WalletModel::getAvailableBalance(const CCoinControl* control) {
621+
return control && control->HasSelected() ? wallet().getAvailableBalance(*control) : getCachedBalance().balance;
622+
}

src/qt/walletmodel.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ class WalletModel : public QObject
161161
// Retrieve the cached wallet balance
162162
interfaces::WalletBalances getCachedBalance() const;
163163

164+
// If coin control has selected outputs, searches the total amount inside the wallet.
165+
// Otherwise, uses the wallet's cached available balance.
166+
CAmount getAvailableBalance(const wallet::CCoinControl* control);
167+
164168
private:
165169
std::unique_ptr<interfaces::Wallet> m_wallet;
166170
std::unique_ptr<interfaces::Handler> m_handler_unload;

0 commit comments

Comments
 (0)