Skip to content

Commit

Permalink
Add commands to list outputs (#114)
Browse files Browse the repository at this point in the history
* Add commands to list outputs

* Order!

* Order!

* 3rd person

* Clippy

Co-authored-by: Thibault Martinez <[email protected]>
  • Loading branch information
Thoralf-M and thibault-martinez authored Jun 29, 2022
1 parent f587edb commit e44051e
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 30 deletions.
42 changes: 40 additions & 2 deletions documentation/docs/03_account.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ Displays the account interface usage.

### `melt-native-token`

Melt a native token.
Melts a native token.

#### Parameters

Expand Down Expand Up @@ -212,6 +212,8 @@ Mint a native token with a maximum supply and foundry metadata.

### `mint-nft`

Mints an NFT.

#### Parameters

| Name | Optional | Default | Example |
Expand Down Expand Up @@ -247,6 +249,32 @@ Generates a new address.
> Account "main": new-address
```

### `output`

Displays an output that is stored in the account.

#### Parameters

| Name | Optional | Default | Example |
| ----------- | --------- | ------- | ------------------------------------------------------------------------ |
| `output_id` || N/A | "0x1c7a765db0c1f5eceb0ea5578585359c5b0c1ab8d958829f5990997b93f0ec7d0100" |

#### Example

```sh
> Account "main": output 0x1c7a765db0c1f5eceb0ea5578585359c5b0c1ab8d958829f5990997b93f0ec7d0100
```

### `outputs`

Displays all outputs that are stored in the account.

#### Example

```sh
> Account "main": outputs
```

### `send`

Sends an amount to an address.
Expand Down Expand Up @@ -328,10 +356,20 @@ Synchronises the account.

### `transactions`

List all account transactions.
Lists all account transactions.

#### Example

```sh
> Account "main": transactions
```

### `unspent-outputs`

Displays all unspent outputs that are stored in the account.

#### Example

```sh
> Account "main": unspent-outputs
```
8 changes: 6 additions & 2 deletions src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ use crate::{
command::account::{
addresses_command, balance_command, burn_native_token_command, burn_nft_command, claim_command,
consolidate_command, destroy_alias_command, destroy_foundry_command, faucet_command, melt_native_token_command,
mint_native_token_command, mint_nft_command, new_address_command, send_command, send_micro_command,
send_native_token_command, send_nft_command, sync_command, transactions_command, AccountCli, AccountCommand,
mint_native_token_command, mint_nft_command, new_address_command, output_command, outputs_command,
send_command, send_micro_command, send_native_token_command, send_nft_command, sync_command,
transactions_command, unspent_outputs_command, AccountCli, AccountCommand,
},
error::Error,
};
Expand Down Expand Up @@ -84,6 +85,8 @@ pub async fn account_prompt_internal(account_handle: AccountHandle) -> Result<bo
metadata,
} => mint_nft_command(&account_handle, address, immutable_metadata, metadata).await,
AccountCommand::NewAddress => new_address_command(&account_handle).await,
AccountCommand::Output { output_id } => output_command(&account_handle, output_id).await,
AccountCommand::Outputs => outputs_command(&account_handle).await,
AccountCommand::Send { address, amount } => send_command(&account_handle, address, amount).await,
AccountCommand::SendMicro { address, amount } => {
send_micro_command(&account_handle, address, amount).await
Expand All @@ -96,6 +99,7 @@ pub async fn account_prompt_internal(account_handle: AccountHandle) -> Result<bo
AccountCommand::SendNft { address, nft_id } => send_nft_command(&account_handle, address, nft_id).await,
AccountCommand::Sync => sync_command(&account_handle).await,
AccountCommand::Transactions => transactions_command(&account_handle).await,
AccountCommand::UnspentOutputs => unspent_outputs_command(&account_handle).await,
} {
log::error!("{}", err);
}
Expand Down
7 changes: 4 additions & 3 deletions src/account_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ use crate::{
};

pub async fn new_account_manager(cli: AccountManagerCli) -> Result<(AccountManager, Option<String>), Error> {
let storage_path = var_os("WALLET_DATABASE_PATH")
.map(|os_str| os_str.into_string().expect("invalid WALLET_DATABASE_PATH"))
.unwrap_or_else(|| "./stardust-cli-wallet-db".to_string());
let storage_path = var_os("WALLET_DATABASE_PATH").map_or_else(
|| "./stardust-cli-wallet-db".to_string(),
|os_str| os_str.into_string().expect("invalid WALLET_DATABASE_PATH"),
);
let stronghold_path = std::path::Path::new("./stardust-cli-wallet.stronghold");

let password = get_password(stronghold_path)?;
Expand Down
75 changes: 52 additions & 23 deletions src/command/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ use std::str::FromStr;

use clap::{Parser, Subcommand};
use iota_wallet::{
account::{
types::{AccountAddress, Transaction},
AccountHandle, OutputsToClaim,
},
account::{types::AccountAddress, AccountHandle, OutputsToClaim},
iota_client::{
bee_block::output::{AliasId, FoundryId, NftId, TokenId},
bee_block::output::{AliasId, FoundryId, NftId, OutputId, TokenId},
request_funds_from_faucet,
},
AddressAndNftId, AddressNativeTokens, AddressWithAmount, AddressWithMicroAmount, NativeTokenOptions, NftOptions,
Expand Down Expand Up @@ -68,6 +65,10 @@ pub enum AccountCommand {
},
/// Generate a new address.
NewAddress,
/// Display an output.
Output { output_id: String },
/// List all outputs.
Outputs,
/// Send an amount to a bech32 encoded address: `send
/// rms1qztwng6cty8cfm42nzvq099ev7udhrnk0rw8jt8vttf9kpqnxhpsx869vr3 1000000`
Send { address: String, amount: u64 },
Expand All @@ -88,6 +89,8 @@ pub enum AccountCommand {
Sync,
/// List the account transactions.
Transactions,
/// List the unspent outputs.
UnspentOutputs,
}

/// `addresses` command
Expand Down Expand Up @@ -298,6 +301,33 @@ pub async fn new_address_command(account_handle: &AccountHandle) -> Result<(), E
Ok(())
}

/// `output` command
pub async fn output_command(account_handle: &AccountHandle, output_id: String) -> Result<(), Error> {
let output = account_handle.get_output(&OutputId::from_str(&output_id)?).await;

if let Some(output) = output {
log::info!("{output:#?}");
} else {
log::info!("Output not found");
}

Ok(())
}

/// `outputs` command
pub async fn outputs_command(account_handle: &AccountHandle) -> Result<(), Error> {
let outputs = account_handle.list_outputs().await?;

if outputs.is_empty() {
log::info!("No outputs found");
} else {
let output_ids: Vec<OutputId> = outputs.iter().map(|o| o.output_id).collect();
log::info!("Outputs: {output_ids:#?}");
}

Ok(())
}

// `send` command
pub async fn send_command(account_handle: &AccountHandle, address: String, amount: u64) -> Result<(), Error> {
let outputs = vec![AddressWithAmount { address, amount }];
Expand Down Expand Up @@ -375,7 +405,23 @@ pub async fn transactions_command(account_handle: &AccountHandle) -> Result<(),
if transactions.is_empty() {
log::info!("No transactions found");
} else {
transactions.iter().for_each(print_transaction);
// Format to not take too much space with pretty printing but still have a clear separation between transactions
let txs = transactions.iter().map(|tx| format!("{tx:?}")).collect::<Vec<String>>();
log::info!("{txs:#?}");
}

Ok(())
}

/// `unspent-outputs` command
pub async fn unspent_outputs_command(account_handle: &AccountHandle) -> Result<(), Error> {
let outputs = account_handle.list_unspent_outputs().await?;

if outputs.is_empty() {
log::info!("No outputs found");
} else {
let output_ids: Vec<OutputId> = outputs.iter().map(|o| o.output_id).collect();
log::info!("Unspent outputs: {output_ids:#?}");
}

Ok(())
Expand All @@ -390,23 +436,6 @@ pub async fn transactions_command(account_handle: &AccountHandle) -> Result<(),
// Ok(())
// }

fn print_transaction(transaction: &Transaction) {
log::info!("{transaction:?}");
// if let Some(MessagePayload::Transaction(tx)) = message.payload() {
// let TransactionEssence::Regular(essence) = tx.essence();
// println!("--- Value: {:?}", essence.value());
// }
// println!("--- Timestamp: {:?}", message.timestamp());
// println!(
// "--- Broadcasted: {}, confirmed: {}",
// message.broadcasted(),
// match message.confirmed() {
// Some(c) => c.to_string(),
// None => "unknown".to_string(),
// }
// );
}

pub async fn print_address(account_handle: &AccountHandle, address: &AccountAddress) -> Result<(), Error> {
let mut log = format!("Address {}: {}", address.key_index(), address.address().to_bech32());

Expand Down

0 comments on commit e44051e

Please sign in to comment.