Search in sources :

Example 6 with Tx

use of bisq.core.dao.blockchain.vo.Tx in project bisq-desktop by bisq-network.

the class VoteListItem method setupConfidence.

private void setupConfidence() {
    calculateStake();
    final Tx tx = readableBsqBlockChain.getTxMap().get(myVote.getBlindVote().getTxId());
    if (tx != null) {
        final String txId = tx.getId();
        // We cache the walletTransaction once found
        if (walletTransaction == null) {
            final Optional<Transaction> transactionOptional = bsqWalletService.isWalletTransaction(txId);
            transactionOptional.ifPresent(transaction -> walletTransaction = transaction);
        }
        if (walletTransaction != null) {
            // It is our tx so we get confidence updates
            if (txConfidenceListener == null) {
                txConfidenceListener = new TxConfidenceListener(txId) {

                    @Override
                    public void onTransactionConfidenceChanged(TransactionConfidence confidence) {
                        updateConfidence(confidence.getConfidenceType(), confidence.getDepthInBlocks(), confidence.numBroadcastPeers());
                    }
                };
                bsqWalletService.addTxConfidenceListener(txConfidenceListener);
            }
        } else {
            // tx from other users, we dont get confidence updates but as we have the bsq tx we can calculate it
            // we get setupConfidence called at each new block from above listener so no need to register a new listener
            int depth = bsqWalletService.getChainHeightProperty().get() - tx.getBlockHeight() + 1;
            if (depth > 0)
                updateConfidence(TransactionConfidence.ConfidenceType.BUILDING, depth, -1);
        // log.error("name={}, id ={}, depth={}", compensationRequest.getPayload().getName(), compensationRequest.getPayload().getUid(), depth);
        }
        final TransactionConfidence confidence = bsqWalletService.getConfidenceForTxId(txId);
        if (confidence != null)
            updateConfidence(confidence, confidence.getDepthInBlocks());
    }
}
Also used : Tx(bisq.core.dao.blockchain.vo.Tx) Transaction(org.bitcoinj.core.Transaction) TxConfidenceListener(bisq.core.btc.listeners.TxConfidenceListener) ToString(lombok.ToString) TransactionConfidence(org.bitcoinj.core.TransactionConfidence)

Example 7 with Tx

use of bisq.core.dao.blockchain.vo.Tx in project bisq-desktop by bisq-network.

the class BsqTxView method getCompensationRequestTxListItems.

// We add manually a modified copy of the compensation request tx if it has become an issuance tx
// It is a bit weird to have one tx displayed 2 times but I think it is better to show both aspects
// separately. First the compensation request tx with the fee then after voting the issuance.
private List<BsqTxListItem> getCompensationRequestTxListItems(List<BsqTxListItem> items) {
    List<BsqTxListItem> issuanceTxList = new ArrayList<>();
    items.stream().filter(item -> item.getTxType() == TxType.COMPENSATION_REQUEST).peek(item -> {
        final Tx tx = readableBsqBlockChain.getTx(item.getTxId()).get();
        // We have mandatory BSQ change at output 0
        long changeValue = tx.getOutputs().get(0).getValue();
        long inputValue = tx.getInputs().stream().filter(input -> input.getConnectedTxOutput() != null).mapToLong(input -> input.getConnectedTxOutput().getValue()).sum();
        // We want to show fee as negative number
        long fee = changeValue - inputValue;
        item.setAmount(Coin.valueOf(fee));
    }).filter(item -> {
        final Optional<Tx> optionalTx = readableBsqBlockChain.getTx(item.getTxId());
        if (optionalTx.isPresent()) {
            final List<TxOutput> outputs = optionalTx.get().getOutputs();
            if (!outputs.isEmpty()) {
                return outputs.get(0).getTxOutputType() == TxOutputType.BSQ_OUTPUT;
            }
        }
        return false;
    }).forEach(item -> {
        final Tx tx = readableBsqBlockChain.getTx(item.getTxId()).get();
        final int blockHeight = tx.getBlockHeight();
        final int issuanceBlockHeight = daoPeriodService.getAbsoluteStartBlockOfPhase(blockHeight, DaoPeriodService.Phase.ISSUANCE);
        // We use the time of the block height of the start of the issuance period
        final long blockTimeInSec = readableBsqBlockChain.getBlockTime(issuanceBlockHeight);
        final BsqTxListItem issuanceItem = new BsqTxListItem(item.getTransaction(), bsqWalletService, btcWalletService, Optional.of(TxType.ISSUANCE), item.isBurnedBsqTx(), new Date(blockTimeInSec * 1000), bsqFormatter);
        // On output 1 we have the issuance candidate
        long issuanceValue = tx.getOutputs().get(1).getValue();
        issuanceItem.setAmount(Coin.valueOf(issuanceValue));
        issuanceTxList.add(issuanceItem);
    });
    return issuanceTxList;
}
Also used : Transaction(org.bitcoinj.core.Transaction) HyperlinkWithIcon(bisq.desktop.components.HyperlinkWithIcon) Coin(org.bitcoinj.core.Coin) Date(java.util.Date) ReadableBsqBlockChain(bisq.core.dao.blockchain.ReadableBsqBlockChain) VBox(javafx.scene.layout.VBox) BsqBalanceListener(bisq.core.btc.wallet.BsqBalanceListener) BsqNode(bisq.core.dao.node.BsqNode) ReadOnlyObjectWrapper(javafx.beans.property.ReadOnlyObjectWrapper) ListChangeListener(javafx.collections.ListChangeListener) Res(bisq.core.locale.Res) TableView(javafx.scene.control.TableView) Pane(javafx.scene.layout.Pane) SortedList(javafx.collections.transformation.SortedList) HBox(javafx.scene.layout.HBox) AutoTooltipTableColumn(bisq.desktop.components.AutoTooltipTableColumn) AutoTooltipLabel(bisq.desktop.components.AutoTooltipLabel) FormBuilder(bisq.desktop.util.FormBuilder) TxOutput(bisq.core.dao.blockchain.vo.TxOutput) Collectors(java.util.stream.Collectors) BsqWalletService(bisq.core.btc.wallet.BsqWalletService) BsqBalanceUtil(bisq.desktop.main.dao.wallet.BsqBalanceUtil) List(java.util.List) Preferences(bisq.core.user.Preferences) SimpleDoubleProperty(javafx.beans.property.SimpleDoubleProperty) Optional(java.util.Optional) ObservableList(javafx.collections.ObservableList) AwesomeIcon(de.jensd.fx.fontawesome.AwesomeIcon) GUIUtil(bisq.desktop.util.GUIUtil) BtcWalletService(bisq.core.btc.wallet.BtcWalletService) ActivatableView(bisq.desktop.common.view.ActivatableView) FXCollections(javafx.collections.FXCollections) DoubleProperty(javafx.beans.property.DoubleProperty) BsqNodeProvider(bisq.core.dao.node.BsqNodeProvider) FxmlView(bisq.desktop.common.view.FxmlView) TableColumn(javafx.scene.control.TableColumn) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) TableCell(javafx.scene.control.TableCell) ProgressBar(javafx.scene.control.ProgressBar) Insets(javafx.geometry.Insets) AddressWithIconAndDirection(bisq.desktop.components.AddressWithIconAndDirection) Callback(javafx.util.Callback) Tooltip(javafx.scene.control.Tooltip) GridPane(javafx.scene.layout.GridPane) Label(javafx.scene.control.Label) DaoPeriodService(bisq.core.dao.DaoPeriodService) Tx(bisq.core.dao.blockchain.vo.Tx) AwesomeDude(de.jensd.fx.fontawesome.AwesomeDude) BisqEnvironment(bisq.core.app.BisqEnvironment) TxType(bisq.core.dao.blockchain.vo.TxType) BsqFormatter(bisq.desktop.util.BsqFormatter) TxOutputType(bisq.core.dao.blockchain.vo.TxOutputType) ChangeListener(javafx.beans.value.ChangeListener) Tx(bisq.core.dao.blockchain.vo.Tx) Optional(java.util.Optional) ArrayList(java.util.ArrayList) SortedList(javafx.collections.transformation.SortedList) List(java.util.List) ObservableList(javafx.collections.ObservableList) ArrayList(java.util.ArrayList) Date(java.util.Date)

Example 8 with Tx

use of bisq.core.dao.blockchain.vo.Tx in project bisq-core by bisq-network.

the class BsqWalletService method getValueSentFromMeForTransaction.

@Override
public Coin getValueSentFromMeForTransaction(Transaction transaction) throws ScriptException {
    Coin result = Coin.ZERO;
    // We check all our inputs and get the connected outputs.
    for (int i = 0; i < transaction.getInputs().size(); i++) {
        TransactionInput input = transaction.getInputs().get(i);
        // We grab the connected output for that input
        TransactionOutput connectedOutput = input.getConnectedOutput();
        if (connectedOutput != null) {
            // We grab the parent tx of the connected output
            final Transaction parentTransaction = connectedOutput.getParentTransaction();
            final boolean isConfirmed = parentTransaction != null && parentTransaction.getConfidence().getConfidenceType() == TransactionConfidence.ConfidenceType.BUILDING;
            if (connectedOutput.isMineOrWatched(wallet)) {
                if (isConfirmed) {
                    // We lookup if we have a BSQ tx matching the parent tx
                    // We cannot make that findTx call outside of the loop as the parent tx can change at each iteration
                    Optional<Tx> txOptional = readableBsqBlockChain.getTx(parentTransaction.getHash().toString());
                    if (txOptional.isPresent()) {
                        // BSQ tx and BitcoinJ tx have same outputs (mirrored data structure)
                        TxOutput txOutput = txOptional.get().getOutputs().get(connectedOutput.getIndex());
                        if (txOutput.isVerified()) {
                            // TODO check why values are not the same
                            if (txOutput.getValue() != connectedOutput.getValue().value)
                                log.warn("getValueSentToMeForTransaction: Value of BSQ output do not match BitcoinJ tx output. " + "txOutput.getValue()={}, output.getValue().value={}, txId={}", txOutput.getValue(), connectedOutput.getValue().value, txOptional.get().getId());
                            // If it is a valid BSQ output we add it
                            result = result.add(Coin.valueOf(txOutput.getValue()));
                        }
                    }
                }
            /*else {
                        // TODO atm we don't display amounts of unconfirmed txs but that might change so we leave that code
                        // if it will be required
                        // If the tx is not confirmed yet we add the value and assume it is a valid BSQ output.
                        result = result.add(connectedOutput.getValue());
                    }*/
            }
        }
    }
    return result;
}
Also used : Coin(org.bitcoinj.core.Coin) TxOutput(bisq.core.dao.blockchain.vo.TxOutput) TransactionOutput(org.bitcoinj.core.TransactionOutput) Transaction(org.bitcoinj.core.Transaction) Tx(bisq.core.dao.blockchain.vo.Tx) TransactionOutPoint(org.bitcoinj.core.TransactionOutPoint) TransactionInput(org.bitcoinj.core.TransactionInput)

Example 9 with Tx

use of bisq.core.dao.blockchain.vo.Tx in project bisq-core by bisq-network.

the class BsqWalletService method getValueSentToMeForTransaction.

@Override
public Coin getValueSentToMeForTransaction(Transaction transaction) throws ScriptException {
    Coin result = Coin.ZERO;
    final String txId = transaction.getHashAsString();
    // We check if we have a matching BSQ tx. We do that call here to avoid repeated calls in the loop.
    Optional<Tx> txOptional = readableBsqBlockChain.getTx(txId);
    // We check all the outputs of our tx
    for (int i = 0; i < transaction.getOutputs().size(); i++) {
        TransactionOutput output = transaction.getOutputs().get(i);
        final boolean isConfirmed = output.getParentTransaction() != null && output.getParentTransaction().getConfidence().getConfidenceType() == TransactionConfidence.ConfidenceType.BUILDING;
        if (output.isMineOrWatched(wallet)) {
            if (isConfirmed) {
                if (txOptional.isPresent()) {
                    // The index of the BSQ tx outputs are the same like the bitcoinj tx outputs
                    TxOutput txOutput = txOptional.get().getOutputs().get(i);
                    if (txOutput.isVerified()) {
                        // TODO check why values are not the same
                        if (txOutput.getValue() != output.getValue().value)
                            log.warn("getValueSentToMeForTransaction: Value of BSQ output do not match BitcoinJ tx output. " + "txOutput.getValue()={}, output.getValue().value={}, txId={}", txOutput.getValue(), output.getValue().value, txId);
                        // If it is a valid BSQ output we add it
                        result = result.add(Coin.valueOf(txOutput.getValue()));
                    }
                }
            }
        /*else {
                    // TODO atm we don't display amounts of unconfirmed txs but that might change so we leave that code
                    // if it will be required
                    // If the tx is not confirmed yet we add the value and assume it is a valid BSQ output.
                    result = result.add(output.getValue());
                }*/
        }
    }
    return result;
}
Also used : Coin(org.bitcoinj.core.Coin) TxOutput(bisq.core.dao.blockchain.vo.TxOutput) TransactionOutput(org.bitcoinj.core.TransactionOutput) Tx(bisq.core.dao.blockchain.vo.Tx) TransactionOutPoint(org.bitcoinj.core.TransactionOutPoint)

Example 10 with Tx

use of bisq.core.dao.blockchain.vo.Tx in project bisq-core by bisq-network.

the class BsqWalletService method updateBsqBalance.

// /////////////////////////////////////////////////////////////////////////////////////////
// Balance
// /////////////////////////////////////////////////////////////////////////////////////////
private void updateBsqBalance() {
    pendingBalance = Coin.valueOf(getTransactions(false).stream().flatMap(tx -> tx.getOutputs().stream()).filter(out -> {
        final Transaction parentTx = out.getParentTransaction();
        return parentTx != null && out.isMine(wallet) && parentTx.getConfidence().getConfidenceType() == PENDING;
    }).mapToLong(out -> out.getValue().value).sum());
    Set<String> confirmedTxIdSet = getTransactions(false).stream().filter(tx -> tx.getConfidence().getConfidenceType() == BUILDING).map(Transaction::getHashAsString).collect(Collectors.toSet());
    lockedForVotingBalance = Coin.valueOf(readableBsqBlockChain.getLockedForVoteTxOutputs().stream().filter(txOutput -> confirmedTxIdSet.contains(txOutput.getTxId())).mapToLong(TxOutput::getValue).sum());
    lockedInBondsBalance = Coin.valueOf(readableBsqBlockChain.getLockedInBondsOutputs().stream().filter(txOutput -> confirmedTxIdSet.contains(txOutput.getTxId())).mapToLong(TxOutput::getValue).sum());
    availableBalance = bsqCoinSelector.select(NetworkParameters.MAX_MONEY, wallet.calculateAllSpendCandidates()).valueGathered.subtract(lockedForVotingBalance).subtract(lockedInBondsBalance);
    if (availableBalance.isNegative())
        availableBalance = Coin.ZERO;
    bsqBalanceListeners.forEach(e -> e.onUpdateBalances(availableBalance, pendingBalance, lockedForVotingBalance, lockedInBondsBalance));
}
Also used : BsqBlockChainChangeDispatcher(bisq.core.dao.blockchain.BsqBlockChainChangeDispatcher) Transaction(org.bitcoinj.core.Transaction) TransactionConfidence(org.bitcoinj.core.TransactionConfidence) Getter(lombok.Getter) Coin(org.bitcoinj.core.Coin) ScriptException(org.bitcoinj.core.ScriptException) CoinSelection(org.bitcoinj.wallet.CoinSelection) ReadableBsqBlockChain(bisq.core.dao.blockchain.ReadableBsqBlockChain) Wallet(org.bitcoinj.wallet.Wallet) FXCollections(javafx.collections.FXCollections) BsqNode(bisq.core.dao.node.BsqNode) Function(java.util.function.Function) Inject(javax.inject.Inject) HashSet(java.util.HashSet) NetworkParameters(org.bitcoinj.core.NetworkParameters) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) TransactionVerificationException(bisq.core.btc.exceptions.TransactionVerificationException) SendRequest(org.bitcoinj.wallet.SendRequest) Map(java.util.Map) AbstractWalletEventListener(org.bitcoinj.wallet.listeners.AbstractWalletEventListener) BUILDING(org.bitcoinj.core.TransactionConfidence.ConfidenceType.BUILDING) PENDING(org.bitcoinj.core.TransactionConfidence.ConfidenceType.PENDING) AddressFormatException(org.bitcoinj.core.AddressFormatException) WalletException(bisq.core.btc.exceptions.WalletException) TransactionOutPoint(org.bitcoinj.core.TransactionOutPoint) BlockChain(org.bitcoinj.core.BlockChain) Tx(bisq.core.dao.blockchain.vo.Tx) Set(java.util.Set) BisqEnvironment(bisq.core.app.BisqEnvironment) TxOutput(bisq.core.dao.blockchain.vo.TxOutput) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) InsufficientMoneyException(org.bitcoinj.core.InsufficientMoneyException) Collectors(java.util.stream.Collectors) ECKey(org.bitcoinj.core.ECKey) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Script(org.bitcoinj.script.Script) TransactionInput(org.bitcoinj.core.TransactionInput) Preferences(bisq.core.user.Preferences) TransactionOutput(org.bitcoinj.core.TransactionOutput) Optional(java.util.Optional) FeeService(bisq.core.provider.fee.FeeService) Address(org.bitcoinj.core.Address) ObservableList(javafx.collections.ObservableList) Restrictions(bisq.core.btc.Restrictions) Transaction(org.bitcoinj.core.Transaction)

Aggregations

Tx (bisq.core.dao.blockchain.vo.Tx)12 TxOutput (bisq.core.dao.blockchain.vo.TxOutput)8 Coin (org.bitcoinj.core.Coin)6 Transaction (org.bitcoinj.core.Transaction)5 List (java.util.List)4 Collectors (java.util.stream.Collectors)4 ReadableBsqBlockChain (bisq.core.dao.blockchain.ReadableBsqBlockChain)3 TxInput (bisq.core.dao.blockchain.vo.TxInput)3 ArrayList (java.util.ArrayList)3 TransactionOutPoint (org.bitcoinj.core.TransactionOutPoint)3 TransactionOutput (org.bitcoinj.core.TransactionOutput)3 BisqEnvironment (bisq.core.app.BisqEnvironment)2 TxConfidenceListener (bisq.core.btc.listeners.TxConfidenceListener)2 DaoOptionKeys (bisq.core.dao.DaoOptionKeys)2 TxType (bisq.core.dao.blockchain.vo.TxType)2 BsqNode (bisq.core.dao.node.BsqNode)2 Preferences (bisq.core.user.Preferences)2 Inject (com.google.inject.Inject)2 Date (java.util.Date)2 Named (javax.inject.Named)2