From f11c816cda7225e87d27d19e9db0222129f32ede Mon Sep 17 00:00:00 2001 From: woodser Date: Thu, 15 Dec 2022 21:38:52 +0000 Subject: [PATCH] enforce trade limit over grpc --- .../bisq/core/api/CorePaymentAccountsService.java | 4 ++++ .../main/java/bisq/core/offer/OpenOfferManager.java | 12 +++++++++--- .../bisq/core/offer/placeoffer/PlaceOfferModel.java | 7 ++++++- .../core/offer/placeoffer/tasks/ValidateOffer.java | 7 ++++--- core/src/main/java/bisq/core/trade/Trade.java | 2 -- .../java/bisq/core/offer/OpenOfferManagerTest.java | 9 ++++++--- 6 files changed, 29 insertions(+), 12 deletions(-) diff --git a/core/src/main/java/bisq/core/api/CorePaymentAccountsService.java b/core/src/main/java/bisq/core/api/CorePaymentAccountsService.java index 456e9f745d..2f07eb44a1 100644 --- a/core/src/main/java/bisq/core/api/CorePaymentAccountsService.java +++ b/core/src/main/java/bisq/core/api/CorePaymentAccountsService.java @@ -96,6 +96,10 @@ class CorePaymentAccountsService { } } + PaymentAccount getPaymentAccount(String paymentAccountId) { + return user.getPaymentAccount(paymentAccountId); + } + Set getPaymentAccounts() { return user.getPaymentAccounts(); } diff --git a/core/src/main/java/bisq/core/offer/OpenOfferManager.java b/core/src/main/java/bisq/core/offer/OpenOfferManager.java index 65ce022a27..2987206e24 100644 --- a/core/src/main/java/bisq/core/offer/OpenOfferManager.java +++ b/core/src/main/java/bisq/core/offer/OpenOfferManager.java @@ -17,6 +17,7 @@ package bisq.core.offer; +import bisq.core.account.witness.AccountAgeWitnessService; import bisq.core.api.CoreContext; import bisq.core.api.CoreMoneroConnectionsService; import bisq.core.btc.wallet.BtcWalletService; @@ -136,6 +137,8 @@ public class OpenOfferManager implements PeerManager.Listener, DecryptedDirectMe private Timer periodicRepublishOffersTimer, periodicRefreshOffersTimer, retryRepublishOffersTimer; @Getter private final ObservableList> invalidOffers = FXCollections.observableArrayList(); + @Getter + private final AccountAgeWitnessService accountAgeWitnessService; /////////////////////////////////////////////////////////////////////////////////////////// @@ -161,7 +164,8 @@ public class OpenOfferManager implements PeerManager.Listener, DecryptedDirectMe FilterManager filterManager, Broadcaster broadcaster, PersistenceManager> persistenceManager, - PersistenceManager signedOfferPersistenceManager) { + PersistenceManager signedOfferPersistenceManager, + AccountAgeWitnessService accountAgeWitnessService) { this.coreContext = coreContext; this.keyRing = keyRing; this.user = user; @@ -181,6 +185,7 @@ public class OpenOfferManager implements PeerManager.Listener, DecryptedDirectMe this.broadcaster = broadcaster; this.persistenceManager = persistenceManager; this.signedOfferPersistenceManager = signedOfferPersistenceManager; + this.accountAgeWitnessService = accountAgeWitnessService; this.persistenceManager.initialize(openOffers, "OpenOffers", PersistenceManager.Source.PRIVATE); this.signedOfferPersistenceManager.initialize(signedOffers, "SignedOffers", PersistenceManager.Source.PRIVATE); // arbitrator stores reserve tx for signed offers @@ -741,7 +746,7 @@ public class OpenOfferManager implements PeerManager.Listener, DecryptedDirectMe } private void signAndPostOffer(OpenOffer openOffer, - Coin offerReserveAmount, // TODO: switch to BigInteger + Coin offerReserveAmount, boolean useSavingsWallet, // TODO: remove this TransactionResultHandler resultHandler, ErrorMessageHandler errorMessageHandler) { log.info("Signing and posting offer " + openOffer.getId()); @@ -760,7 +765,8 @@ public class OpenOfferManager implements PeerManager.Listener, DecryptedDirectMe tradeStatisticsManager, user, keyRing, - filterManager); + filterManager, + accountAgeWitnessService); // create protocol PlaceOfferProtocol placeOfferProtocol = new PlaceOfferProtocol(model, diff --git a/core/src/main/java/bisq/core/offer/placeoffer/PlaceOfferModel.java b/core/src/main/java/bisq/core/offer/placeoffer/PlaceOfferModel.java index 53672d6717..948e6dfc82 100644 --- a/core/src/main/java/bisq/core/offer/placeoffer/PlaceOfferModel.java +++ b/core/src/main/java/bisq/core/offer/placeoffer/PlaceOfferModel.java @@ -17,6 +17,7 @@ package bisq.core.offer.placeoffer; +import bisq.core.account.witness.AccountAgeWitnessService; import bisq.core.btc.wallet.BtcWalletService; import bisq.core.btc.wallet.TradeWalletService; import bisq.core.btc.wallet.XmrWalletService; @@ -63,6 +64,8 @@ public class PlaceOfferModel implements Model { private final KeyRing keyRing; @Getter private final FilterManager filterManager; + @Getter + private final AccountAgeWitnessService accountAgeWitnessService; // Mutable @Setter @@ -87,7 +90,8 @@ public class PlaceOfferModel implements Model { TradeStatisticsManager tradeStatisticsManager, User user, KeyRing keyRing, - FilterManager filterManager) { + FilterManager filterManager, + AccountAgeWitnessService accountAgeWitnessService) { this.offer = offer; this.reservedFundsForOffer = reservedFundsForOffer; this.useSavingsWallet = useSavingsWallet; @@ -102,6 +106,7 @@ public class PlaceOfferModel implements Model { this.user = user; this.keyRing = keyRing; this.filterManager = filterManager; + this.accountAgeWitnessService = accountAgeWitnessService; } @Override diff --git a/core/src/main/java/bisq/core/offer/placeoffer/tasks/ValidateOffer.java b/core/src/main/java/bisq/core/offer/placeoffer/tasks/ValidateOffer.java index 1cd51c4d80..65e6182918 100644 --- a/core/src/main/java/bisq/core/offer/placeoffer/tasks/ValidateOffer.java +++ b/core/src/main/java/bisq/core/offer/placeoffer/tasks/ValidateOffer.java @@ -19,6 +19,7 @@ package bisq.core.offer.placeoffer.tasks; import bisq.core.offer.Offer; import bisq.core.offer.placeoffer.PlaceOfferModel; +import bisq.core.trade.HavenoUtils; import bisq.core.trade.messages.TradeMessage; import bisq.common.taskrunner.Task; @@ -65,8 +66,9 @@ public class ValidateOffer extends Task { /*checkArgument(offer.getMinAmount().compareTo(ProposalConsensus.getMinTradeAmount()) >= 0, "MinAmount is less than " + ProposalConsensus.getMinTradeAmount().toFriendlyString());*/ - checkArgument(offer.getAmount().compareTo(offer.getPaymentMethod().getMaxTradeLimitAsCoin(offer.getCurrencyCode())) <= 0, - "Amount is larger than " + offer.getPaymentMethod().getMaxTradeLimitAsCoin(offer.getCurrencyCode()).toFriendlyString()); + long maxAmount = model.getAccountAgeWitnessService().getMyTradeLimit(model.getUser().getPaymentAccount(offer.getMakerPaymentAccountId()), offer.getCurrencyCode(), offer.getDirection()); + checkArgument(offer.getAmount().longValue() <= maxAmount, + "Amount is larger than " + HavenoUtils.coinToXmr(offer.getPaymentMethod().getMaxTradeLimitAsCoin(offer.getCurrencyCode())) + " XMR"); checkArgument(offer.getAmount().compareTo(offer.getMinAmount()) >= 0, "MinAmount is larger than Amount"); checkNotNull(offer.getPrice(), "Price is null"); @@ -89,7 +91,6 @@ public class ValidateOffer extends Task { "maxTradePeriod must be positive. maxTradePeriod=" + offer.getMaxTradePeriod()); // TODO check upper and lower bounds for fiat // TODO check rest of new parameters - // TODO check for account age witness base tradeLimit is missing complete(); } catch (Exception e) { diff --git a/core/src/main/java/bisq/core/trade/Trade.java b/core/src/main/java/bisq/core/trade/Trade.java index 9dce37f2a7..ebc87e1e5e 100644 --- a/core/src/main/java/bisq/core/trade/Trade.java +++ b/core/src/main/java/bisq/core/trade/Trade.java @@ -38,7 +38,6 @@ import bisq.core.trade.protocol.TradeListener; import bisq.core.trade.protocol.TradeProtocol; import bisq.core.trade.protocol.TradingPeer; import bisq.core.trade.txproof.AssetTxProofResult; -import bisq.core.util.ParsingUtils; import bisq.core.util.VolumeUtil; import bisq.network.p2p.AckMessage; import bisq.network.p2p.NodeAddress; @@ -701,7 +700,6 @@ public abstract class Trade implements Tradable, Model { public MoneroTxWallet createPayoutTx() { // gather info - XmrWalletService walletService = processModel.getProvider().getXmrWalletService(); MoneroWallet multisigWallet = getWallet(); if (multisigWallet.isMultisigImportNeeded()) throw new RuntimeException("Cannot create payout tx because multisig import is needed"); String sellerPayoutAddress = this.getSeller().getPayoutAddressString(); diff --git a/core/src/test/java/bisq/core/offer/OpenOfferManagerTest.java b/core/src/test/java/bisq/core/offer/OpenOfferManagerTest.java index 494e0be761..416546eaac 100644 --- a/core/src/test/java/bisq/core/offer/OpenOfferManagerTest.java +++ b/core/src/test/java/bisq/core/offer/OpenOfferManagerTest.java @@ -73,7 +73,8 @@ public class OpenOfferManagerTest { null, null, persistenceManager, - signedOfferPersistenceManager); + signedOfferPersistenceManager, + null); AtomicBoolean startEditOfferSuccessful = new AtomicBoolean(false); @@ -119,7 +120,8 @@ public class OpenOfferManagerTest { null, null, persistenceManager, - signedOfferPersistenceManager); + signedOfferPersistenceManager, + null); AtomicBoolean startEditOfferSuccessful = new AtomicBoolean(false); @@ -159,7 +161,8 @@ public class OpenOfferManagerTest { null, null, persistenceManager, - signedOfferPersistenceManager); + signedOfferPersistenceManager, + null); AtomicBoolean startEditOfferSuccessful = new AtomicBoolean(false);