Browse Source

List transactions

checkpoints
Aditya Kulkarni 5 years ago
parent
commit
4b4a308717
  1. 17
      rust-lightclient/src/commands.rs
  2. 32
      rust-lightclient/src/lightclient.rs

17
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<HashMap<String, Box<dyn Command>>> {
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)
}

32
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::<Vec<(TxId, Option<Memo>)>>();
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::<Vec<Option<String>>>();
println!("All Wallet Txns {:?}", memos);
//println!("All Wallet Txns {:?}", memos);
}
pub fn do_send(&self, addr: String, value: u64, memo: Option<String>) {

Loading…
Cancel
Save