From f261a38cce8dcab4627cd8af305164f42ab7550e Mon Sep 17 00:00:00 2001 From: woodser Date: Mon, 30 Oct 2023 09:17:51 -0400 Subject: [PATCH] fix adjustment < min amount creating offers --- .../main/java/haveno/core/util/coin/CoinUtil.java | 14 ++++++-------- .../java/haveno/core/util/coin/CoinUtilTest.java | 6 +++--- .../desktop/main/offer/MutableOfferDataModel.java | 1 + 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/core/src/main/java/haveno/core/util/coin/CoinUtil.java b/core/src/main/java/haveno/core/util/coin/CoinUtil.java index 4c4dc516cc..ecdbdc15d0 100644 --- a/core/src/main/java/haveno/core/util/coin/CoinUtil.java +++ b/core/src/main/java/haveno/core/util/coin/CoinUtil.java @@ -47,7 +47,7 @@ public class CoinUtil { public static double getFeePerVbyte(Coin miningFee, int txVsize) { double value = miningFee != null ? miningFee.value : 0; - return MathUtils.roundDouble((value / (double) txVsize), 2); + return MathUtils.roundDouble((value / txVsize), 2); } /** @@ -127,8 +127,8 @@ public class CoinUtil { @VisibleForTesting static BigInteger getAdjustedAmount(BigInteger amount, Price price, long maxTradeLimit, int factor) { checkArgument( - amount.longValueExact() >= HavenoUtils.xmrToAtomicUnits(0.0001).longValueExact(), - "amount needs to be above minimum of 0.0001 xmr" // TODO: update amount for XMR + amount.longValueExact() >= Restrictions.getMinTradeAmount().longValueExact(), + "amount needs to be above minimum of " + HavenoUtils.atomicUnitsToXmr(Restrictions.getMinTradeAmount()) + " xmr" ); checkArgument( factor > 0, @@ -143,10 +143,9 @@ public class CoinUtil { BigInteger smallestUnitForAmount = price.getAmountByVolume(smallestUnitForVolume); long minTradeAmount = Restrictions.getMinTradeAmount().longValueExact(); - // We use 10 000 satoshi as min allowed amount checkArgument( - minTradeAmount >= HavenoUtils.xmrToAtomicUnits(0.0001).longValueExact(), - "MinTradeAmount must be at least 0.0001 xmr" // TODO: update amount for XMR + minTradeAmount >= Restrictions.getMinTradeAmount().longValueExact(), + "MinTradeAmount must be at least " + HavenoUtils.atomicUnitsToXmr(Restrictions.getMinTradeAmount()) + " xmr" ); smallestUnitForAmount = BigInteger.valueOf(Math.max(minTradeAmount, smallestUnitForAmount.longValueExact())); // We don't allow smaller amount values than smallestUnitForAmount @@ -164,8 +163,7 @@ public class CoinUtil { BigInteger amountByVolume = price.getAmountByVolume(volume); // For the amount we allow only 4 decimal places - // TODO: remove rounding for XMR? - long adjustedAmount = HavenoUtils.centinerosToAtomicUnits(Math.round((double) HavenoUtils.atomicUnitsToCentineros(amountByVolume) / 10000d) * 10000).longValueExact(); + long adjustedAmount = HavenoUtils.centinerosToAtomicUnits(Math.round(HavenoUtils.atomicUnitsToCentineros(amountByVolume) / 10000d) * 10000).longValueExact(); // If we are above our trade limit we reduce the amount by the smallestUnitForAmount while (adjustedAmount > maxTradeLimit) { diff --git a/core/src/test/java/haveno/core/util/coin/CoinUtilTest.java b/core/src/test/java/haveno/core/util/coin/CoinUtilTest.java index 9ff4fdf2ca..2981958018 100644 --- a/core/src/test/java/haveno/core/util/coin/CoinUtilTest.java +++ b/core/src/test/java/haveno/core/util/coin/CoinUtilTest.java @@ -59,7 +59,7 @@ public class CoinUtilTest { @Test public void testGetAdjustedAmount() { BigInteger result = CoinUtil.getAdjustedAmount( - HavenoUtils.xmrToAtomicUnits(0.001), + HavenoUtils.xmrToAtomicUnits(0.1), Price.valueOf("USD", 1000_0000), HavenoUtils.xmrToAtomicUnits(0.2).longValueExact(), 1); @@ -78,7 +78,7 @@ public class CoinUtilTest { fail("Expected IllegalArgumentException to be thrown when amount is too low."); } catch (IllegalArgumentException iae) { assertEquals( - "amount needs to be above minimum of 0.0001 xmr", + "amount needs to be above minimum of 0.1 xmr", iae.getMessage(), "Unexpected exception message." ); @@ -112,7 +112,7 @@ public class CoinUtilTest { // 0.05 USD worth, which is below the factor of 1 USD, but does respect the maxTradeLimit. // Basically the given constraints (maxTradeLimit vs factor) are impossible to both fulfill.. result = CoinUtil.getAdjustedAmount( - HavenoUtils.xmrToAtomicUnits(0.001), + HavenoUtils.xmrToAtomicUnits(0.1), Price.valueOf("USD", 1000_0000), HavenoUtils.xmrToAtomicUnits(0.00005).longValueExact(), 1); diff --git a/desktop/src/main/java/haveno/desktop/main/offer/MutableOfferDataModel.java b/desktop/src/main/java/haveno/desktop/main/offer/MutableOfferDataModel.java index d424c2b37c..53876d9ca2 100644 --- a/desktop/src/main/java/haveno/desktop/main/offer/MutableOfferDataModel.java +++ b/desktop/src/main/java/haveno/desktop/main/offer/MutableOfferDataModel.java @@ -524,6 +524,7 @@ public abstract class MutableOfferDataModel extends OfferDataModel { // if the volume != amount * price, we need to adjust the amount if (amount.get() == null || !volumeBefore.equals(price.get().getVolumeByAmount(amount.get()))) { BigInteger value = price.get().getAmountByVolume(volumeBefore); + value = value.max(Restrictions.getMinTradeAmount()); // adjust if below minimum value = CoinUtil.getRoundedAmount(value, price.get(), getMaxTradeLimit(), tradeCurrencyCode.get(), paymentAccount.getPaymentMethod().getId()); amount.set(value); }