Skip to content

Commit

Permalink
show reserved balance for offer funding subaddresses and reset if unused
Browse files Browse the repository at this point in the history
  • Loading branch information
woodser committed Dec 27, 2024
1 parent ed87b36 commit 018ac61
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -62,7 +63,6 @@ protected void run() {
model.getXmrWalletService().getXmrConnectionService().verifyConnection();

// create reserve tx
MoneroTxWallet reserveTx = null;
synchronized (HavenoUtils.xmrWalletService.getWalletLock()) {

// reset protocol timeout
Expand All @@ -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++) {
Expand Down Expand Up @@ -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<MoneroOutputWallet> 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) {
Expand Down
31 changes: 22 additions & 9 deletions core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java
Original file line number Diff line number Diff line change
Expand Up @@ -578,15 +578,6 @@ public void thawOutputs(Collection<String> keyImages) {
}
}

public BigInteger getOutputsAmount(Collection<String> keyImages) {
BigInteger sum = BigInteger.ZERO;
for (String keyImage : keyImages) {
List<MoneroOutputWallet> outputs = getOutputs(new MoneroOutputQuery().setIsSpent(false).setKeyImage(new MoneroKeyImage(keyImage)));
if (!outputs.isEmpty()) sum = sum.add(outputs.get(0).getAmount());
}
return sum;
}

private List<Integer> getSubaddressesWithExactInput(BigInteger amount) {

// fetch unspent, unfrozen, unlocked outputs
Expand Down Expand Up @@ -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<MoneroOutputWallet> 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();
Expand Down Expand Up @@ -1250,6 +1250,19 @@ public List<MoneroOutputWallet> getOutputs(MoneroOutputQuery query) {
return filteredOutputs;
}

public List<MoneroOutputWallet> getOutputs(Collection<String> keyImages) {
List<MoneroOutputWallet> outputs = new ArrayList<MoneroOutputWallet>();
for (String keyImage : keyImages) {
List<MoneroOutputWallet> outputList = getOutputs(new MoneroOutputQuery().setIsSpent(false).setKeyImage(new MoneroKeyImage(keyImage)));
if (!outputList.isEmpty()) outputs.add(outputList.get(0));
}
return outputs;
}

public BigInteger getOutputsAmount(Collection<String> keyImages) {
return getOutputs(keyImages).stream().map(output -> output.getAmount()).reduce(BigInteger.ZERO, BigInteger::add);
}

///////////////////////////////////////////////////////////////////////////////////////////
// Util
///////////////////////////////////////////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit 018ac61

Please sign in to comment.