From 4b4a3087179e7aa7d42e884221cb8004e61f6cd3 Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Fri, 6 Sep 2019 15:18:38 -0700 Subject: [PATCH] List transactions --- rust-lightclient/src/commands.rs | 17 +++++++++++++++ rust-lightclient/src/lightclient.rs | 32 ++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/rust-lightclient/src/commands.rs b/rust-lightclient/src/commands.rs index 64707f4..e2e9fc8 100644 --- a/rust-lightclient/src/commands.rs +++ b/rust-lightclient/src/commands.rs @@ -124,6 +124,22 @@ impl Command for ReadCommand { } } +struct TransactionsCommand {} +impl Command for TransactionsCommand { + fn help(&self) { + println!("Show transactions"); + } + + fn short_help(&self) -> String { + "List all transactions in the wallet".to_string() + } + + fn exec(&self, _args: &[String], lightclient: &mut LightClient) { + lightclient.do_list_transactions(); + } + +} + struct QuitCommand {} impl Command for QuitCommand { fn help(&self) { @@ -151,6 +167,7 @@ pub fn get_commands() -> Box>> { map.insert("save".to_string(), Box::new(SaveCommand{})); map.insert("read".to_string(), Box::new(ReadCommand{})); map.insert("quit".to_string(), Box::new(QuitCommand{})); + map.insert("list".to_string(), Box::new(TransactionsCommand{})); Box::new(map) } diff --git a/rust-lightclient/src/lightclient.rs b/rust-lightclient/src/lightclient.rs index 584d9c9..35e21f6 100644 --- a/rust-lightclient/src/lightclient.rs +++ b/rust-lightclient/src/lightclient.rs @@ -114,6 +114,32 @@ impl LightClient { tokio::runtime::current_thread::Runtime::new().unwrap().block_on(say_hello).unwrap() } + pub fn do_list_transactions(&self) { + // Go over all the transactions + self.wallet.txs.read().unwrap().iter().for_each( + | (k, v) | { + println!("Block {}", v.block); + println!("Txid {}", k); + v.notes.iter().for_each( |nd| { + println!("Spent in txid {}", match nd.spent { + Some(txid) => format!("{}", txid), + _ => "(not spent)".to_string() + }); + println!("Value {}", nd.note.value); + println!("Memo {}", match &nd.memo { + Some(memo) => { + match memo.to_utf8() { + Some(Ok(memo_str)) => memo_str, + _ => "".to_string() + } + } + _ => "".to_string() + }); + }); + } + ) + } + pub fn do_sync(&self) { // Sync is 3 parts // 1. Get the latest block @@ -185,7 +211,7 @@ impl LightClient { }) .collect::)>>(); - println!("{:?}", txids_and_memos); + //println!("{:?}", txids_and_memos); // TODO: Assert that all the memos here are None txids_to_fetch = txids_and_memos.iter() @@ -197,7 +223,7 @@ impl LightClient { // read the memos for txid in txids_to_fetch { let light_wallet_clone = self.wallet.clone(); - println!("Scanning txid {}", txid); + println!("Fetching full Tx: {}", txid); self.fetch_full_tx(txid, move |tx_bytes: &[u8] | { let tx = Transaction::read(tx_bytes).unwrap(); @@ -222,7 +248,7 @@ impl LightClient { }) .collect::>>(); - println!("All Wallet Txns {:?}", memos); + //println!("All Wallet Txns {:?}", memos); } pub fn do_send(&self, addr: String, value: u64, memo: Option) {