Skip to content

Commit

Permalink
rusk-wallet: Remove requestty dependency and replace with Inquire
Browse files Browse the repository at this point in the history
  • Loading branch information
Daksh14 committed Nov 13, 2024
1 parent 802b06b commit 85b6499
Show file tree
Hide file tree
Showing 12 changed files with 177 additions and 208 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ humantime-serde = "=1.1.1"
hyper = "=1.4.1"
hyper-tungstenite = "=0.13.0"
hyper-util = "=0.1.9"
inquire = "=0.7.5"
konst = "=0.3.9"
lazy_static = "=1.5.0"
lru = "=0.12.4"
Expand All @@ -114,7 +115,6 @@ parking_lot = "=0.12.3"
pin-project = "=1.1.5"
rand = { version = "=0.8.5", default-features = false }
rand_chacha = { version = "=0.3.1", default-features = false }
requestty = "=0.5.0"
reqwest = "=0.12.7"
ringbuffer = "=0.15.0"
rkyv = { version = "=0.7.39", default-features = false }
Expand Down Expand Up @@ -149,6 +149,7 @@ version_check = "=0.9.5"
zeroize = { version = "=1.8.1", default-features = false }
zip = "=0.5.13"


[profile.dev.build-override]
opt-level = 3
debug = false
Expand Down
3 changes: 1 addition & 2 deletions rusk-wallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ serde_json = { workspace = true }
hex = { workspace = true }
tiny-bip39 = { workspace = true }
crossterm = { workspace = true }
requestty = { workspace = true }
futures = { workspace = true }
base64 = { workspace = true }
blake3 = { workspace = true }
Expand Down Expand Up @@ -55,7 +54,7 @@ tracing-subscriber = { workspace = true, features = [
] }

rkyv = { workspace = true }

inquire = { workspace = true }
konst = { workspace = true }

[dev-dependencies]
Expand Down
35 changes: 17 additions & 18 deletions rusk-wallet/src/bin/command/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::fmt::{self, Display};

use rusk_wallet::{DecodedNote, gql::{GraphQL, TransactionBlock}};
use rusk_wallet::{BlockTransaction, DecodedNote, GraphQL};

use execution_core::{dusk, from_dusk, transfer::Transaction};

Expand Down Expand Up @@ -94,23 +94,22 @@ pub(crate) async fn transaction_from_notes(

let note_hash = decoded_note.note.hash();
// Looking for the transaction which created the note
let note_creator = txs.iter_mut().find(|t| {
let t = &t.tx;
t.outputs().iter().any(|n| n.hash().eq(&note_hash))
|| t.nullifiers()
.iter()
.any(|tx_null| nullifiers.iter().any(|(n, _)| n == tx_null))
});
if let Some(TransactionBlock { tx, id ,gas_spent}) = note_creator {
let t = tx;
let tx_id = id;
let gas_spent = *gas_spent;
let note_creator = txs.iter_mut().find(|tx_block| {
let t = &tx_block.tx;

let inputs_amount: f64 = t
t.outputs().iter().any(|note| note.hash().eq(&note_hash))
|| t.nullifiers().iter().any(|tx_null| {
nullifiers.iter().any(|(nullifier, _)| nullifier == tx_null)
})
});
if let Some(BlockTransaction { tx, id, gas_spent }) = note_creator {
let inputs_amount: f64 = tx
.nullifiers()
.iter()
.filter_map(|input| {
nullifiers.iter().find_map(|n| n.0.eq(input).then_some(n.1))
nullifiers.iter().find_map(|(nullifier, gas)| {
nullifier.eq(input).then_some(gas)
})
})
.sum::<u64>() as f64;

Expand All @@ -119,15 +118,15 @@ pub(crate) async fn transaction_from_notes(
false => TransactionDirection::In,
};

match ret.iter_mut().find(|th| &th.id == tx_id) {
match ret.iter_mut().find(|th| &th.id == id) {
Some(tx) => tx.amount += note_amount,
None => ret.push(TransactionHistory {
direction,
height: decoded_note.block_height,
amount: note_amount - inputs_amount,
fee: gas_spent * t.gas_price(),
tx: t.clone(),
id: tx_id.clone(),
fee: *gas_spent * tx.gas_price(),
tx: tx.clone(),
id: id.clone(),
}),
}
} else {
Expand Down
229 changes: 97 additions & 132 deletions rusk-wallet/src/bin/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
mod command_menu;

use std::fmt::Display;
use std::future::Future;
use std::pin::Pin;

use bip39::{Language, Mnemonic, MnemonicType};
use inquire::{InquireError, Select};
Expand All @@ -19,154 +17,121 @@ use rusk_wallet::{
Address, Error, Profile, Wallet, WalletPath, MAX_PROFILES,
};

use crate::connect;
use crate::{
io::{self, prompt},
settings::Settings,
Command, GraphQL, RunResult, WalletFile,
};

/// Run the interactive UX loop with a loaded wallet
pub(crate) fn run_loop<'a>(
wallet: Wallet<WalletFile>,
settings: &'a Settings,
wallet_path: &'a WalletPath,
) -> Pin<Box<dyn Future<Output = anyhow::Result<()>> + 'a>> {
Box::pin(async move {
let mut wallet = wallet;
// by default we assume we're working with default address
let mut id = 0;
pub(crate) async fn run_loop(
wallet: &mut Wallet<WalletFile>,
settings: &Settings,
) -> anyhow::Result<()> {
loop {
// let the user choose (or create) a profile
let profile_index = profile_idx(wallet, settings).await?;

loop {
// let the user choose (or create) a profile
match profile_idx(&mut wallet, settings).await {
Ok(idx) => {
id = idx;
}
Err(err) => {
if let Some(InquireError::OperationCanceled) =
err.downcast_ref::<InquireError>()
{
let file_version = wallet.get_file_version();

wallet.close();
drop(wallet);

let re_load =
load_wallet(wallet_path, settings, file_version)
.await?;

wallet = connect(re_load, settings, |_| {}).await?;
} else {
return Err(err);
}
}
};
let profile = &wallet.profiles()[profile_index as usize];
prompt::hide_cursor()?;

'inner: loop {
let profile = &wallet.profiles()[id as usize];
prompt::hide_cursor()?;
let op = if !wallet.is_online().await {
println!("\r{}", profile.shielded_address_string());
println!("{}", profile.public_account_string());
println!();

let op = if !wallet.is_online().await {
println!("\r{}", profile.shielded_address_string());
println!("{}", profile.public_account_string());
println!();

command_menu::offline(id, settings)
} else {
// get balance for this profile
let moonlight_bal =
wallet.get_moonlight_balance(id).await?;
let phoenix_bal = wallet.get_phoenix_balance(id).await?;
let phoenix_spendable = phoenix_bal.spendable.into();
let phoenix_total: Dusk = phoenix_bal.value.into();

// display profile information
// display shielded balance and keys information
println!("{}", profile.shielded_address_string());
let is_synced = wallet.is_synced().await?;
if is_synced {
println!(
"{0: <16} - Spendable: {phoenix_spendable}",
"Shielded Balance",
);
println!("{0: <16} - Total: {phoenix_total}", "",);
} else {
println!("Syncing...");
}
println!();
// display public balance and keys information
println!("{}", profile.public_account_string());
command_menu::offline(profile_index, settings)
} else {
// get balance for this profile
let moonlight_bal =
wallet.get_moonlight_balance(profile_index).await?;
let phoenix_bal =
wallet.get_phoenix_balance(profile_index).await?;
let phoenix_spendable = phoenix_bal.spendable.into();
let phoenix_total: Dusk = phoenix_bal.value.into();

// display profile information
// display shielded balance and keys information
println!("{}", profile.shielded_address_string());
let is_synced = wallet.is_synced().await?;
if is_synced {
println!(
"{0: <16} - Total: {moonlight_bal}",
"Public Balance",
"{0: <16} - Spendable: {phoenix_spendable}",
"Shielded Balance",
);
println!();

command_menu::online(
id,
&wallet,
phoenix_spendable,
moonlight_bal,
settings,
)
};

prompt::hide_cursor()?;

// perform operations with this profile
match op {
Ok(ProfileOp::Run(cmd)) => {
// request confirmation before running
if confirm(&cmd, &wallet)? {
// run command
prompt::hide_cursor()?;
let result = cmd.run(&mut wallet, settings).await;
prompt::show_cursor()?;
// output results
match result {
Ok(res) => {
println!("\r{}", res);
if let RunResult::Tx(hash) = res {
let tx_id =
hex::encode(hash.to_bytes());

// Wait for transaction confirmation
// from
// network
let gql = GraphQL::new(
settings.state.to_string(),
io::status::interactive,
);
gql.wait_for(&tx_id).await?;

if let Some(explorer) =
&settings.explorer
{
let url =
format!("{explorer}{tx_id}");
println!("> URL: {url}");
prompt::launch_explorer(url)?;
}
println!("{0: <16} - Total: {phoenix_total}", "",);
} else {
println!("Syncing...");
}
println!();
// display public balance and keys information
println!("{}", profile.public_account_string());
println!(
"{0: <16} - Total: {moonlight_bal}",
"Public Balance",
);
println!();

command_menu::online(
profile_index,
wallet,
phoenix_spendable,
moonlight_bal,
settings,
)
.await
};

prompt::hide_cursor()?;

// perform operations with this profile
match op {
Ok(ProfileOp::Run(cmd)) => {
// request confirmation before running
if confirm(&cmd, wallet)? {
// run command
prompt::hide_cursor()?;
let result = cmd.run(wallet, settings).await;
prompt::show_cursor()?;
// output results
match result {
Ok(res) => {
println!("\r{}", res);
if let RunResult::Tx(hash) = res {
let tx_id = hex::encode(hash.to_bytes());

// Wait for transaction confirmation
// from network
let gql = GraphQL::new(
settings.state.to_string(),
io::status::interactive,
);
gql.wait_for(&tx_id).await?;

if let Some(explorer) = &settings.explorer {
let url = format!("{explorer}{tx_id}");
println!("> URL: {url}");
prompt::launch_explorer(url)?;
}
}

Err(err) => println!("{err}"),
}

Err(err) => return Err(err),
}
}
Ok(ProfileOp::Stay) => (),
Ok(ProfileOp::Back) => {
break 'inner;
}
Err(e) => match e.downcast_ref::<InquireError>() {
Some(InquireError::OperationCanceled) => (),
_ => return Err(e),
},
};
}
}
Ok(ProfileOp::Stay) => (),
Ok(ProfileOp::Back) => {
break;
}
Err(e) => match e.downcast_ref::<InquireError>() {
Some(InquireError::OperationCanceled) => (),
_ => return Err(e),
},
};
}
})
}
}

#[derive(PartialEq, Eq, Debug, Clone)]
Expand Down Expand Up @@ -219,7 +184,7 @@ async fn profile_idx(
}
}

/// Allows the user to choose an profile from the selected wallet
/// Allows the user to choose a profile from the selected wallet
/// to start performing operations.
fn menu_profile(wallet: &Wallet<WalletFile>) -> anyhow::Result<ProfileSelect> {
let mut menu_items = Vec::new();
Expand Down
Loading

0 comments on commit 85b6499

Please sign in to comment.