/** * Convert an amount expressed in the way humans are used to into units. */ public static Value valueOf(final ValueType type, final int coins, final int cents) { checkArgument(cents < 100); checkArgument(cents >= 0); checkArgument(coins >= 0); return type.oneCoin().multiply(coins).add(type.oneCoin().divide(100).multiply(cents)); }
private void calculateNewBalance() { currentBalance = null; for (WalletAccount w : wallet.getAllAccounts()) { ExchangeRate rate = exchangeRates.get(w.getCoinType().getSymbol()); if (rate == null) { log.info("Missing exchange rate for {}, skipping...", w.getCoinType().getName()); continue; } if (currentBalance != null) { currentBalance = currentBalance.add(rate.rate.convert(w.getBalance())); } else { currentBalance = rate.rate.convert(w.getBalance()); } } }
@Test(expected = IllegalArgumentException.class) public void testAddFail() { BTC.oneCoin().add(LTC.oneCoin()); }
@Override public Value convert(Value convertingValue) { Value converted = convertValue(convertingValue); if (!converted.isZero() && minerFee != null) { Value fee; // Deposit -> withdrawal if (converted.type.equals(minerFee.type)) { fee = minerFee.negate(); // Miner fee is removed from withdraw value } else { // Withdrawal -> deposit fee = convertValue(minerFee); // Miner fee is added to the deposit value } converted = converted.add(fee); // If the miner fee is higher than the value we are converting we get 0 if (converted.isNegative()) converted = converted.multiply(0); } return converted; }
public void runTestParseValue2(ValueType type) { // BigDecimal version Value cent = type.oneCoin().divide(100); BigDecimal bigDecimalCent = BigDecimal.ONE.divide(new BigDecimal(100)); assertEquals(cent, parse(type, bigDecimalCent)); assertEquals(type.oneCoin().add(cent), parse(type, BigDecimal.ONE.add(bigDecimalCent))); assertEquals(type.oneCoin().negate(), parse(type, BigDecimal.ONE.negate())); try { parse(type, new BigDecimal("2E-20")); org.junit.Assert.fail("should not have accepted fractional satoshis"); } catch (ArithmeticException e) {} }
@Nullable public Value getRawTxFee(TransactionWatcherWallet wallet) { checkState(!isTrimmed, "Cannot get raw tx fee from a trimmed transaction"); Value fee = type.value(0); for (TransactionInput input : tx.getInputs()) { TransactionOutPoint outPoint = input.getOutpoint(); BitTransaction inTx = wallet.getTransaction(outPoint.getHash()); if (inTx == null || !inTx.isOutputAvailable((int) outPoint.getIndex())) { return null; } TransactionOutput txo = inTx.getOutput((int) outPoint.getIndex()); fee = fee.add(txo.getValue()); } for (TransactionOutput output : getOutputs()) { fee = fee.subtract(output.getValue()); } return fee; }
continue; sent = sent.add(connected.getValue());
if (lastCalculatedSize > 0 && coinType.getFeePolicy() == FeePolicy.FEE_PER_KB) { fees = fees.add(req.feePerTxSize.multiply((lastCalculatedSize / 1000) + 1)); } else { fees = fees.add(req.feePerTxSize); // First time around the loop. fees = fees.add(req.feePerTxSize.multiply(numberOfSoftDustOutputs)); break; case NO_POLICY: additionalValueForNextCategory = req.feePerTxSize.add( minNonDust.add(Coin.SATOSHI)).toCoin(); } else { additionalValueForNextCategory = req.feePerTxSize.add(Coin.SATOSHI).toCoin();
public void runTestParseValue(ValueType type) { // String version Value cent = type.oneCoin().divide(100); assertEquals(cent, parse(type, "0.01")); assertEquals(cent, parse(type, "1E-2")); assertEquals(type.oneCoin().add(cent), parse(type, "1.01")); assertEquals(type.oneCoin().negate(), parse(type, "-1")); try { parse(type, "2E-20"); org.junit.Assert.fail("should not have accepted fractional satoshis"); } catch (ArithmeticException e) {} }
@Test public void testMathOperators() { assertEquals(BTC.value("3.14159"), BTC.value("3").add(BTC.value(".14159"))); assertEquals(BTC.value("2"), BTC.oneCoin().add(Coin.COIN)); assertEquals(LTC.value("1"), LTC.value("100").subtract(LTC.value("99"))); assertEquals(LTC.value("1"), LTC.value("100").subtract("99")); assertEquals(100L, USD.value("100").divide(USD.value("1"))); assertArrayEquals(new Value[]{NBT.value("0.0001"), NBT.value("0.0002")}, NBT.value("0.0012").divideAndRemainder(10)); // max assertEquals(BTC.value("10"), Value.max(BTC.value("1"), BTC.value("10"))); assertEquals(BTC.value("0.5"), Value.max(BTC.value("0.5"), BTC.value("-0.5"))); assertEquals(BTC.value("1"), Value.max(BTC.value("1"), BTC.value("0"))); // min assertEquals(BTC.value("1"), Value.min(BTC.value("1"), BTC.value("10"))); assertEquals(BTC.value("-0.5"), Value.min(BTC.value("0.5"), BTC.value("-0.5"))); assertEquals(BTC.value("0"), Value.min(BTC.value("1"), BTC.value("0"))); }