From 4b48ce079b6b068cb59e5780a5566e582876aff2 Mon Sep 17 00:00:00 2001 From: HenrikJannsen Date: Mon, 9 Oct 2023 21:27:36 +0700 Subject: [PATCH] Add CheckRestrictions task Signed-off-by: HenrikJannsen --- .../bisq/core/btc/wallet/Restrictions.java | 4 ++ .../bisq_v1/BuyerAsMakerProtocol.java | 2 + .../bisq_v1/BuyerAsTakerProtocol.java | 2 + .../bisq_v1/SellerAsMakerProtocol.java | 2 + .../bisq_v1/SellerAsTakerProtocol.java | 2 + .../bisq_v1/tasks/CheckRestrictions.java | 71 +++++++++++++++++++ 6 files changed, 83 insertions(+) create mode 100644 core/src/main/java/bisq/core/trade/protocol/bisq_v1/tasks/CheckRestrictions.java diff --git a/core/src/main/java/bisq/core/btc/wallet/Restrictions.java b/core/src/main/java/bisq/core/btc/wallet/Restrictions.java index 893637bc25..9394fa283f 100644 --- a/core/src/main/java/bisq/core/btc/wallet/Restrictions.java +++ b/core/src/main/java/bisq/core/btc/wallet/Restrictions.java @@ -78,6 +78,10 @@ public class Restrictions { return 0.15; // 15% of trade amount. } + public static double getMinSellerSecurityDepositAsPercent() { + return 0.15; // 15% of trade amount. + } + public static Coin getMinSellerSecurityDepositAsCoin() { if (SELLER_SECURITY_DEPOSIT == null) SELLER_SECURITY_DEPOSIT = Coin.parseCoin("0.001"); // 0.001 BTC is 60 USD @ 60000 USD/BTC diff --git a/core/src/main/java/bisq/core/trade/protocol/bisq_v1/BuyerAsMakerProtocol.java b/core/src/main/java/bisq/core/trade/protocol/bisq_v1/BuyerAsMakerProtocol.java index 83bda8ac8b..0ff1fc34c5 100644 --- a/core/src/main/java/bisq/core/trade/protocol/bisq_v1/BuyerAsMakerProtocol.java +++ b/core/src/main/java/bisq/core/trade/protocol/bisq_v1/BuyerAsMakerProtocol.java @@ -26,6 +26,7 @@ import bisq.core.trade.protocol.bisq_v1.messages.InputsForDepositTxRequest; import bisq.core.trade.protocol.bisq_v1.messages.PayoutTxPublishedMessage; import bisq.core.trade.protocol.bisq_v1.tasks.ApplyFilter; import bisq.core.trade.protocol.bisq_v1.tasks.CheckIfDaoStateIsInSync; +import bisq.core.trade.protocol.bisq_v1.tasks.CheckRestrictions; import bisq.core.trade.protocol.bisq_v1.tasks.TradeTask; import bisq.core.trade.protocol.bisq_v1.tasks.buyer.BuyerFinalizesDelayedPayoutTx; import bisq.core.trade.protocol.bisq_v1.tasks.buyer.BuyerProcessDelayedPayoutTxSignatureRequest; @@ -75,6 +76,7 @@ public class BuyerAsMakerProtocol extends BuyerProtocol implements MakerProtocol CheckIfDaoStateIsInSync.class, MakerProcessesInputsForDepositTxRequest.class, ApplyFilter.class, + CheckRestrictions.class, getVerifyPeersFeePaymentClass(), MakerSetsLockTime.class, MakerCreateAndSignContract.class, diff --git a/core/src/main/java/bisq/core/trade/protocol/bisq_v1/BuyerAsTakerProtocol.java b/core/src/main/java/bisq/core/trade/protocol/bisq_v1/BuyerAsTakerProtocol.java index d7b9413db5..17bb29abf6 100644 --- a/core/src/main/java/bisq/core/trade/protocol/bisq_v1/BuyerAsTakerProtocol.java +++ b/core/src/main/java/bisq/core/trade/protocol/bisq_v1/BuyerAsTakerProtocol.java @@ -28,6 +28,7 @@ import bisq.core.trade.protocol.bisq_v1.messages.InputsForDepositTxResponse; import bisq.core.trade.protocol.bisq_v1.messages.PayoutTxPublishedMessage; import bisq.core.trade.protocol.bisq_v1.tasks.ApplyFilter; import bisq.core.trade.protocol.bisq_v1.tasks.CheckIfDaoStateIsInSync; +import bisq.core.trade.protocol.bisq_v1.tasks.CheckRestrictions; import bisq.core.trade.protocol.bisq_v1.tasks.TradeTask; import bisq.core.trade.protocol.bisq_v1.tasks.buyer.BuyerFinalizesDelayedPayoutTx; import bisq.core.trade.protocol.bisq_v1.tasks.buyer.BuyerProcessDelayedPayoutTxSignatureRequest; @@ -80,6 +81,7 @@ public class BuyerAsTakerProtocol extends BuyerProtocol implements TakerProtocol .setup(tasks( CheckIfDaoStateIsInSync.class, ApplyFilter.class, + CheckRestrictions.class, getVerifyPeersFeePaymentClass(), CreateTakerFeeTx.class, BuyerAsTakerCreatesDepositTxInputs.class, diff --git a/core/src/main/java/bisq/core/trade/protocol/bisq_v1/SellerAsMakerProtocol.java b/core/src/main/java/bisq/core/trade/protocol/bisq_v1/SellerAsMakerProtocol.java index c88e7de0f8..8e1fb608d6 100644 --- a/core/src/main/java/bisq/core/trade/protocol/bisq_v1/SellerAsMakerProtocol.java +++ b/core/src/main/java/bisq/core/trade/protocol/bisq_v1/SellerAsMakerProtocol.java @@ -28,6 +28,7 @@ import bisq.core.trade.protocol.bisq_v1.messages.DepositTxMessage; import bisq.core.trade.protocol.bisq_v1.messages.InputsForDepositTxRequest; import bisq.core.trade.protocol.bisq_v1.tasks.ApplyFilter; import bisq.core.trade.protocol.bisq_v1.tasks.CheckIfDaoStateIsInSync; +import bisq.core.trade.protocol.bisq_v1.tasks.CheckRestrictions; import bisq.core.trade.protocol.bisq_v1.tasks.TradeTask; import bisq.core.trade.protocol.bisq_v1.tasks.maker.MakerCreateAndSignContract; import bisq.core.trade.protocol.bisq_v1.tasks.maker.MakerProcessesInputsForDepositTxRequest; @@ -78,6 +79,7 @@ public class SellerAsMakerProtocol extends SellerProtocol implements MakerProtoc MaybeCreateSubAccount.class, MakerProcessesInputsForDepositTxRequest.class, ApplyFilter.class, + CheckRestrictions.class, getVerifyPeersFeePaymentClass(), MakerSetsLockTime.class, MakerCreateAndSignContract.class, diff --git a/core/src/main/java/bisq/core/trade/protocol/bisq_v1/SellerAsTakerProtocol.java b/core/src/main/java/bisq/core/trade/protocol/bisq_v1/SellerAsTakerProtocol.java index 42804e7cd3..0c13feb767 100644 --- a/core/src/main/java/bisq/core/trade/protocol/bisq_v1/SellerAsTakerProtocol.java +++ b/core/src/main/java/bisq/core/trade/protocol/bisq_v1/SellerAsTakerProtocol.java @@ -27,6 +27,7 @@ import bisq.core.trade.protocol.bisq_v1.messages.DelayedPayoutTxSignatureRespons import bisq.core.trade.protocol.bisq_v1.messages.InputsForDepositTxResponse; import bisq.core.trade.protocol.bisq_v1.tasks.ApplyFilter; import bisq.core.trade.protocol.bisq_v1.tasks.CheckIfDaoStateIsInSync; +import bisq.core.trade.protocol.bisq_v1.tasks.CheckRestrictions; import bisq.core.trade.protocol.bisq_v1.tasks.TradeTask; import bisq.core.trade.protocol.bisq_v1.tasks.seller.MaybeCreateSubAccount; import bisq.core.trade.protocol.bisq_v1.tasks.seller.SellerCreatesDelayedPayoutTx; @@ -77,6 +78,7 @@ public class SellerAsTakerProtocol extends SellerProtocol implements TakerProtoc CheckIfDaoStateIsInSync.class, MaybeCreateSubAccount.class, ApplyFilter.class, + CheckRestrictions.class, getVerifyPeersFeePaymentClass(), CreateTakerFeeTx.class, SellerAsTakerCreatesDepositTxInputs.class, diff --git a/core/src/main/java/bisq/core/trade/protocol/bisq_v1/tasks/CheckRestrictions.java b/core/src/main/java/bisq/core/trade/protocol/bisq_v1/tasks/CheckRestrictions.java new file mode 100644 index 0000000000..e698f029d6 --- /dev/null +++ b/core/src/main/java/bisq/core/trade/protocol/bisq_v1/tasks/CheckRestrictions.java @@ -0,0 +1,71 @@ +/* + * This file is part of Bisq. + * + * Bisq is free software: you can redistribute it and/or modify it + * under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or (at + * your option) any later version. + * + * Bisq is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public + * License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Bisq. If not, see . + */ + +package bisq.core.trade.protocol.bisq_v1.tasks; + +import bisq.core.btc.wallet.Restrictions; +import bisq.core.trade.model.bisq_v1.Trade; +import bisq.core.util.coin.CoinUtil; + +import bisq.common.taskrunner.TaskRunner; + +import org.bitcoinj.core.Coin; + +import lombok.extern.slf4j.Slf4j; + +import static com.google.common.base.Preconditions.checkArgument; + +@Slf4j +public class CheckRestrictions extends TradeTask { + public CheckRestrictions(TaskRunner taskHandler, Trade trade) { + super(taskHandler, trade); + } + + @Override + protected void run() { + try { + runInterceptHook(); + + Coin amount = trade.getAmount(); + + Coin buyerSecurityDeposit = trade.getOffer().getBuyerSecurityDeposit(); + Coin minBuyerSecurityDeposit = Restrictions.getMinBuyerSecurityDepositAsCoin(); + checkArgument(buyerSecurityDeposit.getValue() >= minBuyerSecurityDeposit.getValue(), + "Buyer security deposit is less then the min. buyer security deposit (as coin)"); + + double minBuyerSecurityDepositAsPercent = Restrictions.getMinBuyerSecurityDepositAsPercent(); + Coin minBuyerSecurityDepositFromPercentage = CoinUtil.getPercentOfAmountAsCoin(minBuyerSecurityDepositAsPercent, amount); + checkArgument(buyerSecurityDeposit.getValue() >= minBuyerSecurityDepositFromPercentage.getValue(), + "Buyer security deposit is less then the min. buyer security deposit (as percentage)"); + + Coin sellerSecurityDeposit = trade.getOffer().getSellerSecurityDeposit(); + Coin minSellerSecurityDeposit = Restrictions.getMinSellerSecurityDepositAsCoin(); + checkArgument(sellerSecurityDeposit.getValue() >= minSellerSecurityDeposit.getValue(), + "Seller security deposit is less then the min. seller security deposit (as coin)"); + + double minSellerSecurityDepositAsPercent = Restrictions.getMinSellerSecurityDepositAsPercent(); + Coin minSellerSecurityDepositFromPercentage = CoinUtil.getPercentOfAmountAsCoin(minSellerSecurityDepositAsPercent, amount); + checkArgument(sellerSecurityDeposit.getValue() >= minSellerSecurityDepositFromPercentage.getValue(), + "Seller security deposit is less then the min. seller security deposit (as percentage)"); + + complete(); + } catch (Throwable t) { + failed(t); + } + } +} +