Json for tx list

This commit is contained in:
Aditya Kulkarni 2019-09-08 15:21:37 -07:00
parent 972558620c
commit f9d0878a81
2 changed files with 21 additions and 36 deletions

View File

@ -136,7 +136,8 @@ impl Command for TransactionsCommand {
} }
fn exec(&self, _args: &[String], lightclient: &mut LightClient) { fn exec(&self, _args: &[String], lightclient: &mut LightClient) {
lightclient.do_list_transactions(); let txns = lightclient.do_list_transactions();
println!("{}", txns.pretty(2));
} }
} }

View File

@ -32,15 +32,6 @@ use crate::grpc_client::client::CompactTxStreamer;
// Used below to return the grpc "Client" type to calling methods // Used below to return the grpc "Client" type to calling methods
type Client = crate::grpc_client::client::CompactTxStreamer<tower_request_modifier::RequestModifier<tower_hyper::client::Connection<tower_grpc::BoxBody>, tower_grpc::BoxBody>>; type Client = crate::grpc_client::client::CompactTxStreamer<tower_request_modifier::RequestModifier<tower_hyper::client::Connection<tower_grpc::BoxBody>, tower_grpc::BoxBody>>;
#[derive(Debug)]
pub struct TransactionListItem {
pub block_height: i32, // Block height in which this transaction was confirmed
pub txid: String,
pub amount: i64, // Amount of this Tx. -ve values are spends, +ve are recieve
pub address: String, // for recieves, it is the incoming address. For sends, it is the address sent from
pub memo: Option<String>, // Optional string
}
pub struct LightClient { pub struct LightClient {
pub wallet : Arc<LightWallet>, pub wallet : Arc<LightWallet>,
pub sapling_output : Vec<u8>, pub sapling_output : Vec<u8>,
@ -142,11 +133,11 @@ impl LightClient {
tokio::runtime::current_thread::Runtime::new().unwrap().block_on(say_hello).unwrap() tokio::runtime::current_thread::Runtime::new().unwrap().block_on(say_hello).unwrap()
} }
pub fn do_list_transactions(&self) { pub fn do_list_transactions(&self) -> JsonValue {
// Create a list of TransactionItems // Create a list of TransactionItems
let mut tx_list = self.wallet.txs.read().unwrap().iter() let mut tx_list = self.wallet.txs.read().unwrap().iter()
.flat_map(| (_k, v) | { .flat_map(| (_k, v) | {
let mut txns = Vec::new(); let mut txns: Vec<JsonValue> = vec![];
if v.total_shielded_value_spent > 0 { if v.total_shielded_value_spent > 0 {
// If money was spent, create a transaction. For this, we'll subtract // If money was spent, create a transaction. For this, we'll subtract
@ -158,12 +149,12 @@ impl LightClient {
// TODO: What happens if change is > than sent ? // TODO: What happens if change is > than sent ?
txns.push(TransactionListItem { txns.push(object! {
block_height: v.block, "block_height" => v.block,
txid : format!("{}", v.txid), "txid" => format!("{}", v.txid),
amount : total_change as i64 - v.total_shielded_value_spent as i64, "amount" => total_change as i64 - v.total_shielded_value_spent as i64,
address : "".to_string(), // TODO: For send, we don't have an address "address" => None::<String>, // TODO: For send, we don't have an address
memo : None "memo" => None::<String>
}); });
} }
@ -171,12 +162,12 @@ impl LightClient {
txns.extend(v.notes.iter() txns.extend(v.notes.iter()
.filter( |nd| !nd.is_change ) .filter( |nd| !nd.is_change )
.map ( |nd| .map ( |nd|
TransactionListItem { object! {
block_height: v.block, "block_height" => v.block,
txid : format!("{}", v.txid), "txid" => format!("{}", v.txid),
amount : nd.note.value as i64, "amount" => nd.note.value as i64,
address : nd.note_address().unwrap(), "address" => nd.note_address().unwrap(),
memo : match &nd.memo { "memo" => match &nd.memo {
Some(memo) => { Some(memo) => {
match memo.to_utf8() { match memo.to_utf8() {
Some(Ok(memo_str)) => Some(memo_str), Some(Ok(memo_str)) => Some(memo_str),
@ -190,23 +181,16 @@ impl LightClient {
txns txns
}) })
.collect::<Vec<TransactionListItem>>(); .collect::<Vec<JsonValue>>();
tx_list.sort_by( |a, b| if a.block_height == b.block_height { tx_list.sort_by( |a, b| if a["block_height"] == b["block_height"] {
a.txid.cmp(&b.txid) a["txid"].as_str().cmp(&b["txid"].as_str())
} else { } else {
a.block_height.cmp(&b.block_height) a["block_height"].as_i32().cmp(&b["block_height"].as_i32())
} }
); );
tx_list.iter().for_each(|tx| { JsonValue::Array(tx_list)
println!("height: {}", tx.block_height);
println!("txid: {}", tx.txid);
println!("amount: {}", tx.amount);
println!("address: {}", tx.address);
println!("memo: {}", tx.memo.as_ref().unwrap_or(&"".to_string()));
println!("");
});
} }
pub fn do_sync(&self) { pub fn do_sync(&self) {