2024 12 11 issue 5625 (#5803)

* wallet: Get UTXOHandlingApi.listUtxos() to be account specific

* wallet: Add UTXOHandlingApi.{getBalance(), getUnconfirmedBalance(), getConfirmedBalance(), getBalance(account), getUnconfirmedBalance(account), getConfirmedBalance(account)}

* wallet: Remove listDefaultAccountUtxos(), use listUtxos() instead
This commit is contained in:
Chris Stewart 2024-12-11 14:26:22 -06:00 committed by GitHub
parent eb6edab240
commit d29dad4472
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 57 additions and 16 deletions

View file

@ -33,6 +33,19 @@ trait UtxoHandlingApi {
findByOutPoints(Vector(outPoint)).map(_.headOption)
}
/** Get total wallet balance for the default HD account */
def getBalance(): Future[CurrencyUnit]
/** Get total confirmed wallet balance for the default HD account */
def getConfirmedBalance(): Future[CurrencyUnit]
/** Get total unconfirmed wallet balance for the default HD account */
def getUnconfirmedBalance(): Future[CurrencyUnit]
def getBalance(account: HDAccount): Future[CurrencyUnit]
def getConfirmedBalance(account: HDAccount): Future[CurrencyUnit]
def getUnconfirmedBalance(account: HDAccount): Future[CurrencyUnit]
def getUnconfirmedBalance(tag: AddressTag): Future[CurrencyUnit]
def getConfirmedBalance(tag: AddressTag): Future[CurrencyUnit]
final def getBalance(tag: AddressTag)(implicit
@ -45,8 +58,6 @@ trait UtxoHandlingApi {
} yield u + c
}
def listDefaultAccountUtxos(): Future[Vector[SpendingInfoDb]]
/** Lists unspent transaction outputs in the wallet
* @return
* Vector[SpendingInfoDb]

View file

@ -61,7 +61,7 @@ class NeutrinoNodeWithWalletTest extends NodeTestWithCachedBitcoindNewest {
for {
balance <- wallet.getBalance()
addresses <- wallet.addressHandling.listAddresses()
utxos <- wallet.utxoHandling.listDefaultAccountUtxos()
utxos <- wallet.utxoHandling.listUtxos()
} yield {
// +- fee rate because signatures could vary in size
(expectedBalance === balance +- FeeRate.currencyUnit) &&
@ -175,7 +175,7 @@ class NeutrinoNodeWithWalletTest extends NodeTestWithCachedBitcoindNewest {
rescan <- wallet.isRescanning()
balance <- wallet.getBalance()
addresses <- wallet.addressHandling.listAddresses()
utxos <- wallet.utxoHandling.listDefaultAccountUtxos()
utxos <- wallet.utxoHandling.listUtxos()
spks = utxos
.map(_.output.scriptPubKey)
} yield {
@ -188,7 +188,7 @@ class NeutrinoNodeWithWalletTest extends NodeTestWithCachedBitcoindNewest {
for {
addresses <- wallet.addressHandling.listAddresses()
utxos <- wallet.utxoHandling.listDefaultAccountUtxos()
utxos <- wallet.utxoHandling.listUtxos()
_ = assert(addresses.size == 6)
_ = assert(utxos.size == 3)
@ -198,7 +198,7 @@ class NeutrinoNodeWithWalletTest extends NodeTestWithCachedBitcoindNewest {
.sendToAddress(address, TestAmount)
addresses <- wallet.addressHandling.listAddresses()
utxos <- wallet.utxoHandling.listDefaultAccountUtxos()
utxos <- wallet.utxoHandling.listUtxos()
_ = assert(addresses.size == 7)
_ = assert(utxos.size == 3)
_ <-
@ -214,7 +214,7 @@ class NeutrinoNodeWithWalletTest extends NodeTestWithCachedBitcoindNewest {
})
_ <- wallet.utxoHandling.clearAllUtxos()
addresses <- wallet.addressHandling.listAddresses()
utxos <- wallet.utxoHandling.listDefaultAccountUtxos()
utxos <- wallet.utxoHandling.listUtxos()
_ = assert(addresses.nonEmpty)
_ = assert(utxos.isEmpty)

View file

@ -246,15 +246,15 @@ case class Wallet(
override def getBalance()(implicit
ec: ExecutionContext
): Future[CurrencyUnit] = {
safeDatabase.run(spendingInfoDAO.getBalanceAction())
utxoHandling.getBalance()
}
override def getConfirmedBalance(): Future[CurrencyUnit] = {
safeDatabase.run(spendingInfoDAO.getConfirmedBalanceAction())
utxoHandling.getConfirmedBalance()
}
override def getUnconfirmedBalance(): Future[CurrencyUnit] = {
safeDatabase.run(spendingInfoDAO.getUnconfirmedBalanceAction())
utxoHandling.getUnconfirmedBalance()
}
override def getWalletName(): Future[String] = {

View file

@ -106,11 +106,8 @@ case class UtxoHandling(
/** @inheritdoc */
override def listUtxos(): Future[Vector[SpendingInfoDb]] = {
spendingInfoDAO.findAllUnspent()
}
def listDefaultAccountUtxos(): Future[Vector[SpendingInfoDb]] =
listUtxos(walletConfig.defaultAccount)
}
def listUtxos(
hdAccount: HDAccount
@ -128,7 +125,7 @@ case class UtxoHandling(
}
override def listUtxos(tag: AddressTag): Future[Vector[SpendingInfoDb]] = {
spendingInfoDAO.findAllUnspentForTag(tag)
listUtxos(walletConfig.defaultAccount, tag)
}
override def listUtxos(
@ -143,7 +140,7 @@ case class UtxoHandling(
}
override def listUtxos(state: TxoState): Future[Vector[SpendingInfoDb]] = {
spendingInfoDAO.findByTxoState(state)
listUtxos(walletConfig.defaultAccount, state)
}
override def listUtxos(
@ -490,6 +487,39 @@ case class UtxoHandling(
)
utxoF
}
/** Get total wallet balance for the default HD account */
override def getBalance(): Future[CurrencyUnit] = {
getBalance(walletConfig.defaultAccount)
}
/** Get total confirmed wallet balance for the default HD account */
override def getConfirmedBalance(): Future[CurrencyUnit] = {
getConfirmedBalance(walletConfig.defaultAccount)
}
/** Get total unconfirmed wallet balance for the default HD account */
override def getUnconfirmedBalance(): Future[CurrencyUnit] = {
getUnconfirmedBalance(walletConfig.defaultAccount)
}
override def getBalance(account: HDAccount): Future[CurrencyUnit] = {
val action = spendingInfoDAO.getBalanceAction(accountOpt = Some(account))
safeDatabase.run(action)
}
override def getConfirmedBalance(account: HDAccount): Future[CurrencyUnit] = {
val action =
spendingInfoDAO.getConfirmedBalanceAction(accountOpt = Some(account))
safeDatabase.run(action)
}
override def getUnconfirmedBalance(
account: HDAccount): Future[CurrencyUnit] = {
val action =
spendingInfoDAO.getUnconfirmedBalanceAction(accountOpt = Some(account))
safeDatabase.run(action)
}
}
object UtxoHandling {