Skip to content

Commit

Permalink
feat(rpc_client): show total accumulated balance when decrypting tran…
Browse files Browse the repository at this point in the history
…sfers received
  • Loading branch information
bochaco committed Oct 26, 2023
1 parent e680108 commit 6cc4689
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
10 changes: 8 additions & 2 deletions sn_node/examples/safenode_rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,12 @@ pub async fn transfers_events(
sk: String,
log_cash_notes: Option<PathBuf>,
) -> Result<()> {
let (client, wallet) = match SecretKey::from_hex(&sk) {
let (client, mut wallet) = match SecretKey::from_hex(&sk) {
Ok(sk) => {
let client = Client::new(sk.clone(), None, None).await?;
let main_sk = MainSecretKey::new(sk);
let wallet_dir = TempDir::new()?;
let wallet = LocalWallet::load_from_main_key(wallet_dir.path(), main_sk)?;
let wallet = LocalWallet::load_from_main_key(&wallet_dir, main_sk)?;
(client, wallet)
}
Err(err) => return Err(eyre!("Failed to parse hex-encoded SK: {err:?}")),
Expand Down Expand Up @@ -270,6 +270,8 @@ pub async fn transfers_events(
}
}

wallet.deposit(&cash_notes)?;

for cn in cash_notes {
println!(
"CashNote received with {:?}, value: {}",
Expand All @@ -291,6 +293,10 @@ pub async fn transfers_events(
fs::write(cash_note_file_path, &hex)?;
}
}
println!(
"New balance after depositing received CashNote/s: {}",
wallet.balance()
);
println!();
}
Ok(_) => continue,
Expand Down
22 changes: 22 additions & 0 deletions sn_transfers/src/wallet/local_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,28 @@ impl LocalWallet {
Ok(())
}

/// Store the given cash_notes on the wallet (without storing them to disk).
pub fn deposit(&mut self, received_cash_notes: &Vec<CashNote>) -> Result<()> {
for cash_note in received_cash_notes {
let id = cash_note.unique_pubkey();

if self.wallet.spent_cash_notes.contains(&id) {
debug!("skipping: cash_note is spent");
continue;
}

if cash_note.derived_key(&self.key).is_err() {
debug!("skipping: cash_note is not our key");
continue;
}

let value = cash_note.value()?;
self.wallet.available_cash_notes.insert(id, value);
}

Ok(())
}

/// Store the given cash_notes to the `cash_notes` dir in the wallet dir.
/// Update and store the updated wallet to disk
/// This function locks the wallet to prevent concurrent processes from writing to it
Expand Down

0 comments on commit 6cc4689

Please sign in to comment.