Bitcoinj
與比特幣j的交易歷史
我是比特幣j的新手。我想列印出如下錢包的交易列表:
1- Sent 1 BTC to Adress1 on 'A' Time 2- Received 5 BTC from Adress2 on 'B' time 3- Sent 2 BTC to Address 3 on 'C' time
我怎樣才能做到這一點?
創建一個錢包應用套件對象,例如:
WalletAppKit kit = new WalletAppKit(params, new File("."), "filename"); kit.startAsync(); kit.awaitRunning(); Iterable<WalletTransaction> walletTransactions = kit.wallet().getWalletTransactions();
此 walletTransactions 將為您提供與錢包相關的所有交易。
您可以像這樣獲取任何交易的交易歷史
private void txHistory() { Set<Transaction> txx = kit.wallet().getTransactions(true); if (!txx.isEmpty()) { int i = 1; for (Transaction tx : txx) { System.out.println(i + " ________________________"); System.out.println("Date and Time: " + tx.getUpdateTime().toString()); System.out.println("From Address: " + tx.getOutput(0).getAddressFromP2PKHScript(params)); System.out.println("To Address: " + tx.getOutput(0).getAddressFromP2PKHScript(params)); System.out.println("Amount Sent to me: " + tx.getValueSentToMe(kit.wallet()).toFriendlyString()); System.out.println("Amount Sent from me: " + tx.getValueSentFromMe(kit.wallet()).toFriendlyString()); long fee = (tx.getInputSum().getValue() > 0 ? tx.getInputSum().getValue() - tx.getOutputSum().getValue() : 0); System.out.println("Fee: " + Coin.valueOf(fee).toFriendlyString()); System.out.println("Transaction Depth: " + tx.getConfidence().getDepthInBlocks()); System.out.println("Transaction Blocks: " + tx.getConfidence().toString()); System.out.println("Tx Hex: " + tx.getHashAsString()); System.out.println("Tx: " + tx.toString()); i++; } } else { System.err.println("No Transaction Found"); } }