Skip to content

Commit 18fe9d7

Browse files
committed
fix amount adjustment when creating new offer
1 parent ca1dde0 commit 18fe9d7

File tree

5 files changed

+45
-20
lines changed

5 files changed

+45
-20
lines changed

core/src/main/java/haveno/core/offer/CreateOfferService.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import haveno.core.trade.statistics.TradeStatisticsManager;
3636
import haveno.core.user.User;
3737
import haveno.core.util.coin.CoinUtil;
38+
import haveno.core.xmr.wallet.Restrictions;
3839
import haveno.core.xmr.wallet.XmrWalletService;
3940
import haveno.network.p2p.NodeAddress;
4041
import haveno.network.p2p.P2PService;
@@ -92,7 +93,6 @@ public String getRandomOfferId() {
9293
Version.VERSION.replace(".", "");
9394
}
9495

95-
// TODO: add trigger price?
9696
public Offer createAndGetOffer(String offerId,
9797
OfferDirection direction,
9898
String currencyCode,
@@ -158,8 +158,9 @@ public Offer createAndGetOffer(String offerId,
158158
}
159159

160160
// adjust amount and min amount
161-
amount = CoinUtil.getRoundedAmount(amount, fixedPrice, minAmount, amount, currencyCode, paymentAccount.getPaymentMethod().getId());
162-
minAmount = CoinUtil.getRoundedAmount(minAmount, fixedPrice, minAmount, amount, currencyCode, paymentAccount.getPaymentMethod().getId());
161+
BigInteger maxTradeLimit = offerUtil.getMaxTradeLimitForRelease(paymentAccount, currencyCode, direction, buyerAsTakerWithoutDeposit);
162+
amount = CoinUtil.getRoundedAmount(amount, fixedPrice, Restrictions.getMinTradeAmount(), maxTradeLimit, currencyCode, paymentAccount.getPaymentMethod().getId());
163+
minAmount = CoinUtil.getRoundedAmount(minAmount, fixedPrice, Restrictions.getMinTradeAmount(), maxTradeLimit, currencyCode, paymentAccount.getPaymentMethod().getId());
163164

164165
// generate one-time challenge for private offer
165166
String challenge = null;
@@ -241,7 +242,6 @@ public Offer createAndGetOffer(String offerId,
241242
return offer;
242243
}
243244

244-
// TODO: add trigger price?
245245
public Offer createClonedOffer(Offer sourceOffer,
246246
String currencyCode,
247247
Price fixedPrice,

core/src/main/java/haveno/core/offer/OfferUtil.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import haveno.core.payment.PaymentAccount;
5757
import haveno.core.provider.price.MarketPrice;
5858
import haveno.core.provider.price.PriceFeedService;
59+
import haveno.core.trade.HavenoUtils;
5960
import haveno.core.trade.statistics.ReferralIdService;
6061
import haveno.core.user.AutoConfirmSettings;
6162
import haveno.core.user.Preferences;
@@ -269,4 +270,21 @@ public static boolean isTraditionalOffer(Offer offer) {
269270
public static boolean isCryptoOffer(Offer offer) {
270271
return offer.getCounterCurrencyCode().equals("XMR");
271272
}
273+
274+
public BigInteger getMaxTradeLimitForRelease(PaymentAccount paymentAccount,
275+
String currencyCode,
276+
OfferDirection direction,
277+
boolean buyerAsTakerWithoutDeposit) {
278+
279+
// disallow offers which no buyer can take due to trade limits on release
280+
if (HavenoUtils.isReleasedWithinDays(HavenoUtils.RELEASE_LIMIT_DAYS)) {
281+
return BigInteger.valueOf(accountAgeWitnessService.getMyTradeLimit(paymentAccount, currencyCode, OfferDirection.BUY, buyerAsTakerWithoutDeposit));
282+
}
283+
284+
if (paymentAccount != null) {
285+
return BigInteger.valueOf(accountAgeWitnessService.getMyTradeLimit(paymentAccount, currencyCode, direction, buyerAsTakerWithoutDeposit));
286+
} else {
287+
return BigInteger.ZERO;
288+
}
289+
}
272290
}

core/src/main/java/haveno/core/trade/HavenoUtils.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,17 @@ public static boolean isDaemon() {
148148
@SuppressWarnings("unused")
149149
public static Date getReleaseDate() {
150150
if (RELEASE_DATE == null) return null;
151-
try {
152-
return DATE_FORMAT.parse(RELEASE_DATE);
153-
} catch (Exception e) {
154-
log.error("Failed to parse release date: " + RELEASE_DATE, e);
155-
throw new IllegalArgumentException(e);
151+
return parseDate(RELEASE_DATE);
152+
}
153+
154+
private static Date parseDate(String date) {
155+
synchronized (DATE_FORMAT) {
156+
try {
157+
return DATE_FORMAT.parse(date);
158+
} catch (Exception e) {
159+
log.error("Failed to parse date: " + date, e);
160+
throw new IllegalArgumentException(e);
161+
}
156162
}
157163
}
158164

core/src/main/java/haveno/core/util/coin/CoinUtil.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,17 @@ static BigInteger getAdjustedAmount(BigInteger amount, Price price, BigInteger m
133133
minAmount.longValueExact() >= Restrictions.getMinTradeAmount().longValueExact(),
134134
"minAmount needs to be above minimum of " + HavenoUtils.atomicUnitsToXmr(Restrictions.getMinTradeAmount()) + " xmr but was " + HavenoUtils.atomicUnitsToXmr(minAmount) + " xmr"
135135
);
136+
if (maxAmount != null) {
137+
checkArgument(
138+
amount.longValueExact() <= maxAmount.longValueExact(),
139+
"amount needs to be below maximum of " + HavenoUtils.atomicUnitsToXmr(maxAmount) + " xmr but was " + HavenoUtils.atomicUnitsToXmr(amount) + " xmr"
140+
);
141+
checkArgument(
142+
maxAmount.longValueExact() >= minAmount.longValueExact(),
143+
"maxAmount needs to be above minimum of " + HavenoUtils.atomicUnitsToXmr(Restrictions.getMinTradeAmount()) + " xmr but was " + HavenoUtils.atomicUnitsToXmr(maxAmount) + " xmr"
144+
);
145+
}
146+
136147
checkArgument(
137148
factor > 0,
138149
"factor needs to be positive"

desktop/src/main/java/haveno/desktop/main/offer/MutableOfferDataModel.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -473,17 +473,7 @@ public double getMarketPriceMarginPct() {
473473
}
474474

475475
BigInteger getMaxTradeLimit() {
476-
477-
// disallow offers which no buyer can take due to trade limits on release
478-
if (HavenoUtils.isReleasedWithinDays(HavenoUtils.RELEASE_LIMIT_DAYS)) {
479-
return BigInteger.valueOf(accountAgeWitnessService.getMyTradeLimit(paymentAccount, tradeCurrencyCode.get(), OfferDirection.BUY, buyerAsTakerWithoutDeposit.get()));
480-
}
481-
482-
if (paymentAccount != null) {
483-
return BigInteger.valueOf(accountAgeWitnessService.getMyTradeLimit(paymentAccount, tradeCurrencyCode.get(), direction, buyerAsTakerWithoutDeposit.get()));
484-
} else {
485-
return BigInteger.ZERO;
486-
}
476+
return offerUtil.getMaxTradeLimitForRelease(paymentAccount, tradeCurrencyCode.get(), direction, buyerAsTakerWithoutDeposit.get());
487477
}
488478

489479
BigInteger getMinTradeLimit() {

0 commit comments

Comments
 (0)