Print transactions list
This commit is contained in:
@@ -27,6 +27,14 @@ 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>,
|
||||||
@@ -118,31 +126,70 @@ impl LightClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn do_list_transactions(&self) {
|
pub fn do_list_transactions(&self) {
|
||||||
// Go over all the transactions
|
// Create a list of TransactionItems
|
||||||
self.wallet.txs.read().unwrap().iter().for_each(
|
let mut tx_list = self.wallet.txs.read().unwrap().iter()
|
||||||
| (k, v) | {
|
.flat_map(| (_k, v) | {
|
||||||
println!("Block {}", v.block);
|
let mut txns = Vec::new();
|
||||||
println!("Txid {}", k);
|
|
||||||
v.notes.iter().for_each( |nd| {
|
if v.total_shielded_value_spent > 0 {
|
||||||
println!("Spent in txid {}", match nd.spent {
|
// If money was spent, create a transaction. For this, we'll subtract
|
||||||
Some(txid) => format!("{}", txid),
|
// all the change notes. TODO: Add transparent change here to subtract it also
|
||||||
_ => "(not spent)".to_string()
|
let total_change: u64 = v.notes.iter()
|
||||||
|
.filter( |nd| nd.is_change )
|
||||||
|
.map( |nd| nd.note.value )
|
||||||
|
.sum();
|
||||||
|
|
||||||
|
// TODO: What happens if change is > than sent ?
|
||||||
|
|
||||||
|
txns.push(TransactionListItem {
|
||||||
|
block_height: v.block,
|
||||||
|
txid : format!("{}", v.txid),
|
||||||
|
amount : total_change as i64 - v.total_shielded_value_spent as i64,
|
||||||
|
address : "".to_string(), // TODO: For send, we don't have an address
|
||||||
|
memo : None
|
||||||
});
|
});
|
||||||
println!("Value {}", nd.note.value);
|
}
|
||||||
println!("Memo {}", match &nd.memo {
|
|
||||||
Some(memo) => {
|
// For each note that is not a change, add a Tx.
|
||||||
match memo.to_utf8() {
|
txns.extend(v.notes.iter()
|
||||||
Some(Ok(memo_str)) => memo_str,
|
.filter( |nd| !nd.is_change )
|
||||||
_ => "".to_string()
|
.map ( |nd|
|
||||||
}
|
TransactionListItem {
|
||||||
}
|
block_height: v.block,
|
||||||
_ => "".to_string()
|
txid : format!("{}", v.txid),
|
||||||
});
|
amount : nd.note.value as i64,
|
||||||
});
|
address : nd.note_address().unwrap(),
|
||||||
|
memo : match &nd.memo {
|
||||||
println!("Total spent: {}", v.total_shielded_value_spent);
|
Some(memo) => {
|
||||||
}
|
match memo.to_utf8() {
|
||||||
)
|
Some(Ok(memo_str)) => Some(memo_str),
|
||||||
|
_ => None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => None
|
||||||
|
}
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
txns
|
||||||
|
})
|
||||||
|
.collect::<Vec<TransactionListItem>>();
|
||||||
|
|
||||||
|
tx_list.sort_by( |a, b| if a.block_height == b.block_height {
|
||||||
|
a.txid.cmp(&b.txid)
|
||||||
|
} else {
|
||||||
|
a.block_height.cmp(&b.block_height)
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
tx_list.iter().for_each(|tx| {
|
||||||
|
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) {
|
||||||
|
|||||||
@@ -90,7 +90,6 @@ impl BlockData {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub struct SaplingNoteData {
|
pub struct SaplingNoteData {
|
||||||
account: usize,
|
account: usize,
|
||||||
extfvk: ExtendedFullViewingKey, // Technically, this should be recoverable from the account number, but we're going to refactor this in the future, so I'll write it again here.
|
extfvk: ExtendedFullViewingKey, // Technically, this should be recoverable from the account number, but we're going to refactor this in the future, so I'll write it again here.
|
||||||
@@ -254,6 +253,13 @@ impl SaplingNoteData {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn note_address(&self) -> Option<String> {
|
||||||
|
match self.extfvk.fvk.vk.into_payment_address(self.diversifier, &JUBJUB) {
|
||||||
|
Some(pa) => Some(encode_payment_address(HRP_SAPLING_PAYMENT_ADDRESS, &pa)),
|
||||||
|
None => None
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct WalletTx {
|
pub struct WalletTx {
|
||||||
|
|||||||
Reference in New Issue
Block a user