use of io.bitsquare.trade.exceptions.TradePriceOutOfToleranceException in project bitsquare by bitsquare.
the class Offer method checkTradePriceTolerance.
public void checkTradePriceTolerance(long takersTradePrice) throws TradePriceOutOfToleranceException, MarketPriceNotAvailableException, IllegalArgumentException {
checkArgument(takersTradePrice > 0, "takersTradePrice must be positive");
Fiat tradePriceAsFiat = Fiat.valueOf(getCurrencyCode(), takersTradePrice);
Fiat offerPriceAsFiat = getPrice();
if (offerPriceAsFiat == null)
throw new MarketPriceNotAvailableException("Market price required for calculating trade price is not available.");
double factor = (double) takersTradePrice / (double) offerPriceAsFiat.value;
// from one provider.
if (Math.abs(1 - factor) > 0.02) {
String msg = "Taker's trade price is too far away from our calculated price based on the market price.\n" + "tradePriceAsFiat=" + tradePriceAsFiat.toFriendlyString() + "\n" + "offerPriceAsFiat=" + offerPriceAsFiat.toFriendlyString();
log.warn(msg);
throw new TradePriceOutOfToleranceException(msg);
}
}
use of io.bitsquare.trade.exceptions.TradePriceOutOfToleranceException in project bitsquare by bitsquare.
the class OpenOfferManager method handleOfferAvailabilityRequest.
///////////////////////////////////////////////////////////////////////////////////////////
// Offer Availability
///////////////////////////////////////////////////////////////////////////////////////////
private void handleOfferAvailabilityRequest(OfferAvailabilityRequest message, NodeAddress sender) {
log.trace("handleNewMessage: message = " + message.getClass().getSimpleName() + " from " + sender);
if (!stopped) {
try {
nonEmptyStringOf(message.offerId);
checkNotNull(message.getPubKeyRing());
} catch (Throwable t) {
log.warn("Invalid message " + message.toString());
return;
}
Optional<OpenOffer> openOfferOptional = findOpenOffer(message.offerId);
AvailabilityResult availabilityResult;
if (openOfferOptional.isPresent()) {
if (openOfferOptional.get().getState() == OpenOffer.State.AVAILABLE) {
final Offer offer = openOfferOptional.get().getOffer();
if (!preferences.getIgnoreTradersList().stream().filter(i -> i.equals(offer.getOffererNodeAddress().getHostNameWithoutPostFix())).findAny().isPresent()) {
availabilityResult = AvailabilityResult.AVAILABLE;
List<NodeAddress> acceptedArbitrators = user.getAcceptedArbitratorAddresses();
if (acceptedArbitrators != null && !acceptedArbitrators.isEmpty()) {
// We need to be backward compatible. takersTradePrice was not used before 0.4.9.
if (message.takersTradePrice > 0) {
// losses and therefore an outdated market price.
try {
offer.checkTradePriceTolerance(message.takersTradePrice);
} catch (TradePriceOutOfToleranceException e) {
log.warn("Trade price check failed because takers price is outside out tolerance.");
availabilityResult = AvailabilityResult.PRICE_OUT_OF_TOLERANCE;
} catch (MarketPriceNotAvailableException e) {
log.warn(e.getMessage());
availabilityResult = AvailabilityResult.MARKET_PRICE_NOT_AVAILABLE;
} catch (Throwable e) {
log.warn("Trade price check failed. " + e.getMessage());
availabilityResult = AvailabilityResult.UNKNOWN_FAILURE;
}
}
} else {
log.warn("acceptedArbitrators is null or empty: acceptedArbitrators=" + acceptedArbitrators);
availabilityResult = AvailabilityResult.NO_ARBITRATORS;
}
} else {
availabilityResult = AvailabilityResult.USER_IGNORED;
}
} else {
availabilityResult = AvailabilityResult.OFFER_TAKEN;
}
} else {
log.warn("handleOfferAvailabilityRequest: openOffer not found. That should never happen.");
availabilityResult = AvailabilityResult.OFFER_TAKEN;
}
try {
p2PService.sendEncryptedDirectMessage(sender, message.getPubKeyRing(), new OfferAvailabilityResponse(message.offerId, availabilityResult), new SendDirectMessageListener() {
@Override
public void onArrived() {
log.trace("OfferAvailabilityResponse successfully arrived at peer");
}
@Override
public void onFault() {
log.debug("Sending OfferAvailabilityResponse failed.");
}
});
} catch (Throwable t) {
t.printStackTrace();
log.debug("Exception at handleRequestIsOfferAvailableMessage " + t.getMessage());
}
} else {
log.debug("We have stopped already. We ignore that handleOfferAvailabilityRequest call.");
}
}
use of io.bitsquare.trade.exceptions.TradePriceOutOfToleranceException in project bitsquare by bitsquare.
the class ProcessPayDepositRequest method run.
@Override
protected void run() {
try {
runInterceptHook();
log.debug("current trade state " + trade.getState());
PayDepositRequest payDepositRequest = (PayDepositRequest) processModel.getTradeMessage();
checkNotNull(payDepositRequest);
checkTradeId(processModel.getId(), payDepositRequest);
PaymentAccountContractData paymentAccountContractData = checkNotNull(payDepositRequest.takerPaymentAccountContractData);
final PaymentAccountFilter[] appliedPaymentAccountFilter = new PaymentAccountFilter[1];
if (processModel.isPeersPaymentAccountDataAreBanned(paymentAccountContractData, appliedPaymentAccountFilter)) {
failed("Other trader is banned by his trading account data.\n" + "paymentAccountContractData=" + paymentAccountContractData.getPaymentDetails() + "\n" + "banFilter=" + appliedPaymentAccountFilter[0].toString());
return;
}
processModel.tradingPeer.setPaymentAccountContractData(paymentAccountContractData);
processModel.tradingPeer.setRawTransactionInputs(checkNotNull(payDepositRequest.rawTransactionInputs));
checkArgument(payDepositRequest.rawTransactionInputs.size() > 0);
processModel.tradingPeer.setChangeOutputValue(payDepositRequest.changeOutputValue);
if (payDepositRequest.changeOutputAddress != null)
processModel.tradingPeer.setChangeOutputAddress(payDepositRequest.changeOutputAddress);
processModel.tradingPeer.setMultiSigPubKey(checkNotNull(payDepositRequest.takerMultiSigPubKey));
processModel.tradingPeer.setPayoutAddressString(nonEmptyStringOf(payDepositRequest.takerPayoutAddressString));
processModel.tradingPeer.setPubKeyRing(checkNotNull(payDepositRequest.takerPubKeyRing));
processModel.tradingPeer.setAccountId(nonEmptyStringOf(payDepositRequest.takerAccountId));
trade.setTakeOfferFeeTxId(nonEmptyStringOf(payDepositRequest.takeOfferFeeTxId));
processModel.setTakerAcceptedArbitratorNodeAddresses(checkNotNull(payDepositRequest.acceptedArbitratorNodeAddresses));
if (payDepositRequest.acceptedArbitratorNodeAddresses.isEmpty())
failed("acceptedArbitratorNames must not be empty");
trade.applyArbitratorNodeAddress(checkNotNull(payDepositRequest.arbitratorNodeAddress));
try {
long takersTradePrice = payDepositRequest.tradePrice;
trade.getOffer().checkTradePriceTolerance(takersTradePrice);
trade.setTradePrice(takersTradePrice);
} catch (TradePriceOutOfToleranceException e) {
failed(e.getMessage());
} catch (Throwable e2) {
failed(e2);
}
checkArgument(payDepositRequest.tradeAmount > 0);
trade.setTradeAmount(Coin.valueOf(payDepositRequest.tradeAmount));
// check and update to the latest peer address of our peer if the payDepositRequest is correct
checkArgument(payDepositRequest.getSenderNodeAddress().equals(processModel.getTempTradingPeerNodeAddress()));
trade.setTradingPeerNodeAddress(processModel.getTempTradingPeerNodeAddress());
removeMailboxMessageAfterProcessing();
complete();
} catch (Throwable t) {
failed(t);
}
}
Aggregations