diff --git a/src/commands.rs b/src/commands.rs index 15f96bacd9e9eef0bd51f4edc3ddcb6d864e88b5..24debbeb2d984716eca3f22a3d20f3d3b5ccd20c 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -16,3 +16,4 @@ pub mod sudo; pub mod transfer; pub mod ud; pub mod vault; +pub mod commented; diff --git a/src/commands/account.rs b/src/commands/account.rs index df5c5539e7f0d394a68550d5d9bee04a683ca069..0f0f138d991d98433048069d5bba19d6b3028709 100644 --- a/src/commands/account.rs +++ b/src/commands/account.rs @@ -12,12 +12,15 @@ pub enum Subcommand { amount: u64, /// Destination address dest: AccountId, - /// Prevent from going below account existential deposit - #[clap(short = 'k', long = "keep-alive")] - keep_alive: bool, + /// Allow going below account existential deposit + #[clap(short = 'k', long = "allow-death")] + allow_death: bool, /// Use universal dividends instead of units #[clap(short = 'u', long = "ud")] is_ud: bool, + /// Add a transaction comment + #[clap(short = 'c', long = "comment")] + comment: Option<String>, }, /// Transfer the same amount for each space-separated address. /// If an address appears mutiple times, it will get multiple times the same amount @@ -39,10 +42,20 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE Subcommand::Transfer { amount, dest, - keep_alive, + allow_death, is_ud, + comment, } => { - commands::transfer::transfer(&data, amount, dest, keep_alive, is_ud).await?; + if let Some(comment) = comment { + if is_ud || allow_death { + return Err(GcliError::Input( + "ud or allow death commented transfers are not handled yet".to_string(), + )); + } + commands::commented::transfer(&data, amount, dest, comment).await?; + } else { + commands::transfer::transfer(&data, amount, dest, allow_death, is_ud).await?; + } } Subcommand::TransferMultiple { amount, dests } => { commands::transfer::transfer_multiple(&data, amount, dests).await?; diff --git a/src/commands/commented.rs b/src/commands/commented.rs new file mode 100644 index 0000000000000000000000000000000000000000..6d24e2b11b129860aec620bfc4d11b2f5b52a906 --- /dev/null +++ b/src/commands/commented.rs @@ -0,0 +1,36 @@ +use crate::*; + +#[cfg(any(feature = "dev", feature = "gdev"))] // find how to get runtime calls +type Call = runtime::runtime_types::gdev_runtime::RuntimeCall; +type BalancesCall = runtime::runtime_types::pallet_balances::pallet::Call; +type SystemCall = runtime::runtime_types::frame_system::pallet::Call; + +/// commented balance transfer +pub async fn transfer( + data: &Data, + amount: u64, + dest: AccountId, + comment: String, +) -> Result<(), subxt::Error> { + // build transfer call + let transfer_call = Call::Balances(BalancesCall::transfer_keep_alive { + dest: dest.into(), + value: amount, + }); + // build comment call + let comment_call = Call::System(SystemCall::remark_with_event { + remark: comment.as_bytes().to_vec(), + }); + + // wrap these calls in a batch call + submit_call_and_look_event::< + runtime::utility::events::BatchCompleted, + Payload<runtime::utility::calls::types::Batch>, + >( + data, + &runtime::tx() + .utility() + .batch(vec![transfer_call, comment_call]), + ) + .await +} diff --git a/src/commands/transfer.rs b/src/commands/transfer.rs index 5f31cd6a0dfa12af5c6b9223c7f5ac7b9204bd1e..3689f5d7b246e12f004bc1b67c619abf65a50e3f 100644 --- a/src/commands/transfer.rs +++ b/src/commands/transfer.rs @@ -9,11 +9,11 @@ pub async fn transfer( data: &Data, balance: u64, dest: AccountId, - keep_alive: bool, + allow_deat: bool, is_ud: bool, ) -> Result<(), subxt::Error> { - match (keep_alive, is_ud) { - (true, false) => { + match (allow_deat, is_ud) { + (false, false) => { submit_call_and_look_event::< runtime::balances::events::Transfer, Payload<runtime::balances::calls::types::TransferKeepAlive>, @@ -25,7 +25,7 @@ pub async fn transfer( ) .await } - (false, false) => { + (true, false) => { submit_call_and_look_event::< runtime::balances::events::Transfer, Payload<runtime::balances::calls::types::TransferAllowDeath>, @@ -37,7 +37,7 @@ pub async fn transfer( ) .await } - (true, true) => { + (false, true) => { submit_call_and_look_event::< runtime::balances::events::Transfer, Payload<runtime::universal_dividend::calls::types::TransferUdKeepAlive>, @@ -49,7 +49,7 @@ pub async fn transfer( ) .await } - (false, true) => { + (true, true) => { submit_call_and_look_event::< runtime::balances::events::Transfer, Payload<runtime::universal_dividend::calls::types::TransferUd>,