Search in sources :

Example 6 with Arbitrator

use of bisq.core.arbitration.Arbitrator in project bisq-core by bisq-network.

the class CreateMakerFeeTx method run.

@Override
protected void run() {
    Offer offer = model.getOffer();
    try {
        runInterceptHook();
        String id = offer.getId();
        BtcWalletService walletService = model.getWalletService();
        NodeAddress selectedArbitratorNodeAddress = ArbitratorSelectionRule.select(model.getUser().getAcceptedArbitratorAddresses(), model.getOffer());
        log.debug("selectedArbitratorAddress " + selectedArbitratorNodeAddress);
        Arbitrator selectedArbitrator = model.getUser().getAcceptedArbitratorByAddress(selectedArbitratorNodeAddress);
        checkNotNull(selectedArbitrator, "selectedArbitrator must not be null at CreateOfferFeeTx");
        Address fundingAddress = walletService.getOrCreateAddressEntry(id, AddressEntry.Context.OFFER_FUNDING).getAddress();
        Address reservedForTradeAddress = walletService.getOrCreateAddressEntry(id, AddressEntry.Context.RESERVED_FOR_TRADE).getAddress();
        Address changeAddress = walletService.getOrCreateAddressEntry(AddressEntry.Context.AVAILABLE).getAddress();
        final TradeWalletService tradeWalletService = model.getTradeWalletService();
        if (offer.isCurrencyForMakerFeeBtc()) {
            tradeFeeTx = tradeWalletService.createBtcTradingFeeTx(fundingAddress, reservedForTradeAddress, changeAddress, model.getReservedFundsForOffer(), model.isUseSavingsWallet(), offer.getMakerFee(), offer.getTxFee(), selectedArbitrator.getBtcAddress(), new FutureCallback<Transaction>() {

                @Override
                public void onSuccess(Transaction transaction) {
                    // we delay one render frame to be sure we don't get called before the method call has
                    // returned (tradeFeeTx would be null in that case)
                    UserThread.execute(() -> {
                        if (!completed) {
                            offer.setOfferFeePaymentTxId(transaction.getHashAsString());
                            model.setTransaction(transaction);
                            walletService.swapTradeEntryToAvailableEntry(id, AddressEntry.Context.OFFER_FUNDING);
                            model.getOffer().setState(Offer.State.OFFER_FEE_PAID);
                            complete();
                        } else {
                            log.warn("We got the onSuccess callback called after the timeout has been triggered a complete().");
                        }
                    });
                }

                @Override
                public void onFailure(@NotNull Throwable t) {
                    if (!completed) {
                        failed(t);
                    } else {
                        log.warn("We got the onFailure callback called after the timeout has been triggered a complete().");
                    }
                }
            });
        } else {
            final BsqWalletService bsqWalletService = model.getBsqWalletService();
            Transaction preparedBurnFeeTx = model.getBsqWalletService().getPreparedBurnFeeTx(offer.getMakerFee());
            Transaction txWithBsqFee = tradeWalletService.completeBsqTradingFeeTx(preparedBurnFeeTx, fundingAddress, reservedForTradeAddress, changeAddress, model.getReservedFundsForOffer(), model.isUseSavingsWallet(), offer.getTxFee());
            Transaction signedTx = model.getBsqWalletService().signTx(txWithBsqFee);
            WalletService.checkAllScriptSignaturesForTx(signedTx);
            bsqWalletService.commitTx(signedTx);
            // We need to create another instance, otherwise the tx would trigger an invalid state exception
            // if it gets committed 2 times
            tradeWalletService.commitTx(tradeWalletService.getClonedTransaction(signedTx));
            bsqWalletService.broadcastTx(signedTx, new FutureCallback<Transaction>() {

                @Override
                public void onSuccess(@Nullable Transaction transaction) {
                    if (transaction != null) {
                        offer.setOfferFeePaymentTxId(transaction.getHashAsString());
                        model.setTransaction(transaction);
                        log.debug("onSuccess, offerId={}, OFFER_FUNDING", id);
                        walletService.swapTradeEntryToAvailableEntry(id, AddressEntry.Context.OFFER_FUNDING);
                        log.debug("Successfully sent tx with id " + transaction.getHashAsString());
                        model.getOffer().setState(Offer.State.OFFER_FEE_PAID);
                        complete();
                    }
                }

                @Override
                public void onFailure(@NotNull Throwable t) {
                    log.error(t.toString());
                    t.printStackTrace();
                    offer.setErrorMessage("An error occurred.\n" + "Error message:\n" + t.getMessage());
                    failed(t);
                }
            });
        }
    } catch (Throwable t) {
        offer.setErrorMessage("An error occurred.\n" + "Error message:\n" + t.getMessage());
        failed(t);
    }
}
Also used : NodeAddress(bisq.network.p2p.NodeAddress) Address(org.bitcoinj.core.Address) TradeWalletService(bisq.core.btc.wallet.TradeWalletService) Arbitrator(bisq.core.arbitration.Arbitrator) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) NotNull(org.jetbrains.annotations.NotNull) Transaction(org.bitcoinj.core.Transaction) Offer(bisq.core.offer.Offer) BtcWalletService(bisq.core.btc.wallet.BtcWalletService) BsqWalletService(bisq.core.btc.wallet.BsqWalletService) NodeAddress(bisq.network.p2p.NodeAddress) FutureCallback(com.google.common.util.concurrent.FutureCallback)

Example 7 with Arbitrator

use of bisq.core.arbitration.Arbitrator in project bisq-core by bisq-network.

the class CreateTakerFeeTx method run.

@Override
protected void run() {
    try {
        runInterceptHook();
        User user = processModel.getUser();
        NodeAddress selectedArbitratorNodeAddress = ArbitratorSelectionRule.select(user.getAcceptedArbitratorAddresses(), processModel.getOffer());
        log.debug("selectedArbitratorAddress " + selectedArbitratorNodeAddress);
        Arbitrator selectedArbitrator = user.getAcceptedArbitratorByAddress(selectedArbitratorNodeAddress);
        checkNotNull(selectedArbitrator, "selectedArbitrator must not be null at CreateTakeOfferFeeTx");
        BtcWalletService walletService = processModel.getBtcWalletService();
        String id = processModel.getOffer().getId();
        AddressEntry addressEntry = walletService.getOrCreateAddressEntry(id, AddressEntry.Context.OFFER_FUNDING);
        AddressEntry reservedForTradeAddressEntry = walletService.getOrCreateAddressEntry(id, AddressEntry.Context.RESERVED_FOR_TRADE);
        AddressEntry changeAddressEntry = walletService.getOrCreateAddressEntry(AddressEntry.Context.AVAILABLE);
        Address fundingAddress = addressEntry.getAddress();
        Address reservedForTradeAddress = reservedForTradeAddressEntry.getAddress();
        Address changeAddress = changeAddressEntry.getAddress();
        final TradeWalletService tradeWalletService = processModel.getTradeWalletService();
        if (trade.isCurrencyForTakerFeeBtc()) {
            tradeFeeTx = tradeWalletService.createBtcTradingFeeTx(fundingAddress, reservedForTradeAddress, changeAddress, processModel.getFundsNeededForTradeAsLong(), processModel.isUseSavingsWallet(), trade.getTakerFee(), trade.getTxFee(), selectedArbitrator.getBtcAddress(), new FutureCallback<Transaction>() {

                @Override
                public void onSuccess(Transaction transaction) {
                    // we delay one render frame to be sure we don't get called before the method call has
                    // returned (tradeFeeTx would be null in that case)
                    UserThread.execute(() -> {
                        if (!completed) {
                            processModel.setTakeOfferFeeTx(tradeFeeTx);
                            trade.setTakerFeeTxId(tradeFeeTx.getHashAsString());
                            walletService.swapTradeEntryToAvailableEntry(id, AddressEntry.Context.OFFER_FUNDING);
                            trade.setState(Trade.State.TAKER_PUBLISHED_TAKER_FEE_TX);
                            complete();
                        } else {
                            log.warn("We got the onSuccess callback called after the timeout has been triggered a complete().");
                        }
                    });
                }

                @Override
                public void onFailure(@NotNull Throwable t) {
                    if (!completed) {
                        failed(t);
                    } else {
                        log.warn("We got the onFailure callback called after the timeout has been triggered a complete().");
                    }
                }
            });
        } else {
            final BsqWalletService bsqWalletService = processModel.getBsqWalletService();
            Transaction preparedBurnFeeTx = processModel.getBsqWalletService().getPreparedBurnFeeTx(trade.getTakerFee());
            Transaction txWithBsqFee = tradeWalletService.completeBsqTradingFeeTx(preparedBurnFeeTx, fundingAddress, reservedForTradeAddress, changeAddress, processModel.getFundsNeededForTradeAsLong(), processModel.isUseSavingsWallet(), trade.getTxFee());
            Transaction signedTx = processModel.getBsqWalletService().signTx(txWithBsqFee);
            WalletService.checkAllScriptSignaturesForTx(signedTx);
            bsqWalletService.commitTx(signedTx);
            // We need to create another instance, otherwise the tx would trigger an invalid state exception
            // if it gets committed 2 times
            tradeWalletService.commitTx(tradeWalletService.getClonedTransaction(signedTx));
            bsqWalletService.broadcastTx(signedTx, new FutureCallback<Transaction>() {

                @Override
                public void onSuccess(@Nullable Transaction transaction) {
                    if (!completed) {
                        if (transaction != null) {
                            log.debug("Successfully sent tx with id " + transaction.getHashAsString());
                            trade.setTakerFeeTxId(transaction.getHashAsString());
                            processModel.setTakeOfferFeeTx(transaction);
                            walletService.swapTradeEntryToAvailableEntry(id, AddressEntry.Context.OFFER_FUNDING);
                            trade.setState(Trade.State.TAKER_PUBLISHED_TAKER_FEE_TX);
                            complete();
                        }
                    } else {
                        log.warn("We got the onSuccess callback called after the timeout has been triggered a complete().");
                    }
                }

                @Override
                public void onFailure(@NotNull Throwable t) {
                    if (!completed) {
                        log.error(t.toString());
                        t.printStackTrace();
                        trade.setErrorMessage("An error occurred.\n" + "Error message:\n" + t.getMessage());
                        failed(t);
                    } else {
                        log.warn("We got the onFailure callback called after the timeout has been triggered a complete().");
                    }
                }
            });
        }
    } catch (Throwable t) {
        failed(t);
    }
}
Also used : User(bisq.core.user.User) NodeAddress(bisq.network.p2p.NodeAddress) Address(org.bitcoinj.core.Address) AddressEntry(bisq.core.btc.AddressEntry) TradeWalletService(bisq.core.btc.wallet.TradeWalletService) Arbitrator(bisq.core.arbitration.Arbitrator) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) NotNull(org.jetbrains.annotations.NotNull) Transaction(org.bitcoinj.core.Transaction) BtcWalletService(bisq.core.btc.wallet.BtcWalletService) BsqWalletService(bisq.core.btc.wallet.BsqWalletService) NodeAddress(bisq.network.p2p.NodeAddress) FutureCallback(com.google.common.util.concurrent.FutureCallback)

Aggregations

Arbitrator (bisq.core.arbitration.Arbitrator)7 AddressEntry (bisq.core.btc.AddressEntry)2 BsqWalletService (bisq.core.btc.wallet.BsqWalletService)2 BtcWalletService (bisq.core.btc.wallet.BtcWalletService)2 TradeWalletService (bisq.core.btc.wallet.TradeWalletService)2 NodeAddress (bisq.network.p2p.NodeAddress)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)2 FutureCallback (com.google.common.util.concurrent.FutureCallback)2 Address (org.bitcoinj.core.Address)2 Transaction (org.bitcoinj.core.Transaction)2 NotNull (org.jetbrains.annotations.NotNull)2 Offer (bisq.core.offer.Offer)1 User (bisq.core.user.User)1 Date (java.util.Date)1