Skip to content

Commit

Permalink
Add get_or_create_account (#971)
Browse files Browse the repository at this point in the history
* Add `get_or_create_account`

* clippy

* changelog
  • Loading branch information
Alexandcoats authored Aug 3, 2023
1 parent 35c8178 commit 862cdfd
Show file tree
Hide file tree
Showing 15 changed files with 33 additions and 88 deletions.
1 change: 1 addition & 0 deletions sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- `Wallet::get_or_create_account` convenience method;
- `Output::kind_str()` method;

### Changed
Expand Down
15 changes: 3 additions & 12 deletions sdk/examples/wallet/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use iota_sdk::{
secret::{mnemonic::MnemonicSecretManager, SecretManager},
utils::request_funds_from_faucet,
},
wallet::{Account, ClientOptions, Result, Wallet},
wallet::{ClientOptions, Result, Wallet},
};

// The number of addresses to generate
Expand All @@ -41,11 +41,11 @@ async fn main() -> Result<()> {
.await?;

// Get or create first account
let _ = get_or_create_account(&wallet, "Alice").await?;
let _ = wallet.get_or_create_account("Alice").await?;

// Get or create second account
let alias2 = "Bob";
let account2 = get_or_create_account(&wallet, alias2).await?;
let account2 = wallet.get_or_create_account("Alice").await?;

let accounts = wallet.get_accounts().await?;
println!("WALLET ACCOUNTS:");
Expand Down Expand Up @@ -94,12 +94,3 @@ async fn main() -> Result<()> {

Ok(())
}

async fn get_or_create_account(wallet: &Wallet, alias: &str) -> Result<Account> {
Ok(if let Ok(account) = wallet.get_account(alias).await {
account
} else {
println!("Creating account '{alias}'");
wallet.create_account().with_alias(alias).finish().await?
})
}
8 changes: 1 addition & 7 deletions sdk/examples/wallet/background_syncing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,7 @@ async fn main() -> Result<()> {
.await?;

// Get or create new account
let alias = "Alice";
let account = if let Ok(account) = wallet.get_account(alias).await {
account
} else {
println!("Creating account '{alias}'");
wallet.create_account().with_alias(alias).finish().await?
};
let account = wallet.get_or_create_account("Alice").await?;
let addresses = account.addresses().await?;

// Manually sync to ensure we have the correct funds to start with
Expand Down
8 changes: 1 addition & 7 deletions sdk/examples/wallet/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,7 @@ async fn main() -> Result<()> {
.await;

// Get or create an account
let alias = "Alice";
let account = if let Ok(account) = wallet.get_account(alias).await {
account
} else {
println!("Creating account '{alias}'");
wallet.create_account().with_alias(alias).finish().await?
};
let account = wallet.get_or_create_account("Alice").await?;

let balance = account.sync(None).await?;
println!("Balance BEFORE:\n{:#?}", balance.base_coin());
Expand Down
7 changes: 1 addition & 6 deletions sdk/examples/wallet/ledger_nano.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,7 @@ async fn main() -> Result<()> {
println!("{:?}", wallet.get_ledger_nano_status().await?);

// Get or create a new account
let account = if let Ok(account) = wallet.get_account(ACCOUNT_ALIAS).await {
account
} else {
println!("Creating account '{ACCOUNT_ALIAS}'");
wallet.create_account().with_alias(ACCOUNT_ALIAS).finish().await?
};
let account = wallet.get_or_create_account(ACCOUNT_ALIAS).await?;

println!("Generating {NUM_ADDRESSES_TO_GENERATE} addresses...");
let now = tokio::time::Instant::now();
Expand Down
8 changes: 1 addition & 7 deletions sdk/examples/wallet/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,7 @@ async fn main() -> Result<()> {
.await?;

// Get or create a new account
let alias = "Alice";
let account = if let Ok(account) = wallet.get_account(alias).await {
account
} else {
println!("Creating account '{alias}'");
wallet.create_account().with_alias(alias).finish().await?
};
let account = wallet.get_or_create_account("Alice").await?;

println!("Generating {NUM_ADDRESSES_TO_GENERATE} addresses...");
let _ = account
Expand Down
11 changes: 1 addition & 10 deletions sdk/examples/wallet/spammer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async fn main() -> Result<()> {
.with_coin_type(SHIMMER_COIN_TYPE)
.finish()
.await?;
let account = get_or_create_account(&wallet, ACCOUNT_ALIAS).await?;
let account = wallet.get_or_create_account(ACCOUNT_ALIAS).await?;

let recv_address = *account.addresses().await?[0].address();
println!("Recv address: {}", recv_address);
Expand Down Expand Up @@ -141,15 +141,6 @@ async fn main() -> Result<()> {
Ok(())
}

async fn get_or_create_account(wallet: &Wallet, alias: &str) -> Result<Account> {
Ok(if let Ok(account) = wallet.get_account(alias).await {
account
} else {
println!("Creating account '{alias}'");
wallet.create_account().with_alias(alias).finish().await?
})
}

async fn ensure_enough_funds(account: &Account, bech32_address: &Bech32Address) -> Result<()> {
let balance = account.sync(None).await?;
let available_funds = balance.base_coin().available();
Expand Down
11 changes: 1 addition & 10 deletions sdk/examples/wallet/split_funds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async fn main() -> Result<()> {
.await?;

// Get account or create a new one
let account = create_account(&wallet, "Alice").await?;
let account = wallet.get_or_create_account("Alice").await?;

let _ = ensure_enough_addresses(&account, ADDRESSES_TO_SPLIT_FUNDS).await?;

Expand Down Expand Up @@ -109,15 +109,6 @@ async fn main() -> Result<()> {
Ok(())
}

async fn create_account(wallet: &Wallet, alias: &str) -> Result<Account> {
Ok(if let Ok(account) = wallet.get_account(alias).await {
account
} else {
println!("Creating account '{alias}'");
wallet.create_account().with_alias(alias).finish().await?
})
}

async fn sync_print_balance(account: &Account) -> Result<()> {
let alias = account.alias().await;
let now = tokio::time::Instant::now();
Expand Down
11 changes: 1 addition & 10 deletions sdk/examples/wallet/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async fn main() -> Result<()> {
.await?;

// Get account or create a new one
let account = get_or_create_account(&wallet, "Alice").await?;
let account = wallet.get_or_create_account("Alice").await?;

let addresses = generate_max_addresses(&account, MAX_ADDRESSES_TO_GENERATE).await?;
let bech32_addresses = addresses
Expand All @@ -57,15 +57,6 @@ async fn main() -> Result<()> {
Ok(())
}

async fn get_or_create_account(wallet: &Wallet, alias: &str) -> Result<Account> {
Ok(if let Ok(account) = wallet.get_account(alias).await {
account
} else {
println!("Creating account '{alias}'");
wallet.create_account().with_alias(alias).finish().await?
})
}

async fn generate_max_addresses(account: &Account, max: usize) -> Result<Vec<AccountAddress>> {
let alias = account.alias().await;
if account.addresses().await?.len() < max {
Expand Down
12 changes: 1 addition & 11 deletions sdk/examples/wallet/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async fn main() -> Result<()> {

let wallet = create_wallet().await?;

let account = get_or_create_account(&wallet, "Alice").await?;
let account = wallet.get_or_create_account("Alice").await?;
print_accounts(&wallet).await?;

generate_addresses(&account, MAX_ADDRESSES_TO_GENERATE).await?;
Expand Down Expand Up @@ -72,16 +72,6 @@ async fn create_wallet() -> Result<Wallet> {
.await
}

async fn get_or_create_account(wallet: &Wallet, alias: &str) -> Result<Account> {
let account = if let Ok(account) = wallet.get_account(alias).await {
account
} else {
println!("Creating account '{alias}'");
wallet.create_account().with_alias(alias).finish().await?
};
Ok(account)
}

async fn print_accounts(wallet: &Wallet) -> Result<()> {
let accounts = wallet.get_accounts().await?;
println!("Accounts:");
Expand Down
4 changes: 2 additions & 2 deletions sdk/src/types/block/output/feature/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ pub enum Feature {

impl PartialOrd for Feature {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
self.kind().partial_cmp(&other.kind())
Some(self.cmp(other))
}
}
impl Ord for Feature {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
self.partial_cmp(other).unwrap()
self.kind().cmp(&other.kind())
}
}

Expand Down
4 changes: 2 additions & 2 deletions sdk/src/types/block/output/native_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ impl NativeToken {

impl PartialOrd for NativeToken {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
self.token_id.partial_cmp(&other.token_id)
Some(self.cmp(other))
}
}
impl Ord for NativeToken {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
self.partial_cmp(other).unwrap()
self.token_id.cmp(&other.token_id)
}
}

Expand Down
4 changes: 2 additions & 2 deletions sdk/src/types/block/output/unlock_condition/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ pub enum UnlockCondition {

impl PartialOrd for UnlockCondition {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
self.kind().partial_cmp(&other.kind())
Some(self.cmp(other))
}
}
impl Ord for UnlockCondition {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
self.partial_cmp(other).unwrap()
self.kind().cmp(&other.kind())
}
}

Expand Down
4 changes: 2 additions & 2 deletions sdk/src/types/block/payload/milestone/option/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ impl MilestoneOption {

impl PartialOrd for MilestoneOption {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
self.kind().partial_cmp(&other.kind())
Some(self.cmp(other))
}
}
impl Ord for MilestoneOption {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
self.partial_cmp(other).unwrap()
self.kind().cmp(&other.kind())
}
}

Expand Down
13 changes: 13 additions & 0 deletions sdk/src/wallet/core/operations/get_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,16 @@ impl<S: SecretManage> Wallet<S> {
)?))
}
}

impl<S: 'static + SecretManage> Wallet<S>
where
crate::wallet::Error: From<S::Error>,
{
pub async fn get_or_create_account(&self, alias: impl Into<String> + Send) -> crate::wallet::Result<Account<S>> {
let alias = alias.into();
match self.get_account(&alias).await {
Err(crate::wallet::Error::AccountNotFound(_)) => self.create_account().with_alias(alias).finish().await,
res => res,
}
}
}

0 comments on commit 862cdfd

Please sign in to comment.