Skip to content

Commit

Permalink
rusk-wallet: Add is_sycned() function call to display balance placeho…
Browse files Browse the repository at this point in the history
…lder
  • Loading branch information
Daksh14 committed Sep 30, 2024
1 parent f52e13f commit 3018798
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 7 deletions.
44 changes: 37 additions & 7 deletions rusk-wallet/src/bin/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ pub(crate) async fn run_loop(
addr: wallet.bls_public_key(addr.index()?),
};

let is_synced = wallet.is_synced().await?;

loop {
// get balance for this address
prompt::hide_cursor()?;
Expand All @@ -82,25 +84,46 @@ pub(crate) async fn run_loop(
let spendable = phoenix_bal.spendable.into();
let total: Dusk = phoenix_bal.value.into();

let mut spendable_str = format!("{}", spendable);
let mut total_str = format!("{}", total);
let mut moonlight_bal_str = format!("{}", moonlight_bal);

// display placeholders if not synced yet
if !is_synced {
moonlight_bal_str = String::from("XXX");
spendable_str = String::from("XXX");
total_str = String::from("XXX");
}

prompt::hide_cursor()?;

// display address information
println!();
println!();
println!("{0: <20} - Total: {moonlight_bal}", "Moonlight Balance");
println!(
"{0: <20} - Total: {moonlight_bal_str}",
"Moonlight Balance"
);
println!("{0: <20} {moonlight}", "Moonlight Address");

println!();
println!("{0: <20} - Spendable: {spendable}", "Phoenix Balance",);
println!("{0: <20} - Total: {total}", "");
println!(
"{0: <20} - Spendable: {spendable_str}",
"Phoenix Balance",
);
println!("{0: <20} - Total: {total_str}", "");
println!("{0: <20} {addr}", "Phoenix Address");
println!();

// request operation to perform
let op = match wallet.is_online().await {
true => {
menu_op(addr.clone(), spendable, moonlight_bal, settings)
}
true => menu_op(
addr.clone(),
spendable,
moonlight_bal,
settings,
is_synced,
),
false => menu_op_offline(addr.clone(), settings),
};

Expand Down Expand Up @@ -417,6 +440,7 @@ fn menu_op(
phoenix_balance: Dusk,
moonlight_balance: Dusk,
settings: &Settings,
is_synced: bool,
) -> anyhow::Result<AddrOp> {
use CommandMenuItem as CMI;

Expand All @@ -431,8 +455,14 @@ fn menu_op(
.add(CMI::Back, "Back")
.separator();

let mut msg = "What do you want to do?";

if !is_synced {
msg = "Not Synced yet, wait before peroforming any operation.";
}

let q = Question::select("theme")
.message("What would you like to do?")
.message(msg)
.choices(cmd_menu.clone())
.build();

Expand Down
19 changes: 19 additions & 0 deletions rusk-wallet/src/clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,25 @@ impl State {
Ok(branch)
}

/// Queries the transfer contract for the latest network position.
pub async fn fetch_network_pos(&self) -> Result<u64, Error> {
let status = self.status;
status("Fetching latest note position...");

let data = self
.client
.contract_query::<_, _, { u64::SIZE }>(
TRANSFER_CONTRACT,
"num_notes",
&(),
)
.await?;

let res: u64 = rkyv::from_bytes(&data).map_err(|_| Error::Rkyv)?;

Ok(res)
}

pub fn close(&mut self) {
// UNWRAP: its okay to panic here because we're closing the database
// if there's an error we want an exception to happen
Expand Down
9 changes: 9 additions & 0 deletions rusk-wallet/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,15 @@ impl<F: SecureWalletFile + Debug> Wallet<F> {
}
}

/// Check if the wallet is synced
pub async fn is_synced(&mut self) -> Result<bool, Error> {
let state = self.state()?;
let db_pos = state.cache().last_pos()?.unwrap_or(0);
let network_last_pos = state.fetch_network_pos().await? - 1;

Ok(network_last_pos == db_pos)
}

/// Close the wallet and zeroize the seed
pub fn close(&mut self) {
self.store.inner_mut().zeroize();
Expand Down

0 comments on commit 3018798

Please sign in to comment.