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 4b44a810..a33b9b83 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 haveno.core.xmr.model.XmrAddressEntry; 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 @@ public class MakerReserveOfferFunds extends Task { model.getXmrWalletService().getXmrConnectionService().verifyConnection(); // create reserve tx - MoneroTxWallet reserveTx = null; synchronized (HavenoUtils.xmrWalletService.getWalletLock()) { // reset protocol timeout @@ -79,6 +79,7 @@ public class MakerReserveOfferFunds extends Task { 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 @@ public class MakerReserveOfferFunds extends Task { 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 79e9248c..ccd8d491 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 class XmrWalletService extends XmrWalletBase { } } - 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 class XmrWalletService extends XmrWalletBase { 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 class XmrWalletService extends XmrWalletBase { 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 180b883d..9e21f19b 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 @@ class DepositListItem { 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());