Skip to content
Snippets Groups Projects
Commit 6ccda071 authored by Hugo Trentesaux's avatar Hugo Trentesaux Committed by Hugo Trentesaux
Browse files

WIP refac to track tx progress

parent 31c17838
Branches
Tags
1 merge request!7Big refacto
...@@ -85,7 +85,7 @@ pub async fn technical_committee_vote( ...@@ -85,7 +85,7 @@ pub async fn technical_committee_vote(
proposal_hash: H256, proposal_hash: H256,
proposal_index: u32, proposal_index: u32,
vote: bool, vote: bool,
) -> Result<()> { ) -> Result<TxProgress, subxt::Error> {
client client
.tx() .tx()
.sign_and_submit_then_watch( .sign_and_submit_then_watch(
...@@ -95,7 +95,5 @@ pub async fn technical_committee_vote( ...@@ -95,7 +95,5 @@ pub async fn technical_committee_vote(
&PairSigner::new(pair), &PairSigner::new(pair),
BaseExtrinsicParamsBuilder::new(), BaseExtrinsicParamsBuilder::new(),
) )
.await?; .await
Ok(())
} }
...@@ -5,7 +5,6 @@ use crate::runtime::runtime_types::common_runtime::entities::IdtyData; ...@@ -5,7 +5,6 @@ use crate::runtime::runtime_types::common_runtime::entities::IdtyData;
use crate::runtime::runtime_types::pallet_identity::types::*; use crate::runtime::runtime_types::pallet_identity::types::*;
use crate::runtime::runtime_types::sp_core::sr25519::Signature; use crate::runtime::runtime_types::sp_core::sr25519::Signature;
use crate::runtime::runtime_types::sp_runtime::MultiSignature; use crate::runtime::runtime_types::sp_runtime::MultiSignature;
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;
use subxt::tx::{BaseExtrinsicParamsBuilder, PairSigner}; use subxt::tx::{BaseExtrinsicParamsBuilder, PairSigner};
...@@ -15,7 +14,7 @@ pub async fn get_identity( ...@@ -15,7 +14,7 @@ pub async fn get_identity(
mut account_id: Option<AccountId32>, mut account_id: Option<AccountId32>,
mut identity_id: Option<u32>, mut identity_id: Option<u32>,
mut username: Option<String>, mut username: Option<String>,
) -> Result<()> { ) -> Result<(), anyhow::Error> {
let client = data.client(); let client = data.client();
let indexer = data.indexer.clone(); let indexer = data.indexer.clone();
...@@ -73,7 +72,7 @@ pub async fn get_identity( ...@@ -73,7 +72,7 @@ pub async fn get_identity(
pub async fn get_idty_index_by_account_id( pub async fn get_idty_index_by_account_id(
client: &Client, client: &Client,
account_id: &AccountId32, account_id: &AccountId32,
) -> Result<Option<u32>> { ) -> Result<Option<u32>, anyhow::Error> {
Ok(client Ok(client
.storage() .storage()
.fetch( .fetch(
...@@ -86,7 +85,7 @@ pub async fn get_idty_index_by_account_id( ...@@ -86,7 +85,7 @@ pub async fn get_idty_index_by_account_id(
pub async fn get_identity_by_index( pub async fn get_identity_by_index(
client: &Client, client: &Client,
idty_index: u32, idty_index: u32,
) -> Result<Option<IdtyValue<u32, AccountId32, IdtyData>>> { ) -> Result<Option<IdtyValue<u32, AccountId32, IdtyData>>, anyhow::Error> {
Ok(client Ok(client
.storage() .storage()
.fetch(&runtime::storage().identity().identities(idty_index), None) .fetch(&runtime::storage().identity().identities(idty_index), None)
......
use crate::*; use crate::*;
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};
...@@ -9,7 +8,7 @@ pub async fn create_oneshot_account( ...@@ -9,7 +8,7 @@ pub async fn create_oneshot_account(
client: &Client, client: &Client,
balance: u64, balance: u64,
dest: AccountId32, dest: AccountId32,
) -> Result<()> { ) -> Result<TxProgress, subxt::Error> {
client client
.tx() .tx()
.sign_and_submit_then_watch( .sign_and_submit_then_watch(
...@@ -19,9 +18,7 @@ pub async fn create_oneshot_account( ...@@ -19,9 +18,7 @@ pub async fn create_oneshot_account(
&PairSigner::new(pair), &PairSigner::new(pair),
BaseExtrinsicParamsBuilder::new(), BaseExtrinsicParamsBuilder::new(),
) )
.await?; .await
Ok(())
} }
pub async fn consume_oneshot_account( pub async fn consume_oneshot_account(
...@@ -29,7 +26,7 @@ pub async fn consume_oneshot_account( ...@@ -29,7 +26,7 @@ pub async fn consume_oneshot_account(
client: &Client, client: &Client,
dest: AccountId32, dest: AccountId32,
dest_oneshot: bool, dest_oneshot: bool,
) -> Result<()> { ) -> Result<TxProgress, subxt::Error> {
let number = client let number = client
.storage() .storage()
.fetch(&runtime::storage().system().number(), None) .fetch(&runtime::storage().system().number(), None)
...@@ -53,9 +50,7 @@ pub async fn consume_oneshot_account( ...@@ -53,9 +50,7 @@ pub async fn consume_oneshot_account(
&PairSigner::new(pair), &PairSigner::new(pair),
BaseExtrinsicParamsBuilder::new(), BaseExtrinsicParamsBuilder::new(),
) )
.await?; .await
Ok(())
} }
pub async fn consume_oneshot_account_with_remaining( pub async fn consume_oneshot_account_with_remaining(
...@@ -66,7 +61,7 @@ pub async fn consume_oneshot_account_with_remaining( ...@@ -66,7 +61,7 @@ pub async fn consume_oneshot_account_with_remaining(
dest_oneshot: bool, dest_oneshot: bool,
remaining_to: AccountId32, remaining_to: AccountId32,
remaining_to_oneshot: bool, remaining_to_oneshot: bool,
) -> Result<()> { ) -> Result<TxProgress, subxt::Error> {
let number = client let number = client
.storage() .storage()
.fetch(&runtime::storage().system().number(), None) .fetch(&runtime::storage().system().number(), None)
...@@ -102,12 +97,10 @@ pub async fn consume_oneshot_account_with_remaining( ...@@ -102,12 +97,10 @@ pub async fn consume_oneshot_account_with_remaining(
&PairSigner::new(pair), &PairSigner::new(pair),
BaseExtrinsicParamsBuilder::new(), BaseExtrinsicParamsBuilder::new(),
) )
.await?; .await
Ok(())
} }
pub async fn oneshot_account_balance(client: &Client, account: AccountId32) -> Result<()> { pub async fn oneshot_account_balance(client: &Client, account: AccountId32) -> Result<(), anyhow::Error> {
log::info!( log::info!(
"{}", "{}",
client client
......
use crate::*; use crate::*;
use anyhow::{anyhow, Result};
use sp_core::{crypto::AccountId32, sr25519::Pair, Pair as _}; use sp_core::{crypto::AccountId32, sr25519::Pair, Pair as _};
use std::ops::Deref; use std::ops::Deref;
use subxt::tx::{BaseExtrinsicParamsBuilder, PairSigner, TxStatus}; use subxt::tx::{BaseExtrinsicParamsBuilder, PairSigner, TxStatus};
type SessionKeys = [u8; 128]; type SessionKeys = [u8; 128];
pub async fn rotate_keys(client: &Client) -> Result<SessionKeys> { pub async fn rotate_keys(client: &Client) -> Result<SessionKeys, anyhow::Error> {
client client
.rpc() .rpc()
.rotate_keys() .rotate_keys()
...@@ -21,7 +20,7 @@ pub async fn set_session_keys( ...@@ -21,7 +20,7 @@ pub async fn set_session_keys(
pair: Pair, pair: Pair,
client: &Client, client: &Client,
session_keys: SessionKeys, session_keys: SessionKeys,
) -> Result<()> { ) -> Result<TxProgress, subxt::Error> {
client client
.tx() .tx()
.sign_and_submit_then_watch( .sign_and_submit_then_watch(
...@@ -31,17 +30,15 @@ pub async fn set_session_keys( ...@@ -31,17 +30,15 @@ pub async fn set_session_keys(
&PairSigner::new(pair), &PairSigner::new(pair),
BaseExtrinsicParamsBuilder::new(), BaseExtrinsicParamsBuilder::new(),
) )
.await?; .await
Ok(())
} }
pub async fn update_session_keys(pair: Pair, client: &Client) -> Result<()> { pub async fn update_session_keys(pair: Pair, client: &Client) -> Result<TxProgress, GcliError> {
let session_keys = rotate_keys(client).await?; let session_keys = rotate_keys(client).await?;
set_session_keys(pair, client, session_keys).await set_session_keys(pair, client, session_keys).await.map_err(|e| e.into())
} }
pub async fn go_online(pair: Pair, client: &Client) -> Result<()> { pub async fn go_online(pair: Pair, client: &Client) -> Result<TxProgress, GcliError> {
if client if client
.storage() .storage()
.fetch( .fetch(
...@@ -53,7 +50,7 @@ pub async fn go_online(pair: Pair, client: &Client) -> Result<()> { ...@@ -53,7 +50,7 @@ pub async fn go_online(pair: Pair, client: &Client) -> Result<()> {
.await? .await?
.is_none() .is_none()
{ {
return Err(anyhow!("This account has not set session keys!")); return Err(GcliError::Logic("This account has not set session keys!".to_string()));
} }
client client
...@@ -63,12 +60,10 @@ pub async fn go_online(pair: Pair, client: &Client) -> Result<()> { ...@@ -63,12 +60,10 @@ pub async fn go_online(pair: Pair, client: &Client) -> Result<()> {
&PairSigner::new(pair), &PairSigner::new(pair),
BaseExtrinsicParamsBuilder::new(), BaseExtrinsicParamsBuilder::new(),
) )
.await?; .await.map_err(|e| e.into())
Ok(())
} }
pub async fn go_offline(pair: Pair, client: &Client) -> Result<()> { pub async fn go_offline(pair: Pair, client: &Client) -> Result<TxProgress, subxt::Error> {
client client
.tx() .tx()
.sign_and_submit_then_watch( .sign_and_submit_then_watch(
...@@ -76,12 +71,10 @@ pub async fn go_offline(pair: Pair, client: &Client) -> Result<()> { ...@@ -76,12 +71,10 @@ pub async fn go_offline(pair: Pair, client: &Client) -> Result<()> {
&PairSigner::new(pair), &PairSigner::new(pair),
BaseExtrinsicParamsBuilder::new(), BaseExtrinsicParamsBuilder::new(),
) )
.await?; .await
Ok(())
} }
pub async fn online(data: &Data) -> Result<()> { pub async fn online(data: &Data) -> Result<(), anyhow::Error> {
let client = data.client(); let client = data.client();
let indexer = data.indexer.clone(); let indexer = data.indexer.clone();
...@@ -162,7 +155,7 @@ pub async fn online(data: &Data) -> Result<()> { ...@@ -162,7 +155,7 @@ pub async fn online(data: &Data) -> Result<()> {
} }
/// submit a certification and track progress /// submit a certification and track progress
pub 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<(), anyhow::Error> {
let mut progress = client let mut progress = client
.tx() .tx()
.sign_and_submit_then_watch( .sign_and_submit_then_watch(
......
use crate::*; use crate::*;
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};
pub async fn set_key(pair: Pair, client: &Client, new_key: AccountId32) -> Result<()> { pub async fn set_key(
pair: Pair,
client: &Client,
new_key: AccountId32,
) -> Result<TxProgress, subxt::Error> {
client client
.tx() .tx()
.sign_and_submit_then_watch( .sign_and_submit_then_watch(
...@@ -12,7 +15,5 @@ pub async fn set_key(pair: Pair, client: &Client, new_key: AccountId32) -> Resul ...@@ -12,7 +15,5 @@ pub async fn set_key(pair: Pair, client: &Client, new_key: AccountId32) -> Resul
&PairSigner::new(pair), &PairSigner::new(pair),
BaseExtrinsicParamsBuilder::new(), BaseExtrinsicParamsBuilder::new(),
) )
.await?; .await
Ok(())
} }
use crate::*; use crate::*;
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};
...@@ -14,7 +13,7 @@ pub async fn transfer( ...@@ -14,7 +13,7 @@ pub async fn transfer(
balance: u64, balance: u64,
dest: AccountId32, dest: AccountId32,
keep_alive: bool, keep_alive: bool,
) -> Result<()> { ) -> Result<TxProgress, subxt::Error> {
if keep_alive { if keep_alive {
client client
.tx() .tx()
...@@ -23,7 +22,7 @@ pub async fn transfer( ...@@ -23,7 +22,7 @@ pub async fn transfer(
&PairSigner::new(pair), &PairSigner::new(pair),
BaseExtrinsicParamsBuilder::new(), BaseExtrinsicParamsBuilder::new(),
) )
.await?; .await
} else { } else {
client client
.tx() .tx()
...@@ -34,10 +33,8 @@ pub async fn transfer( ...@@ -34,10 +33,8 @@ pub async fn transfer(
&PairSigner::new(pair), &PairSigner::new(pair),
BaseExtrinsicParamsBuilder::new(), BaseExtrinsicParamsBuilder::new(),
) )
.await?; .await
} }
Ok(())
} }
pub async fn transfer_multiple( pub async fn transfer_multiple(
...@@ -45,7 +42,7 @@ pub async fn transfer_multiple( ...@@ -45,7 +42,7 @@ pub async fn transfer_multiple(
client: &Client, client: &Client,
amount: u64, amount: u64,
dests: Vec<AccountId32>, dests: Vec<AccountId32>,
) -> Result<()> { ) -> Result<TxProgress, subxt::Error> {
// build the list of transactions from the destination accounts // build the list of transactions from the destination accounts
let transactions: Vec<Call> = dests let transactions: Vec<Call> = dests
.into_iter() .into_iter()
...@@ -65,7 +62,5 @@ pub async fn transfer_multiple( ...@@ -65,7 +62,5 @@ pub async fn transfer_multiple(
&PairSigner::new(pair.clone()), &PairSigner::new(pair.clone()),
BaseExtrinsicParamsBuilder::new(), BaseExtrinsicParamsBuilder::new(),
) )
.await?; .await
Ok(())
} }
...@@ -5,6 +5,7 @@ mod data; ...@@ -5,6 +5,7 @@ mod data;
mod indexer; mod indexer;
mod keys; mod keys;
use anyhow::anyhow;
use clap::Parser; use clap::Parser;
use codec::Encode; use codec::Encode;
use data::*; use data::*;
...@@ -119,6 +120,8 @@ pub enum GcliError { ...@@ -119,6 +120,8 @@ pub enum GcliError {
Duniter(String), Duniter(String),
/// error coming from indexer /// error coming from indexer
Indexer(String), Indexer(String),
/// logic error (illegal operation or security)
Logic(String),
/// error coming from anyhow /// error coming from anyhow
Anyhow(anyhow::Error), Anyhow(anyhow::Error),
} }
...@@ -315,7 +318,7 @@ async fn main() -> Result<(), GcliError> { ...@@ -315,7 +318,7 @@ async fn main() -> Result<(), GcliError> {
} }
Subcommand::CreateOneshot { balance, dest } => { Subcommand::CreateOneshot { balance, dest } => {
data = data.build_client().await?; data = data.build_client().await?;
commands::oneshot::create_oneshot_account( let progress = commands::oneshot::create_oneshot_account(
get_keys( get_keys(
args.secret_format, args.secret_format,
&args.address, &args.address,
...@@ -328,11 +331,12 @@ async fn main() -> Result<(), GcliError> { ...@@ -328,11 +331,12 @@ async fn main() -> Result<(), GcliError> {
balance, balance,
dest, dest,
) )
.await? .await?;
track_progress(progress).await?;
} }
Subcommand::ConsumeOneshot { dest, dest_oneshot } => { Subcommand::ConsumeOneshot { dest, dest_oneshot } => {
data = data.build_client().await?; data = data.build_client().await?;
commands::oneshot::consume_oneshot_account( let progress = commands::oneshot::consume_oneshot_account(
get_keys( get_keys(
args.secret_format, args.secret_format,
&args.address, &args.address,
...@@ -345,7 +349,8 @@ async fn main() -> Result<(), GcliError> { ...@@ -345,7 +349,8 @@ async fn main() -> Result<(), GcliError> {
dest, dest,
dest_oneshot, dest_oneshot,
) )
.await? .await?;
track_progress(progress).await?;
} }
Subcommand::ConsumeOneshotWithRemaining { Subcommand::ConsumeOneshotWithRemaining {
balance, balance,
...@@ -355,7 +360,7 @@ async fn main() -> Result<(), GcliError> { ...@@ -355,7 +360,7 @@ async fn main() -> Result<(), GcliError> {
remaining_to_oneshot, remaining_to_oneshot,
} => { } => {
data = data.build_client().await?; data = data.build_client().await?;
commands::oneshot::consume_oneshot_account_with_remaining( let progress = commands::oneshot::consume_oneshot_account_with_remaining(
get_keys( get_keys(
args.secret_format, args.secret_format,
&args.address, &args.address,
...@@ -371,7 +376,8 @@ async fn main() -> Result<(), GcliError> { ...@@ -371,7 +376,8 @@ async fn main() -> Result<(), GcliError> {
remaining_to, remaining_to,
remaining_to_oneshot, remaining_to_oneshot,
) )
.await? .await?;
track_progress(progress).await?;
} }
Subcommand::Expire { blocks, sessions } => { Subcommand::Expire { blocks, sessions } => {
data = data.build_client().await?; data = data.build_client().await?;
...@@ -402,7 +408,7 @@ async fn main() -> Result<(), GcliError> { ...@@ -402,7 +408,7 @@ async fn main() -> Result<(), GcliError> {
} }
Subcommand::GoOffline => { Subcommand::GoOffline => {
data = data.build_client().await?; data = data.build_client().await?;
commands::smith::go_offline( let progress = commands::smith::go_offline(
get_keys( get_keys(
args.secret_format, args.secret_format,
&args.address, &args.address,
...@@ -413,11 +419,12 @@ async fn main() -> Result<(), GcliError> { ...@@ -413,11 +419,12 @@ async fn main() -> Result<(), GcliError> {
.unwrap(), .unwrap(),
data.client(), data.client(),
) )
.await? .await?;
track_progress(progress).await?;
} }
Subcommand::GoOnline => { Subcommand::GoOnline => {
data = data.build_client().await?; data = data.build_client().await?;
commands::smith::go_online( let progress = commands::smith::go_online(
get_keys( get_keys(
args.secret_format, args.secret_format,
&args.address, &args.address,
...@@ -428,7 +435,8 @@ async fn main() -> Result<(), GcliError> { ...@@ -428,7 +435,8 @@ async fn main() -> Result<(), GcliError> {
.unwrap(), .unwrap(),
data.client(), data.client(),
) )
.await? .await?;
track_progress(progress).await?;
} }
Subcommand::OneshotBalance { account } => { Subcommand::OneshotBalance { account } => {
data = data.build_client().await?; data = data.build_client().await?;
...@@ -476,7 +484,8 @@ async fn main() -> Result<(), GcliError> { ...@@ -476,7 +484,8 @@ async fn main() -> Result<(), GcliError> {
} }
Subcommand::SudoSetKey { new_key } => { Subcommand::SudoSetKey { new_key } => {
data = data.build_keypair().build_client().await?; data = data.build_keypair().build_client().await?;
commands::sudo::set_key(data.keypair(), data.client(), new_key).await? let progress = commands::sudo::set_key(data.keypair(), data.client(), new_key).await?;
track_progress(progress).await?;
} }
Subcommand::SmithCert { to } => { Subcommand::SmithCert { to } => {
data = data data = data
...@@ -502,7 +511,7 @@ async fn main() -> Result<(), GcliError> { ...@@ -502,7 +511,7 @@ async fn main() -> Result<(), GcliError> {
1 => true, 1 => true,
_ => panic!("Vote must be written 0 if you disagree, or 1 if you agree."), _ => panic!("Vote must be written 0 if you disagree, or 1 if you agree."),
}; };
commands::collective::technical_committee_vote( let progress = commands::collective::technical_committee_vote(
get_keys( get_keys(
args.secret_format, args.secret_format,
&args.address, &args.address,
...@@ -516,7 +525,8 @@ async fn main() -> Result<(), GcliError> { ...@@ -516,7 +525,8 @@ async fn main() -> Result<(), GcliError> {
index, index,
vote, vote,
) )
.await? .await?;
track_progress(progress).await?;
} }
Subcommand::Transfer { Subcommand::Transfer {
amount, amount,
...@@ -524,7 +534,7 @@ async fn main() -> Result<(), GcliError> { ...@@ -524,7 +534,7 @@ async fn main() -> Result<(), GcliError> {
keep_alive, keep_alive,
} => { } => {
data = data.build_client().await?; data = data.build_client().await?;
commands::transfer::transfer( let progress = commands::transfer::transfer(
get_keys( get_keys(
args.secret_format, args.secret_format,
&args.address, &args.address,
...@@ -538,11 +548,12 @@ async fn main() -> Result<(), GcliError> { ...@@ -538,11 +548,12 @@ async fn main() -> Result<(), GcliError> {
dest, dest,
keep_alive, keep_alive,
) )
.await? .await?;
track_progress(progress).await?;
} }
Subcommand::TransferMultiple { amount, dests } => { Subcommand::TransferMultiple { amount, dests } => {
data = data.build_client().await?; data = data.build_client().await?;
commands::transfer::transfer_multiple( let progress = commands::transfer::transfer_multiple(
get_keys( get_keys(
args.secret_format, args.secret_format,
&args.address, &args.address,
...@@ -555,11 +566,12 @@ async fn main() -> Result<(), GcliError> { ...@@ -555,11 +566,12 @@ async fn main() -> Result<(), GcliError> {
amount, amount,
dests, dests,
) )
.await? .await?;
track_progress(progress).await?;
} }
Subcommand::UpdateKeys => { Subcommand::UpdateKeys => {
data = data.build_client().await?; data = data.build_client().await?;
commands::smith::update_session_keys( let progress = commands::smith::update_session_keys(
get_keys( get_keys(
args.secret_format, args.secret_format,
&args.address, &args.address,
...@@ -570,8 +582,8 @@ async fn main() -> Result<(), GcliError> { ...@@ -570,8 +582,8 @@ async fn main() -> Result<(), GcliError> {
.unwrap(), .unwrap(),
data.client(), data.client(),
) )
.await .await?;
.unwrap() track_progress(progress).await?;
} }
Subcommand::RuntimeInfo => { Subcommand::RuntimeInfo => {
data = data.build_client().await?.fetch_system_properties().await?; data = data.build_client().await?.fetch_system_properties().await?;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment