From 018ac61054a9f286de24c1407ab57b96458719dc Mon Sep 17 00:00:00 2001 From: woodser Date: Thu, 26 Dec 2024 09:07:02 -0500 Subject: [PATCH] show reserved balance for offer funding subaddresses and reset if unused --- .../tasks/MakerReserveOfferFunds.java | 14 ++++++++- .../core/xmr/wallet/XmrWalletService.java | 31 +++++++++++++------ .../main/funds/deposit/DepositListItem.java | 2 +- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/core/src/main/java/haveno/core/offer/placeoffer/tasks/MakerReserveOfferFunds.java b/core/src/main/java/haveno/core/offer/placeoffer/tasks/MakerReserveOfferFunds.java index 4b44a8102d1..a33b9b836d3 100644 --- a/core/src/main/java/haveno/core/offer/placeoffer/tasks/MakerReserveOfferFunds.java +++ b/core/src/main/java/haveno/core/offer/placeoffer/tasks/MakerReserveOfferFunds.java @@ -33,6 +33,7 @@ import lombok.extern.slf4j.Slf4j; import monero.common.MoneroRpcConnection; import monero.daemon.model.MoneroOutput; +import monero.wallet.model.MoneroOutputWallet; import monero.wallet.model.MoneroTxWallet; @Slf4j @@ -62,7 +63,6 @@ protected void run() { model.getXmrWalletService().getXmrConnectionService().verifyConnection(); // create reserve tx - MoneroTxWallet reserveTx = null; synchronized (HavenoUtils.xmrWalletService.getWalletLock()) { // reset protocol timeout @@ -79,6 +79,7 @@ protected void run() { Integer preferredSubaddressIndex = fundingEntry == null ? null : fundingEntry.getSubaddressIndex(); // attempt creating reserve tx + MoneroTxWallet reserveTx = null; try { synchronized (HavenoUtils.getWalletFunctionLock()) { for (int i = 0; i < TradeProtocol.MAX_ATTEMPTS; i++) { @@ -121,6 +122,17 @@ protected void run() { openOffer.setReserveTxHex(reserveTx.getFullHex()); openOffer.setReserveTxKey(reserveTx.getKey()); offer.getOfferPayload().setReserveTxKeyImages(reservedKeyImages); + + // reset offer funding address entry if unused + List inputs = model.getXmrWalletService().getOutputs(reservedKeyImages); + boolean usesFundingEntry = false; + for (MoneroOutputWallet input : inputs) { + if (input.getAccountIndex() == 0 && input.getSubaddressIndex() == fundingEntry.getSubaddressIndex()) { + usesFundingEntry = true; + break; + } + } + if (!usesFundingEntry) model.getXmrWalletService().swapAddressEntryToAvailable(offer.getId(), XmrAddressEntry.Context.OFFER_FUNDING); } complete(); } catch (Throwable t) { diff --git a/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java b/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java index 79e9248c64c..ccd8d491ce2 100644 --- a/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java +++ b/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java @@ -578,15 +578,6 @@ public void thawOutputs(Collection keyImages) { } } - public BigInteger getOutputsAmount(Collection keyImages) { - BigInteger sum = BigInteger.ZERO; - for (String keyImage : keyImages) { - List outputs = getOutputs(new MoneroOutputQuery().setIsSpent(false).setKeyImage(new MoneroKeyImage(keyImage))); - if (!outputs.isEmpty()) sum = sum.add(outputs.get(0).getAmount()); - } - return sum; - } - private List getSubaddressesWithExactInput(BigInteger amount) { // fetch unspent, unfrozen, unlocked outputs @@ -1125,6 +1116,15 @@ public BigInteger getBalanceForSubaddress(int subaddressIndex) { return subaddress == null ? BigInteger.ZERO : subaddress.getBalance(); } + public BigInteger getBalanceForSubaddress(int subaddressIndex, boolean includeFrozen) { + return getBalanceForSubaddress(subaddressIndex).add(includeFrozen ? getFrozenBalanceForSubaddress(subaddressIndex) : BigInteger.ZERO); + } + + public BigInteger getFrozenBalanceForSubaddress(int subaddressIndex) { + List outputs = getOutputs(new MoneroOutputQuery().setIsFrozen(true).setIsSpent(false).setAccountIndex(0).setSubaddressIndex(subaddressIndex)); + return outputs.stream().map(output -> output.getAmount()).reduce(BigInteger.ZERO, BigInteger::add); + } + public BigInteger getAvailableBalanceForSubaddress(int subaddressIndex) { MoneroSubaddress subaddress = getSubaddress(subaddressIndex); return subaddress == null ? BigInteger.ZERO : subaddress.getUnlockedBalance(); @@ -1250,6 +1250,19 @@ public List getOutputs(MoneroOutputQuery query) { return filteredOutputs; } + public List getOutputs(Collection keyImages) { + List outputs = new ArrayList(); + for (String keyImage : keyImages) { + List outputList = getOutputs(new MoneroOutputQuery().setIsSpent(false).setKeyImage(new MoneroKeyImage(keyImage))); + if (!outputList.isEmpty()) outputs.add(outputList.get(0)); + } + return outputs; + } + + public BigInteger getOutputsAmount(Collection keyImages) { + return getOutputs(keyImages).stream().map(output -> output.getAmount()).reduce(BigInteger.ZERO, BigInteger::add); + } + /////////////////////////////////////////////////////////////////////////////////////////// // Util /////////////////////////////////////////////////////////////////////////////////////////// diff --git a/desktop/src/main/java/haveno/desktop/main/funds/deposit/DepositListItem.java b/desktop/src/main/java/haveno/desktop/main/funds/deposit/DepositListItem.java index 180b883d61b..9e21f19b5fd 100644 --- a/desktop/src/main/java/haveno/desktop/main/funds/deposit/DepositListItem.java +++ b/desktop/src/main/java/haveno/desktop/main/funds/deposit/DepositListItem.java @@ -60,7 +60,7 @@ private LazyFields lazy() { this.xmrWalletService = xmrWalletService; this.addressEntry = addressEntry; - balanceAsBI = xmrWalletService.getBalanceForSubaddress(addressEntry.getSubaddressIndex()); + balanceAsBI = xmrWalletService.getBalanceForSubaddress(addressEntry.getSubaddressIndex(), true); balance.set(HavenoUtils.formatXmr(balanceAsBI)); updateUsage(addressEntry.getSubaddressIndex());