Skip to content

Commit

Permalink
Merge pull request #6202 from ghubstan/fix-editoffer-price-bug
Browse files Browse the repository at this point in the history
Fix API editoffer bug: set fixed-price=0 on margin offers
  • Loading branch information
ripcurlx authored May 16, 2022
2 parents 2662ef8 + 381985e commit 0925370
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
27 changes: 20 additions & 7 deletions core/src/main/java/bisq/core/api/CoreOffersService.java
Original file line number Diff line number Diff line change
Expand Up @@ -464,22 +464,35 @@ private OfferPayload getMergedOfferPayload(EditOfferValidator editOfferValidator
// in OfferPayload.
Offer offer = openOffer.getOffer();
String currencyCode = offer.getCurrencyCode();
boolean isEditingPrice = editType.equals(FIXED_PRICE_ONLY) || editType.equals(FIXED_PRICE_AND_ACTIVATION_STATE);
Price editedPrice;
if (isEditingPrice) {
editedPrice = Price.valueOf(currencyCode, priceStringToLong(editedPriceAsString, currencyCode));
boolean isUsingMktPriceMargin = editOfferValidator.isEditingUseMktPriceMarginFlag.test(offer, editType);
boolean isEditingFixedPrice = editType.equals(FIXED_PRICE_ONLY) || editType.equals(FIXED_PRICE_AND_ACTIVATION_STATE);
Price editedFixedPrice;
if (isEditingFixedPrice) {
editedFixedPrice = Price.valueOf(currencyCode, priceStringToLong(editedPriceAsString, currencyCode));
} else {
editedPrice = offer.getPrice();
// When isUsingMktPriceMargin=true, (fixed) price must be set to 0 on the server.
// The client, however, still must show the calculated price when
// isUsingMktPriceMargin=true.
editedFixedPrice = isUsingMktPriceMargin ? Price.valueOf(currencyCode, 0) : offer.getPrice();
}

boolean isUsingMktPriceMargin = editOfferValidator.isEditingUseMktPriceMarginFlag.test(offer, editType);
// If isUsingMktPriceMargin=true, throw exception if new fixed-price != 0.
// If isUsingMktPriceMargin=false, throw exception if new fixed-price == 0.
if (isUsingMktPriceMargin && editedFixedPrice.getValue() != 0)
throw new IllegalStateException(
format("Fixed price on mkt price margin based offer %s must be set to 0 in server.",
offer.getId()));
else if (!isUsingMktPriceMargin && editedFixedPrice.getValue() == 0)
throw new IllegalStateException(
format("Fixed price on fixed price offer %s cannot be 0.", offer.getId()));

boolean isEditingMktPriceMargin = editOfferValidator.isEditingMktPriceMargin.test(editType);
double newMarketPriceMargin = isEditingMktPriceMargin
? exactMultiply(editedMarketPriceMargin, 0.01)
: offer.getMarketPriceMargin();

MutableOfferPayloadFields mutableOfferPayloadFields = new MutableOfferPayloadFields(
Objects.requireNonNull(editedPrice).getValue(),
Objects.requireNonNull(editedFixedPrice).getValue(),
isUsingMktPriceMargin ? newMarketPriceMargin : 0.00,
isUsingMktPriceMargin,
offer.getBaseCurrencyCode(),
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/bisq/core/offer/OfferUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ public OfferPayload getMergedOfferPayload(OpenOffer openOffer,
original.getOwnerNodeAddress(),
original.getPubKeyRing(),
original.getDirection(),
mutableOfferPayloadFields.getPrice(),
mutableOfferPayloadFields.getFixedPrice(),
mutableOfferPayloadFields.getMarketPriceMargin(),
mutableOfferPayloadFields.isUseMarketBasedPrice(),
original.getAmount(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
@Setter
public final class MutableOfferPayloadFields {

private final long price;
private final long fixedPrice; // Must be 0 when marketPriceMargin = true (on server).
private final double marketPriceMargin;
private final boolean useMarketBasedPrice;
private final String baseCurrencyCode;
Expand Down Expand Up @@ -69,7 +69,7 @@ public MutableOfferPayloadFields(OfferPayload offerPayload) {
offerPayload.getExtraDataMap());
}

public MutableOfferPayloadFields(long price,
public MutableOfferPayloadFields(long fixedPrice,
double marketPriceMargin,
boolean useMarketBasedPrice,
String baseCurrencyCode,
Expand All @@ -83,7 +83,7 @@ public MutableOfferPayloadFields(long price,
@Nullable String bankId,
@Nullable List<String> acceptedBankIds,
@Nullable Map<String, String> extraDataMap) {
this.price = price;
this.fixedPrice = fixedPrice;
this.marketPriceMargin = marketPriceMargin;
this.useMarketBasedPrice = useMarketBasedPrice;
this.baseCurrencyCode = baseCurrencyCode;
Expand All @@ -102,7 +102,7 @@ public MutableOfferPayloadFields(long price,
@Override
public String toString() {
return "MutableOfferPayloadFields{" + "\n" +
" price=" + price + "\n" +
" fixedPrice=" + fixedPrice + "\n" +
", marketPriceMargin=" + marketPriceMargin + "\n" +
", useMarketBasedPrice=" + useMarketBasedPrice + "\n" +
", baseCurrencyCode='" + baseCurrencyCode + '\'' + "\n" +
Expand Down

0 comments on commit 0925370

Please sign in to comment.