Skip to content

Commit

Permalink
refactor: pay_for_chunks returns cost and new balance
Browse files Browse the repository at this point in the history
Rather than the client printing out information relating to the cost of the chunks and the resulting
new balance, the information is returned to the caller, who can then print it out.

The error handling for storing the wallet is also changed to propagate the error rather than
suppressing it by printing output.
  • Loading branch information
jacderida committed Oct 4, 2023
1 parent eb0c2ce commit 7d95d86
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
7 changes: 6 additions & 1 deletion sn_cli/src/subcommands/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,14 @@ async fn upload_files(
"Getting upload costs from network for {} chunks...",
chunks_batch.len()
);
file_api
let (cost, new_balance) = file_api
.pay_for_chunks(chunks_batch.iter().map(|(name, _)| *name).collect(), true)
.await?;
println!("Made payment of {cost} for {} chunks", chunks_batch.len());
println!(
"Stored wallet with cached payment proofs. New balance: {}",
new_balance
);

// Verification will be carried out later on, if being asked to.
// Hence no need to carry out verification within the first attempt.
Expand Down
26 changes: 12 additions & 14 deletions sn_client/src/file_apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use sn_protocol::{
storage::{Chunk, ChunkAddress},
NetworkAddress, PrettyPrintRecordKey,
};
use sn_transfers::LocalWallet;
use sn_transfers::{LocalWallet, NanoTokens};

use std::{
fs::{self, create_dir_all, File},
Expand Down Expand Up @@ -186,8 +186,14 @@ impl Files {
Ok(())
}

/// Pay for a given set of chunks
pub async fn pay_for_chunks(&self, chunks: Vec<XorName>, verify_store: bool) -> Result<()> {
/// Pay for a given set of chunks.
///
/// Returns the cost and the resulting new balance of the local wallet.
pub async fn pay_for_chunks(
&self,
chunks: Vec<XorName>,
verify_store: bool,
) -> Result<(NanoTokens, NanoTokens)> {
let mut wallet_client = self.wallet()?;
info!("Paying for and uploading {:?} chunks", chunks.len());

Expand All @@ -199,18 +205,10 @@ impl Files {
verify_store,
)
.await?;
println!("Made payment of {cost} for {} chunks", chunks.len(),);

if let Err(err) = wallet_client.store_local_wallet() {
println!("Failed to store wallet: {err:?}");
} else {
println!(
"Stored wallet with cached payment proofs. New balance: {}",
wallet_client.balance()
);
}

Ok(())
wallet_client.store_local_wallet()?;
let new_balance = wallet_client.balance();
Ok((cost, new_balance))
}

// --------------------------------------------
Expand Down

0 comments on commit 7d95d86

Please sign in to comment.