Java Code Examples for org.bitcoinj.core.Coin#equals()

The following examples show how to use org.bitcoinj.core.Coin#equals() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultCoinSelector.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
@Override
public CoinSelection select(Coin target, List<TransactionOutput> candidates) {
    ArrayList<TransactionOutput> selected = new ArrayList<>();
    // Sort the inputs by age*value so we get the highest "coindays" spent.
    // TODO: Consider changing the wallets internal format to track just outputs and keep them ordered.
    ArrayList<TransactionOutput> sortedOutputs = new ArrayList<>(candidates);
    // When calculating the wallet balance, we may be asked to select all possible coins, if so, avoid sorting
    // them in order to improve performance.
    // TODO: Take in network parameters when instanatiated, and then test against the current network. Or just have a boolean parameter for "give me everything"
    if (!target.equals(NetworkParameters.MAX_MONEY)) {
        sortOutputs(sortedOutputs);
    }
    // Now iterate over the sorted outputs until we have got as close to the target as possible or a little
    // bit over (excessive value will be change).
    long total = 0;
    for (TransactionOutput output : sortedOutputs) {
        if (total >= target.value) break;
        // Only pick chain-included transactions, or transactions that are ours and pending.
        if (!shouldSelect(output.getParentTransaction())) continue;
        selected.add(output);
        total += output.getValue().value;
    }
    // Total may be lower than target here, if the given candidates were insufficient to create to requested
    // transaction.
    return new CoinSelection(Coin.valueOf(total), selected);
}
 
Example 2
Source File: DefaultCoinSelector.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
@Override
public CoinSelection select(Coin target, List<TransactionOutput> candidates) {
    ArrayList<TransactionOutput> selected = new ArrayList<>();
    // Sort the inputs by age*value so we get the highest "coindays" spent.
    // TODO: Consider changing the wallets internal format to track just outputs and keep them ordered.
    ArrayList<TransactionOutput> sortedOutputs = new ArrayList<>(candidates);
    // When calculating the wallet balance, we may be asked to select all possible coins, if so, avoid sorting
    // them in order to improve performance.
    // TODO: Take in network parameters when instanatiated, and then test against the current network. Or just have a boolean parameter for "give me everything"
    if (!target.equals(NetworkParameters.MAX_MONEY)) {
        sortOutputs(sortedOutputs);
    }
    // Now iterate over the sorted outputs until we have got as close to the target as possible or a little
    // bit over (excessive value will be change).
    long total = 0;
    for (TransactionOutput output : sortedOutputs) {
        if (total >= target.value) break;
        // Only pick chain-included transactions, or transactions that are ours and pending.
        if (!shouldSelect(output.getParentTransaction())) continue;
        selected.add(output);
        total += output.getValue().value;
    }
    // Total may be lower than target here, if the given candidates were insufficient to create to requested
    // transaction.
    return new CoinSelection(Coin.valueOf(total), selected);
}
 
Example 3
Source File: DefaultCoinSelector.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@Override
public CoinSelection select(Coin target, List<TransactionOutput> candidates) {
    ArrayList<TransactionOutput> selected = new ArrayList<>();
    // Sort the inputs by age*value so we get the highest "coindays" spent.
    // TODO: Consider changing the wallets internal format to track just outputs and keep them ordered.
    ArrayList<TransactionOutput> sortedOutputs = new ArrayList<>(candidates);
    // When calculating the wallet balance, we may be asked to select all possible coins, if so, avoid sorting
    // them in order to improve performance.
    // TODO: Take in network parameters when instanatiated, and then test against the current network. Or just have a boolean parameter for "give me everything"
    if (!target.equals(NetworkParameters.MAX_MONEY)) {
        sortOutputs(sortedOutputs);
    }
    // Now iterate over the sorted outputs until we have got as close to the target as possible or a little
    // bit over (excessive value will be change).
    long total = 0;
    for (TransactionOutput output : sortedOutputs) {
        if (total >= target.value)
            break;
        // Only pick chain-included transactions, or transactions that are ours and pending.
        if (!shouldSelect(output.getParentTransaction()))
            continue;
        selected.add(output);
        total += output.getValue().value;
    }
    // Total may be lower than target here, if the given candidates were insufficient to create to requested
    // transaction.
    return new CoinSelection(Coin.valueOf(total), selected);
}
 
Example 4
Source File: MyceliumCoinSelector.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@Override
public CoinSelection select(Coin target, List<TransactionOutput> candidates) {
    ArrayList<TransactionOutput> selected = new ArrayList<>();
    // Sort the inputs by age*value so we get the highest "coindays" spent.
    // TODO: Consider changing the wallets internal format to track just outputs and keep them ordered.
    ArrayList<TransactionOutput> sortedOutputs = new ArrayList<>(candidates);
    // When calculating the wallet balance, we may be asked to select all possible coins, if so, avoid sorting
    // them in order to improve performance.
    // TODO: Take in network parameters when instanatiated, and then test against the current network. Or just have a boolean parameter for "give me everything"
    if (!target.equals(NetworkParameters.MAX_MONEY)) {
        sortOutputs(sortedOutputs);
    }
    // Now iterate over the sorted outputs until we have got as close to the target as possible or a little
    // bit over (excessive value will be change).
    long total = 0;
    for (TransactionOutput output : sortedOutputs) {
        if (total >= target.value)
            break;
        // Only pick chain-included transactions, or transactions that are ours and pending.
        if (!shouldSelect(output.getParentTransaction()))
            continue;
        selected.add(output);
        total += output.getValue().value;
    }
    // Total may be lower than target here, if the given candidates were insufficient to create to requested
    // transaction.
    return new CoinSelection(Coin.valueOf(total), selected);
}
 
Example 5
Source File: BisqDefaultCoinSelector.java    From bisq-core with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public CoinSelection select(Coin target, List<TransactionOutput> candidates) {
    ArrayList<TransactionOutput> selected = new ArrayList<>();
    // Sort the inputs by age*value so we get the highest "coin days" spent.
    ArrayList<TransactionOutput> sortedOutputs = new ArrayList<>(candidates);
    // If we spend all we don't need to sort
    if (!target.equals(NetworkParameters.MAX_MONEY))
        sortOutputs(sortedOutputs);

    // Now iterate over the sorted outputs until we have got as close to the target as possible or a little
    // bit over (excessive value will be change).
    long total = 0;
    long targetValue = target.value;
    for (TransactionOutput output : sortedOutputs) {
        if (total >= targetValue) {
            long change = total - targetValue;
            if (change == 0 || change >= Restrictions.getMinNonDustOutput().value)
                break;
        }

        if (output.getParentTransaction() != null &&
                isTxSpendable(output.getParentTransaction()) &&
                isTxOutputSpendable(output)) {
            selected.add(output);
            total += output.getValue().value;
        }
    }
    // Total may be lower than target here, if the given candidates were insufficient to create to requested
    // transaction.
    return new CoinSelection(Coin.valueOf(total), selected);
}
 
Example 6
Source File: DisputeSummaryWindow.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
private void applyTradeAmountRadioButtonStates() {
    Contract contract = dispute.getContract();
    Offer offer = new Offer(contract.getOfferPayload());
    Coin buyerSecurityDeposit = offer.getBuyerSecurityDeposit();
    Coin sellerSecurityDeposit = offer.getSellerSecurityDeposit();
    Coin tradeAmount = contract.getTradeAmount();

    Coin buyerPayoutAmount = disputeResult.getBuyerPayoutAmount();
    Coin sellerPayoutAmount = disputeResult.getSellerPayoutAmount();

    buyerPayoutAmountInputTextField.setText(formatter.formatCoin(buyerPayoutAmount));
    sellerPayoutAmountInputTextField.setText(formatter.formatCoin(sellerPayoutAmount));

    boolean isMediationDispute = getDisputeManager(dispute) instanceof MediationManager;
    // At mediation we require a min. payout to the losing party to keep incentive for the trader to accept the
    // mediated payout. For Refund agent cases we do not have that restriction.
    Coin minRefundAtDispute = isMediationDispute ? Restrictions.getMinRefundAtMediatedDispute() : Coin.ZERO;
    Coin maxPayoutAmount = tradeAmount
            .add(buyerSecurityDeposit)
            .add(sellerSecurityDeposit)
            .subtract(minRefundAtDispute);

    if (buyerPayoutAmount.equals(tradeAmount.add(buyerSecurityDeposit)) &&
            sellerPayoutAmount.equals(sellerSecurityDeposit)) {
        buyerGetsTradeAmountRadioButton.setSelected(true);
    } else if (buyerPayoutAmount.equals(maxPayoutAmount) &&
            sellerPayoutAmount.equals(minRefundAtDispute)) {
        buyerGetsAllRadioButton.setSelected(true);
    } else if (sellerPayoutAmount.equals(tradeAmount.add(sellerSecurityDeposit))
            && buyerPayoutAmount.equals(buyerSecurityDeposit)) {
        sellerGetsTradeAmountRadioButton.setSelected(true);
    } else if (sellerPayoutAmount.equals(maxPayoutAmount)
            && buyerPayoutAmount.equals(minRefundAtDispute)) {
        sellerGetsAllRadioButton.setSelected(true);
    } else {
        customRadioButton.setSelected(true);
    }
}
 
Example 7
Source File: MutableOfferViewModel.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
public void onFocusOutMinAmountTextField(boolean oldValue, boolean newValue) {
    if (oldValue && !newValue) {
        InputValidator.ValidationResult result = isBtcInputValid(minAmount.get());
        minAmountValidationResult.set(result);
        if (result.isValid) {
            Coin minAmountAsCoin = dataModel.getMinAmount().get();
            syncMinAmountWithAmount = minAmountAsCoin != null && minAmountAsCoin.equals(dataModel.getAmount().get());
            setMinAmountToModel();

            dataModel.calculateMinVolume();

            if (dataModel.getMinVolume().get() != null) {
                InputValidator.ValidationResult minVolumeResult = isVolumeInputValid(
                        DisplayUtils.formatVolume(dataModel.getMinVolume().get()));

                volumeValidationResult.set(minVolumeResult);

                updateButtonDisableState();
            }

            this.minAmount.set(btcFormatter.formatCoin(minAmountAsCoin));

            if (!dataModel.isMinAmountLessOrEqualAmount()) {
                this.amount.set(this.minAmount.get());
            } else {
                minAmountValidationResult.set(result);
                if (this.amount.get() != null)
                    amountValidationResult.set(isBtcInputValid(this.amount.get()));
            }
        } else {
            syncMinAmountWithAmount = true;
        }
    }
}
 
Example 8
Source File: BisqDefaultCoinSelector.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public CoinSelection select(Coin target, List<TransactionOutput> candidates) {
    ArrayList<TransactionOutput> selected = new ArrayList<>();
    // Sort the inputs by age*value so we get the highest "coin days" spent.
    ArrayList<TransactionOutput> sortedOutputs = new ArrayList<>(candidates);
    // If we spend all we don't need to sort
    if (!target.equals(NetworkParameters.MAX_MONEY))
        sortOutputs(sortedOutputs);

    // Now iterate over the sorted outputs until we have got as close to the target as possible or a little
    // bit over (excessive value will be change).
    long total = 0;
    long targetValue = target.value;
    for (TransactionOutput output : sortedOutputs) {
        if (!isDustAttackUtxo(output)) {
            if (total >= targetValue) {
                long change = total - targetValue;
                if (change == 0 || change >= Restrictions.getMinNonDustOutput().value)
                    break;
            }

            if (output.getParentTransaction() != null &&
                    isTxSpendable(output.getParentTransaction()) &&
                    isTxOutputSpendable(output)) {
                selected.add(output);
                total += output.getValue().value;
            }
        }
    }
    // Total may be lower than target here, if the given candidates were insufficient to create to requested
    // transaction.
    return new CoinSelection(Coin.valueOf(total), selected);
}
 
Example 9
Source File: MainFragment.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
private void updateBalance() {
    Log.d(TAG, "Updating balance");
    if (isZombie())
        return;

    final GaService service = getGAService();
    final Coin balance = service.getCoinBalance(mSubaccount);
    if (service.getLoginData() == null || balance == null)
        return;

    final FontAwesomeTextView balanceUnit = UI.find(mView, R.id.mainBalanceUnit);
    final TextView balanceText = UI.find(mView, R.id.mainBalanceText);
    UI.setCoinText(service, balanceUnit, balanceText, balance);

    final Coin verifiedBalance = service.getSPVVerifiedBalance(mSubaccount);

    // Hide balance question mark if we know our balance is verified
    // (or we are in watch only mode and so have no SPV to verify it with)
    final boolean verified = balance.equals(verifiedBalance) || !service.isSPVEnabled();
    final TextView balanceQuestionMark = UI.find(mView, R.id.mainBalanceQuestionMark);
    UI.hideIf(verified, balanceQuestionMark);

    final TextView balanceFiatText = UI.find(mView, R.id.mainLocalBalanceText);
    final FontAwesomeTextView balanceFiatIcon = UI.find(mView, R.id.mainLocalBalanceIcon);

    final int nChars = balanceText.getText().length() + balanceQuestionMark.getText().length() + balanceUnit.getText().length();
    final int size = Math.min(50 - nChars, 34);
    balanceText.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
    balanceUnit.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);

    UI.setAmountText(balanceFiatText, service.getFiatBalance(mSubaccount));

    if (!service.isElements())
        AmountFields.changeFiatIcon(balanceFiatIcon, service.getFiatCurrency());
    else {
        balanceUnit.setText(String.format("%s ", service.getAssetSymbol()));
        balanceText.setText(service.getAssetFormat().format(balance));

        if (!mIsExchanger) {
            // No fiat values in elements multiasset
            UI.hide(UI.find(mView, R.id.mainLocalBalance));
            // Currently no SPV either
            UI.hide(balanceQuestionMark);
        }
    }

    if (service.showBalanceInTitle())
        UI.hide(balanceText, balanceUnit);
}