You need to sign in or sign up before continuing.
Newer
Older
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/// 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(())
}
pub async fn create_oneshot_account(
let progress = data
.client()
.oneshot_account()
.create_oneshot_account(dest.into(), balance),
&PairSigner::new(data.keypair()),
if data.args.no_wait {
return Ok(());
}
let events = track_progress(progress).await?;
if let Some(e) =
events.find_first::<runtime::oneshot_account::events::OneshotAccountCreated>()?
{
println!("{e:?}");
}
Ok(())
pub async fn consume_oneshot_account(
data: &Data,
dest: AccountId,
let client = data.client();
runtime::runtime_types::pallet_oneshot_account::types::Account::Oneshot(
runtime::runtime_types::pallet_oneshot_account::types::Account::Normal(
dest.into(),
)
&PairSigner::new(data.keypair()),
if data.args.no_wait {
return Ok(());
}
let events = track_progress(progress).await?;
if let Some(e) =
events.find_first::<runtime::oneshot_account::events::OneshotAccountConsumed>()?
/// consume oneshot account with remaining
pub async fn consume_oneshot_account_with_remaining(
let client = data.client();
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
let call = &runtime::tx()
.oneshot_account()
.consume_oneshot_account_with_remaining(
number,
if dest_oneshot {
runtime::runtime_types::pallet_oneshot_account::types::Account::Oneshot(dest.into())
} else {
runtime::runtime_types::pallet_oneshot_account::types::Account::Normal(dest.into())
},
if remaining_to_oneshot {
runtime::runtime_types::pallet_oneshot_account::types::Account::Oneshot(
remaining_to.into(),
)
} else {
runtime::runtime_types::pallet_oneshot_account::types::Account::Normal(
remaining_to.into(),
)
},
balance,
);
submit_call_and_look_event::<
runtime::oneshot_account::events::OneshotAccountConsumed,
StaticTxPayload<runtime::oneshot_account::calls::ConsumeOneshotAccountWithRemaining>,
>(data, call)
.await