Newer
Older
/// define account subcommands
#[derive(Clone, Default, Debug, clap::Parser)]
pub enum Subcommand {
/// Fetch account balance
#[default]
Balance,
/// Transfer some currency to an account
Transfer {
/// Amount to transfer
amount: u64,
/// Destination address
dest: AccountId,
/// Prevent from going below account existential deposit
#[clap(short = 'k', long = "keep-alive")]
keep_alive: bool,
},
/// Transfer the same amount for each space-separated address.
/// If an address appears mutiple times, it will get multiple times the same amount
TransferMultiple {
/// Amount given to each destination address
amount: u64,
/// List of target addresses
dests: Vec<AccountId>,
},
}
/// handle account commands
pub async fn handle_command(data: Data, command: Subcommand) -> anyhow::Result<()> {
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
match command {
Subcommand::Balance => {
data = data
.build_address()
.fetch_system_properties()
.await?;
commands::account::get_balance(data).await?
}
Subcommand::Transfer {
amount,
dest,
keep_alive,
} => {
data = data.build_client().await?;
commands::transfer::transfer(
&data,
amount,
dest,
keep_alive,
)
.await?;
}
Subcommand::TransferMultiple { amount, dests } => {
data = data.build_client().await?;
commands::transfer::transfer_multiple(&data,
amount,
dests,
)
.await?;
}
};
Ok(())
}
/// get balance
pub async fn get_balance(data: Data) -> Result<(), anyhow::Error> {
let account_id = data.address();
let account_info = get_account_info(data.client(), &account_id).await?;
if let Some(account_info) = account_info {
println!(
"{account_id} has {}",
data.format_balance(account_info.data.free)
);
} else {
println!("account {account_id} does not exist")
}
Ok(())
}
) -> Result<Option<AccountInfo>, subxt::Error> {
client
.storage()
.fetch(&runtime::storage().system().account(account_id), None)