Skip to content
Snippets Groups Projects
Commit 30bafa01 authored by Hugo Trentesaux's avatar Hugo Trentesaux
Browse files

feat: batch transfer

parent 44cefc17
No related branches found
No related tags found
1 merge request!2feat: batch transfer
...@@ -2,6 +2,10 @@ ...@@ -2,6 +2,10 @@
CLI client for [Duniter-V2S](https://git.duniter.org/nodes/rust/duniter-v2s/). CLI client for [Duniter-V2S](https://git.duniter.org/nodes/rust/duniter-v2s/).
Using
- https://github.com/duniter/substrate
- https://github.com/duniter/subxt
## Usage ## Usage
If using a different runtime, update the metadata for the client to compile: If using a different runtime, update the metadata for the client to compile:
......
No preview for this file type
...@@ -4,6 +4,9 @@ use anyhow::Result; ...@@ -4,6 +4,9 @@ use anyhow::Result;
use sp_core::{crypto::AccountId32, sr25519::Pair}; use sp_core::{crypto::AccountId32, sr25519::Pair};
use subxt::tx::{BaseExtrinsicParamsBuilder, PairSigner}; use subxt::tx::{BaseExtrinsicParamsBuilder, PairSigner};
type Call = gdev::runtime_types::gdev_runtime::Call;
type BalancesCall = gdev::runtime_types::pallet_balances::pallet::Call;
pub async fn transfer( pub async fn transfer(
pair: Pair, pair: Pair,
client: Client, client: Client,
...@@ -35,3 +38,33 @@ pub async fn transfer( ...@@ -35,3 +38,33 @@ pub async fn transfer(
Ok(()) Ok(())
} }
pub async fn transfer_multiple(
pair: Pair,
client: Client,
amount: u64,
dests: Vec<AccountId32>,
) -> Result<()> {
// build the list of transactions from the destination accounts
let transactions: Vec<Call> = dests
.into_iter()
.map(|dest| {
Call::Balances(BalancesCall::transfer_keep_alive {
dest: dest.into(),
value: amount,
})
})
.collect();
// wrap these calls in a batch call
client
.tx()
.sign_and_submit_then_watch(
&gdev::tx().utility().batch(transactions),
&PairSigner::new(pair.clone()),
BaseExtrinsicParamsBuilder::new(),
)
.await?;
Ok(())
}
...@@ -127,11 +127,22 @@ pub enum Subcommand { ...@@ -127,11 +127,22 @@ pub enum Subcommand {
new_key: sp_core::crypto::AccountId32, new_key: sp_core::crypto::AccountId32,
}, },
Transfer { Transfer {
balance: u64, /// Amount to transfer
amount: u64,
/// Destination address
dest: sp_core::crypto::AccountId32, dest: sp_core::crypto::AccountId32,
#[clap(short = 'k')] /// Prevent from going below account existential deposit
#[clap(short = 'k', long = "keep-alive")]
keep_alive: bool, 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<sp_core::crypto::AccountId32>,
},
/// Rotate and set session keys /// Rotate and set session keys
UpdateKeys, UpdateKeys,
} }
...@@ -174,7 +185,7 @@ async fn main() -> Result<()> { ...@@ -174,7 +185,7 @@ async fn main() -> Result<()> {
println!("Account address: {}", account_id); println!("Account address: {}", account_id);
} }
let client = Client::new().await.unwrap(); let client = Client::new().await?;
if let Some(account_id) = &account_id { if let Some(account_id) = &account_id {
let account = client let account = client
...@@ -272,19 +283,28 @@ async fn main() -> Result<()> { ...@@ -272,19 +283,28 @@ async fn main() -> Result<()> {
.await? .await?
} }
Subcommand::Transfer { Subcommand::Transfer {
balance, amount,
dest, dest,
keep_alive, keep_alive,
} => { } => {
commands::transfer::transfer( commands::transfer::transfer(
pair.expect("This subcommand needs a secret."), pair.expect("This subcommand needs a secret."),
client, client,
balance, amount,
dest, dest,
keep_alive, keep_alive,
) )
.await? .await?
} }
Subcommand::TransferMultiple { amount, dests } => {
commands::transfer::transfer_multiple(
pair.expect("This subcommand needs a secret."),
client,
amount,
dests,
)
.await?
}
Subcommand::UpdateKeys => commands::smith::update_session_keys( Subcommand::UpdateKeys => commands::smith::update_session_keys(
pair.expect("This subcommand needs a secret."), pair.expect("This subcommand needs a secret."),
client, client,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment