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

Update trigger price validation #6109

Merged
merged 2 commits into from Mar 21, 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
17 changes: 9 additions & 8 deletions core/src/main/java/bisq/core/util/PriceUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static MonetaryValidator getPriceValidator(boolean isFiatCurrency) {
}

public static InputValidator.ValidationResult isTriggerPriceValid(String triggerPriceAsString,
Price price,
Copy link
Contributor

@ripcurlx ripcurlx Mar 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather use the MarketPrice object exposing the currencyCode property instead of adding another parameter. WDYT?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea - fixed

MarketPrice marketPrice,
boolean isSellOffer,
boolean isFiatCurrency) {
if (triggerPriceAsString == null || triggerPriceAsString.isEmpty()) {
Expand All @@ -84,20 +84,21 @@ public static InputValidator.ValidationResult isTriggerPriceValid(String trigger
return result;
}

long triggerPriceAsLong = PriceUtil.getMarketPriceAsLong(triggerPriceAsString, price.getCurrencyCode());
long priceAsLong = price.getValue();
String priceAsString = FormattingUtils.formatPrice(price);
long triggerPriceAsLong = PriceUtil.getMarketPriceAsLong(triggerPriceAsString, marketPrice.getCurrencyCode());
long marketPriceAsLong = PriceUtil.getMarketPriceAsLong("" + marketPrice.getPrice(), marketPrice.getCurrencyCode());
String marketPriceAsString = FormattingUtils.formatMarketPrice(marketPrice.getPrice(), marketPrice.getCurrencyCode());

if ((isSellOffer && isFiatCurrency) || (!isSellOffer && !isFiatCurrency)) {
if (triggerPriceAsLong >= priceAsLong) {
if (triggerPriceAsLong >= marketPriceAsLong) {
return new InputValidator.ValidationResult(false,
Res.get("createOffer.triggerPrice.invalid.tooHigh", priceAsString));
Res.get("createOffer.triggerPrice.invalid.tooHigh", marketPriceAsString));
} else {
return new InputValidator.ValidationResult(true);
}
} else {
if (triggerPriceAsLong <= priceAsLong) {
if (triggerPriceAsLong <= marketPriceAsLong) {
return new InputValidator.ValidationResult(false,
Res.get("createOffer.triggerPrice.invalid.tooLow", priceAsString));
Res.get("createOffer.triggerPrice.invalid.tooLow", marketPriceAsString));
} else {
return new InputValidator.ValidationResult(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -791,12 +791,14 @@ public void onTriggerPriceTextFieldChanged() {
// if not reset here. Not clear why...
triggerPriceValidationResult.set(new InputValidator.ValidationResult(true));

if (dataModel.getPrice().get() == null) // fix NPE @ bisq/issues/5166
return;
String currencyCode = dataModel.getTradeCurrencyCode().get();
MarketPrice marketPrice = priceFeedService.getMarketPrice(currencyCode);

InputValidator.ValidationResult result = PriceUtil.isTriggerPriceValid(triggerPriceAsString,
dataModel.getPrice().get(),
marketPrice,
dataModel.isSellOffer(),
dataModel.isFiatCurrency());
dataModel.isFiatCurrency()
);
triggerPriceValidationResult.set(result);
updateButtonDisableState();
if (result.isValid) {
Expand Down