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

The following examples show how to use org.bitcoinj.core.Coin#compareTo() . 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: DisputeSummaryWindow.java    From bisq with GNU Affero General Public License v3.0 6 votes vote down vote up
private boolean isPayoutAmountValid() {
    Coin buyerAmount = ParsingUtils.parseToCoin(buyerPayoutAmountInputTextField.getText(), formatter);
    Coin sellerAmount = ParsingUtils.parseToCoin(sellerPayoutAmountInputTextField.getText(), formatter);
    Contract contract = dispute.getContract();
    Coin tradeAmount = contract.getTradeAmount();
    Offer offer = new Offer(contract.getOfferPayload());
    Coin available = tradeAmount
            .add(offer.getBuyerSecurityDeposit())
            .add(offer.getSellerSecurityDeposit());
    Coin totalAmount = buyerAmount.add(sellerAmount);

    if (!totalAmount.isPositive()) {
        return false;
    }

    if (getDisputeManager(dispute) instanceof RefundManager) {
        // We allow to spend less in case of RefundAgent
        return totalAmount.compareTo(available) <= 0;
    } else {
        return totalAmount.compareTo(available) == 0;
    }
}
 
Example 2
Source File: BsqValidator.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
private ValidationResult validateIfNotExceedsMaxBtcValue(String input) {
    try {
        final Coin coin = ParsingUtils.parseToCoin(input, bsqFormatter);
        if (maxValue != null && coin.compareTo(maxValue) > 0)
            return new ValidationResult(false, Res.get("validation.btc.toLarge", bsqFormatter.formatCoinWithCode(maxValue)));
        else
            return new ValidationResult(true);
    } catch (Throwable t) {
        return new ValidationResult(false, Res.get("validation.invalidInput", t.getMessage()));
    }
}
 
Example 3
Source File: BsqValidator.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
private ValidationResult validateIfNotBelowMinValue(String input) {
    try {
        final Coin coin = ParsingUtils.parseToCoin(input, bsqFormatter);
        if (minValue != null && coin.compareTo(minValue) < 0)
            return new ValidationResult(false, Res.get("validation.bsq.amountBelowMinAmount",
                    bsqFormatter.formatCoinWithCode(minValue)));
        else
            return new ValidationResult(true);
    } catch (Throwable t) {
        return new ValidationResult(false, Res.get("validation.invalidInput", t.getMessage()));
    }
}
 
Example 4
Source File: BtcValidator.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
protected ValidationResult validateIfNotExceedsMaxBtcValue(String input) {
    try {
        final Coin coin = Coin.parseCoin(input);
        if (maxValue != null && coin.compareTo(maxValue) > 0)
            return new ValidationResult(false, Res.get("validation.btc.toLarge", formatter.formatCoinWithCode(maxValue)));
        else
            return new ValidationResult(true);
    } catch (Throwable t) {
        return new ValidationResult(false, Res.get("validation.invalidInput", t.getMessage()));
    }
}
 
Example 5
Source File: BtcValidator.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
protected ValidationResult validateIfNotExceedsMaxTradeLimit(String input) {
    try {
        final Coin coin = Coin.parseCoin(input);
        if (maxTradeLimit != null && coin.compareTo(maxTradeLimit) > 0)
            return new ValidationResult(false, Res.get("validation.btc.exceedsMaxTradeLimit", formatter.formatCoinWithCode(maxTradeLimit)));
        else
            return new ValidationResult(true);
    } catch (Throwable t) {
        return new ValidationResult(false, Res.get("validation.invalidInput", t.getMessage()));
    }
}
 
Example 6
Source File: BtcValidator.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
protected ValidationResult validateIfNotUnderMinValue(String input) {
    try {
        final Coin coin = Coin.parseCoin(input);
        if (minValue != null && coin.compareTo(minValue) < 0)
            return new ValidationResult(false, Res.get("validation.btc.toSmall", formatter.formatCoinWithCode(minValue)));
        else
            return new ValidationResult(true);
    } catch (Throwable t) {
        return new ValidationResult(false, Res.get("validation.invalidInput", t.getMessage()));
    }
}
 
Example 7
Source File: Restrictions.java    From bisq-core with GNU Affero General Public License v3.0 4 votes vote down vote up
public static boolean isAboveDust(Coin amount) {
    return amount.compareTo(getMinNonDustOutput()) >= 0;
}
 
Example 8
Source File: CoinUtil.java    From bisq-core with GNU Affero General Public License v3.0 4 votes vote down vote up
public static Coin minCoin(Coin a, Coin b) {
    return a.compareTo(b) <= 0 ? a : b;
}
 
Example 9
Source File: CoinUtil.java    From bisq-core with GNU Affero General Public License v3.0 4 votes vote down vote up
public static Coin maxCoin(Coin a, Coin b) {
    return a.compareTo(b) >= 0 ? a : b;
}
 
Example 10
Source File: TransactionActivity.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
private static Pair<Integer, JSONMap>
createRawTransaction(final GaService service,
                     final Transaction tx, final List<JSONMap> usedUtxos,
                     final List<JSONMap> utxos, final int subAccount,
                     GATx.ChangeOutput changeOutput,
                     final Coin amount, final Coin oldFee,
                     final Coin feeRate,
                     final JSONMap privateData, final boolean sendAll) {

    final boolean isRBF = usedUtxos != null;
    final boolean haveExistingChange = changeOutput != null;
    Coin total =  isRBF ? getUtxoSum(usedUtxos) : Coin.ZERO;
    Coin fee;

    // First add inputs until we cover the amount to send
    while ((sendAll || total.isLessThan(amount)) && !utxos.isEmpty())
        total = total.add(GATx.addUtxo(service, tx, utxos, usedUtxos));

    // Then add inputs until we cover amount + fee/change
    while (true) {
        fee = GATx.getTxFee(service, tx, feeRate);
        if (isRBF) {
            final Coin bandwidthFee = GATx.getTxFee(service, tx, service.getMinFeeRate());
            fee = (fee.isLessThan(oldFee) ? oldFee : fee).add(bandwidthFee);
        }

        final Coin minChange = changeOutput == null ? Coin.ZERO : service.getDustThreshold();
        final int cmp = sendAll ? 0 : total.compareTo(amount.add(fee).add(minChange));
        if (cmp < 0) {
            // Need more inputs to cover amount + fee/change
            if (utxos.isEmpty())
                return createFailed(R.string.insufficientFundsText); // None left, fail

            total = total.add(GATx.addUtxo(service, tx, utxos, usedUtxos));
            continue;
        }

        if (cmp == 0 || changeOutput != null) {
            // Inputs exactly match amount + fee/change, or are greater
            // and we have a change output for the excess
            break;
        }

        // Inputs greater than amount + fee, add a change output and try again
        changeOutput = GATx.addChangeOutput(service, tx, subAccount);
        if (changeOutput == null)
            return createFailed(R.string.unable_to_create_change);
    }

    boolean randomizedChange = false;
    if (changeOutput != null) {
        // Set the value of the change output
        if (tx.getOutputs().size() == 1)
            changeOutput.mOutput.setValue(total.subtract(fee)); // Redeposit
        else
            changeOutput.mOutput.setValue(total.subtract(amount).subtract(fee));
        if (haveExistingChange)
            randomizedChange = changeOutput.mOutput == tx.getOutput(0);
        else
            randomizedChange = GATx.randomizeChangeOutput(tx);
    }

    if (sendAll) {
        final Coin actualAmount = total.subtract(fee);
        if (!actualAmount.isGreaterThan(Coin.ZERO))
            return createFailed(R.string.insufficientFundsText);
        final int amtIndex = tx.getOutputs().size() == 1 ? 0 : (randomizedChange ? 1 : 0);
        tx.getOutput(amtIndex).setValue(actualAmount);
    }

    tx.setLockTime(service.getCurrentBlock()); // Prevent fee sniping

    int changeIndex = -1;
    if (changeOutput != null && tx.getOutputs().size() != 1)
        changeIndex = randomizedChange ? 0 : 1;
    return new Pair<>(0,
                      GATx.makeLimitsData(fee.subtract(oldFee), fee, changeIndex));
}
 
Example 11
Source File: SendFragment.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
private int createRawTransaction(final List<JSONMap> utxos, final String recipient,
                                 final Coin amount, final JSONMap privateData,
                                 final boolean sendAll, final Coin feeRate) {
    final GaService service = getGAService();

    if (service.isElements())
        return createRawElementsTransaction(utxos, recipient, amount, privateData, sendAll, feeRate);

    final GaActivity gaActivity = getGaActivity();

    final List<JSONMap> usedUtxos = new ArrayList<>();

    final Transaction tx = new Transaction(service.getNetworkParameters());

    if (!GATx.addTxOutput(service, tx, amount, recipient))
        return R.string.invalidAddress;

    Coin total = Coin.ZERO;
    Coin fee;
    boolean randomizedChange = false;
    GATx.ChangeOutput changeOutput = null;

    // First add inputs until we cover the amount to send
    while ((sendAll || total.isLessThan(amount)) && !utxos.isEmpty())
        total = total.add(GATx.addUtxo(service, tx, utxos, usedUtxos));

    // Then add inputs until we cover amount + fee/change
    while (true) {
        fee = GATx.getTxFee(service, tx, feeRate);

        final Coin minChange = changeOutput == null ? Coin.ZERO : service.getDustThreshold();
        final int cmp = sendAll ? 0 : total.compareTo(amount.add(fee).add(minChange));
        if (cmp < 0) {
            // Need more inputs to cover amount + fee/change
            if (utxos.isEmpty())
                return R.string.insufficientFundsText; // None left, fail

            total = total.add(GATx.addUtxo(service, tx, utxos, usedUtxos));
            continue;
        }

        if (cmp == 0 || changeOutput != null) {
            // Inputs exactly match amount + fee/change, or are greater
            // and we have a change output for the excess
            break;
        }

        // Inputs greater than amount + fee, add a change output and try again
        changeOutput = GATx.addChangeOutput(service, tx, mSubaccount);
        if (changeOutput == null)
            return R.string.unable_to_create_change;
    }

    if (changeOutput != null) {
        // Set the value of the change output
        changeOutput.mOutput.setValue(total.subtract(amount).subtract(fee));
        randomizedChange = GATx.randomizeChangeOutput(tx);
    }

    final Coin actualAmount;
    if (!sendAll)
        actualAmount = amount;
    else {
        actualAmount = total.subtract(fee);
        if (!actualAmount.isGreaterThan(Coin.ZERO))
            return R.string.insufficientFundsText;
        tx.getOutput(randomizedChange ? 1 : 0).setValue(actualAmount);
    }

    tx.setLockTime(service.getCurrentBlock()); // Prevent fee sniping

    final PreparedTransaction ptx;
    ptx = GATx.signTransaction(service, tx, usedUtxos, mSubaccount, changeOutput);

    final int changeIndex = changeOutput == null ? -1 : (randomizedChange ? 0 : 1);
    final JSONMap underLimits = GATx.makeLimitsData(actualAmount.add(fee), fee, changeIndex);

    final boolean skipChoice = service.isUnderLimit(underLimits.getCoin("amount"));
    final Coin sendFee = fee;
    gaActivity.runOnUiThread(new Runnable() {
        public void run() {
            mSendButton.setEnabled(true);
            mTwoFactor = UI.popupTwoFactorChoice(gaActivity, service, skipChoice,
                                                 new CB.Runnable1T<String>() {
                public void run(String method) {
                    if (skipChoice && service.hasAnyTwoFactor())
                        method = "limit";
                    onTransactionValidated(null, tx, recipient, actualAmount,
                                           method, sendFee, privateData, underLimits);
                }
            });
            if (mTwoFactor != null)
                mTwoFactor.show();
        }
    });
    return 0;
}
 
Example 12
Source File: OfferDataModel.java    From bisq with GNU Affero General Public License v3.0 4 votes vote down vote up
private boolean isBalanceSufficient(Coin balance) {
    return totalToPayAsCoin.get() != null && balance.compareTo(totalToPayAsCoin.get()) >= 0;
}
 
Example 13
Source File: Restrictions.java    From bisq with GNU Affero General Public License v3.0 4 votes vote down vote up
public static boolean isAboveDust(Coin amount) {
    return amount.compareTo(getMinNonDustOutput()) >= 0;
}
 
Example 14
Source File: CoinUtil.java    From bisq with GNU Affero General Public License v3.0 4 votes vote down vote up
public static Coin minCoin(Coin a, Coin b) {
    return a.compareTo(b) <= 0 ? a : b;
}
 
Example 15
Source File: CoinUtil.java    From bisq with GNU Affero General Public License v3.0 4 votes vote down vote up
public static Coin maxCoin(Coin a, Coin b) {
    return a.compareTo(b) >= 0 ? a : b;
}