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

refac

WIP add tracking progress

WIP refac progress tracking

WIP generic runtime

wip needing runtime name

wip refac smith cert
parent 4e0d1617
No related branches found
No related tags found
1 merge request!5add features and refac data
...@@ -22,3 +22,11 @@ serde_json = "1.0.94" ...@@ -22,3 +22,11 @@ serde_json = "1.0.94"
sp-core = { git = "https://github.com/duniter/substrate", branch = "duniter-substrate-v0.9.32" } sp-core = { git = "https://github.com/duniter/substrate", branch = "duniter-substrate-v0.9.32" }
subxt = { git = "https://github.com/duniter/subxt.git", branch = "duniter-substrate-v0.9.32" } subxt = { git = "https://github.com/duniter/subxt.git", branch = "duniter-substrate-v0.9.32" }
tokio = { version = "1.26.0", features = ["macros"] } tokio = { version = "1.26.0", features = ["macros"] }
# allows to build gcli with different predefined networks
[features]
default = ["dev"] # default feature is "dev"
dev = []
gdev = []
gtest = []
g1 = []
use crate::{gdev, indexer::*, Client}; use crate::indexer::*;
use crate::*;
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use std::collections::{hash_map, HashMap}; use std::collections::{hash_map, HashMap};
...@@ -31,7 +32,7 @@ impl<'a> IdentityCache<'a> { ...@@ -31,7 +32,7 @@ impl<'a> IdentityCache<'a> {
.client .client
.storage() .storage()
.fetch( .fetch(
&gdev::storage().identity().identities(identity_id), &runtime::storage().identity().identities(identity_id),
Some(parent_hash), Some(parent_hash),
) )
.await? .await?
......
use crate::{gdev, indexer::*, Args, Client}; use crate::indexer::*;
use crate::*;
use anyhow::Result; use anyhow::Result;
use sp_core::{sr25519::Pair, H256}; use sp_core::{sr25519::Pair, H256};
...@@ -7,7 +8,7 @@ use subxt::tx::{BaseExtrinsicParamsBuilder, PairSigner}; ...@@ -7,7 +8,7 @@ use subxt::tx::{BaseExtrinsicParamsBuilder, PairSigner};
pub async fn technical_committee_members(client: Client, args: &Args) -> Result<()> { pub async fn technical_committee_members(client: Client, args: &Args) -> Result<()> {
let parent_hash = client let parent_hash = client
.storage() .storage()
.fetch(&gdev::storage().system().parent_hash(), None) .fetch(&runtime::storage().system().parent_hash(), None)
.await? .await?
.unwrap(); .unwrap();
...@@ -27,7 +28,7 @@ pub async fn technical_committee_members(client: Client, args: &Args) -> Result< ...@@ -27,7 +28,7 @@ pub async fn technical_committee_members(client: Client, args: &Args) -> Result<
for account_id in client for account_id in client
.storage() .storage()
.fetch( .fetch(
&gdev::storage().technical_committee().members(), &runtime::storage().technical_committee().members(),
Some(parent_hash), Some(parent_hash),
) )
.await? .await?
...@@ -45,7 +46,7 @@ pub async fn technical_committee_members(client: Client, args: &Args) -> Result< ...@@ -45,7 +46,7 @@ pub async fn technical_committee_members(client: Client, args: &Args) -> Result<
client client
.storage() .storage()
.fetch( .fetch(
&gdev::storage().identity().identity_index_of(&account_id), &runtime::storage().identity().identity_index_of(&account_id),
Some(parent_hash), Some(parent_hash),
) )
.await .await
...@@ -66,14 +67,14 @@ pub async fn technical_committee_members(client: Client, args: &Args) -> Result< ...@@ -66,14 +67,14 @@ pub async fn technical_committee_members(client: Client, args: &Args) -> Result<
pub async fn technical_committee_proposals(client: Client) -> Result<()> { pub async fn technical_committee_proposals(client: Client) -> Result<()> {
let parent_hash = client let parent_hash = client
.storage() .storage()
.fetch(&gdev::storage().system().parent_hash(), None) .fetch(&runtime::storage().system().parent_hash(), None)
.await? .await?
.unwrap(); .unwrap();
let mut proposals_iter = client let mut proposals_iter = client
.storage() .storage()
.iter( .iter(
gdev::storage() runtime::storage()
.technical_committee() .technical_committee()
.proposal_of(H256::default()), .proposal_of(H256::default()),
10, 10,
...@@ -99,7 +100,7 @@ pub async fn technical_committee_vote( ...@@ -99,7 +100,7 @@ pub async fn technical_committee_vote(
client client
.tx() .tx()
.sign_and_submit_then_watch( .sign_and_submit_then_watch(
&gdev::tx() &runtime::tx()
.technical_committee() .technical_committee()
.vote(proposal_hash, proposal_index, vote), .vote(proposal_hash, proposal_index, vote),
&PairSigner::new(pair), &PairSigner::new(pair),
......
use crate::{cache, gdev, indexer::*, Args, Client}; use crate::indexer::*;
use crate::*;
use anyhow::Result; use anyhow::Result;
use futures::join; use futures::join;
...@@ -16,11 +17,11 @@ pub async fn monitor_expirations( ...@@ -16,11 +17,11 @@ pub async fn monitor_expirations(
let parent_hash = client let parent_hash = client
.storage() .storage()
.fetch(&gdev::storage().system().parent_hash(), None) .fetch(&runtime::storage().system().parent_hash(), None)
.await? .await?
.unwrap(); .unwrap();
let addr_current_block = gdev::storage().system().number(); let addr_current_block = runtime::storage().system().number();
let addr_current_session = gdev::storage().session().current_index(); let addr_current_session = runtime::storage().session().current_index();
let (current_block, current_session) = join!( let (current_block, current_session) = join!(
client client
.storage() .storage()
...@@ -51,7 +52,7 @@ pub async fn monitor_expirations( ...@@ -51,7 +52,7 @@ pub async fn monitor_expirations(
let mut must_rotate_keys_before_iter = client let mut must_rotate_keys_before_iter = client
.storage() .storage()
.iter( .iter(
gdev::storage() runtime::storage()
.authority_members() .authority_members()
.must_rotate_keys_before(0), .must_rotate_keys_before(0),
10, 10,
...@@ -85,7 +86,7 @@ pub async fn monitor_expirations( ...@@ -85,7 +86,7 @@ pub async fn monitor_expirations(
let mut basic_certs_iter = client let mut basic_certs_iter = client
.storage() .storage()
.iter( .iter(
gdev::storage().cert().storage_certs_removable_on(0), runtime::storage().cert().storage_certs_removable_on(0),
10, 10,
Some(parent_hash), Some(parent_hash),
) )
...@@ -101,7 +102,9 @@ pub async fn monitor_expirations( ...@@ -101,7 +102,9 @@ pub async fn monitor_expirations(
let mut smith_certs_iter = client let mut smith_certs_iter = client
.storage() .storage()
.iter( .iter(
gdev::storage().smith_cert().storage_certs_removable_on(0), runtime::storage()
.smith_cert()
.storage_certs_removable_on(0),
10, 10,
Some(parent_hash), Some(parent_hash),
) )
...@@ -143,7 +146,7 @@ pub async fn monitor_expirations( ...@@ -143,7 +146,7 @@ pub async fn monitor_expirations(
let mut basic_membership_iter = client let mut basic_membership_iter = client
.storage() .storage()
.iter( .iter(
gdev::storage().membership().memberships_expire_on(0), runtime::storage().membership().memberships_expire_on(0),
10, 10,
Some(parent_hash), Some(parent_hash),
) )
...@@ -162,7 +165,9 @@ pub async fn monitor_expirations( ...@@ -162,7 +165,9 @@ pub async fn monitor_expirations(
let mut smith_membership_iter = client let mut smith_membership_iter = client
.storage() .storage()
.iter( .iter(
gdev::storage().smith_membership().memberships_expire_on(0), runtime::storage()
.smith_membership()
.memberships_expire_on(0),
10, 10,
Some(parent_hash), Some(parent_hash),
) )
......
use crate::{gdev, indexer::*, Args, Client}; use crate::indexer::*;
use crate::*;
use crate::gdev::runtime_types::common_runtime::entities::IdtyData; use crate::runtime::runtime_types::common_runtime::entities::IdtyData;
use crate::gdev::runtime_types::pallet_identity::types::*; use crate::runtime::runtime_types::pallet_identity::types::*;
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use sp_core::{crypto::AccountId32, sr25519::Pair}; use sp_core::{crypto::AccountId32, sr25519::Pair};
use std::str::FromStr; use std::str::FromStr;
...@@ -84,7 +85,7 @@ pub async fn get_idty_index_by_account_id( ...@@ -84,7 +85,7 @@ pub async fn get_idty_index_by_account_id(
Ok(client Ok(client
.storage() .storage()
.fetch( .fetch(
&gdev::storage().identity().identity_index_of(account_id), &runtime::storage().identity().identity_index_of(account_id),
None, None,
) )
.await?) .await?)
...@@ -96,32 +97,36 @@ pub async fn get_identity_by_index( ...@@ -96,32 +97,36 @@ pub async fn get_identity_by_index(
) -> Result<Option<IdtyValue<u32, AccountId32, IdtyData>>> { ) -> Result<Option<IdtyValue<u32, AccountId32, IdtyData>>> {
Ok(client Ok(client
.storage() .storage()
.fetch(&gdev::storage().identity().identities(idty_index), None) .fetch(&runtime::storage().identity().identities(idty_index), None)
.await?) .await?)
} }
pub async fn create_identity(pair: Pair, client: Client, target: AccountId32) -> Result<()> { pub async fn create_identity(
client pair: Pair,
client: Client,
target: AccountId32,
) -> Result<TxProgress, subxt::Error> {
Ok(client
.tx() .tx()
.sign_and_submit_then_watch( .sign_and_submit_then_watch(
&gdev::tx().identity().create_identity(target), &runtime::tx().identity().create_identity(target),
&PairSigner::new(pair), &PairSigner::new(pair),
BaseExtrinsicParamsBuilder::new(), BaseExtrinsicParamsBuilder::new(),
) )
.await?; .await?)
Ok(())
} }
pub async fn confirm_identity(pair: Pair, client: Client, name: String) -> Result<()> { pub async fn confirm_identity(
client pair: Pair,
client: Client,
name: String,
) -> Result<TxProgress, subxt::Error> {
Ok(client
.tx() .tx()
.sign_and_submit_then_watch( .sign_and_submit_then_watch(
&gdev::tx().identity().confirm_identity(name), &runtime::tx().identity().confirm_identity(name),
&PairSigner::new(pair), &PairSigner::new(pair),
BaseExtrinsicParamsBuilder::new(), BaseExtrinsicParamsBuilder::new(),
) )
.await?; .await?)
Ok(())
} }
use crate::{gdev, Client, GdevConfig}; use crate::*;
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use sp_core::{crypto::AccountId32, sr25519::Pair, DeriveJunction, Pair as _}; use sp_core::{crypto::AccountId32, sr25519::Pair, DeriveJunction, Pair as _};
...@@ -36,7 +36,7 @@ pub async fn repart( ...@@ -36,7 +36,7 @@ pub async fn repart(
if let Some(pair_i_account) = client if let Some(pair_i_account) = client
.storage() .storage()
.fetch( .fetch(
&gdev::storage().system().account(&pair_i.public().into()), &runtime::storage().system().account(&pair_i.public().into()),
None, None,
) )
.await? .await?
...@@ -50,8 +50,7 @@ pub async fn repart( ...@@ -50,8 +50,7 @@ pub async fn repart(
pub async fn spam_roll(pair: Pair, client: Client, actual_repart: usize) -> Result<()> { pub async fn spam_roll(pair: Pair, client: Client, actual_repart: usize) -> Result<()> {
let mut nonce = 0; let mut nonce = 0;
let mut pairs = let mut pairs = Vec::<(PairSigner<Runtime, Pair>, AccountId32)>::with_capacity(actual_repart);
Vec::<(PairSigner<GdevConfig, Pair>, AccountId32)>::with_capacity(actual_repart);
for i in 0..actual_repart { for i in 0..actual_repart {
let pair_i = pair let pair_i = pair
.derive(std::iter::once(DeriveJunction::hard::<u32>(i as u32)), None) .derive(std::iter::once(DeriveJunction::hard::<u32>(i as u32)), None)
...@@ -68,7 +67,7 @@ pub async fn spam_roll(pair: Pair, client: Client, actual_repart: usize) -> Resu ...@@ -68,7 +67,7 @@ pub async fn spam_roll(pair: Pair, client: Client, actual_repart: usize) -> Resu
let watcher = client let watcher = client
.tx() .tx()
.create_signed_with_nonce( .create_signed_with_nonce(
&gdev::tx().balances().transfer(MultiAddress::Id(dest), 1), &runtime::tx().balances().transfer(MultiAddress::Id(dest), 1),
&pairs[i].0, &pairs[i].0,
nonce, nonce,
BaseExtrinsicParamsBuilder::new(), BaseExtrinsicParamsBuilder::new(),
...@@ -83,7 +82,7 @@ pub async fn spam_roll(pair: Pair, client: Client, actual_repart: usize) -> Resu ...@@ -83,7 +82,7 @@ pub async fn spam_roll(pair: Pair, client: Client, actual_repart: usize) -> Resu
let watcher = client let watcher = client
.tx() .tx()
.sign_and_submit_then_watch( .sign_and_submit_then_watch(
&gdev::tx().balances().transfer(MultiAddress::Id(dest), 1), &runtime::tx().balances().transfer(MultiAddress::Id(dest), 1),
&pairs[actual_repart - 1].0, &pairs[actual_repart - 1].0,
BaseExtrinsicParamsBuilder::new(), BaseExtrinsicParamsBuilder::new(),
) )
......
use crate::{gdev, Client}; use crate::*;
use anyhow::Result; use anyhow::Result;
use sp_core::{crypto::AccountId32, sr25519::Pair}; use sp_core::{crypto::AccountId32, sr25519::Pair};
...@@ -13,7 +13,7 @@ pub async fn create_oneshot_account( ...@@ -13,7 +13,7 @@ pub async fn create_oneshot_account(
client client
.tx() .tx()
.sign_and_submit_then_watch( .sign_and_submit_then_watch(
&gdev::tx() &runtime::tx()
.oneshot_account() .oneshot_account()
.create_oneshot_account(dest.into(), balance), .create_oneshot_account(dest.into(), balance),
&PairSigner::new(pair), &PairSigner::new(pair),
...@@ -32,20 +32,22 @@ pub async fn consume_oneshot_account( ...@@ -32,20 +32,22 @@ pub async fn consume_oneshot_account(
) -> Result<()> { ) -> Result<()> {
let number = client let number = client
.storage() .storage()
.fetch(&gdev::storage().system().number(), None) .fetch(&runtime::storage().system().number(), None)
.await? .await?
.unwrap(); .unwrap();
client client
.tx() .tx()
.sign_and_submit_then_watch( .sign_and_submit_then_watch(
&gdev::tx().oneshot_account().consume_oneshot_account( &runtime::tx().oneshot_account().consume_oneshot_account(
number, number,
if dest_oneshot { if dest_oneshot {
gdev::runtime_types::pallet_oneshot_account::types::Account::Oneshot( runtime::runtime_types::pallet_oneshot_account::types::Account::Oneshot(
dest.into(), dest.into(),
) )
} else { } else {
gdev::runtime_types::pallet_oneshot_account::types::Account::Normal(dest.into()) runtime::runtime_types::pallet_oneshot_account::types::Account::Normal(
dest.into(),
)
}, },
), ),
&PairSigner::new(pair), &PairSigner::new(pair),
...@@ -67,31 +69,31 @@ pub async fn consume_oneshot_account_with_remaining( ...@@ -67,31 +69,31 @@ pub async fn consume_oneshot_account_with_remaining(
) -> Result<()> { ) -> Result<()> {
let number = client let number = client
.storage() .storage()
.fetch(&gdev::storage().system().number(), None) .fetch(&runtime::storage().system().number(), None)
.await? .await?
.unwrap(); .unwrap();
client client
.tx() .tx()
.sign_and_submit_then_watch( .sign_and_submit_then_watch(
&gdev::tx() &runtime::tx()
.oneshot_account() .oneshot_account()
.consume_oneshot_account_with_remaining( .consume_oneshot_account_with_remaining(
number, number,
if dest_oneshot { if dest_oneshot {
gdev::runtime_types::pallet_oneshot_account::types::Account::Oneshot( runtime::runtime_types::pallet_oneshot_account::types::Account::Oneshot(
dest.into(), dest.into(),
) )
} else { } else {
gdev::runtime_types::pallet_oneshot_account::types::Account::Normal( runtime::runtime_types::pallet_oneshot_account::types::Account::Normal(
dest.into(), dest.into(),
) )
}, },
if remaining_to_oneshot { if remaining_to_oneshot {
gdev::runtime_types::pallet_oneshot_account::types::Account::Oneshot( runtime::runtime_types::pallet_oneshot_account::types::Account::Oneshot(
remaining_to.into(), remaining_to.into(),
) )
} else { } else {
gdev::runtime_types::pallet_oneshot_account::types::Account::Normal( runtime::runtime_types::pallet_oneshot_account::types::Account::Normal(
remaining_to.into(), remaining_to.into(),
) )
}, },
...@@ -111,7 +113,9 @@ pub async fn oneshot_account_balance(client: Client, account: AccountId32) -> Re ...@@ -111,7 +113,9 @@ pub async fn oneshot_account_balance(client: Client, account: AccountId32) -> Re
client client
.storage() .storage()
.fetch( .fetch(
&gdev::storage().oneshot_account().oneshot_accounts(&account), &runtime::storage()
.oneshot_account()
.oneshot_accounts(&account),
None None
) )
.await? .await?
......
use crate::{gdev, Client}; use crate::*;
use anyhow::Result; use anyhow::Result;
use futures::join; use futures::join;
...@@ -6,8 +6,8 @@ use sp_core::{sr25519::Pair, Encode, Pair as _}; ...@@ -6,8 +6,8 @@ use sp_core::{sr25519::Pair, Encode, Pair as _};
pub async fn gen_revoc_doc(api: &Client, pair: &Pair) -> Result<()> { pub async fn gen_revoc_doc(api: &Client, pair: &Pair) -> Result<()> {
let account_id: sp_core::crypto::AccountId32 = pair.public().into(); let account_id: sp_core::crypto::AccountId32 = pair.public().into();
let addr_idty_index = gdev::storage().identity().identity_index_of(&account_id); let addr_idty_index = runtime::storage().identity().identity_index_of(&account_id);
let addr_block_hash = gdev::storage().system().block_hash(0); let addr_block_hash = runtime::storage().system().block_hash(0);
let (idty_index, genesis_hash) = join!( let (idty_index, genesis_hash) = join!(
api.storage().fetch(&addr_idty_index, None,), api.storage().fetch(&addr_idty_index, None,),
api.storage().fetch(&addr_block_hash, None) api.storage().fetch(&addr_block_hash, None)
......
...@@ -22,7 +22,7 @@ pub async fn set_session_keys(pair: Pair, client: Client, session_keys: SessionK ...@@ -22,7 +22,7 @@ pub async fn set_session_keys(pair: Pair, client: Client, session_keys: SessionK
client client
.tx() .tx()
.sign_and_submit_then_watch( .sign_and_submit_then_watch(
&gdev::tx() &runtime::tx()
.authority_members() .authority_members()
.set_session_keys(session_keys), .set_session_keys(session_keys),
&PairSigner::new(pair), &PairSigner::new(pair),
...@@ -42,7 +42,7 @@ pub async fn go_online(pair: Pair, client: Client) -> Result<()> { ...@@ -42,7 +42,7 @@ pub async fn go_online(pair: Pair, client: Client) -> Result<()> {
if client if client
.storage() .storage()
.fetch( .fetch(
&gdev::storage() &runtime::storage()
.session() .session()
.next_keys(AccountId32::from(pair.public())), .next_keys(AccountId32::from(pair.public())),
None, None,
...@@ -56,7 +56,7 @@ pub async fn go_online(pair: Pair, client: Client) -> Result<()> { ...@@ -56,7 +56,7 @@ pub async fn go_online(pair: Pair, client: Client) -> Result<()> {
client client
.tx() .tx()
.sign_and_submit_then_watch( .sign_and_submit_then_watch(
&gdev::tx().authority_members().go_online(), &runtime::tx().authority_members().go_online(),
&PairSigner::new(pair), &PairSigner::new(pair),
BaseExtrinsicParamsBuilder::new(), BaseExtrinsicParamsBuilder::new(),
) )
...@@ -69,7 +69,7 @@ pub async fn go_offline(pair: Pair, client: Client) -> Result<()> { ...@@ -69,7 +69,7 @@ pub async fn go_offline(pair: Pair, client: Client) -> Result<()> {
client client
.tx() .tx()
.sign_and_submit_then_watch( .sign_and_submit_then_watch(
&gdev::tx().authority_members().go_offline(), &runtime::tx().authority_members().go_offline(),
&PairSigner::new(pair), &PairSigner::new(pair),
BaseExtrinsicParamsBuilder::new(), BaseExtrinsicParamsBuilder::new(),
) )
...@@ -81,7 +81,7 @@ pub async fn go_offline(pair: Pair, client: Client) -> Result<()> { ...@@ -81,7 +81,7 @@ pub async fn go_offline(pair: Pair, client: Client) -> Result<()> {
pub async fn online(client: Client, args: &Args) -> Result<()> { pub async fn online(client: Client, args: &Args) -> Result<()> {
let parent_hash = client let parent_hash = client
.storage() .storage()
.fetch(&gdev::storage().system().parent_hash(), None) .fetch(&runtime::storage().system().parent_hash(), None)
.await? .await?
.unwrap(); .unwrap();
...@@ -104,7 +104,7 @@ pub async fn online(client: Client, args: &Args) -> Result<()> { ...@@ -104,7 +104,7 @@ pub async fn online(client: Client, args: &Args) -> Result<()> {
let online_authorities = client let online_authorities = client
.storage() .storage()
.fetch( .fetch(
&gdev::storage().authority_members().online_authorities(), &runtime::storage().authority_members().online_authorities(),
Some(parent_hash), Some(parent_hash),
) )
.await? .await?
...@@ -124,7 +124,9 @@ pub async fn online(client: Client, args: &Args) -> Result<()> { ...@@ -124,7 +124,9 @@ pub async fn online(client: Client, args: &Args) -> Result<()> {
let incoming_authorities = client let incoming_authorities = client
.storage() .storage()
.fetch( .fetch(
&gdev::storage().authority_members().incoming_authorities(), &runtime::storage()
.authority_members()
.incoming_authorities(),
Some(parent_hash), Some(parent_hash),
) )
.await? .await?
...@@ -144,7 +146,9 @@ pub async fn online(client: Client, args: &Args) -> Result<()> { ...@@ -144,7 +146,9 @@ pub async fn online(client: Client, args: &Args) -> Result<()> {
let outgoing_authorities = client let outgoing_authorities = client
.storage() .storage()
.fetch( .fetch(
&gdev::storage().authority_members().outgoing_authorities(), &runtime::storage()
.authority_members()
.outgoing_authorities(),
Some(parent_hash), Some(parent_hash),
) )
.await? .await?
...@@ -164,41 +168,12 @@ pub async fn online(client: Client, args: &Args) -> Result<()> { ...@@ -164,41 +168,12 @@ pub async fn online(client: Client, args: &Args) -> Result<()> {
Ok(()) Ok(())
} }
/// emit a new smith cert from signer's identity to target identity
pub async fn emit_cert(args: Args, receiver: u32) -> Result<()> {
// issuer key
let pair = get_keys(
args.secret_format,
&args.address,
&args.secret,
NeededKeys::Secret,
)?
.1
.unwrap();
// connect to client
let client = Client::from_url(&args.url).await.unwrap();
// get issuer index
let issuer = commands::identity::get_idty_index_by_account_id(
client.clone(),
&AccountId32::from(pair.public()),
)
.await?
.ok_or(anyhow!("can not certify if not member"))?;
// submit and track certification
cert(client, pair, issuer, receiver).await?;
Ok(())
}
/// submit a certification and track progress /// submit a certification and track progress
async fn cert(client: Client, pair: Pair, issuer: u32, receiver: u32) -> Result<()> { pub async fn cert(client: Client, pair: Pair, issuer: u32, receiver: u32) -> Result<()> {
let mut progress = client let mut progress = client
.tx() .tx()
.sign_and_submit_then_watch( .sign_and_submit_then_watch(
&gdev::tx().smith_cert().add_cert(issuer, receiver), &runtime::tx().smith_cert().add_cert(issuer, receiver),
&PairSigner::new(pair), &PairSigner::new(pair),
BaseExtrinsicParamsBuilder::new(), BaseExtrinsicParamsBuilder::new(),
) )
...@@ -222,8 +197,8 @@ async fn cert(client: Client, pair: Pair, issuer: u32, receiver: u32) -> Result< ...@@ -222,8 +197,8 @@ async fn cert(client: Client, pair: Pair, issuer: u32, receiver: u32) -> Result<
// get the block events and return if ExtrinsicFailed // get the block events and return if ExtrinsicFailed
let events = in_block.wait_for_success().await?; let events = in_block.wait_for_success().await?;
// look for the expected event // look for the expected event
let new_cert_event = events.find_first::<gdev::smith_cert::events::NewCert>()?; let new_cert_event = events.find_first::<runtime::smith_cert::events::NewCert>()?;
let renew_cert_event = events.find_first::<gdev::smith_cert::events::RenewedCert>()?; let renew_cert_event = events.find_first::<runtime::smith_cert::events::RenewedCert>()?;
if let Some(event) = new_cert_event { if let Some(event) = new_cert_event {
println!("{event:?}"); println!("{event:?}");
......
use crate::{gdev, Client}; use crate::*;
use anyhow::Result; use anyhow::Result;
use sp_core::{crypto::AccountId32, sr25519::Pair}; use sp_core::{crypto::AccountId32, sr25519::Pair};
...@@ -8,7 +8,7 @@ pub async fn set_key(pair: Pair, client: Client, new_key: AccountId32) -> Result ...@@ -8,7 +8,7 @@ pub async fn set_key(pair: Pair, client: Client, new_key: AccountId32) -> Result
client client
.tx() .tx()
.sign_and_submit_then_watch( .sign_and_submit_then_watch(
&gdev::tx().sudo().set_key(new_key.into()), &runtime::tx().sudo().set_key(new_key.into()),
&PairSigner::new(pair), &PairSigner::new(pair),
BaseExtrinsicParamsBuilder::new(), BaseExtrinsicParamsBuilder::new(),
) )
......
use crate::{gdev, Client}; use crate::*;
use anyhow::Result; 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::RuntimeCall; #[cfg(any(feature = "dev", feature = "gdev"))] // find how to get runtime calls
type BalancesCall = gdev::runtime_types::pallet_balances::pallet::Call; type Call = runtime::runtime_types::gdev_runtime::RuntimeCall;
type BalancesCall = runtime::runtime_types::pallet_balances::pallet::Call;
pub async fn transfer( pub async fn transfer(
pair: Pair, pair: Pair,
...@@ -18,7 +19,7 @@ pub async fn transfer( ...@@ -18,7 +19,7 @@ pub async fn transfer(
client client
.tx() .tx()
.sign_and_submit_then_watch( .sign_and_submit_then_watch(
&gdev::tx().balances().transfer(dest.into(), balance), &runtime::tx().balances().transfer(dest.into(), balance),
&PairSigner::new(pair), &PairSigner::new(pair),
BaseExtrinsicParamsBuilder::new(), BaseExtrinsicParamsBuilder::new(),
) )
...@@ -27,7 +28,7 @@ pub async fn transfer( ...@@ -27,7 +28,7 @@ pub async fn transfer(
client client
.tx() .tx()
.sign_and_submit_then_watch( .sign_and_submit_then_watch(
&gdev::tx() &runtime::tx()
.balances() .balances()
.transfer_keep_alive(dest.into(), balance), .transfer_keep_alive(dest.into(), balance),
&PairSigner::new(pair), &PairSigner::new(pair),
...@@ -60,7 +61,7 @@ pub async fn transfer_multiple( ...@@ -60,7 +61,7 @@ pub async fn transfer_multiple(
client client
.tx() .tx()
.sign_and_submit_then_watch( .sign_and_submit_then_watch(
&gdev::tx().utility().batch(transactions), &runtime::tx().utility().batch(transactions),
&PairSigner::new(pair.clone()), &PairSigner::new(pair.clone()),
BaseExtrinsicParamsBuilder::new(), BaseExtrinsicParamsBuilder::new(),
) )
......
...@@ -14,11 +14,12 @@ pub enum NeededKeys { ...@@ -14,11 +14,12 @@ pub enum NeededKeys {
Secret, Secret,
} }
#[derive(Clone, Copy, Debug, Eq, PartialEq)] #[derive(Clone, Copy, Debug, Eq, PartialEq, Default)]
pub enum SecretFormat { pub enum SecretFormat {
/// Raw 32B seed /// Raw 32B seed
Seed, Seed,
/// Substrate secret key or BIP39 mnemonic (optionally followed by derivation path) /// Substrate secret key or BIP39 mnemonic (optionally followed by derivation path)
#[default]
Substrate, Substrate,
} }
......
...@@ -3,25 +3,28 @@ mod commands; ...@@ -3,25 +3,28 @@ mod commands;
mod indexer; mod indexer;
mod keys; mod keys;
use keys::*;
use anyhow::Result;
use clap::Parser; use clap::Parser;
use codec::Encode; use codec::Encode;
use keys::*;
use sp_core::sr25519::Pair;
use sp_core::H256; use sp_core::H256;
#[cfg(feature = "dev")]
#[subxt::subxt(runtime_metadata_path = "res/metadata.scale")] #[subxt::subxt(runtime_metadata_path = "res/metadata.scale")]
pub mod gdev {} pub mod runtime {}
pub type Client = subxt::OnlineClient<GdevConfig>; 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 enum GdevConfig {} pub enum Runtime {}
impl subxt::config::Config for GdevConfig { impl subxt::config::Config for Runtime {
type Index = u32; type Index = u32;
type BlockNumber = u32; type BlockNumber = u32;
type Hash = sp_core::H256; type Hash = sp_core::H256;
type Hashing = subxt::ext::sp_runtime::traits::BlakeTwo256; type Hashing = subxt::ext::sp_runtime::traits::BlakeTwo256;
type AccountId = subxt::ext::sp_runtime::AccountId32; type AccountId = AccountId;
type Address = subxt::ext::sp_runtime::MultiAddress<Self::AccountId, u32>; type Address = subxt::ext::sp_runtime::MultiAddress<Self::AccountId, u32>;
type Header = subxt::ext::sp_runtime::generic::Header< type Header = subxt::ext::sp_runtime::generic::Header<
Self::BlockNumber, Self::BlockNumber,
...@@ -49,7 +52,7 @@ impl From<u64> for Tip { ...@@ -49,7 +52,7 @@ impl From<u64> for Tip {
} }
} }
#[derive(Parser, Debug)] #[derive(Clone, Parser, Debug, Default)]
#[clap(author, version, about, long_about = None)] #[clap(author, version, about, long_about = None)]
pub struct Args { pub struct Args {
#[clap(subcommand)] #[clap(subcommand)]
...@@ -76,20 +79,144 @@ pub struct Args { ...@@ -76,20 +79,144 @@ pub struct Args {
url: String, url: String,
} }
#[derive(Debug, clap::Subcommand)] /// 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>,
}
// implement helper functions for Data
impl Data {
/// constructor
pub fn new(args: Args) -> Self {
Self {
args,
..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.clone().unwrap()
}
// --- 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)
}
}
/// 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)]
pub enum Subcommand { pub enum Subcommand {
/// Confirm an identity // TODO flodef
/// #[default]
/// To be called by the certified not-yet-member account, to become member. GetBalance,
ConfirmIdentity {
name: String,
},
/// Create and certify an identity /// Create and certify an identity
/// ///
/// Caller must be member, and the target account must exist. /// Caller must be member, and the target account must exist.
CreateIdentity { CreateIdentity {
target: sp_core::crypto::AccountId32, target: sp_core::crypto::AccountId32,
}, },
/// Confirm an identity
///
/// To be called by the certified not-yet-member account, to become member.
ConfirmIdentity {
name: String,
},
CreateOneshot { CreateOneshot {
balance: u64, balance: u64,
dest: sp_core::crypto::AccountId32, dest: sp_core::crypto::AccountId32,
...@@ -189,54 +316,25 @@ pub enum Subcommand { ...@@ -189,54 +316,25 @@ pub enum Subcommand {
} }
#[tokio::main(flavor = "current_thread")] #[tokio::main(flavor = "current_thread")]
async fn main() -> Result<()> { async fn main() -> Result<(), GcliError> {
env_logger::init(); env_logger::init();
let args = Args::parse(); let args = Args::parse();
let mut data = Data::new(args.clone());
/*if let Some(account_id) = &account_id {
println!("Account address: {account_id}");
}*/
/*if let Some(account_id) = &account_id {
let account = client
.storage()
.fetch(&gdev::storage().system().account(account_id), None)
.await?
.expect("Cannot fetch account");
logs::info!("Account free balance: {}", account.data.free);
}*/
match args.subcommand { match args.subcommand {
Subcommand::ConfirmIdentity { name } => { Subcommand::GetBalance => {}
commands::identity::confirm_identity(
get_keys(
args.secret_format,
&args.address,
&args.secret,
NeededKeys::Secret,
)?
.1
.unwrap(),
Client::from_url(&args.url).await.unwrap(),
name,
)
.await?
}
Subcommand::CreateIdentity { target } => { Subcommand::CreateIdentity { target } => {
commands::identity::create_identity( data = data.build_client().await.build_keypair();
get_keys( let progress =
args.secret_format, commands::identity::create_identity(data.keypair(), data.client(), target).await?;
&args.address, track_progress(progress).await?
&args.secret, }
NeededKeys::Secret, Subcommand::ConfirmIdentity { name } => {
)? data = data.build_client().await.build_keypair();
.1 let progress =
.unwrap(), commands::identity::confirm_identity(data.keypair(), data.client(), name).await?;
Client::from_url(&args.url).await.unwrap(), track_progress(progress).await?
target,
)
.await?
} }
Subcommand::CreateOneshot { balance, dest } => { Subcommand::CreateOneshot { balance, dest } => {
commands::oneshot::create_oneshot_account( commands::oneshot::create_oneshot_account(
...@@ -419,7 +517,15 @@ async fn main() -> Result<()> { ...@@ -419,7 +517,15 @@ async fn main() -> Result<()> {
) )
.await? .await?
} }
Subcommand::SmithCert { to } => commands::smith::emit_cert(args, to).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?
}
Subcommand::TechMembers => { Subcommand::TechMembers => {
commands::collective::technical_committee_members( commands::collective::technical_committee_members(
Client::from_url(&args.url).await.unwrap(), Client::from_url(&args.url).await.unwrap(),
......
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