Newer
Older
#[subxt::subxt(
runtime_metadata_path = "res/metadata.scale",
derive_for_all_types = "Debug"
)]
pub mod runtime {
// IF NEEDED
// #[subxt(substitute_type = "spcore::sr25519::Signature")]
// use crate::gdev::runtime_types::sp_core::sr25519::Signature;
}
pub type Client = subxt::OnlineClient<Runtime>;
pub type AccountId = subxt::ext::sp_runtime::AccountId32;
pub type TxInBlock = subxt::tx::TxInBlock<Runtime, Client>;
pub type TxProgress = subxt::tx::TxProgress<Runtime, Client>;
pub type Balance = u64;
pub type AccountData = runtime::runtime_types::pallet_duniter_account::types::AccountData<Balance>;
pub type AccountInfo = runtime::runtime_types::frame_system::AccountInfo<u32, AccountData>;
type Index = u32;
type BlockNumber = u32;
type Hash = sp_core::H256;
type Hashing = subxt::ext::sp_runtime::traits::BlakeTwo256;
type Address = subxt::ext::sp_runtime::MultiAddress<Self::AccountId, u32>;
type Header = subxt::ext::sp_runtime::generic::Header<
Self::BlockNumber,
subxt::ext::sp_runtime::traits::BlakeTwo256,
>;
type Signature = subxt::ext::sp_runtime::MultiSignature;
type ExtrinsicParams = subxt::tx::BaseExtrinsicParams<Self, Tip>;
#[derive(Copy, Clone, Debug, Default, Encode)]
pub struct Tip {
pub fn new(amount: u64) -> Self {
Tip { tip: amount }
}
#[clap(subcommand)]
pub subcommand: Subcommand,
#[clap(short, long, default_value = "http://localhost:8080/v1/graphql")]
indexer: String,
/// Do not use indexer
#[clap(long)]
no_indexer: bool,
/// Secret key or BIP39 mnemonic
/// (eventually followed by derivation path)
#[clap(short, long)]
secret: Option<String>,
/// Secret key format (seed, substrate)
#[clap(short = 'S', long, default_value = SecretFormat::Substrate)]
secret_format: SecretFormat,
/// Address
#[clap(short, long)]
address: Option<String>,
/// Websocket RPC endpoint
#[clap(short, long, default_value = "ws://localhost:9944")]
url: String,
/// Data of current command
/// can also include fetched information
#[derive(Default)]
pub struct Data {
pub args: Args,
pub client: Option<Client>,
pub address: Option<AccountId>,
pub keypair: Option<Pair>,
pub idty_index: Option<u32>,
pub token_decimals: u32,
pub token_symbol: String,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct SystemProperties {
token_decimals: u32,
token_symbol: String,
}
// implement helper functions for Data
impl Data {
token_decimals: 0,
token_symbol: "tokens".into(),
..Default::default()
}
}
// --- getters ---
// the "unwrap" should not fail if data is well prepared
pub fn client(&self) -> Client {
self.client.clone().unwrap()
}
pub fn address(&self) -> AccountId {
self.address.clone().unwrap()
}
pub fn keypair(&self) -> Pair {
self.keypair.clone().unwrap()
}
pub fn idty_index(&self) -> u32 {
self.idty_index.unwrap()
}
// --- methods ---
pub fn format_balance(&self, amount: Balance) -> String {
let base: u32 = 10;
format!(
"{} {}",
(amount as f32) / (base.pow(self.token_decimals) as f32),
self.token_symbol
)
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
}
// --- mutators ---
/// force an address if needed
pub fn build_address(mut self) -> Self {
self.address = Some(
get_keys(
self.args.secret_format,
&self.args.address,
&self.args.secret,
NeededKeys::Public,
)
.expect("needed")
.0
.expect("needed"),
);
self
}
/// force a keypair if needed
pub fn build_keypair(mut self) -> Self {
let (address, keypair) = get_keys(
self.args.secret_format,
&self.args.address,
&self.args.secret,
NeededKeys::Secret,
)
.expect("needed");
self.address = address;
self.keypair = keypair;
self
}
/// build a client from url
// TODO get client from a pre-defined list
pub async fn build_client(mut self) -> Self {
self.client = Some(Client::from_url(&self.args.url).await.expect("needed"));
self
}
/// get issuer index
/// needs address and client first
pub async fn fetch_idty_index(mut self) -> Result<Self, anyhow::Error> {
self.idty_index = Some(
commands::identity::get_idty_index_by_account_id(
self.client().clone(),
&self.address(),
)
.await?
.ok_or(anyhow::anyhow!("needs to be member to use this command"))?,
);
Ok(self)
}
/// get properties
pub async fn fetch_system_properties(mut self) -> Result<Self, anyhow::Error> {
let system_properties = self.client().clone().rpc().system_properties().await?;
let system_properties = serde_json::from_value::<SystemProperties>(
serde_json::Value::Object(system_properties),
)?;
self.token_decimals = system_properties.token_decimals;
self.token_symbol = system_properties.token_symbol;
Ok(self)
}
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
}
/// track progress of transaction on the network
/// until it is in block with success or failure
pub async fn track_progress(progress: TxProgress) -> anyhow::Result<()> {
println!("submitted transaction to network, waiting 6 seconds...");
// wait for in block
let tx = progress.wait_for_in_block().await?;
// print result
println!("{:?}", tx.wait_for_success().await?);
// return empty
Ok(())
}
/// custom error type intended to provide more convenient error message to user
#[derive(Debug)]
pub enum GcliError {
/// error coming from subxt
Subxt(subxt::Error),
/// error coming from anyhow
Anyhow(anyhow::Error),
}
impl std::fmt::Display for GcliError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}
impl std::error::Error for GcliError {}
impl From<subxt::Error> for GcliError {
fn from(e: subxt::Error) -> GcliError {
GcliError::Subxt(e)
}
}
impl From<anyhow::Error> for GcliError {
fn from(e: anyhow::Error) -> GcliError {
GcliError::Anyhow(e)
}
}
#[derive(Clone, Debug, clap::Subcommand, Default)]
/// Create and certify an identity
///
/// Caller must be member, and the target account must exist.
CreateIdentity {
target: sp_core::crypto::AccountId32,
},
/// Confirm an identity
///
/// To be called by the certified not-yet-member account, to become member.
ConfirmIdentity {
name: String,
},
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
CreateOneshot {
balance: u64,
dest: sp_core::crypto::AccountId32,
},
ConsumeOneshot {
dest: sp_core::crypto::AccountId32,
#[clap(long = "oneshot")]
dest_oneshot: bool,
},
ConsumeOneshotWithRemaining {
balance: u64,
dest: sp_core::crypto::AccountId32,
#[clap(long = "one")]
dest_oneshot: bool,
remaining_to: sp_core::crypto::AccountId32,
#[clap(long = "rem-one")]
remaining_to_oneshot: bool,
},
/// List upcoming expirations that require an action
Expire {
/// Show certs that expire within less than this number of blocks
#[clap(short, long, default_value_t = 100800)]
blocks: u32,
/// Show authorities that should rotate keys within less than this number of sessions
#[clap(short, long, default_value_t = 100)]
sessions: u32,
},
/// Fetch identity
Identity {
#[clap(short = 'p', long = "pubkey")]
account_id: Option<sp_core::crypto::AccountId32>,
#[clap(short = 'i', long = "identity")]
identity_id: Option<u32>,
#[clap(short = 'u', long = "username")]
username: Option<String>,
},
/// Generate a revocation document for the provided account
GenRevocDoc,
GoOffline,
GoOnline,
OneshotBalance {
account: sp_core::crypto::AccountId32,
},
/// List online authorities
Online,
#[clap(hide = true)]
Repart {
// Number of transactions per block to target
target: u32,
#[clap(short = 'o', long = "old-repart")]
// Old/actual repartition
actual_repart: Option<u32>,
},
#[clap(hide = true)]
SpamRoll {
actual_repart: usize,
},
SudoSetKey {
new_key: sp_core::crypto::AccountId32,
},
/// Emit a smith certification
SmithCert {
to: u32,
},
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/// List members of the technical committee
TechMembers,
/// List proposals to the technical committee
TechProposals,
/// Vote a proposal to the technical committee
TechVote {
/// Proposal hash
hash: H256,
/// Proposal index
index: u32,
/// Vote (0=against, 1=for)
vote: u8,
},
Transfer {
/// Amount to transfer
amount: u64,
/// Destination address
dest: sp_core::crypto::AccountId32,
/// Prevent from going below account existential deposit
#[clap(short = 'k', long = "keep-alive")]
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
UpdateKeys,
/// Revoke an identity
RevokeIdentity,
Subcommand::GetBalance => {
data = data
.build_keypair()
.build_client()
.await
.fetch_system_properties()
.await?;
commands::account::get_balance(data).await?
}
Subcommand::CreateIdentity { target } => {
data = data.build_client().await.build_keypair();
let progress =
commands::identity::create_identity(data.keypair(), data.client(), target).await?;
track_progress(progress).await?
}
Subcommand::ConfirmIdentity { name } => {
data = data.build_client().await.build_keypair();
let progress =
commands::identity::confirm_identity(data.keypair(), data.client(), name).await?;
track_progress(progress).await?
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
Subcommand::CreateOneshot { balance, dest } => {
commands::oneshot::create_oneshot_account(
get_keys(
args.secret_format,
&args.address,
&args.secret,
NeededKeys::Secret,
)?
.1
.unwrap(),
Client::from_url(&args.url).await.unwrap(),
balance,
dest,
)
.await?
}
Subcommand::ConsumeOneshot { dest, dest_oneshot } => {
commands::oneshot::consume_oneshot_account(
get_keys(
args.secret_format,
&args.address,
&args.secret,
NeededKeys::Secret,
)?
.1
.unwrap(),
Client::from_url(&args.url).await.unwrap(),
dest,
dest_oneshot,
)
.await?
}
Subcommand::ConsumeOneshotWithRemaining {
balance,
dest,
dest_oneshot,
remaining_to,
remaining_to_oneshot,
} => {
commands::oneshot::consume_oneshot_account_with_remaining(
get_keys(
args.secret_format,
&args.address,
&args.secret,
NeededKeys::Secret,
)?
.1
.unwrap(),
Client::from_url(&args.url).await.unwrap(),
balance,
dest,
dest_oneshot,
remaining_to,
remaining_to_oneshot,
)
.await?
}
Subcommand::Expire { blocks, sessions } => {
commands::expire::monitor_expirations(
Client::from_url(&args.url).await.unwrap(),
blocks,
sessions,
&args,
)
.await?
}
Subcommand::Identity {
ref account_id,
identity_id,
ref username,
} => {
commands::identity::get_identity(
Client::from_url(&args.url).await.unwrap(),
account_id.clone(),
identity_id,
username.clone(),
&args,
)
.await?
}
Subcommand::GenRevocDoc => {
commands::revocation::generate_revoc_doc(
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
&Client::from_url(&args.url).await.unwrap(),
&get_keys(
args.secret_format,
&args.address,
&args.secret,
NeededKeys::Secret,
)?
.1
.unwrap(),
)
.await?
}
Subcommand::GoOffline => {
commands::smith::go_offline(
get_keys(
args.secret_format,
&args.address,
&args.secret,
NeededKeys::Secret,
)?
.1
.unwrap(),
Client::from_url(&args.url).await.unwrap(),
)
.await?
}
Subcommand::GoOnline => {
commands::smith::go_online(
get_keys(
args.secret_format,
&args.address,
&args.secret,
NeededKeys::Secret,
)?
.1
.unwrap(),
Client::from_url(&args.url).await.unwrap(),
)
.await?
}
Subcommand::OneshotBalance { account } => {
commands::oneshot::oneshot_account_balance(
Client::from_url(&args.url).await.unwrap(),
account,
)
.await?
}
Subcommand::Online => {
commands::smith::online(Client::from_url(&args.url).await.unwrap(), &args).await?
}
Subcommand::Repart {
target,
actual_repart,
} => {
commands::net_test::repart(
get_keys(
args.secret_format,
&args.address,
&args.secret,
NeededKeys::Secret,
)?
.1
.unwrap(),
Client::from_url(&args.url).await.unwrap(),
target,
actual_repart,
)
.await?
}
Subcommand::SpamRoll { actual_repart } => {
commands::net_test::spam_roll(
get_keys(
args.secret_format,
&args.address,
&args.secret,
NeededKeys::Secret,
)?
.1
.unwrap(),
Client::from_url(&args.url).await.unwrap(),
actual_repart,
)
.await?
}
Subcommand::SudoSetKey { new_key } => {
commands::sudo::set_key(
get_keys(
args.secret_format,
&args.address,
&args.secret,
NeededKeys::Secret,
)?
.1
.unwrap(),
Client::from_url(&args.url).await.unwrap(),
new_key,
)
.await?
}
Subcommand::SmithCert { to } => {
data = data
.build_client()
.await
.build_keypair()
.fetch_idty_index()
.await?;
commands::smith::cert(data.client(), data.keypair(), data.idty_index(), to).await?
}
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
Subcommand::TechMembers => {
commands::collective::technical_committee_members(
Client::from_url(&args.url).await.unwrap(),
&args,
)
.await?
}
Subcommand::TechProposals => {
commands::collective::technical_committee_proposals(
Client::from_url(&args.url).await.unwrap(),
)
.await?
}
Subcommand::TechVote { hash, index, vote } => {
let vote = match vote {
0 => false,
1 => true,
_ => panic!("Vote must be written 0 if you disagree, or 1 if you agree."),
};
commands::collective::technical_committee_vote(
get_keys(
args.secret_format,
&args.address,
&args.secret,
NeededKeys::Secret,
)?
.1
.unwrap(),
Client::from_url(&args.url).await.unwrap(),
hash, //H256::from_str(&hash).expect("Invalid hash formatting"),
index,
vote,
)
.await?
}
Subcommand::Transfer {
amount,
dest,
keep_alive,
} => {
commands::transfer::transfer(
get_keys(
args.secret_format,
&args.address,
&args.secret,
NeededKeys::Secret,
)?
.1
.unwrap(),
Client::from_url(&args.url).await.unwrap(),
amount,
dest,
keep_alive,
)
.await?
}
Subcommand::TransferMultiple { amount, dests } => {
commands::transfer::transfer_multiple(
get_keys(
args.secret_format,
&args.address,
&args.secret,
NeededKeys::Secret,
)?
.1
.unwrap(),
Client::from_url(&args.url).await.unwrap(),
amount,
dests,
)
.await?
}
Subcommand::UpdateKeys => commands::smith::update_session_keys(
get_keys(
args.secret_format,
&args.address,
&args.secret,
NeededKeys::Secret,
)?
.1
.unwrap(),
Client::from_url(&args.url).await.unwrap(),
)
.await
.unwrap(),
Subcommand::RevokeIdentity => {
let (address, pair) = get_keys(
args.secret_format,
&args.address,
&args.secret,
NeededKeys::Secret,
)?;
commands::identity::revoke_identity(
pair.unwrap(),
Client::from_url(&args.url).await.unwrap(),
address.unwrap(),
)
.await?
}