Search in sources :

Example 6 with TxBroadcastException

use of bisq.core.btc.wallet.TxBroadcastException 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.getFreshAddressEntry();
        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 TxBroadcaster.Callback() {

                @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(TxBroadcastException exception) {
                    if (!completed) {
                        failed(exception);
                    } 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 TxBroadcaster.Callback() {

                @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(TxBroadcastException exception) {
                    if (!completed) {
                        log.error(exception.toString());
                        exception.printStackTrace();
                        trade.setErrorMessage("An error occurred.\n" + "Error message:\n" + exception.getMessage());
                        failed(exception);
                    } 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) TxBroadcastException(bisq.core.btc.wallet.TxBroadcastException) TxBroadcaster(bisq.core.btc.wallet.TxBroadcaster) Transaction(org.bitcoinj.core.Transaction) BtcWalletService(bisq.core.btc.wallet.BtcWalletService) BsqWalletService(bisq.core.btc.wallet.BsqWalletService) NodeAddress(bisq.network.p2p.NodeAddress)

Example 7 with TxBroadcastException

use of bisq.core.btc.wallet.TxBroadcastException in project bisq-core by bisq-network.

the class LockupService method publishLockupTx.

public void publishLockupTx(Coin lockupAmount, int lockTime, LockupType lockupType, BondedRole bondedRole, ResultHandler resultHandler, ExceptionHandler exceptionHandler) {
    checkArgument(lockTime <= BondingConsensus.getMaxLockTime() && lockTime >= BondingConsensus.getMinLockTime(), "lockTime not in rage");
    try {
        byte[] hash = BondingConsensus.getHash(lockupType, bondedRole);
        byte[] opReturnData = BondingConsensus.getLockupOpReturnData(lockTime, lockupType, hash);
        final Transaction lockupTx = getLockupTx(lockupAmount, opReturnData);
        // noinspection Duplicates
        walletsManager.publishAndCommitBsqTx(lockupTx, new TxBroadcaster.Callback() {

            @Override
            public void onSuccess(Transaction transaction) {
                resultHandler.handleResult();
            }

            @Override
            public void onTxMalleability(TxMalleabilityException exception) {
                exceptionHandler.handleException(exception);
            }

            @Override
            public void onFailure(TxBroadcastException exception) {
                exceptionHandler.handleException(exception);
            }
        });
    } catch (TransactionVerificationException | InsufficientMoneyException | WalletException | IOException exception) {
        exceptionHandler.handleException(exception);
    }
}
Also used : WalletException(bisq.core.btc.exceptions.WalletException) TxMalleabilityException(bisq.core.btc.wallet.TxMalleabilityException) TxBroadcaster(bisq.core.btc.wallet.TxBroadcaster) Transaction(org.bitcoinj.core.Transaction) TransactionVerificationException(bisq.core.btc.exceptions.TransactionVerificationException) InsufficientMoneyException(org.bitcoinj.core.InsufficientMoneyException) IOException(java.io.IOException) TxBroadcastException(bisq.core.btc.wallet.TxBroadcastException)

Example 8 with TxBroadcastException

use of bisq.core.btc.wallet.TxBroadcastException in project bisq-core by bisq-network.

the class UnlockService method publishUnlockTx.

public void publishUnlockTx(String lockupTxId, ResultHandler resultHandler, ExceptionHandler exceptionHandler) {
    try {
        TxOutput lockupTxOutput = bsqStateService.getLockupTxOutput(lockupTxId).get();
        final Transaction unlockTx = getUnlockTx(lockupTxOutput);
        // noinspection Duplicates
        walletsManager.publishAndCommitBsqTx(unlockTx, new TxBroadcaster.Callback() {

            @Override
            public void onSuccess(Transaction transaction) {
                resultHandler.handleResult();
            }

            @Override
            public void onTxMalleability(TxMalleabilityException exception) {
                exceptionHandler.handleException(exception);
            }

            @Override
            public void onFailure(TxBroadcastException exception) {
                exceptionHandler.handleException(exception);
            }
        });
    } catch (TransactionVerificationException | InsufficientMoneyException | WalletException exception) {
        exceptionHandler.handleException(exception);
    }
}
Also used : WalletException(bisq.core.btc.exceptions.WalletException) TxOutput(bisq.core.dao.state.blockchain.TxOutput) TxMalleabilityException(bisq.core.btc.wallet.TxMalleabilityException) TxBroadcaster(bisq.core.btc.wallet.TxBroadcaster) Transaction(org.bitcoinj.core.Transaction) TransactionVerificationException(bisq.core.btc.exceptions.TransactionVerificationException) InsufficientMoneyException(org.bitcoinj.core.InsufficientMoneyException) TxBroadcastException(bisq.core.btc.wallet.TxBroadcastException)

Aggregations

TxBroadcastException (bisq.core.btc.wallet.TxBroadcastException)8 TxBroadcaster (bisq.core.btc.wallet.TxBroadcaster)8 Transaction (org.bitcoinj.core.Transaction)8 BtcWalletService (bisq.core.btc.wallet.BtcWalletService)5 AddressEntry (bisq.core.btc.AddressEntry)3 TransactionVerificationException (bisq.core.btc.exceptions.TransactionVerificationException)3 WalletException (bisq.core.btc.exceptions.WalletException)3 TradeWalletService (bisq.core.btc.wallet.TradeWalletService)3 Arbitrator (bisq.core.arbitration.Arbitrator)2 RawTransactionInput (bisq.core.btc.data.RawTransactionInput)2 BsqWalletService (bisq.core.btc.wallet.BsqWalletService)2 TxMalleabilityException (bisq.core.btc.wallet.TxMalleabilityException)2 Contract (bisq.core.trade.Contract)2 TradingPeer (bisq.core.trade.protocol.TradingPeer)2 NodeAddress (bisq.network.p2p.NodeAddress)2 Address (org.bitcoinj.core.Address)2 InsufficientMoneyException (org.bitcoinj.core.InsufficientMoneyException)2 Timer (bisq.common.Timer)1 UserThread (bisq.common.UserThread)1 KeyRing (bisq.common.crypto.KeyRing)1