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

[WIP] Refactor offerpayload #5121

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static protobuf.OfferPayload.Direction.BUY;
import static protobuf.FeeTxOfferPayload.Direction.BUY;

@Disabled
@Slf4j
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/bisq/core/offer/CreateOfferService.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public Offer createAndGetOffer(String offerId,
currencyCode,
makerFeeAsCoin);

OfferPayload offerPayload = new OfferPayload(offerId,
FeeTxOfferPayload offerPayload = new FeeTxOfferPayload(offerId,
creationTime,
makerAddress,
pubKeyRing,
Expand Down
394 changes: 394 additions & 0 deletions core/src/main/java/bisq/core/offer/FeeTxOfferPayload.java

Large diffs are not rendered by default.

95 changes: 52 additions & 43 deletions core/src/main/java/bisq/core/offer/Offer.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public enum State {

@Getter
private final OfferPayload offerPayload;

@JsonExclude
@Getter
final transient private ObjectProperty<Offer.State> stateProperty = new SimpleObjectProperty<>(Offer.State.UNKNOWN);
Expand Down Expand Up @@ -130,11 +131,12 @@ public Offer(OfferPayload offerPayload) {

@Override
public protobuf.Offer toProtoMessage() {
return protobuf.Offer.newBuilder().setOfferPayload(offerPayload.toProtoMessage().getOfferPayload()).build();
return protobuf.Offer.newBuilder().setFeeTxOfferPayload(offerPayload.toProtoMessage().getFeeTxOfferPayload())
.build();
}

public static Offer fromProto(protobuf.Offer proto) {
return new Offer(OfferPayload.fromProto(proto.getOfferPayload()));
return new Offer(FeeTxOfferPayload.fromProto(proto.getFeeTxOfferPayload()));
}


Expand Down Expand Up @@ -166,40 +168,40 @@ public void cancelAvailabilityRequest() {
@Nullable
public Price getPrice() {
String currencyCode = getCurrencyCode();
if (offerPayload.isUseMarketBasedPrice()) {
checkNotNull(priceFeedService, "priceFeed must not be null");
MarketPrice marketPrice = priceFeedService.getMarketPrice(currencyCode);
if (marketPrice != null && marketPrice.isRecentExternalPriceAvailable()) {
double factor;
double marketPriceMargin = offerPayload.getMarketPriceMargin();
if (CurrencyUtil.isCryptoCurrency(currencyCode)) {
factor = getDirection() == OfferPayload.Direction.SELL ?
1 - marketPriceMargin : 1 + marketPriceMargin;
} else {
factor = getDirection() == OfferPayload.Direction.BUY ?
1 - marketPriceMargin : 1 + marketPriceMargin;
}
double marketPriceAsDouble = marketPrice.getPrice();
double targetPriceAsDouble = marketPriceAsDouble * factor;
try {
int precision = CurrencyUtil.isCryptoCurrency(currencyCode) ?
Altcoin.SMALLEST_UNIT_EXPONENT :
Fiat.SMALLEST_UNIT_EXPONENT;
double scaled = MathUtils.scaleUpByPowerOf10(targetPriceAsDouble, precision);
final long roundedToLong = MathUtils.roundDoubleToLong(scaled);
return Price.valueOf(currencyCode, roundedToLong);
} catch (Exception e) {
log.error("Exception at getPrice / parseToFiat: " + e.toString() + "\n" +
"That case should never happen.");
return null;
}
if (!offerPayload.isUseMarketBasedPrice()) {
return Price.valueOf(currencyCode, offerPayload.getPrice());
}

checkNotNull(priceFeedService, "priceFeed must not be null");
MarketPrice marketPrice = priceFeedService.getMarketPrice(currencyCode);
if (marketPrice != null && marketPrice.isRecentExternalPriceAvailable()) {
double factor;
double marketPriceMargin = offerPayload.getMarketPriceMargin();
if (CurrencyUtil.isCryptoCurrency(currencyCode)) {
factor = getDirection() == OfferPayload.Direction.SELL ?
1 - marketPriceMargin : 1 + marketPriceMargin;
} else {
log.trace("We don't have a market price. " +
"That case could only happen if you don't have a price feed.");
factor = getDirection() == OfferPayload.Direction.BUY ?
1 - marketPriceMargin : 1 + marketPriceMargin;
}
double marketPriceAsDouble = marketPrice.getPrice();
double targetPriceAsDouble = marketPriceAsDouble * factor;
try {
int precision = CurrencyUtil.isCryptoCurrency(currencyCode) ?
Altcoin.SMALLEST_UNIT_EXPONENT :
Fiat.SMALLEST_UNIT_EXPONENT;
double scaled = MathUtils.scaleUpByPowerOf10(targetPriceAsDouble, precision);
final long roundedToLong = MathUtils.roundDoubleToLong(scaled);
return Price.valueOf(currencyCode, roundedToLong);
} catch (Exception e) {
log.error("Exception at getPrice / parseToFiat: " + e.toString() + "\n" +
"That case should never happen.");
return null;
}
} else {
return Price.valueOf(currencyCode, offerPayload.getPrice());
log.trace("We don't have a market price. " +
"That case could only happen if you don't have a price feed.");
return null;
}
}

Expand Down Expand Up @@ -234,17 +236,16 @@ public void checkTradePriceTolerance(long takersTradePrice) throws TradePriceOut
@Nullable
public Volume getVolumeByAmount(Coin amount) {
Price price = getPrice();
if (price != null && amount != null) {
Volume volumeByAmount = price.getVolumeByAmount(amount);
if (offerPayload.getPaymentMethodId().equals(PaymentMethod.HAL_CASH_ID))
volumeByAmount = VolumeUtil.getAdjustedVolumeForHalCash(volumeByAmount);
else if (CurrencyUtil.isFiatCurrency(offerPayload.getCurrencyCode()))
volumeByAmount = VolumeUtil.getRoundedFiatVolume(volumeByAmount);

return volumeByAmount;
} else {
if (price == null || amount == null) {
return null;
}
Volume volumeByAmount = price.getVolumeByAmount(amount);
if (offerPayload.getPaymentMethodId().equals(PaymentMethod.HAL_CASH_ID))
volumeByAmount = VolumeUtil.getAdjustedVolumeForHalCash(volumeByAmount);
else if (CurrencyUtil.isFiatCurrency(offerPayload.getCurrencyCode()))
volumeByAmount = VolumeUtil.getRoundedFiatVolume(volumeByAmount);

return volumeByAmount;
}

public void resetState() {
Expand All @@ -265,7 +266,7 @@ public ObjectProperty<Offer.State> stateProperty() {
}

public void setOfferFeePaymentTxId(String offerFeePaymentTxID) {
offerPayload.setOfferFeePaymentTxId(offerFeePaymentTxID);
getFeeTxPayload(offerPayload).ifPresent(payload -> payload.setOfferFeePaymentTxId(offerFeePaymentTxID));
}

public void setErrorMessage(String errorMessage) {
Expand All @@ -278,6 +279,7 @@ public void setErrorMessage(String errorMessage) {
///////////////////////////////////////////////////////////////////////////////////////////

// converted payload properties

public Coin getTxFee() {
return Coin.valueOf(offerPayload.getTxFee());
}
Expand Down Expand Up @@ -464,7 +466,7 @@ public String getMakerPaymentAccountId() {
}

public String getOfferFeePaymentTxId() {
return offerPayload.getOfferFeePaymentTxId();
return getFeeTxPayload(offerPayload).map(FeeTxOfferPayload::getOfferFeePaymentTxId).orElse(null);
}

public String getVersionNr() {
Expand Down Expand Up @@ -533,6 +535,13 @@ public boolean isXmr() {
return getCurrencyCode().equals("XMR");
}

private Optional<FeeTxOfferPayload> getFeeTxPayload(OfferPayload offerPayload) {
if (offerPayload instanceof FeeTxOfferPayload) {
return Optional.of((FeeTxOfferPayload) offerPayload);
}
return Optional.empty();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Loading