From ab0b9e3b77abd0e0f4f258dcc309caa208bca872 Mon Sep 17 00:00:00 2001 From: woodser Date: Mon, 6 Mar 2023 19:38:57 -0500 Subject: [PATCH] transition Balances to use native xmr atomic units --- .../bisq/core/api/CoreWalletsService.java | 32 ++----------------- .../src/main/java/bisq/core/btc/Balances.java | 28 ++++++++-------- .../bisq/core/offer/OpenOfferManager.java | 4 +-- .../presentation/BalancePresentation.java | 19 +++-------- .../transactions/TransactionsListItem.java | 4 +-- 5 files changed, 24 insertions(+), 63 deletions(-) diff --git a/core/src/main/java/bisq/core/api/CoreWalletsService.java b/core/src/main/java/bisq/core/api/CoreWalletsService.java index 4ad5e48cbf..0c4ceb5659 100644 --- a/core/src/main/java/bisq/core/api/CoreWalletsService.java +++ b/core/src/main/java/bisq/core/api/CoreWalletsService.java @@ -133,16 +133,14 @@ class CoreWalletsService { verifyWalletCurrencyCodeIsValid(currencyCode); verifyWalletsAreAvailable(); verifyEncryptedWalletIsUnlocked(); - if (balances.getAvailableBalance().get() == null) - throw new IllegalStateException("balance is not yet available"); + if (balances.getAvailableBalance().get() == null) throw new IllegalStateException("balance is not yet available"); switch (currencyCode.trim().toUpperCase()) { - case "BTC": - return new BalancesInfo(getBtcBalances(), XmrBalanceInfo.EMPTY); + case "": case "XMR": return new BalancesInfo(BtcBalanceInfo.EMPTY, getXmrBalances()); default: - return new BalancesInfo(getBtcBalances(), getXmrBalances()); + throw new IllegalStateException("Unsupported currency code: " + currencyCode.trim().toUpperCase()); } } @@ -472,30 +470,6 @@ class CoreWalletsService { } } - - // TODO (woodser): delete this since it's serving XMR balances - private BtcBalanceInfo getBtcBalances() { - verifyWalletsAreAvailable(); - verifyEncryptedWalletIsUnlocked(); - - var availableBalance = balances.getAvailableBalance().get(); - if (availableBalance == null) - throw new IllegalStateException("balance is not yet available"); - - var reservedBalance = balances.getReservedBalance().get(); - if (reservedBalance == null) - throw new IllegalStateException("reserved balance is not yet available"); - - var pendingBalance = balances.getPendingBalance().get(); - if (pendingBalance == null) - throw new IllegalStateException("locked balance is not yet available"); - - return new BtcBalanceInfo(availableBalance.value, - reservedBalance.value, - availableBalance.add(reservedBalance).value, - pendingBalance.value); - } - private XmrBalanceInfo getXmrBalances() { verifyWalletsAreAvailable(); verifyEncryptedWalletIsUnlocked(); diff --git a/core/src/main/java/bisq/core/btc/Balances.java b/core/src/main/java/bisq/core/btc/Balances.java index f52c41a3c4..29590784a8 100644 --- a/core/src/main/java/bisq/core/btc/Balances.java +++ b/core/src/main/java/bisq/core/btc/Balances.java @@ -25,7 +25,6 @@ import bisq.core.offer.OpenOfferManager; import bisq.core.support.dispute.Dispute; import bisq.core.support.dispute.refund.RefundManager; import bisq.core.trade.ClosedTradableManager; -import bisq.core.trade.HavenoUtils; import bisq.core.trade.Trade; import bisq.core.trade.TradeManager; import bisq.core.trade.failed.FailedTradesManager; @@ -43,7 +42,6 @@ import monero.common.MoneroError; import monero.wallet.model.MoneroOutputQuery; import monero.wallet.model.MoneroOutputWallet; import monero.wallet.model.MoneroTxWallet; -import org.bitcoinj.core.Coin; @Slf4j public class Balances { @@ -53,15 +51,15 @@ public class Balances { private final RefundManager refundManager; @Getter - private final ObjectProperty availableBalance = new SimpleObjectProperty<>(); + private final ObjectProperty availableBalance = new SimpleObjectProperty<>(); @Getter - private final ObjectProperty pendingBalance = new SimpleObjectProperty<>(); + private final ObjectProperty pendingBalance = new SimpleObjectProperty<>(); @Getter - private final ObjectProperty reservedOfferBalance = new SimpleObjectProperty<>(); + private final ObjectProperty reservedOfferBalance = new SimpleObjectProperty<>(); @Getter - private final ObjectProperty reservedTradeBalance = new SimpleObjectProperty<>(); + private final ObjectProperty reservedTradeBalance = new SimpleObjectProperty<>(); @Getter - private final ObjectProperty reservedBalance = new SimpleObjectProperty<>(); // TODO (woodser): this balance is sum of reserved funds for offers and trade multisigs; remove? + private final ObjectProperty reservedBalance = new SimpleObjectProperty<>(); // TODO (woodser): this balance is sum of reserved funds for offers and trade multisigs; remove? @Inject public Balances(TradeManager tradeManager, @@ -105,26 +103,26 @@ public class Balances { // TODO (woodser): converting to long should generally be avoided since can lose precision, but in practice these amounts are below max value private void updateAvailableBalance() { - availableBalance.set(Coin.valueOf(xmrWalletService.getWallet() == null ? 0 : xmrWalletService.getWallet().getUnlockedBalance(0).longValueExact())); + availableBalance.set(xmrWalletService.getWallet() == null ? BigInteger.valueOf(0) : xmrWalletService.getWallet().getUnlockedBalance(0)); } private void updatePendingBalance() { - BigInteger balance = xmrWalletService.getWallet() == null ? new BigInteger("0") : xmrWalletService.getWallet().getBalance(0); - BigInteger unlockedBalance = xmrWalletService.getWallet() == null ? new BigInteger("0") : xmrWalletService.getWallet().getUnlockedBalance(0); - pendingBalance.set(Coin.valueOf(balance.subtract(unlockedBalance).longValueExact())); + BigInteger balance = xmrWalletService.getWallet() == null ? BigInteger.valueOf(0) : xmrWalletService.getWallet().getBalance(0); + BigInteger unlockedBalance = xmrWalletService.getWallet() == null ? BigInteger.valueOf(0) : xmrWalletService.getWallet().getUnlockedBalance(0); + pendingBalance.set(balance.subtract(unlockedBalance)); } private void updateReservedOfferBalance() { - Coin sum = Coin.valueOf(0); + BigInteger sum = BigInteger.valueOf(0); if (xmrWalletService.getWallet() != null) { List frozenOutputs = xmrWalletService.getWallet().getOutputs(new MoneroOutputQuery().setIsFrozen(true).setIsSpent(false)); - for (MoneroOutputWallet frozenOutput : frozenOutputs) sum = sum.add(Coin.valueOf(frozenOutput.getAmount().longValueExact())); + for (MoneroOutputWallet frozenOutput : frozenOutputs) sum = sum.add(frozenOutput.getAmount()); } reservedOfferBalance.set(sum); } private void updateReservedTradeBalance() { - Coin sum = Coin.valueOf(0); + BigInteger sum = BigInteger.valueOf(0); List openTrades = tradeManager.getTradesStreamWithFundsLockedIn().collect(Collectors.toList()); for (Trade trade : openTrades) { try { @@ -141,7 +139,7 @@ public class Balances { } else { reservedAmt = trade.getContract().isMyRoleBuyer(tradeManager.getKeyRing().getPubKeyRing()) ? offerPayload.getBuyerSecurityDeposit() : offerPayload.getAmount() + offerPayload.getSellerSecurityDeposit(); } - sum = sum.add(Coin.valueOf(HavenoUtils.centinerosToAtomicUnits(reservedAmt).longValueExact())); + sum = sum.add(BigInteger.valueOf(reservedAmt)); } reservedTradeBalance.set(sum); } diff --git a/core/src/main/java/bisq/core/offer/OpenOfferManager.java b/core/src/main/java/bisq/core/offer/OpenOfferManager.java index 9e867e13a4..5833f72324 100644 --- a/core/src/main/java/bisq/core/offer/OpenOfferManager.java +++ b/core/src/main/java/bisq/core/offer/OpenOfferManager.java @@ -831,7 +831,7 @@ public class OpenOfferManager implements PeerManager.Listener, DecryptedDirectMe // get earliest unscheduled txs with sufficient incoming amount List scheduledTxHashes = new ArrayList(); - BigInteger scheduledAmount = new BigInteger("0"); + BigInteger scheduledAmount = BigInteger.valueOf(0); for (MoneroTxWallet lockedTx : lockedTxs) { if (isTxScheduled(openOffers, lockedTx.getHash())) continue; if (lockedTx.getIncomingTransfers() == null || lockedTx.getIncomingTransfers().isEmpty()) continue; @@ -859,7 +859,7 @@ public class OpenOfferManager implements PeerManager.Listener, DecryptedDirectMe } private BigInteger getScheduledAmount(List openOffers) { - BigInteger scheduledAmount = new BigInteger("0"); + BigInteger scheduledAmount = BigInteger.valueOf(0); for (OpenOffer openOffer : openOffers) { if (openOffer.getState() != OpenOffer.State.SCHEDULED) continue; if (openOffer.getScheduledTxHashes() == null) continue; diff --git a/core/src/main/java/bisq/core/presentation/BalancePresentation.java b/core/src/main/java/bisq/core/presentation/BalancePresentation.java index deae1b53af..d064f37dc3 100644 --- a/core/src/main/java/bisq/core/presentation/BalancePresentation.java +++ b/core/src/main/java/bisq/core/presentation/BalancePresentation.java @@ -19,20 +19,18 @@ package bisq.core.presentation; import bisq.common.UserThread; import bisq.core.btc.Balances; +import bisq.core.trade.HavenoUtils; import javax.inject.Inject; import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.StringProperty; -import java.math.BigInteger; - import lombok.Getter; import lombok.extern.slf4j.Slf4j; @Slf4j public class BalancePresentation { - private static final BigInteger AU_PER_XMR = new BigInteger("1000000000000"); @Getter private final StringProperty availableBalance = new SimpleStringProperty(); @@ -44,22 +42,13 @@ public class BalancePresentation { @Inject public BalancePresentation(Balances balances) { balances.getAvailableBalance().addListener((observable, oldValue, newValue) -> { - UserThread.execute(() -> availableBalance.set(longToXmr(newValue.value))); + UserThread.execute(() -> availableBalance.set(HavenoUtils.formatToXmrWithCode(newValue))); }); balances.getPendingBalance().addListener((observable, oldValue, newValue) -> { - UserThread.execute(() -> pendingBalance.set(longToXmr(newValue.value))); + UserThread.execute(() -> pendingBalance.set(HavenoUtils.formatToXmrWithCode(newValue))); }); balances.getReservedBalance().addListener((observable, oldValue, newValue) -> { - UserThread.execute(() -> reservedBalance.set(longToXmr(newValue.value))); + UserThread.execute(() -> reservedBalance.set(HavenoUtils.formatToXmrWithCode(newValue))); }); } - - // TODO: truncate full precision with ellipses to not break layout? - // TODO (woodser): formatting utils in monero-java - private static String longToXmr(long amt) { - BigInteger auAmt = BigInteger.valueOf(amt); - BigInteger[] quotientAndRemainder = auAmt.divideAndRemainder(AU_PER_XMR); - double decimalRemainder = quotientAndRemainder[1].doubleValue() / AU_PER_XMR.doubleValue(); - return quotientAndRemainder[0].doubleValue() + decimalRemainder + " XMR"; - } } diff --git a/desktop/src/main/java/bisq/desktop/main/funds/transactions/TransactionsListItem.java b/desktop/src/main/java/bisq/desktop/main/funds/transactions/TransactionsListItem.java index 2c0738fda4..47cdb73d69 100644 --- a/desktop/src/main/java/bisq/desktop/main/funds/transactions/TransactionsListItem.java +++ b/desktop/src/main/java/bisq/desktop/main/funds/transactions/TransactionsListItem.java @@ -85,8 +85,8 @@ class TransactionsListItem { Optional optionalTradable = Optional.ofNullable(transactionAwareTradable) .map(TransactionAwareTradable::asTradable); - BigInteger valueSentToMe = tx.getIncomingAmount() == null ? new BigInteger("0") : tx.getIncomingAmount(); - BigInteger valueSentFromMe = tx.getOutgoingAmount() == null ? new BigInteger("0") : tx.getOutgoingAmount(); + BigInteger valueSentToMe = tx.getIncomingAmount() == null ? BigInteger.valueOf(0) : tx.getIncomingAmount(); + BigInteger valueSentFromMe = tx.getOutgoingAmount() == null ? BigInteger.valueOf(0) : tx.getOutgoingAmount(); if (tx.getTransfers().get(0).isIncoming()) { addressString = ((MoneroIncomingTransfer) tx.getTransfers().get(0)).getAddress();