Add null checks or price and volume

This commit is contained in:
Manfred Karrer 2016-04-16 14:08:39 +02:00
parent a45f8d7325
commit 3b94eda701
12 changed files with 142 additions and 85 deletions

View file

@ -118,7 +118,7 @@ public final class Offer implements StoragePayload, RequiresOwnerIsOnlinePayload
// E.g. Buy offer with market price 400.- leads to a 360.- price.
// Sell offer with market price 400.- leads to a 440.- price.
private final double marketPriceMargin;
private final long amount;
private final long minAmount;
private final NodeAddress offererNodeAddress;
@ -236,19 +236,27 @@ public final class Offer implements StoragePayload, RequiresOwnerIsOnlinePayload
return getPubKeyRing().equals(keyRing.getPubKeyRing());
}
@Nullable
public Fiat getVolumeByAmount(Coin amount) {
try {
return new ExchangeRate(getPrice()).coinToFiat(amount);
} catch (Throwable t) {
log.error("getVolumeByAmount failed. Error=" + t.getMessage());
return Fiat.valueOf(currencyCode, 0);
Fiat price = getPrice();
if (price != null && amount != null) {
try {
return new ExchangeRate(price).coinToFiat(amount);
} catch (Throwable t) {
log.error("getVolumeByAmount failed. Error=" + t.getMessage());
return null;
}
} else {
return null;
}
}
@Nullable
public Fiat getOfferVolume() {
return getVolumeByAmount(getAmount());
}
@Nullable
public Fiat getMinOfferVolume() {
return getVolumeByAmount(getMinAmount());
}
@ -337,6 +345,7 @@ public final class Offer implements StoragePayload, RequiresOwnerIsOnlinePayload
return pubKeyRing;
}
@Nullable
public Fiat getPrice() {
if (useMarketBasedPrice) {
checkNotNull(priceFeed, "priceFeed must not be null");
@ -357,14 +366,13 @@ public final class Offer implements StoragePayload, RequiresOwnerIsOnlinePayload
return Fiat.parseFiat(currencyCode, String.valueOf(targetPrice));
} catch (Exception e) {
log.error("Exception at getPrice / parseToFiat: " + e.toString() + "\n" +
"We use an inaccessible price to avoid null pointers.\n" +
"That case should never happen.");
return Fiat.valueOf(currencyCode, direction == Direction.BUY ? Long.MIN_VALUE : Long.MAX_VALUE);
return null;
}
} else {
log.warn("We don't have a market price. We use an inaccessible price to avoid null pointers.\n" +
log.warn("We don't have a market price./n" +
"That case could only happen if you don't get a price feed.");
return Fiat.valueOf(currencyCode, direction == Direction.BUY ? Long.MIN_VALUE : Long.MAX_VALUE);
return null;
}
} else {
return Fiat.valueOf(currencyCode, fiatPrice);

View file

@ -82,6 +82,7 @@ public class ProcessPayDepositRequest extends TradeTask {
checkArgument(takersTradePrice > 0);
Fiat tradePriceAsFiat = Fiat.valueOf(trade.getOffer().getCurrencyCode(), takersTradePrice);
Fiat offerPriceAsFiat = trade.getOffer().getPrice();
checkArgument(offerPriceAsFiat != null, "offerPriceAsFiat must not be null");
double factor = (double) takersTradePrice / (double) offerPriceAsFiat.value;
// We allow max. 2 % difference between own offer price calculation and takers calculation.
// Market price might be different at offerers and takers side so we need a bit of tolerance.
@ -95,8 +96,8 @@ public class ProcessPayDepositRequest extends TradeTask {
failed(msg);
}
trade.setTradePrice(takersTradePrice);
checkArgument(payDepositRequest.tradeAmount > 0);
trade.setTradeAmount(Coin.valueOf(payDepositRequest.tradeAmount));