Search in sources :

Example 1 with TradePriceOutOfToleranceException

use of bisq.core.exceptions.TradePriceOutOfToleranceException in project bisq-core by bisq-network.

the class MakerProcessPayDepositRequest method run.

@Override
protected void run() {
    try {
        runInterceptHook();
        log.debug("current trade state " + trade.getState());
        PayDepositRequest payDepositRequest = (PayDepositRequest) processModel.getTradeMessage();
        checkNotNull(payDepositRequest);
        checkTradeId(processModel.getOfferId(), payDepositRequest);
        final TradingPeer tradingPeer = processModel.getTradingPeer();
        tradingPeer.setPaymentAccountPayload(checkNotNull(payDepositRequest.getTakerPaymentAccountPayload()));
        tradingPeer.setRawTransactionInputs(checkNotNull(payDepositRequest.getRawTransactionInputs()));
        checkArgument(payDepositRequest.getRawTransactionInputs().size() > 0);
        tradingPeer.setChangeOutputValue(payDepositRequest.getChangeOutputValue());
        tradingPeer.setChangeOutputAddress(payDepositRequest.getChangeOutputAddress());
        tradingPeer.setMultiSigPubKey(checkNotNull(payDepositRequest.getTakerMultiSigPubKey()));
        tradingPeer.setPayoutAddressString(nonEmptyStringOf(payDepositRequest.getTakerPayoutAddressString()));
        tradingPeer.setPubKeyRing(checkNotNull(payDepositRequest.getTakerPubKeyRing()));
        tradingPeer.setAccountId(nonEmptyStringOf(payDepositRequest.getTakerAccountId()));
        trade.setTakerFeeTxId(nonEmptyStringOf(payDepositRequest.getTakerFeeTxId()));
        processModel.setTakerAcceptedArbitratorNodeAddresses(checkNotNull(payDepositRequest.getAcceptedArbitratorNodeAddresses()));
        processModel.setTakerAcceptedMediatorNodeAddresses(checkNotNull(payDepositRequest.getAcceptedMediatorNodeAddresses()));
        if (payDepositRequest.getAcceptedArbitratorNodeAddresses().isEmpty())
            failed("acceptedArbitratorNodeAddresses must not be empty");
        // Taker has to sign offerId (he cannot manipulate that - so we avoid to have a challenge protocol for passing the nonce we want to get signed)
        tradingPeer.setAccountAgeWitnessNonce(trade.getOffer().getId().getBytes());
        tradingPeer.setAccountAgeWitnessSignature(payDepositRequest.getAccountAgeWitnessSignatureOfOfferId());
        tradingPeer.setCurrentDate(payDepositRequest.getCurrentDate());
        trade.setArbitratorNodeAddress(checkNotNull(payDepositRequest.getArbitratorNodeAddress()));
        trade.setMediatorNodeAddress(checkNotNull(payDepositRequest.getMediatorNodeAddress()));
        try {
            long takersTradePrice = payDepositRequest.getTradePrice();
            trade.getOffer().checkTradePriceTolerance(takersTradePrice);
            trade.setTradePrice(takersTradePrice);
        } catch (TradePriceOutOfToleranceException e) {
            failed(e.getMessage());
        } catch (Throwable e2) {
            failed(e2);
        }
        checkArgument(payDepositRequest.getTradeAmount() > 0);
        trade.setTradeAmount(Coin.valueOf(payDepositRequest.getTradeAmount()));
        trade.setTradingPeerNodeAddress(processModel.getTempTradingPeerNodeAddress());
        processModel.removeMailboxMessageAfterProcessing(trade);
        complete();
    } catch (Throwable t) {
        failed(t);
    }
}
Also used : PayDepositRequest(bisq.core.trade.messages.PayDepositRequest) TradingPeer(bisq.core.trade.protocol.TradingPeer) TradePriceOutOfToleranceException(bisq.core.exceptions.TradePriceOutOfToleranceException)

Example 2 with TradePriceOutOfToleranceException

use of bisq.core.exceptions.TradePriceOutOfToleranceException in project bisq-core by bisq-network.

the class OpenOfferManager method handleOfferAvailabilityRequest.

// /////////////////////////////////////////////////////////////////////////////////////////
// OfferPayload Availability
// /////////////////////////////////////////////////////////////////////////////////////////
private void handleOfferAvailabilityRequest(OfferAvailabilityRequest message, NodeAddress sender) {
    log.trace("handleNewMessage: message = " + message.getClass().getSimpleName() + " from " + sender);
    if (p2PService.isBootstrapped()) {
        if (!stopped) {
            try {
                Validator.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.getMakerNodeAddress().getHostNameWithoutPostFix())).findAny().isPresent()) {
                        availabilityResult = AvailabilityResult.AVAILABLE;
                        // TODO mediators not impl yet
                        List<NodeAddress> acceptedArbitrators = user.getAcceptedArbitratorAddresses();
                        if (acceptedArbitrators != null && !acceptedArbitrators.isEmpty()) {
                            // losses and therefore an outdated market price.
                            try {
                                offer.checkTradePriceTolerance(message.getTakersTradePrice());
                            } 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.");
        }
    } else {
        log.info("We got a handleOfferAvailabilityRequest but we have not bootstrapped yet.");
    }
}
Also used : BtcWalletService(bisq.core.btc.wallet.BtcWalletService) PlaceOfferProtocol(bisq.core.offer.placeoffer.PlaceOfferProtocol) Coin(org.bitcoinj.core.Coin) LoggerFactory(org.slf4j.LoggerFactory) Timer(bisq.common.Timer) User(bisq.core.user.User) PersistenceProtoResolver(bisq.common.proto.persistable.PersistenceProtoResolver) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) TradePriceOutOfToleranceException(bisq.core.exceptions.TradePriceOutOfToleranceException) BootstrapListener(bisq.network.p2p.BootstrapListener) ErrorMessageHandler(bisq.common.handlers.ErrorMessageHandler) Named(javax.inject.Named) PlaceOfferModel(bisq.core.offer.placeoffer.PlaceOfferModel) Validator(bisq.core.util.Validator) Nullable(javax.annotation.Nullable) OfferAvailabilityResponse(bisq.core.offer.messages.OfferAvailabilityResponse) ClosedTradableManager(bisq.core.trade.closed.ClosedTradableManager) PeerManager(bisq.network.p2p.peers.PeerManager) NetworkEnvelope(bisq.common.proto.network.NetworkEnvelope) Logger(org.slf4j.Logger) OfferAvailabilityRequest(bisq.core.offer.messages.OfferAvailabilityRequest) P2PService(bisq.network.p2p.P2PService) TransactionResultHandler(bisq.core.trade.handlers.TransactionResultHandler) PersistedDataHost(bisq.common.proto.persistable.PersistedDataHost) ResultHandler(bisq.common.handlers.ResultHandler) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Set(java.util.Set) Log(bisq.common.app.Log) Collectors(java.util.stream.Collectors) TradableList(bisq.core.trade.TradableList) File(java.io.File) BsqWalletService(bisq.core.btc.wallet.BsqWalletService) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) PriceFeedService(bisq.core.provider.price.PriceFeedService) TradeWalletService(bisq.core.btc.wallet.TradeWalletService) NodeAddress(bisq.network.p2p.NodeAddress) SendDirectMessageListener(bisq.network.p2p.SendDirectMessageListener) Storage(bisq.common.storage.Storage) Preferences(bisq.core.user.Preferences) UserThread(bisq.common.UserThread) Optional(java.util.Optional) KeyRing(bisq.common.crypto.KeyRing) ObservableList(javafx.collections.ObservableList) DecryptedDirectMessageListener(bisq.network.p2p.DecryptedDirectMessageListener) DecryptedMessageWithPubKey(bisq.network.p2p.DecryptedMessageWithPubKey) NotNull(org.jetbrains.annotations.NotNull) TradePriceOutOfToleranceException(bisq.core.exceptions.TradePriceOutOfToleranceException) OfferAvailabilityResponse(bisq.core.offer.messages.OfferAvailabilityResponse) SendDirectMessageListener(bisq.network.p2p.SendDirectMessageListener) NodeAddress(bisq.network.p2p.NodeAddress)

Example 3 with TradePriceOutOfToleranceException

use of bisq.core.exceptions.TradePriceOutOfToleranceException in project bisq-core by bisq-network.

the class Offer method checkTradePriceTolerance.

public void checkTradePriceTolerance(long takersTradePrice) throws TradePriceOutOfToleranceException, MarketPriceNotAvailableException, IllegalArgumentException {
    Price tradePrice = Price.valueOf(getCurrencyCode(), takersTradePrice);
    Price offerPrice = getPrice();
    if (offerPrice == null)
        throw new MarketPriceNotAvailableException("Market price required for calculating trade price is not available.");
    checkArgument(takersTradePrice > 0, "takersTradePrice must be positive");
    double factor = (double) takersTradePrice / (double) offerPrice.getValue();
    // from one provider.
    if (Math.abs(1 - factor) > PRICE_TOLERANCE) {
        String msg = "Taker's trade price is too far away from our calculated price based on the market price.\n" + "tradePrice=" + tradePrice.getValue() + "\n" + "offerPrice=" + offerPrice.getValue();
        log.warn(msg);
        throw new TradePriceOutOfToleranceException(msg);
    }
}
Also used : Price(bisq.core.monetary.Price) MarketPrice(bisq.core.provider.price.MarketPrice) TradePriceOutOfToleranceException(bisq.core.exceptions.TradePriceOutOfToleranceException)

Aggregations

TradePriceOutOfToleranceException (bisq.core.exceptions.TradePriceOutOfToleranceException)3 Timer (bisq.common.Timer)1 UserThread (bisq.common.UserThread)1 Log (bisq.common.app.Log)1 KeyRing (bisq.common.crypto.KeyRing)1 ErrorMessageHandler (bisq.common.handlers.ErrorMessageHandler)1 ResultHandler (bisq.common.handlers.ResultHandler)1 NetworkEnvelope (bisq.common.proto.network.NetworkEnvelope)1 PersistedDataHost (bisq.common.proto.persistable.PersistedDataHost)1 PersistenceProtoResolver (bisq.common.proto.persistable.PersistenceProtoResolver)1 Storage (bisq.common.storage.Storage)1 BsqWalletService (bisq.core.btc.wallet.BsqWalletService)1 BtcWalletService (bisq.core.btc.wallet.BtcWalletService)1 TradeWalletService (bisq.core.btc.wallet.TradeWalletService)1 Price (bisq.core.monetary.Price)1 OfferAvailabilityRequest (bisq.core.offer.messages.OfferAvailabilityRequest)1 OfferAvailabilityResponse (bisq.core.offer.messages.OfferAvailabilityResponse)1 PlaceOfferModel (bisq.core.offer.placeoffer.PlaceOfferModel)1 PlaceOfferProtocol (bisq.core.offer.placeoffer.PlaceOfferProtocol)1 MarketPrice (bisq.core.provider.price.MarketPrice)1