/** Alias for divide */ public Coin div(final long divisor) { return divide(divisor); }
/** Alias for divide */ public Coin div(final long divisor) { return divide(divisor); }
/** Alias for divide */ public Coin div(final int divisor) { return divide(divisor); }
/** Alias for divide */ public Coin div(final long divisor) { return divide(divisor); }
/** Alias for divide */ public Coin div(final int divisor) { return divide(divisor); }
/** Alias for divide */ public Coin div(final long divisor) { return divide(divisor); }
/** Alias for divide */ public Coin div(final int divisor) { return divide(divisor); }
/** Alias for divide */ public Coin div(final int divisor) { return divide(divisor); }
/** * <p>Gets the minimum value for a txout of this size to be considered non-dust by Bitcoin Core * (and thus relayed). See: CTxOut::IsDust() in Bitcoin Core. The assumption is that any output that would * consume more than a third of its value in fees is not something the Bitcoin system wants to deal with right now, * so we call them "dust outputs" and they're made non standard. The choice of one third is somewhat arbitrary and * may change in future.</p> * * <p>You probably should use {@link org.bitcoinj.core.TransactionOutput#getMinNonDustValue()} which uses * a safe fee-per-kb by default.</p> * * @param feePerKb The fee required per kilobyte. Note that this is the same as Bitcoin Core's -minrelaytxfee * 3 */ public Coin getMinNonDustValue(Coin feePerKb) { // A typical output is 33 bytes (pubkey hash + opcodes) and requires an input of 148 bytes to spend so we add // that together to find out the total amount of data used to transfer this amount of value. Note that this // formula is wrong for anything that's not a pay-to-address output, unfortunately, we must follow Bitcoin Core's // wrongness in order to ensure we're considered standard. A better formula would either estimate the // size of data needed to satisfy all different script types, or just hard code 33 below. final long size = this.unsafeBitcoinSerialize().length + 148; return feePerKb.multiply(size).divide(1000); }
/** * <p>Gets the minimum value for a txout of this size to be considered non-dust by Bitcoin Core * (and thus relayed). See: CTxOut::IsDust() in Bitcoin Core. The assumption is that any output that would * consume more than a third of its value in fees is not something the Bitcoin system wants to deal with right now, * so we call them "dust outputs" and they're made non standard. The choice of one third is somewhat arbitrary and * may change in future.</p> * * <p>You probably should use {@link org.bitcoinj.core.TransactionOutput#getMinNonDustValue()} which uses * a safe fee-per-kb by default.</p> * * @param feePerKb The fee required per kilobyte. Note that this is the same as Bitcoin Core's -minrelaytxfee * 3 */ public Coin getMinNonDustValue(Coin feePerKb) { // A typical output is 33 bytes (pubkey hash + opcodes) and requires an input of 148 bytes to spend so we add // that together to find out the total amount of data used to transfer this amount of value. Note that this // formula is wrong for anything that's not a pay-to-address output, unfortunately, we must follow Bitcoin Core's // wrongness in order to ensure we're considered standard. A better formula would either estimate the // size of data needed to satisfy all different script types, or just hard code 33 below. final long size = this.unsafeBitcoinSerialize().length + 148; return feePerKb.multiply(size).divide(1000); }
/** * <p>Gets the minimum value for a txout of this size to be considered non-dust by Bitcoin Core * (and thus relayed). See: CTxOut::IsDust() in Bitcoin Core. The assumption is that any output that would * consume more than a third of its value in fees is not something the Bitcoin system wants to deal with right now, * so we call them "dust outputs" and they're made non standard. The choice of one third is somewhat arbitrary and * may change in future.</p> * * <p>You probably should use {@link org.bitcoinj.core.TransactionOutput#getMinNonDustValue()} which uses * a safe fee-per-kb by default.</p> * * @param feePerKb The fee required per kilobyte. Note that this is the same as Bitcoin Core's -minrelaytxfee * 3 */ public Coin getMinNonDustValue(Coin feePerKb) { // A typical output is 33 bytes (pubkey hash + opcodes) and requires an input of 148 bytes to spend so we add // that together to find out the total amount of data used to transfer this amount of value. Note that this // formula is wrong for anything that's not a pay-to-address output, unfortunately, we must follow Bitcoin Core's // wrongness in order to ensure we're considered standard. A better formula would either estimate the // size of data needed to satisfy all different script types, or just hard code 33 below. final long size = this.unsafeBitcoinSerialize().length + 148; return feePerKb.multiply(size).divide(1000); }
/** * <p>Gets the minimum value for a txout of this size to be considered non-dust by Bitcoin Core * (and thus relayed). See: CTxOut::IsDust() in Bitcoin Core. The assumption is that any output that would * consume more than a third of its value in fees is not something the Bitcoin system wants to deal with right now, * so we call them "dust outputs" and they're made non standard. The choice of one third is somewhat arbitrary and * may change in future.</p> * * <p>You probably should use {@link org.bitcoinj.core.TransactionOutput#getMinNonDustValue()} which uses * a safe fee-per-kb by default.</p> * * @param feePerKb The fee required per kilobyte. Note that this is the same as Bitcoin Core's -minrelaytxfee * 3 */ public Coin getMinNonDustValue(Coin feePerKb) { // A typical output is 33 bytes (pubkey hash + opcodes) and requires an input of 148 bytes to spend so we add // that together to find out the total amount of data used to transfer this amount of value. Note that this // formula is wrong for anything that's not a pay-to-address output, unfortunately, we must follow Bitcoin Core's // wrongness in order to ensure we're considered standard. A better formula would either estimate the // size of data needed to satisfy all different script types, or just hard code 33 below. final long size = this.unsafeBitcoinSerialize().length + 148; return feePerKb.multiply(size).divide(1000); }
/** Reduce the value of the first output of a transaction to pay the given feePerKb as appropriate for its size. */ private boolean adjustOutputDownwardsForFee(Transaction tx, CoinSelection coinSelection, Coin feePerKb, boolean ensureMinRequiredFee) { final int size = tx.unsafeBitcoinSerialize().length + estimateBytesForSigning(coinSelection); Coin fee = feePerKb.multiply(size).divide(1000); if (ensureMinRequiredFee && fee.compareTo(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE) < 0) fee = Transaction.REFERENCE_DEFAULT_MIN_TX_FEE; TransactionOutput output = tx.getOutput(0); output.setValue(output.getValue().subtract(fee)); return !output.isDust(); }
/** Reduce the value of the first output of a transaction to pay the given feePerKb as appropriate for its size. */ private boolean adjustOutputDownwardsForFee(Transaction tx, CoinSelection coinSelection, Coin feePerKb, boolean ensureMinRequiredFee) { final int size = tx.unsafeBitcoinSerialize().length + estimateBytesForSigning(coinSelection); Coin fee = feePerKb.multiply(size).divide(1000); if (ensureMinRequiredFee && fee.compareTo(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE) < 0) fee = Transaction.REFERENCE_DEFAULT_MIN_TX_FEE; TransactionOutput output = tx.getOutput(0); output.setValue(output.getValue().subtract(fee)); return !output.isDust(); }
/** Reduce the value of the first output of a transaction to pay the given feePerKb as appropriate for its size. */ private boolean adjustOutputDownwardsForFee(Transaction tx, CoinSelection coinSelection, Coin feePerKb, boolean ensureMinRequiredFee) { final int size = tx.unsafeBitcoinSerialize().length + estimateBytesForSigning(coinSelection); Coin fee = feePerKb.multiply(size).divide(1000); if (ensureMinRequiredFee && fee.compareTo(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE) < 0) fee = Transaction.REFERENCE_DEFAULT_MIN_TX_FEE; TransactionOutput output = tx.getOutput(0); output.setValue(output.getValue().subtract(fee)); return !output.isDust(); }
/** Reduce the value of the first output of a transaction to pay the given feePerKb as appropriate for its size. */ private boolean adjustOutputDownwardsForFee(Transaction tx, CoinSelection coinSelection, Coin feePerKb, boolean ensureMinRequiredFee, boolean useInstantSend) { final int size = tx.unsafeBitcoinSerialize().length + estimateBytesForSigning(coinSelection); Coin fee = feePerKb.multiply(size).divide(1000); if (ensureMinRequiredFee && fee.compareTo(params.isDIP0001ActiveAtTip() ? Transaction.REFERENCE_DEFAULT_MIN_TX_FEE.div(10) : Transaction.REFERENCE_DEFAULT_MIN_TX_FEE) < 0) fee = params.isDIP0001ActiveAtTip() ? Transaction.REFERENCE_DEFAULT_MIN_TX_FEE.div(10) : Transaction.REFERENCE_DEFAULT_MIN_TX_FEE; if(useInstantSend) fee = TransactionLockRequest.MIN_FEE.multiply(tx.getInputs().size()); TransactionOutput output = tx.getOutput(0); output.setValue(output.getValue().subtract(fee)); return !output.isDust(); }
@Test(expected = VerificationException.ExcessiveValue.class) public void exceedsMaxMoney2() throws Exception { Coin half = PARAMS.getMaxMoney().divide(2).add(Coin.SATOSHI); tx.getOutput(0).setValue(half); tx.addOutput(half, ADDRESS); tx.verify(); }
@Test public void lowerThanDefaultFee() throws InsufficientMoneyException { int feeFactor = 10; Coin fee = Transaction.DEFAULT_TX_FEE.divide(feeFactor); receiveATransactionAmount(wallet, myAddress, Coin.COIN); SendRequest req = SendRequest.to(myAddress, Coin.CENT); req.feePerKb = fee; wallet.completeTx(req); assertEquals(Coin.valueOf(11350).divide(feeFactor), req.tx.getFee()); wallet.commitTx(req.tx); SendRequest emptyReq = SendRequest.emptyWallet(myAddress); emptyReq.feePerKb = fee; emptyReq.ensureMinRequiredFee = true; emptyReq.emptyWallet = true; emptyReq.coinSelector = AllowUnconfirmedCoinSelector.get(); wallet.completeTx(emptyReq); assertEquals(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE, emptyReq.tx.getFee()); wallet.commitTx(emptyReq.tx); }
@Test public void testToFriendlyString() { assertEquals("1.00 BTC", COIN.toFriendlyString()); assertEquals("1.23 BTC", valueOf(1, 23).toFriendlyString()); assertEquals("0.001 BTC", COIN.divide(1000).toFriendlyString()); assertEquals("-1.23 BTC", valueOf(1, 23).negate().toFriendlyString()); }
@Test public void testCategory2WithChange() throws Exception { // Specifically target case 2 with significant change // Generate a ton of small outputs StoredBlock block = new StoredBlock(makeSolvedTestBlock(blockStore, OTHER_ADDRESS), BigInteger.ONE, 1); int i = 0; while (i <= CENT.divide(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE.multiply(10))) { Transaction tx = createFakeTxWithChangeAddress(PARAMS, Transaction.REFERENCE_DEFAULT_MIN_TX_FEE.multiply(10), myAddress, OTHER_ADDRESS); tx.getInput(0).setSequenceNumber(i++); // Keep every transaction unique wallet.receiveFromBlock(tx, block, AbstractBlockChain.NewBlockType.BEST_CHAIN, i); } // The selector will choose 2 with MIN_TX_FEE fee SendRequest request1 = SendRequest.to(OTHER_ADDRESS, CENT.add(SATOSHI)); request1.ensureMinRequiredFee = true; wallet.completeTx(request1); assertEquals(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE, request1.tx.getFee()); assertEquals(request1.tx.getInputs().size(), i); // We should have spent all inputs assertEquals(2, request1.tx.getOutputs().size()); // and gotten change back }