Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix API editoffer bug: set fixed-price=0 on margin offers #6202

Merged
merged 1 commit into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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