Search in sources :

Example 6 with TxOutput

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

the class VoteListItem method calculateStake.

private void calculateStake() {
    if (stake == 0) {
        String txId = myVote.getTxId();
        stake = readableBsqBlockChain.getLockedForVoteTxOutputs().stream().filter(txOutput -> txOutput.getTxId().equals(txId)).filter(txOutput -> txOutput.getIndex() == 0).mapToLong(TxOutput::getValue).sum();
        stakeAsStringProperty.set(bsqFormatter.formatCoin(Coin.valueOf(stake)));
    }
}
Also used : Setter(lombok.Setter) Transaction(org.bitcoinj.core.Transaction) TransactionConfidence(org.bitcoinj.core.TransactionConfidence) Getter(lombok.Getter) Tx(bisq.core.dao.blockchain.vo.Tx) Coin(org.bitcoinj.core.Coin) SimpleStringProperty(javafx.beans.property.SimpleStringProperty) ReadableBsqBlockChain(bisq.core.dao.blockchain.ReadableBsqBlockChain) MyVote(bisq.core.dao.vote.MyVote) TxConfidenceIndicator(bisq.desktop.components.indicator.TxConfidenceIndicator) TxOutput(bisq.core.dao.blockchain.vo.TxOutput) EqualsAndHashCode(lombok.EqualsAndHashCode) BsqNode(bisq.core.dao.node.BsqNode) BsqFormatter(bisq.desktop.util.BsqFormatter) BsqNodeProvider(bisq.core.dao.node.BsqNodeProvider) BsqWalletService(bisq.core.btc.wallet.BsqWalletService) Slf4j(lombok.extern.slf4j.Slf4j) TxConfidenceListener(bisq.core.btc.listeners.TxConfidenceListener) Res(bisq.core.locale.Res) ToString(lombok.ToString) Optional(java.util.Optional) StringProperty(javafx.beans.property.StringProperty) ChangeListener(javafx.beans.value.ChangeListener) Tooltip(javafx.scene.control.Tooltip) TxOutput(bisq.core.dao.blockchain.vo.TxOutput) ToString(lombok.ToString)

Example 7 with TxOutput

use of bisq.core.dao.blockchain.vo.TxOutput 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 8 with TxOutput

use of bisq.core.dao.blockchain.vo.TxOutput 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 9 with TxOutput

use of bisq.core.dao.blockchain.vo.TxOutput 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)

Example 10 with TxOutput

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

the class IssuanceConsensus method applyVoteResult.

public static void applyVoteResult(Map<Proposal, Integer> stakeByProposalMap, ReadableBsqBlockChain readableBsqBlockChain, WritableBsqBlockChain writableBsqBlockChain) {
    Map<String, TxOutput> txOutputsByTxIdMap = new HashMap<>();
    final Set<TxOutput> compReqIssuanceTxOutputs = readableBsqBlockChain.getCompReqIssuanceTxOutputs();
    compReqIssuanceTxOutputs.stream().filter(// our candidate is not yet verified and not set
    txOutput -> !txOutput.isVerified()).forEach(txOutput -> txOutputsByTxIdMap.put(txOutput.getTxId(), txOutput));
    stakeByProposalMap.forEach((proposal, value) -> {
        int stakeResult = value;
        if (stakeResult >= QUORUM) {
            final String txId = proposal.getTxId();
            if (txOutputsByTxIdMap.containsKey(txId)) {
                final TxOutput txOutput = txOutputsByTxIdMap.get(txId);
                writableBsqBlockChain.issueBsq(txOutput);
                log.info("We issued new BSQ to txOutput {} for proposal {}", txOutput, proposal);
            }
        } else {
            log.warn("We got a successful vote result but did not reach the quorum. stake={}, quorum={}", stakeResult, QUORUM);
        }
    });
}
Also used : Encryption(bisq.common.crypto.Encryption) Arrays(java.util.Arrays) Slf4j(lombok.extern.slf4j.Slf4j) ReadableBsqBlockChain(bisq.core.dao.blockchain.ReadableBsqBlockChain) Map(java.util.Map) Set(java.util.Set) HashMap(java.util.HashMap) TxOutput(bisq.core.dao.blockchain.vo.TxOutput) SecretKey(javax.crypto.SecretKey) WritableBsqBlockChain(bisq.core.dao.blockchain.WritableBsqBlockChain) Proposal(bisq.core.dao.proposal.Proposal) Hash(bisq.common.crypto.Hash) TxOutput(bisq.core.dao.blockchain.vo.TxOutput) HashMap(java.util.HashMap)

Aggregations

TxOutput (bisq.core.dao.blockchain.vo.TxOutput)12 Tx (bisq.core.dao.blockchain.vo.Tx)9 Coin (org.bitcoinj.core.Coin)8 ReadableBsqBlockChain (bisq.core.dao.blockchain.ReadableBsqBlockChain)6 Slf4j (lombok.extern.slf4j.Slf4j)5 Transaction (org.bitcoinj.core.Transaction)5 List (java.util.List)4 Optional (java.util.Optional)4 BisqEnvironment (bisq.core.app.BisqEnvironment)3 BsqWalletService (bisq.core.btc.wallet.BsqWalletService)3 TxType (bisq.core.dao.blockchain.vo.TxType)3 ArrayList (java.util.ArrayList)3 Collectors (java.util.stream.Collectors)3 TransactionOutPoint (org.bitcoinj.core.TransactionOutPoint)3 TransactionOutput (org.bitcoinj.core.TransactionOutput)3 Utils (org.bitcoinj.core.Utils)3 NotNull (org.jetbrains.annotations.NotNull)3 Encryption (bisq.common.crypto.Encryption)2 Storage (bisq.common.storage.Storage)2 Utilities (bisq.common.util.Utilities)2