Coin.toFriendlyFormat() includes denomination (BTC). This saves a lot of string concatenation code.

This commit is contained in:
Andreas Schildbach 2014-07-05 17:10:55 +02:00 committed by Mike Hearn
parent 794263436f
commit afcc7e3f13
10 changed files with 25 additions and 26 deletions

View File

@ -206,7 +206,7 @@ public final class Coin implements Comparable<Coin>, Serializable {
return this.value;
}
private static final CoinFormat FRIENDLY_FORMAT = CoinFormat.BTC.minDecimals(2).repeatOptionalDecimals(1, 6).noCode();
private static final CoinFormat FRIENDLY_FORMAT = CoinFormat.BTC.minDecimals(2).repeatOptionalDecimals(1, 6).postfixCode();
/**
* Returns the value as a 0.12 type string. More digits after the decimal place will be used

View File

@ -655,7 +655,7 @@ public class Transaction extends ChildMessage implements Serializable {
Script scriptSig = in.getScriptSig();
s.append(scriptSig);
if (in.getValue() != null)
s.append(" ").append(in.getValue().toFriendlyString()).append(" BTC");
s.append(" ").append(in.getValue().toFriendlyString());
s.append("\n ");
s.append("outpoint:");
final TransactionOutPoint outpoint = in.getOutpoint();
@ -678,7 +678,6 @@ public class Transaction extends ChildMessage implements Serializable {
s.append(scriptPubKey);
s.append(" ");
s.append(out.getValue().toFriendlyString());
s.append(" BTC");
if (!out.isAvailableForSpending()) {
s.append(" Spent");
}

View File

@ -1210,8 +1210,8 @@ public class Wallet extends BaseTaggableObject implements Serializable, BlockCha
Coin valueSentToMe = tx.getValueSentToMe(this);
Coin valueSentFromMe = tx.getValueSentFromMe(this);
if (log.isInfoEnabled()) {
log.info(String.format("Received a pending transaction %s that spends %s BTC from our own wallet," +
" and sends us %s BTC", tx.getHashAsString(), valueSentFromMe.toFriendlyString(),
log.info(String.format("Received a pending transaction %s that spends %s from our own wallet," +
" and sends us %s", tx.getHashAsString(), valueSentFromMe.toFriendlyString(),
valueSentToMe.toFriendlyString()));
}
if (tx.getConfidence().getSource().equals(TransactionConfidence.Source.UNKNOWN)) {
@ -1390,7 +1390,7 @@ public class Wallet extends BaseTaggableObject implements Serializable, BlockCha
Coin valueSentToMe = tx.getValueSentToMe(this);
Coin valueDifference = valueSentToMe.subtract(valueSentFromMe);
log.info("Received tx{} for {} BTC: {} [{}] in block {}", sideChain ? " on a side chain" : "",
log.info("Received tx{} for {}: {} [{}] in block {}", sideChain ? " on a side chain" : "",
valueDifference.toFriendlyString(), tx.getHashAsString(), relativityOffset,
block != null ? block.getHeader().getHash() : "(unit test)");
@ -2495,7 +2495,7 @@ public class Wallet extends BaseTaggableObject implements Serializable, BlockCha
value = value.add(output.getValue());
}
log.info("Completing send tx with {} outputs totalling {} BTC (not including fees)",
log.info("Completing send tx with {} outputs totalling {} (not including fees)",
req.tx.getOutputs().size(), value.toFriendlyString());
// If any inputs have already been added, we don't need to get their value from wallet
@ -2544,7 +2544,7 @@ public class Wallet extends BaseTaggableObject implements Serializable, BlockCha
bestCoinSelection = selector.select(NetworkParameters.MAX_MONEY, candidates);
candidates = null; // Selector took ownership and might have changed candidates. Don't access again.
req.tx.getOutput(0).setValue(bestCoinSelection.valueGathered);
log.info(" emptying {} BTC", bestCoinSelection.valueGathered.toFriendlyString());
log.info(" emptying {}", bestCoinSelection.valueGathered.toFriendlyString());
}
for (TransactionOutput output : bestCoinSelection.gathered)
@ -2560,7 +2560,7 @@ public class Wallet extends BaseTaggableObject implements Serializable, BlockCha
if (bestChangeOutput != null) {
req.tx.addOutput(bestChangeOutput);
log.info(" with {} BTC change", bestChangeOutput.getValue().toFriendlyString());
log.info(" with {} change", bestChangeOutput.getValue().toFriendlyString());
}
// Now shuffle the outputs to obfuscate which is the change.

View File

@ -94,7 +94,7 @@ public class ChainSplitTest {
assertFalse(reorgHappened.get());
assertEquals(2, walletChanged.get());
// We got two blocks which sent 50 coins each to us.
assertEquals("100.00", wallet.getBalance().toFriendlyString());
assertEquals(Coin.valueOf(100, 0), wallet.getBalance());
// We now have the following chain:
// genesis -> b1 -> b2
//
@ -109,7 +109,7 @@ public class ChainSplitTest {
Threading.waitForUserCode();
assertFalse(reorgHappened.get()); // No re-org took place.
assertEquals(2, walletChanged.get());
assertEquals("100.00", wallet.getBalance().toFriendlyString());
assertEquals(Coin.valueOf(100, 0), wallet.getBalance());
// Check we can handle multi-way splits: this is almost certainly going to be extremely rare, but we have to
// handle it anyway. The same transaction appears in b7/b8 (side chain) but not b2 or b3.
// genesis -> b1--> b2
@ -128,7 +128,7 @@ public class ChainSplitTest {
assertEquals(2, wallet.getTransaction(tHash).getAppearsInHashes().size());
assertFalse(reorgHappened.get()); // No re-org took place.
assertEquals(5, walletChanged.get());
assertEquals("100.00", wallet.getBalance().toFriendlyString());
assertEquals(Coin.valueOf(100, 0), wallet.getBalance());
// Now we add another block to make the alternative chain longer.
assertTrue(chain.add(b3.createNextBlock(someOtherGuy)));
Threading.waitForUserCode();
@ -153,7 +153,7 @@ public class ChainSplitTest {
Threading.waitForUserCode();
assertTrue(reorgHappened.get());
assertEquals(9, walletChanged.get());
assertEquals("200.00", wallet.getBalance().toFriendlyString());
assertEquals(Coin.valueOf(200, 0), wallet.getBalance());
}
@Test
@ -520,7 +520,7 @@ public class ChainSplitTest {
BigInteger newWork3 = work3.add(work7).add(work8);
assertEquals(newWork3, txns.get(2).getConfidence().getWorkDone());
assertEquals("250.00", wallet.getBalance().toFriendlyString());
assertEquals(Coin.valueOf(250, 0), wallet.getBalance());
// Now add two more blocks that don't send coins to us. Despite being irrelevant the wallet should still update.
Block b9 = b8.createNextBlock(someOtherGuy);

View File

@ -94,10 +94,10 @@ public class CoinTest {
@Test
public void testToFriendlyString() {
assertEquals("1.00", COIN.toFriendlyString());
assertEquals("1.23", valueOf(1, 23).toFriendlyString());
assertEquals("0.001", COIN.divide(1000).toFriendlyString());
assertEquals("-1.23", valueOf(1, 23).negate().toFriendlyString());
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());
}
/**

View File

@ -533,18 +533,18 @@ public class WalletTest extends TestWithWallet {
confTxns.clear();
sendMoneyToWallet(send1, AbstractBlockChain.NewBlockType.BEST_CHAIN);
Threading.waitForUserCode();
assertEquals(wallet.getBalance().toFriendlyString(), "0.90");
assertEquals(Coin.valueOf(0, 90), wallet.getBalance());
assertEquals(null, txn[0]);
assertEquals(2, confTxns.size());
assertEquals(txn[1].getHash(), send1.getHash());
assertEquals(bigints[2].toFriendlyString(), "1.00");
assertEquals(bigints[3].toFriendlyString(), "0.90");
assertEquals(Coin.COIN, bigints[2]);
assertEquals(Coin.valueOf(0, 90), bigints[3]);
// And we do it again after the catchup.
Transaction send2 = wallet.createSend(new ECKey().toAddress(params), valueOf(0, 10));
// What we'd really like to do is prove the official client would accept it .... no such luck unfortunately.
wallet.commitTx(send2);
sendMoneyToWallet(send2, AbstractBlockChain.NewBlockType.BEST_CHAIN);
assertEquals(wallet.getBalance().toFriendlyString(), "0.80");
assertEquals(Coin.valueOf(0, 80), wallet.getBalance());
Threading.waitForUserCode();
BlockPair b4 = createFakeBlock(blockStore);
confTxns.clear();

View File

@ -169,7 +169,7 @@ public class ExamplePaymentChannelClient {
ListenableFuture<Coin> balanceFuture = appKit.wallet().getBalanceFuture(amountPlusFee, Wallet.BalanceType.ESTIMATED);
if (!balanceFuture.isDone()) {
System.out.println("Please send " + amountPlusFee.toFriendlyString() +
" BTC to " + myKey.toAddress(params));
" to " + myKey.toAddress(params));
Futures.getUnchecked(balanceFuture);
}
}

View File

@ -121,7 +121,7 @@ public class ForwardingService {
private static void forwardCoins(Transaction tx) {
try {
Coin value = tx.getValueSentToMe(kit.wallet());
System.out.println("Forwarding " + value.toFriendlyString() + " BTC");
System.out.println("Forwarding " + value.toFriendlyString());
// Now send the coins back! Send with a small fee attached to ensure rapid confirmation.
final Coin amountToSend = value.subtract(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE);
final Wallet.SendResult sendResult = kit.wallet().sendCoins(kit.peerGroup(), forwardingAddress, amountToSend);

View File

@ -68,7 +68,7 @@ public class PrivateKeys {
peerGroup.stopAsync();
// And take them!
System.out.println("Claiming " + wallet.getBalance().toFriendlyString() + " coins");
System.out.println("Claiming " + wallet.getBalance().toFriendlyString());
wallet.sendCoins(peerGroup, destination, wallet.getBalance());
// Wait a few seconds to let the packets flush out to the network (ugly).
Thread.sleep(5000);

View File

@ -616,7 +616,7 @@ public class WalletTool {
private static void send(PaymentSession session) {
try {
System.out.println("Payment Request");
System.out.println("Coin: " + session.getValue().toFriendlyString() + " BTC");
System.out.println("Coin: " + session.getValue().toFriendlyString());
System.out.println("Date: " + session.getDate());
System.out.println("Memo: " + session.getMemo());
if (session.pkiVerificationData != null) {