use avail_rust_client::{
avail::{
RuntimeCall,
utility::{
events::{
BatchCompleted, BatchCompletedWithErrors, BatchInterrupted, DispatchedAs, ItemCompleted, ItemFailed,
},
tx::BatchAll,
},
},
prelude::*,
};
#[tokio::main]
async fn main() -> Result<(), Error> {
let client = Client::new(LOCAL_ENDPOINT).await?;
let balances = client.tx().balances();
let c1 = balances.transfer_keep_alive(bob().account_id(), constants::ONE_AVAIL);
let c2 = balances.transfer_keep_alive(dave().account_id(), constants::ONE_AVAIL);
let submittable = client.tx().utility().batch_all(vec![c1, c2]);
let submitted = submittable.sign_and_submit(&alice(), Options::default()).await?;
let Some(receipt) = submitted.receipt(true).await? else {
return Err("Transaction got dropped. This should never happen in a local network.".into());
};
let events = receipt.events().await?;
assert!(events.is_extrinsic_success_present());
if events.is_present::<BatchInterrupted>() {
println!("Found Utility::BatchInterrupted");
}
if events.is_present::<BatchCompleted>() {
println!("Found Utility::BatchCompleted");
}
if events.is_present::<BatchCompletedWithErrors>() {
println!("Found Utility::BatchCompletedWithErrors");
}
println!("Found {}x Utility::ItemCompleted", events.count::<ItemCompleted>());
if events.is_present::<ItemFailed>() {
println!("Found Utility::ItemFailed");
}
if events.is_present::<DispatchedAs>() {
println!("Found Utility::DispatchedAs");
}
let ext = receipt.extrinsic::<BatchAll>().await?;
let runtime_calls = ext.call.decode_calls()?;
for runtime_call in runtime_calls {
let RuntimeCall::BalancesTransferKeepAlive(balance_call) = runtime_call else {
return Err("Expected Balance Transfer Keep Alive".into());
};
let dest = AccountId::try_from(&balance_call.dest)
.map(|x| x.to_string())
.unwrap_or_else(|_| std::format!("{:?}", balance_call.dest));
println!("Dest: {:?}, Amount: {}", dest, balance_call.value);
}
Ok(())
}