mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-02-12 10:15:52 +00:00
Coin.toFriendlyFormat() includes denomination (BTC). This saves a lot of string concatenation code.
This commit is contained in:
parent
794263436f
commit
afcc7e3f13
@ -206,7 +206,7 @@ public final class Coin implements Comparable<Coin>, Serializable {
|
|||||||
return this.value;
|
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
|
* Returns the value as a 0.12 type string. More digits after the decimal place will be used
|
||||||
|
@ -655,7 +655,7 @@ public class Transaction extends ChildMessage implements Serializable {
|
|||||||
Script scriptSig = in.getScriptSig();
|
Script scriptSig = in.getScriptSig();
|
||||||
s.append(scriptSig);
|
s.append(scriptSig);
|
||||||
if (in.getValue() != null)
|
if (in.getValue() != null)
|
||||||
s.append(" ").append(in.getValue().toFriendlyString()).append(" BTC");
|
s.append(" ").append(in.getValue().toFriendlyString());
|
||||||
s.append("\n ");
|
s.append("\n ");
|
||||||
s.append("outpoint:");
|
s.append("outpoint:");
|
||||||
final TransactionOutPoint outpoint = in.getOutpoint();
|
final TransactionOutPoint outpoint = in.getOutpoint();
|
||||||
@ -678,7 +678,6 @@ public class Transaction extends ChildMessage implements Serializable {
|
|||||||
s.append(scriptPubKey);
|
s.append(scriptPubKey);
|
||||||
s.append(" ");
|
s.append(" ");
|
||||||
s.append(out.getValue().toFriendlyString());
|
s.append(out.getValue().toFriendlyString());
|
||||||
s.append(" BTC");
|
|
||||||
if (!out.isAvailableForSpending()) {
|
if (!out.isAvailableForSpending()) {
|
||||||
s.append(" Spent");
|
s.append(" Spent");
|
||||||
}
|
}
|
||||||
|
@ -1210,8 +1210,8 @@ public class Wallet extends BaseTaggableObject implements Serializable, BlockCha
|
|||||||
Coin valueSentToMe = tx.getValueSentToMe(this);
|
Coin valueSentToMe = tx.getValueSentToMe(this);
|
||||||
Coin valueSentFromMe = tx.getValueSentFromMe(this);
|
Coin valueSentFromMe = tx.getValueSentFromMe(this);
|
||||||
if (log.isInfoEnabled()) {
|
if (log.isInfoEnabled()) {
|
||||||
log.info(String.format("Received a pending transaction %s that spends %s BTC from our own wallet," +
|
log.info(String.format("Received a pending transaction %s that spends %s from our own wallet," +
|
||||||
" and sends us %s BTC", tx.getHashAsString(), valueSentFromMe.toFriendlyString(),
|
" and sends us %s", tx.getHashAsString(), valueSentFromMe.toFriendlyString(),
|
||||||
valueSentToMe.toFriendlyString()));
|
valueSentToMe.toFriendlyString()));
|
||||||
}
|
}
|
||||||
if (tx.getConfidence().getSource().equals(TransactionConfidence.Source.UNKNOWN)) {
|
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 valueSentToMe = tx.getValueSentToMe(this);
|
||||||
Coin valueDifference = valueSentToMe.subtract(valueSentFromMe);
|
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,
|
valueDifference.toFriendlyString(), tx.getHashAsString(), relativityOffset,
|
||||||
block != null ? block.getHeader().getHash() : "(unit test)");
|
block != null ? block.getHeader().getHash() : "(unit test)");
|
||||||
|
|
||||||
@ -2495,7 +2495,7 @@ public class Wallet extends BaseTaggableObject implements Serializable, BlockCha
|
|||||||
value = value.add(output.getValue());
|
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());
|
req.tx.getOutputs().size(), value.toFriendlyString());
|
||||||
|
|
||||||
// If any inputs have already been added, we don't need to get their value from wallet
|
// 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);
|
bestCoinSelection = selector.select(NetworkParameters.MAX_MONEY, candidates);
|
||||||
candidates = null; // Selector took ownership and might have changed candidates. Don't access again.
|
candidates = null; // Selector took ownership and might have changed candidates. Don't access again.
|
||||||
req.tx.getOutput(0).setValue(bestCoinSelection.valueGathered);
|
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)
|
for (TransactionOutput output : bestCoinSelection.gathered)
|
||||||
@ -2560,7 +2560,7 @@ public class Wallet extends BaseTaggableObject implements Serializable, BlockCha
|
|||||||
|
|
||||||
if (bestChangeOutput != null) {
|
if (bestChangeOutput != null) {
|
||||||
req.tx.addOutput(bestChangeOutput);
|
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.
|
// Now shuffle the outputs to obfuscate which is the change.
|
||||||
|
@ -94,7 +94,7 @@ public class ChainSplitTest {
|
|||||||
assertFalse(reorgHappened.get());
|
assertFalse(reorgHappened.get());
|
||||||
assertEquals(2, walletChanged.get());
|
assertEquals(2, walletChanged.get());
|
||||||
// We got two blocks which sent 50 coins each to us.
|
// 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:
|
// We now have the following chain:
|
||||||
// genesis -> b1 -> b2
|
// genesis -> b1 -> b2
|
||||||
//
|
//
|
||||||
@ -109,7 +109,7 @@ public class ChainSplitTest {
|
|||||||
Threading.waitForUserCode();
|
Threading.waitForUserCode();
|
||||||
assertFalse(reorgHappened.get()); // No re-org took place.
|
assertFalse(reorgHappened.get()); // No re-org took place.
|
||||||
assertEquals(2, walletChanged.get());
|
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
|
// 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.
|
// handle it anyway. The same transaction appears in b7/b8 (side chain) but not b2 or b3.
|
||||||
// genesis -> b1--> b2
|
// genesis -> b1--> b2
|
||||||
@ -128,7 +128,7 @@ public class ChainSplitTest {
|
|||||||
assertEquals(2, wallet.getTransaction(tHash).getAppearsInHashes().size());
|
assertEquals(2, wallet.getTransaction(tHash).getAppearsInHashes().size());
|
||||||
assertFalse(reorgHappened.get()); // No re-org took place.
|
assertFalse(reorgHappened.get()); // No re-org took place.
|
||||||
assertEquals(5, walletChanged.get());
|
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.
|
// Now we add another block to make the alternative chain longer.
|
||||||
assertTrue(chain.add(b3.createNextBlock(someOtherGuy)));
|
assertTrue(chain.add(b3.createNextBlock(someOtherGuy)));
|
||||||
Threading.waitForUserCode();
|
Threading.waitForUserCode();
|
||||||
@ -153,7 +153,7 @@ public class ChainSplitTest {
|
|||||||
Threading.waitForUserCode();
|
Threading.waitForUserCode();
|
||||||
assertTrue(reorgHappened.get());
|
assertTrue(reorgHappened.get());
|
||||||
assertEquals(9, walletChanged.get());
|
assertEquals(9, walletChanged.get());
|
||||||
assertEquals("200.00", wallet.getBalance().toFriendlyString());
|
assertEquals(Coin.valueOf(200, 0), wallet.getBalance());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -520,7 +520,7 @@ public class ChainSplitTest {
|
|||||||
BigInteger newWork3 = work3.add(work7).add(work8);
|
BigInteger newWork3 = work3.add(work7).add(work8);
|
||||||
assertEquals(newWork3, txns.get(2).getConfidence().getWorkDone());
|
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.
|
// 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);
|
Block b9 = b8.createNextBlock(someOtherGuy);
|
||||||
|
@ -94,10 +94,10 @@ public class CoinTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testToFriendlyString() {
|
public void testToFriendlyString() {
|
||||||
assertEquals("1.00", COIN.toFriendlyString());
|
assertEquals("1.00 BTC", COIN.toFriendlyString());
|
||||||
assertEquals("1.23", valueOf(1, 23).toFriendlyString());
|
assertEquals("1.23 BTC", valueOf(1, 23).toFriendlyString());
|
||||||
assertEquals("0.001", COIN.divide(1000).toFriendlyString());
|
assertEquals("0.001 BTC", COIN.divide(1000).toFriendlyString());
|
||||||
assertEquals("-1.23", valueOf(1, 23).negate().toFriendlyString());
|
assertEquals("-1.23 BTC", valueOf(1, 23).negate().toFriendlyString());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -533,18 +533,18 @@ public class WalletTest extends TestWithWallet {
|
|||||||
confTxns.clear();
|
confTxns.clear();
|
||||||
sendMoneyToWallet(send1, AbstractBlockChain.NewBlockType.BEST_CHAIN);
|
sendMoneyToWallet(send1, AbstractBlockChain.NewBlockType.BEST_CHAIN);
|
||||||
Threading.waitForUserCode();
|
Threading.waitForUserCode();
|
||||||
assertEquals(wallet.getBalance().toFriendlyString(), "0.90");
|
assertEquals(Coin.valueOf(0, 90), wallet.getBalance());
|
||||||
assertEquals(null, txn[0]);
|
assertEquals(null, txn[0]);
|
||||||
assertEquals(2, confTxns.size());
|
assertEquals(2, confTxns.size());
|
||||||
assertEquals(txn[1].getHash(), send1.getHash());
|
assertEquals(txn[1].getHash(), send1.getHash());
|
||||||
assertEquals(bigints[2].toFriendlyString(), "1.00");
|
assertEquals(Coin.COIN, bigints[2]);
|
||||||
assertEquals(bigints[3].toFriendlyString(), "0.90");
|
assertEquals(Coin.valueOf(0, 90), bigints[3]);
|
||||||
// And we do it again after the catchup.
|
// And we do it again after the catchup.
|
||||||
Transaction send2 = wallet.createSend(new ECKey().toAddress(params), valueOf(0, 10));
|
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.
|
// What we'd really like to do is prove the official client would accept it .... no such luck unfortunately.
|
||||||
wallet.commitTx(send2);
|
wallet.commitTx(send2);
|
||||||
sendMoneyToWallet(send2, AbstractBlockChain.NewBlockType.BEST_CHAIN);
|
sendMoneyToWallet(send2, AbstractBlockChain.NewBlockType.BEST_CHAIN);
|
||||||
assertEquals(wallet.getBalance().toFriendlyString(), "0.80");
|
assertEquals(Coin.valueOf(0, 80), wallet.getBalance());
|
||||||
Threading.waitForUserCode();
|
Threading.waitForUserCode();
|
||||||
BlockPair b4 = createFakeBlock(blockStore);
|
BlockPair b4 = createFakeBlock(blockStore);
|
||||||
confTxns.clear();
|
confTxns.clear();
|
||||||
|
@ -169,7 +169,7 @@ public class ExamplePaymentChannelClient {
|
|||||||
ListenableFuture<Coin> balanceFuture = appKit.wallet().getBalanceFuture(amountPlusFee, Wallet.BalanceType.ESTIMATED);
|
ListenableFuture<Coin> balanceFuture = appKit.wallet().getBalanceFuture(amountPlusFee, Wallet.BalanceType.ESTIMATED);
|
||||||
if (!balanceFuture.isDone()) {
|
if (!balanceFuture.isDone()) {
|
||||||
System.out.println("Please send " + amountPlusFee.toFriendlyString() +
|
System.out.println("Please send " + amountPlusFee.toFriendlyString() +
|
||||||
" BTC to " + myKey.toAddress(params));
|
" to " + myKey.toAddress(params));
|
||||||
Futures.getUnchecked(balanceFuture);
|
Futures.getUnchecked(balanceFuture);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -121,7 +121,7 @@ public class ForwardingService {
|
|||||||
private static void forwardCoins(Transaction tx) {
|
private static void forwardCoins(Transaction tx) {
|
||||||
try {
|
try {
|
||||||
Coin value = tx.getValueSentToMe(kit.wallet());
|
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.
|
// 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 Coin amountToSend = value.subtract(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE);
|
||||||
final Wallet.SendResult sendResult = kit.wallet().sendCoins(kit.peerGroup(), forwardingAddress, amountToSend);
|
final Wallet.SendResult sendResult = kit.wallet().sendCoins(kit.peerGroup(), forwardingAddress, amountToSend);
|
||||||
|
@ -68,7 +68,7 @@ public class PrivateKeys {
|
|||||||
peerGroup.stopAsync();
|
peerGroup.stopAsync();
|
||||||
|
|
||||||
// And take them!
|
// And take them!
|
||||||
System.out.println("Claiming " + wallet.getBalance().toFriendlyString() + " coins");
|
System.out.println("Claiming " + wallet.getBalance().toFriendlyString());
|
||||||
wallet.sendCoins(peerGroup, destination, wallet.getBalance());
|
wallet.sendCoins(peerGroup, destination, wallet.getBalance());
|
||||||
// Wait a few seconds to let the packets flush out to the network (ugly).
|
// Wait a few seconds to let the packets flush out to the network (ugly).
|
||||||
Thread.sleep(5000);
|
Thread.sleep(5000);
|
||||||
|
@ -616,7 +616,7 @@ public class WalletTool {
|
|||||||
private static void send(PaymentSession session) {
|
private static void send(PaymentSession session) {
|
||||||
try {
|
try {
|
||||||
System.out.println("Payment Request");
|
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("Date: " + session.getDate());
|
||||||
System.out.println("Memo: " + session.getMemo());
|
System.out.println("Memo: " + session.getMemo());
|
||||||
if (session.pkiVerificationData != null) {
|
if (session.pkiVerificationData != null) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user