Skip to content

Commit

Permalink
Rename wallet data (#1934)
Browse files Browse the repository at this point in the history
* rename wallet data

* remove awaits and update signatures

* make it compile

* rename more wallet data mentions

* unmut wallet

* remove mut

* remove more mut

* update save load test

* clean up

* one more mut

* temporarily disable test

* review

* some nits

* revert backup rename; remove a function

* revert restore rename

* visibility; rename

* set alias

* )

Co-authored-by: Thoralf-M <[email protected]>

* doc

Co-authored-by: Thibault Martinez <[email protected]>

* doc fixes

* fix

* doc 2

Co-authored-by: Thibault Martinez <[email protected]>

---------

Co-authored-by: Thoralf-M <[email protected]>
Co-authored-by: Thibault Martinez <[email protected]>
  • Loading branch information
3 people authored Feb 20, 2024
1 parent cb65882 commit e7d1754
Show file tree
Hide file tree
Showing 51 changed files with 553 additions and 511 deletions.
33 changes: 15 additions & 18 deletions bindings/core/src/method_handler/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{method::WalletMethod, response::Response};
/// Call a wallet method.
pub(crate) async fn call_wallet_method_internal(wallet: &Wallet, method: WalletMethod) -> crate::Result<Response> {
let response = match method {
WalletMethod::Accounts => Response::OutputsData(wallet.data().await.accounts().cloned().collect()),
WalletMethod::Accounts => Response::OutputsData(wallet.ledger().await.accounts().cloned().collect()),
#[cfg(feature = "stronghold")]
WalletMethod::Backup { destination, password } => {
wallet.backup(destination, password).await?;
Expand Down Expand Up @@ -144,25 +144,22 @@ pub(crate) async fn call_wallet_method_internal(wallet: &Wallet, method: WalletM
// wallet.deregister_participation_event(&event_id).await?;
// Response::Ok
// }
WalletMethod::GetAddress => {
let address = wallet.address().await;
Response::Address(address)
}
WalletMethod::GetAddress => Response::Address(wallet.address().await),
WalletMethod::GetBalance => Response::Balance(wallet.balance().await?),
WalletMethod::GetFoundryOutput { token_id } => {
let output = wallet.get_foundry_output(token_id).await?;
Response::Output(output)
}
WalletMethod::GetIncomingTransaction { transaction_id } => wallet
.data()
.ledger()
.await
.get_incoming_transaction(&transaction_id)
.map_or_else(
|| Response::Transaction(None),
|transaction| Response::Transaction(Some(Box::new(TransactionWithMetadataDto::from(transaction)))),
),
WalletMethod::GetOutput { output_id } => {
Response::OutputData(wallet.data().await.get_output(&output_id).cloned().map(Box::new))
Response::OutputData(wallet.ledger().await.get_output(&output_id).cloned().map(Box::new))
}
// #[cfg(feature = "participation")]
// WalletMethod::GetParticipationEvent { event_id } => {
Expand Down Expand Up @@ -191,7 +188,7 @@ pub(crate) async fn call_wallet_method_internal(wallet: &Wallet, method: WalletM
// }
WalletMethod::GetTransaction { transaction_id } => Response::Transaction(
wallet
.data()
.ledger()
.await
.get_transaction(&transaction_id)
.map(TransactionWithMetadataDto::from)
Expand Down Expand Up @@ -227,28 +224,28 @@ pub(crate) async fn call_wallet_method_internal(wallet: &Wallet, method: WalletM
Response::PreparedTransaction(data)
}
WalletMethod::ImplicitAccounts => {
Response::OutputsData(wallet.data().await.implicit_accounts().cloned().collect())
Response::OutputsData(wallet.ledger().await.implicit_accounts().cloned().collect())
}
WalletMethod::IncomingTransactions => Response::Transactions(
wallet
.data()
.ledger()
.await
.incoming_transactions()
.values()
.map(TransactionWithMetadataDto::from)
.collect(),
),
WalletMethod::Outputs { filter_options } => {
let wallet_data = wallet.data().await;
let wallet_ledger = wallet.ledger().await;
Response::OutputsData(if let Some(filter) = filter_options {
wallet_data.filtered_outputs(filter).cloned().collect()
wallet_ledger.filtered_outputs(filter).cloned().collect()
} else {
wallet_data.outputs().values().cloned().collect()
wallet_ledger.outputs().values().cloned().collect()
})
}
WalletMethod::PendingTransactions => Response::Transactions(
wallet
.data()
.ledger()
.await
.pending_transactions()
.map(TransactionWithMetadataDto::from)
Expand Down Expand Up @@ -444,19 +441,19 @@ pub(crate) async fn call_wallet_method_internal(wallet: &Wallet, method: WalletM
WalletMethod::Sync { options } => Response::Balance(wallet.sync(options).await?),
WalletMethod::Transactions => Response::Transactions(
wallet
.data()
.ledger()
.await
.transactions()
.values()
.map(TransactionWithMetadataDto::from)
.collect(),
),
WalletMethod::UnspentOutputs { filter_options } => {
let wallet_data = wallet.data().await;
let wallet_ledger = wallet.ledger().await;
Response::OutputsData(if let Some(filter) = filter_options {
wallet_data.filtered_unspent_outputs(filter).cloned().collect()
wallet_ledger.filtered_unspent_outputs(filter).cloned().collect()
} else {
wallet_data.unspent_outputs().values().cloned().collect()
wallet_ledger.unspent_outputs().values().cloned().collect()
})
}
};
Expand Down
2 changes: 1 addition & 1 deletion cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ pub async fn restore_command_stronghold(
if let Err(e) = wallet.restore_backup(backup_path.into(), password, None, None).await {
// Clean up the file system after a failed restore (typically produces a wallet without a secret manager).
// TODO: a better way would be to not create any files/dirs in the first place when it's not clear yet whether
// the restore will be successful.
// the restore will be successful. https://github.com/iotaledger/iota-sdk/issues/2018
if storage_path.is_dir() && !restore_into_existing_wallet {
std::fs::remove_dir_all(storage_path)?;
}
Expand Down
42 changes: 21 additions & 21 deletions cli/src/wallet_cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,8 @@ impl FromStr for OutputSelector {

// `accounts` command
pub async fn accounts_command(wallet: &Wallet) -> Result<(), Error> {
let wallet_data = wallet.data().await;
let accounts = wallet_data.accounts();
let wallet_ledger = wallet.ledger().await;
let accounts = wallet_ledger.accounts();
let hrp = wallet.client().get_bech32_hrp().await?;

println_log_info!("Accounts:\n");
Expand Down Expand Up @@ -430,9 +430,9 @@ pub async fn address_command(wallet: &Wallet) -> Result<(), Error> {
// `allot-mana` command
pub async fn allot_mana_command(wallet: &Wallet, mana: u64, account_id: Option<AccountId>) -> Result<(), Error> {
let account_id = {
let wallet_data = wallet.data().await;
let wallet_ledger = wallet.ledger().await;
account_id
.or_else(|| wallet_data.first_account_id())
.or_else(|| wallet_ledger.first_account_id())
.ok_or(WalletError::AccountNotFound)?
};

Expand Down Expand Up @@ -565,9 +565,9 @@ pub async fn claim_command(wallet: &Wallet, output_id: Option<OutputId>) -> Resu
/// `claimable-outputs` command
pub async fn claimable_outputs_command(wallet: &Wallet) -> Result<(), Error> {
for output_id in wallet.claimable_outputs(OutputsToClaim::All).await? {
let wallet_data = wallet.data().await;
let wallet_ledger = wallet.ledger().await;
// Unwrap: for the iterated `OutputId`s this call will always return `Some(...)`.
let output = &wallet_data.get_output(&output_id).unwrap().output;
let output = &wallet_ledger.get_output(&output_id).unwrap().output;
let kind = match output {
Output::Nft(_) => "Nft",
Output::Basic(_) => "Basic",
Expand Down Expand Up @@ -613,9 +613,9 @@ pub async fn congestion_command(
work_score: Option<u32>,
) -> Result<(), Error> {
let account_id = {
let wallet_data = wallet.data().await;
let wallet_ledger = wallet.ledger().await;
account_id
.or_else(|| wallet_data.first_account_id())
.or_else(|| wallet_ledger.first_account_id())
.ok_or(WalletError::AccountNotFound)?
};

Expand Down Expand Up @@ -868,8 +868,8 @@ pub async fn implicit_account_transition_command(wallet: &Wallet, output_id: Out

// `implicit-accounts` command
pub async fn implicit_accounts_command(wallet: &Wallet) -> Result<(), Error> {
let wallet_data = wallet.data().await;
let implicit_accounts = wallet_data.implicit_accounts();
let wallet_ledger = wallet.ledger().await;
let implicit_accounts = wallet_ledger.implicit_accounts();
let hrp = wallet.client().get_bech32_hrp().await?;

println_log_info!("Implicit accounts:\n");
Expand Down Expand Up @@ -974,11 +974,11 @@ pub async fn node_info_command(wallet: &Wallet) -> Result<(), Error> {

/// `output` command
pub async fn output_command(wallet: &Wallet, selector: OutputSelector, metadata: bool) -> Result<(), Error> {
let wallet_data = wallet.data().await;
let wallet_ledger = wallet.ledger().await;
let output = match selector {
OutputSelector::Id(id) => wallet_data.get_output(&id),
OutputSelector::Id(id) => wallet_ledger.get_output(&id),
OutputSelector::Index(index) => {
let mut outputs = wallet_data.outputs().values().collect::<Vec<_>>();
let mut outputs = wallet_ledger.outputs().values().collect::<Vec<_>>();
outputs.sort_unstable_by_key(|o| o.output_id);
outputs.into_iter().nth(index)
}
Expand All @@ -999,7 +999,7 @@ pub async fn output_command(wallet: &Wallet, selector: OutputSelector, metadata:

/// `outputs` command
pub async fn outputs_command(wallet: &Wallet) -> Result<(), Error> {
print_outputs(wallet.data().await.outputs().values().cloned().collect(), "Outputs:")
print_outputs(wallet.ledger().await.outputs().values().cloned().collect(), "Outputs:")
}

// `send` command
Expand Down Expand Up @@ -1104,11 +1104,11 @@ pub async fn sync_command(wallet: &Wallet) -> Result<(), Error> {

/// `transaction` command
pub async fn transaction_command(wallet: &Wallet, selector: TransactionSelector) -> Result<(), Error> {
let wallet_data = wallet.data().await;
let wallet_ledger = wallet.ledger().await;
let transaction = match selector {
TransactionSelector::Id(id) => wallet_data.get_transaction(&id),
TransactionSelector::Id(id) => wallet_ledger.get_transaction(&id),
TransactionSelector::Index(index) => {
let mut transactions = wallet_data.transactions().values().collect::<Vec<_>>();
let mut transactions = wallet_ledger.transactions().values().collect::<Vec<_>>();
transactions.sort_unstable_by(|a, b| b.timestamp.cmp(&a.timestamp));
transactions.into_iter().nth(index)
}
Expand All @@ -1125,8 +1125,8 @@ pub async fn transaction_command(wallet: &Wallet, selector: TransactionSelector)

/// `transactions` command
pub async fn transactions_command(wallet: &Wallet, show_details: bool) -> Result<(), Error> {
let wallet_data = wallet.data().await;
let mut transactions = wallet_data.transactions().values().collect::<Vec<_>>();
let wallet_ledger = wallet.ledger().await;
let mut transactions = wallet_ledger.transactions().values().collect::<Vec<_>>();
transactions.sort_unstable_by(|a, b| b.timestamp.cmp(&a.timestamp));

if transactions.is_empty() {
Expand All @@ -1150,7 +1150,7 @@ pub async fn transactions_command(wallet: &Wallet, show_details: bool) -> Result
/// `unspent-outputs` command
pub async fn unspent_outputs_command(wallet: &Wallet) -> Result<(), Error> {
print_outputs(
wallet.data().await.unspent_outputs().values().cloned().collect(),
wallet.ledger().await.unspent_outputs().values().cloned().collect(),
"Unspent outputs:",
)
}
Expand Down Expand Up @@ -1253,7 +1253,7 @@ async fn print_wallet_address(wallet: &Wallet) -> Result<(), Error> {
let mut delegations = Vec::new();
let mut anchors = Vec::new();

for output_data in wallet.data().await.unspent_outputs().values() {
for output_data in wallet.ledger().await.unspent_outputs().values() {
let output_id = output_data.output_id;
output_ids.push(output_id);

Expand Down
4 changes: 2 additions & 2 deletions sdk/examples/how_tos/wallet/consolidate_outputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async fn main() -> Result<()> {
// output.
println!("Outputs BEFORE consolidation:");
wallet
.data()
.ledger()
.await
.unspent_outputs()
.values()
Expand Down Expand Up @@ -89,7 +89,7 @@ async fn main() -> Result<()> {
// Outputs after consolidation
println!("Outputs AFTER consolidation:");
wallet
.data()
.ledger()
.await
.unspent_outputs()
.values()
Expand Down
2 changes: 1 addition & 1 deletion sdk/examples/how_tos/wallet/create_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async fn main() -> Result<()> {
let client_options = ClientOptions::new().with_node(&std::env::var("NODE_URL").unwrap())?;

// Create the wallet
let wallet = Wallet::builder()
Wallet::builder()
.with_secret_manager(SecretManager::Stronghold(secret_manager))
.with_storage_path(&std::env::var("WALLET_DB_PATH").unwrap())
.with_client_options(client_options)
Expand Down
4 changes: 2 additions & 2 deletions sdk/examples/how_tos/wallet/list_outputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ async fn main() -> Result<()> {

// Print output ids
println!("Output ids:");
for output in wallet.data().await.outputs().values() {
for output in wallet.ledger().await.outputs().values() {
println!("{}", output.output_id);
}

// Print unspent output ids
println!("Unspent output ids:");
for output in wallet.data().await.unspent_outputs().values() {
for output in wallet.ledger().await.unspent_outputs().values() {
println!("{}", output.output_id);
}

Expand Down
4 changes: 2 additions & 2 deletions sdk/examples/how_tos/wallet/list_transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ async fn main() -> Result<()> {

// Print transaction ids
println!("Sent transactions:");
for transaction_id in wallet.data().await.transactions().keys() {
for transaction_id in wallet.ledger().await.transactions().keys() {
println!("{}", transaction_id);
}

// Print received transaction ids
println!("Received transactions:");
for transaction_id in wallet.data().await.incoming_transactions().keys() {
for transaction_id in wallet.ledger().await.incoming_transactions().keys() {
println!("{}", transaction_id);
}

Expand Down
2 changes: 1 addition & 1 deletion sdk/examples/wallet/spammer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ async fn main() -> Result<()> {
// We make sure that for all threads there are always inputs available to
// fund the transaction, otherwise we create enough unspent outputs.
let num_unspent_basic_outputs_with_send_amount = wallet
.data()
.ledger()
.await
.filtered_unspent_outputs(FilterOptions {
output_types: Some(vec![BasicOutput::KIND]),
Expand Down
1 change: 0 additions & 1 deletion sdk/examples/wallet/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ async fn main() -> Result<()> {
}

async fn sync_print_balance(wallet: &Wallet) -> Result<()> {
let alias = wallet.alias().await;
let now = tokio::time::Instant::now();
let balance = wallet.sync(None).await?;
println!("Wallet synced in: {:.2?}", now.elapsed());
Expand Down
5 changes: 0 additions & 5 deletions sdk/examples/wallet/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,6 @@ async fn create_wallet() -> Result<Wallet> {
.await
}

async fn print_address(wallet: &Wallet) -> Result<()> {
println!("Wallet address: {}", wallet.address().await);
Ok(())
}

async fn sync_print_balance(wallet: &Wallet, full_report: bool) -> Result<()> {
let now = tokio::time::Instant::now();
let balance = wallet.sync(None).await?;
Expand Down
Loading

0 comments on commit e7d1754

Please sign in to comment.