From 5d06cc17f285ab741c3e169cf757c77c134cbc19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Miko=C5=82ajczyk?= Date: Wed, 11 Jan 2023 10:19:54 +0100 Subject: [PATCH 1/3] API proposition --- aleph-client/src/connections.rs | 90 ++++++++++++++++++++++++++++----- 1 file changed, 78 insertions(+), 12 deletions(-) diff --git a/aleph-client/src/connections.rs b/aleph-client/src/connections.rs index b21c1b54f8..9af8f69c04 100644 --- a/aleph-client/src/connections.rs +++ b/aleph-client/src/connections.rs @@ -4,6 +4,7 @@ use anyhow::anyhow; use codec::Decode; use log::info; use subxt::{ + blocks::ExtrinsicEvents, ext::sp_core::Bytes, metadata::DecodeWithMetadata, rpc::RpcParams, @@ -13,7 +14,8 @@ use subxt::{ }; use crate::{ - api, sp_weights::weight_v2::Weight, AccountId, BlockHash, Call, KeyPair, SubxtClient, TxStatus, + api, sp_weights::weight_v2::Weight, AccountId, AlephConfig, BlockHash, Call, KeyPair, + SubxtClient, TxStatus, }; /// Capable of communicating with a live Aleph chain. @@ -115,10 +117,10 @@ pub trait SignedConnectionApi: ConnectionApi { /// Block hash of block where transaction was put or error /// # Examples /// ```ignore - /// let tx = api::tx() - /// .balances() - /// .transfer(MultiAddress::Id(dest), amount); - /// send_tx(tx, status).await + /// let tx = api::tx() + /// .balances() + /// .transfer(MultiAddress::Id(dest), amount); + /// send_tx(tx, status).await /// ``` async fn send_tx( &self, @@ -139,6 +141,42 @@ pub trait SignedConnectionApi: ConnectionApi { status: TxStatus, ) -> anyhow::Result; + /// Send a transaction to a chain. It waits for a given tx `status`. + /// * `tx` - encoded transaction payload + /// * `status` - a [`TxStatus`] for a tx to wait for + /// # Returns + /// In case `status` was either `InBlock` or `Finalized`, returns `ExtrinsicEvents` that + /// corresponds to the submitted `tx`. Otherwise, `None` is returned. + /// # Examples + /// ```ignore + /// let tx = api::tx() + /// .balances() + /// .transfer(MultiAddress::Id(dest), amount); + /// send_tx_and_get_events(tx, status) + /// .await? + /// .unwrap() + /// .find_first::() + /// ``` + async fn send_tx_and_get_events( + &self, + tx: Call, + status: TxStatus, + ) -> anyhow::Result>>; + + /// Send a transaction to a chain. It waits for a given tx `status`. + /// * `tx` - encoded transaction payload + /// * `params` - optional tx params e.g. tip + /// * `status` - a [`TxStatus`] of a tx to wait for + /// # Returns + /// In case `status` was either `InBlock` or `Finalized`, returns `ExtrinsicEvents` that + /// corresponds to the submitted `tx`. Otherwise, `None` is returned. + async fn send_tx_with_params_and_get_events( + &self, + tx: Call, + params: BaseExtrinsicParamsBuilder, + status: TxStatus, + ) -> anyhow::Result>>; + /// Returns account id which signs this connection fn account_id(&self) -> &AccountId; @@ -274,6 +312,29 @@ impl SignedConnectionApi for S { params: BaseExtrinsicParamsBuilder, status: TxStatus, ) -> anyhow::Result { + self.send_tx_with_params_and_get_events(tx, params, status) + .await + .map(|maybe_ee| { + // In case of Submitted hash does not mean anything + maybe_ee.map_or_else(|| BlockHash::from_low_u64_be(0), |ee| ee.block_hash()) + }) + } + + async fn send_tx_and_get_events( + &self, + tx: Call, + status: TxStatus, + ) -> anyhow::Result>> { + self.send_tx_with_params_and_get_events(tx, Default::default(), status) + .await + } + + async fn send_tx_with_params_and_get_events( + &self, + tx: Call, + params: BaseExtrinsicParamsBuilder, + status: TxStatus, + ) -> anyhow::Result>> { if let Some(details) = tx.validation_details() { info!(target:"aleph-client", "Sending extrinsic {}.{} with params: {:?}", details.pallet_name, details.call_name, params); } @@ -286,15 +347,20 @@ impl SignedConnectionApi for S { .await .map_err(|e| anyhow!("Failed to submit transaction: {:?}", e))?; - // In case of Submitted hash does not mean anything - let hash = match status { - TxStatus::InBlock => progress.wait_for_in_block().await?.block_hash(), - TxStatus::Finalized => progress.wait_for_finalized_success().await?.block_hash(), - TxStatus::Submitted => return Ok(BlockHash::from_low_u64_be(0)), + let ee = match status { + TxStatus::InBlock => { + progress + .wait_for_in_block() + .await? + .wait_for_success() + .await? + } + TxStatus::Finalized => progress.wait_for_finalized_success().await?, + TxStatus::Submitted => return Ok(None), }; - info!(target: "aleph-client", "tx included in block {:?}", hash); + info!(target: "aleph-client", "tx included in block {:?}", ee.block_hash()); - Ok(hash) + Ok(Some(ee)) } fn account_id(&self) -> &AccountId { From 836d46786ef6254809090acbb38b970a64e42bbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Miko=C5=82ajczyk?= Date: Wed, 11 Jan 2023 10:21:01 +0100 Subject: [PATCH 2/3] Bump --- aleph-client/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aleph-client/Cargo.toml b/aleph-client/Cargo.toml index 95961566c2..41268b2787 100644 --- a/aleph-client/Cargo.toml +++ b/aleph-client/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "aleph_client" # TODO bump major version when API stablize -version = "2.7.0" +version = "2.8.0" edition = "2021" license = "Apache 2.0" From fccffe1d93563e948b766fc168f8fa3cb7614d53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Miko=C5=82ajczyk?= Date: Wed, 11 Jan 2023 10:23:56 +0100 Subject: [PATCH 3/3] Export EE --- aleph-client/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aleph-client/src/lib.rs b/aleph-client/src/lib.rs index 74d232850f..77d83642fe 100644 --- a/aleph-client/src/lib.rs +++ b/aleph-client/src/lib.rs @@ -13,7 +13,7 @@ extern crate core; pub use contract_transcode; -pub use subxt::ext::sp_core::Pair; +pub use subxt::{blocks::ExtrinsicEvents, ext::sp_core::Pair}; use subxt::{ ext::sp_core::{ed25519, sr25519, H256}, tx::PairSigner,