diff --git a/src/commands/oneshot.rs b/src/commands/oneshot.rs index 3ee2c674b6296496f2aec1a3c25a4c3e5b5e1818..6c8b9c1b9bef214ca8d6d62aa1b6568567e3f345 100644 --- a/src/commands/oneshot.rs +++ b/src/commands/oneshot.rs @@ -1,21 +1,103 @@ use crate::*; -use sp_core::{crypto::AccountId32, sr25519::Pair}; -use subxt::tx::{BaseExtrinsicParamsBuilder, PairSigner}; +/// define oneshot account subcommands +#[derive(Clone, Default, Debug, clap::Parser)] +pub enum Subcommand { + /// get balance of oneshot account + #[default] + Balance, + /// create a oneshot account + Create { balance: u64, dest: AccountId }, + /// consume a oneshot account + Consume { + dest: AccountId, + #[clap(long = "oneshot")] + dest_oneshot: bool, + }, + /// consume a oneshot account whith remaining sent to an other account + ConsumeWithRemaining { + balance: u64, + dest: AccountId, + #[clap(long = "one")] + dest_oneshot: bool, + remaining_to: AccountId, + #[clap(long = "rem-one")] + remaining_to_oneshot: bool, + }, +} + +/// handle oneshot commands +pub async fn handle_command(data: Data, command: Subcommand) -> anyhow::Result<()> { + // build indexer because it is needed for all subcommands + let mut data = data.build_client().await?; + // match subcommand + match command { + Subcommand::Balance => oneshot_account_balance(&data).await?, + Subcommand::Create { balance, dest } => { + data = data.build_client().await?; + create_oneshot_account(&data, balance, dest).await?; + } + Subcommand::Consume { dest, dest_oneshot } => { + data = data.build_client().await?; + consume_oneshot_account(&data, dest, dest_oneshot).await?; + } + Subcommand::ConsumeWithRemaining { + balance, + dest, + dest_oneshot, + remaining_to, + remaining_to_oneshot, + } => { + data = data.build_client().await?; + consume_oneshot_account_with_remaining( + &data, + balance, + dest, + dest_oneshot, + remaining_to, + remaining_to_oneshot, + ) + .await?; + } + }; + + Ok(()) +} + +/// get balance of oneshot account +pub async fn oneshot_account_balance(data: &Data) -> Result<(), anyhow::Error> { + println!( + "balance of oneshot account {} is: {}", + data.address(), + data.client() + .storage() + .fetch( + &runtime::storage() + .oneshot_account() + .oneshot_accounts(data.address()), + None + ) + .await? + .unwrap_or(0) + ); + + Ok(()) +} +/// create oneshot account pub async fn create_oneshot_account( - pair: Pair, - client: &Client, + data: &Data, balance: u64, - dest: AccountId32, + dest: AccountId, ) -> Result<(), subxt::Error> { - let progress = client + let progress = data + .client() .tx() .sign_and_submit_then_watch( &runtime::tx() .oneshot_account() .create_oneshot_account(dest.into(), balance), - &PairSigner::new(pair), + &PairSigner::new(data.keypair()), BaseExtrinsicParamsBuilder::new(), ) .await?; @@ -29,12 +111,15 @@ pub async fn create_oneshot_account( Ok(()) } +/// consume oneshot account pub async fn consume_oneshot_account( - pair: Pair, - client: &Client, - dest: AccountId32, + data: &Data, + + dest: AccountId, dest_oneshot: bool, ) -> Result<(), subxt::Error> { + let client = data.client(); + let number = client .storage() .fetch(&runtime::storage().system().number(), None) @@ -55,7 +140,7 @@ pub async fn consume_oneshot_account( ) }, ), - &PairSigner::new(pair), + &PairSigner::new(data.keypair()), BaseExtrinsicParamsBuilder::new(), ) .await?; @@ -69,15 +154,18 @@ pub async fn consume_oneshot_account( Ok(()) } +/// consume oneshot account with remaining pub async fn consume_oneshot_account_with_remaining( - pair: Pair, - client: &Client, + data: &Data, + balance: u64, - dest: AccountId32, + dest: AccountId, dest_oneshot: bool, - remaining_to: AccountId32, + remaining_to: AccountId, remaining_to_oneshot: bool, ) -> Result<(), subxt::Error> { + let client = data.client(); + let number = client .storage() .fetch(&runtime::storage().system().number(), None) @@ -110,7 +198,7 @@ pub async fn consume_oneshot_account_with_remaining( }, balance, ), - &PairSigner::new(pair), + &PairSigner::new(data.keypair()), BaseExtrinsicParamsBuilder::new(), ) .await?; @@ -123,25 +211,3 @@ pub async fn consume_oneshot_account_with_remaining( } Ok(()) } - -pub async fn oneshot_account_balance( - client: &Client, - account: AccountId32, -) -> Result<(), anyhow::Error> { - println!( - "balance of oneshot account {} is: {}", - &account, - client - .storage() - .fetch( - &runtime::storage() - .oneshot_account() - .oneshot_accounts(&account), - None - ) - .await? - .unwrap_or(0) - ); - - Ok(()) -} diff --git a/src/commands/ud.rs b/src/commands/ud.rs index 4f6fc134929af4cba9b4cafd80de6282731fad9a..0467c3478d70a5143c7cc4c55588b34f59cb412b 100644 --- a/src/commands/ud.rs +++ b/src/commands/ud.rs @@ -1,24 +1,6 @@ use crate::*; -pub async fn claim_ud(data: Data) -> Result<(), anyhow::Error> { - let progress = data - .client() - .tx() - .sign_and_submit_then_watch( - &runtime::tx().universal_dividend().claim_uds(), - &PairSigner::new(data.keypair()), - BaseExtrinsicParamsBuilder::new(), - ) - .await?; - - let events = track_progress(progress).await?; - - if let Some(e) = events.find_first::<runtime::universal_dividend::events::UdsClaimed>()? { - println!("{e:?}"); - } - Ok(()) -} - +/// define universal dividends subcommands #[derive(Clone, Default, Debug, clap::Parser)] pub enum Subcommand { #[default] @@ -33,10 +15,30 @@ pub async fn handle_command(data: Data, command: Subcommand) -> anyhow::Result<( // match subcommand match command { Subcommand::ClaimUds => { - data = data.build_keypair(); + data = data.build_keypair(); claim_ud(data).await?; } }; - Ok(()) + Ok(()) +} + +/// claim universal dividend +pub async fn claim_ud(data: Data) -> Result<(), anyhow::Error> { + let progress = data + .client() + .tx() + .sign_and_submit_then_watch( + &runtime::tx().universal_dividend().claim_uds(), + &PairSigner::new(data.keypair()), + BaseExtrinsicParamsBuilder::new(), + ) + .await?; + + let events = track_progress(progress).await?; + + if let Some(e) = events.find_first::<runtime::universal_dividend::events::UdsClaimed>()? { + println!("{e:?}"); + } + Ok(()) } diff --git a/src/main.rs b/src/main.rs index c069b16743b635cfa8eb859e67168f075590bd45..bb19e550f7e9491572dd34a7e0f3db0949b7dbd4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -187,27 +187,6 @@ pub enum Subcommand { GenRevocDoc, /// Revoke an identity immediately RevokeIdentity, - CreateOneshot { - balance: u64, - dest: AccountId, - }, - ConsumeOneshot { - dest: AccountId, - #[clap(long = "oneshot")] - dest_oneshot: bool, - }, - ConsumeOneshotWithRemaining { - balance: u64, - dest: AccountId, - #[clap(long = "one")] - dest_oneshot: bool, - remaining_to: AccountId, - #[clap(long = "rem-one")] - remaining_to_oneshot: bool, - }, - OneshotBalance { - account: AccountId, - }, /// List upcoming expirations that require an action Expire { /// Show certs that expire within less than this number of blocks @@ -276,6 +255,9 @@ pub enum Subcommand { RuntimeInfo, /// Check current block CurrentBlock, + /// Oneshot account subcommands + #[clap(subcommand)] + Oneshot(commands::oneshot::Subcommand), /// Universal Dividend subcommands #[clap(subcommand)] Ud(commands::ud::Subcommand), @@ -327,66 +309,6 @@ async fn main() -> Result<(), GcliError> { .await?; commands::identity::revoke_identity(data).await?; } - Subcommand::CreateOneshot { balance, dest } => { - data = data.build_client().await?; - commands::oneshot::create_oneshot_account( - get_keys( - args.secret_format, - &args.address, - &args.secret, - NeededKeys::Secret, - )? - .1 - .unwrap(), - data.client(), - balance, - dest, - ) - .await?; - } - Subcommand::ConsumeOneshot { dest, dest_oneshot } => { - data = data.build_client().await?; - commands::oneshot::consume_oneshot_account( - get_keys( - args.secret_format, - &args.address, - &args.secret, - NeededKeys::Secret, - )? - .1 - .unwrap(), - data.client(), - dest, - dest_oneshot, - ) - .await?; - } - Subcommand::ConsumeOneshotWithRemaining { - balance, - dest, - dest_oneshot, - remaining_to, - remaining_to_oneshot, - } => { - data = data.build_client().await?; - commands::oneshot::consume_oneshot_account_with_remaining( - get_keys( - args.secret_format, - &args.address, - &args.secret, - NeededKeys::Secret, - )? - .1 - .unwrap(), - data.client(), - balance, - dest, - dest_oneshot, - remaining_to, - remaining_to_oneshot, - ) - .await?; - } Subcommand::Expire { blocks, sessions } => { data = data.build_client().await?; commands::expire::monitor_expirations(&data, blocks, sessions).await? @@ -444,10 +366,6 @@ async fn main() -> Result<(), GcliError> { ) .await?; } - Subcommand::OneshotBalance { account } => { - data = data.build_client().await?; - commands::oneshot::oneshot_account_balance(data.client(), account).await? - } Subcommand::Online => { data = data.build_client().await?; commands::smith::online(&data).await? @@ -602,6 +520,9 @@ async fn main() -> Result<(), GcliError> { .unwrap() ); } + Subcommand::Oneshot(subcommand) => { + commands::oneshot::handle_command(data, subcommand).await? + } Subcommand::Ud(subcommand) => commands::ud::handle_command(data, subcommand).await?, Subcommand::Indexer(subcommand) => indexer::handle_command(data, subcommand).await?, Subcommand::Config(subcommand) => conf::handle_command(data, subcommand)?,