Skip to content

Commit 050e8b1

Browse files
committed
GUI: 'getAvailableBalance', use 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 96e3264 commit 050e8b1

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

src/qt/sendcoinsdialog.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ void SendCoinsDialog::useAvailableBalance(SendCoinsEntry* entry)
795795
m_coin_control->fAllowWatchOnly = model->wallet().privateKeysDisabled() && !model->wallet().hasExternalSigner();
796796

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

src/qt/walletmodel.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,9 @@ WalletModel::SendCoinsReturn WalletModel::prepareTransaction(WalletModelTransact
203203
return DuplicateAddress;
204204
}
205205

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

208210
if(total > nBalance)
209211
{
@@ -608,3 +610,8 @@ uint256 WalletModel::getLastBlockProcessed() const
608610
{
609611
return m_client_model ? m_client_model->getBestBlockHash() : uint256{};
610612
}
613+
614+
CAmount WalletModel::getAvailableBalance(const CCoinControl* control)
615+
{
616+
return control && control->HasSelected() ? wallet().getAvailableBalance(*control) : getCachedBalance().balance;
617+
}

src/qt/walletmodel.h

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

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

0 commit comments

Comments
 (0)