From 338af85f47529afbc0cede6f3b4b8da11d67cc31 Mon Sep 17 00:00:00 2001 From: haerdib Date: Thu, 22 Dec 2022 13:43:14 +0100 Subject: [PATCH 1/7] add extrinsic offline compose --- src/extrinsic/mod.rs | 1 + src/extrinsic/offline_extrinsic.rs | 47 ++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 src/extrinsic/offline_extrinsic.rs diff --git a/src/extrinsic/mod.rs b/src/extrinsic/mod.rs index cf8df8a1a..d08ae5d31 100644 --- a/src/extrinsic/mod.rs +++ b/src/extrinsic/mod.rs @@ -20,6 +20,7 @@ pub mod balances; pub mod common; pub mod contracts; +pub mod offline_extrinsic; #[cfg(feature = "staking-xt")] pub mod staking; pub mod utility; diff --git a/src/extrinsic/offline_extrinsic.rs b/src/extrinsic/offline_extrinsic.rs new file mode 100644 index 000000000..e5f593d88 --- /dev/null +++ b/src/extrinsic/offline_extrinsic.rs @@ -0,0 +1,47 @@ +/* + Copyright 2019 Supercomputing Systems AG + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +*/ + +//! Helper function to easily create extrinsics offline (without getter calls to the node). +//! +use crate::Api; +use ac_compose_macros::compose_extrinsic_offline; +use ac_primitives::{ExtrinsicParams, FrameSystemConfig, UncheckedExtrinsicV4}; +use codec::Encode; +use sp_core::Pair; +use sp_runtime::{MultiSignature, MultiSigner}; + +impl Api +where + Signer: Pair, + MultiSignature: From, + MultiSigner: From, + Params: ExtrinsicParams, + Runtime: FrameSystemConfig, + Runtime::AccountId: From, +{ + /// Wrapper around the `compose_extrinsic_offline!` macro to be less verbose. + pub fn compose_extrinsic_offline( + &self, + call: Call, + nonce: Runtime::Index, + ) -> UncheckedExtrinsicV4 { + match self.signer() { + Some(signer) => compose_extrinsic_offline!(signer, call, self.extrinsic_params(nonce)), + None => UncheckedExtrinsicV4 { signature: None, function: call }, + } + } +} From 8ed9bba6a79e14aaacaffdb094f0677fe4e6d285 Mon Sep 17 00:00:00 2001 From: haerdib Date: Thu, 22 Dec 2022 13:46:49 +0100 Subject: [PATCH 2/7] fix example --- examples/examples/compose_extrinsic_offline.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/examples/compose_extrinsic_offline.rs b/examples/examples/compose_extrinsic_offline.rs index 4d4e0987b..904770148 100644 --- a/examples/examples/compose_extrinsic_offline.rs +++ b/examples/examples/compose_extrinsic_offline.rs @@ -55,9 +55,8 @@ async fn main() { let to = MultiAddress::Id(AccountKeyring::Bob.to_account_id()); // Compose the extrinsic. - #[allow(clippy::redundant_clone)] let xt: UncheckedExtrinsicV4<_, _> = compose_extrinsic_offline!( - api.signer().unwrap().clone(), + &api.signer().unwrap(), RuntimeCall::Balances(BalancesCall::transfer { dest: to.clone(), value: 42 }), api.extrinsic_params(alice_nonce) ); From 988df20735869c52cd0fc77d76920ebd023e63cb Mon Sep 17 00:00:00 2001 From: haerdib Date: Thu, 22 Dec 2022 13:52:32 +0100 Subject: [PATCH 3/7] simplify example --- examples/examples/compose_extrinsic_offline.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/examples/examples/compose_extrinsic_offline.rs b/examples/examples/compose_extrinsic_offline.rs index 904770148..18dcec4fa 100644 --- a/examples/examples/compose_extrinsic_offline.rs +++ b/examples/examples/compose_extrinsic_offline.rs @@ -20,8 +20,8 @@ use kitchensink_runtime::{BalancesCall, Header, Runtime, RuntimeCall}; use sp_keyring::AccountKeyring; use sp_runtime::{generic::Era, MultiAddress}; use substrate_api_client::{ - compose_extrinsic_offline, rpc::JsonrpseeClient, Api, AssetTipExtrinsicParams, - AssetTipExtrinsicParamsBuilder, GetHeader, SubmitAndWatch, UncheckedExtrinsicV4, XtStatus, + rpc::JsonrpseeClient, Api, AssetTipExtrinsicParams, AssetTipExtrinsicParamsBuilder, GetHeader, + SubmitAndWatch, XtStatus, }; #[tokio::main] @@ -55,11 +55,8 @@ async fn main() { let to = MultiAddress::Id(AccountKeyring::Bob.to_account_id()); // Compose the extrinsic. - let xt: UncheckedExtrinsicV4<_, _> = compose_extrinsic_offline!( - &api.signer().unwrap(), - RuntimeCall::Balances(BalancesCall::transfer { dest: to.clone(), value: 42 }), - api.extrinsic_params(alice_nonce) - ); + let call = RuntimeCall::Balances(BalancesCall::transfer { dest: to, value: 42 }); + let xt = api.compose_extrinsic_offline(call, alice_nonce); println!("[+] Composed Extrinsic:\n {:?}\n", xt); From 28d785c7d7d2ce2657df3d6beed877427f091d21 Mon Sep 17 00:00:00 2001 From: haerdib Date: Thu, 22 Dec 2022 13:56:12 +0100 Subject: [PATCH 4/7] simplfy examples --- examples/examples/benchmark_bulk_xt.rs | 17 ++++++----------- examples/examples/custom_nonce.rs | 13 ++++--------- 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/examples/examples/benchmark_bulk_xt.rs b/examples/examples/benchmark_bulk_xt.rs index 43919ec6c..f892b6af2 100644 --- a/examples/examples/benchmark_bulk_xt.rs +++ b/examples/examples/benchmark_bulk_xt.rs @@ -21,8 +21,7 @@ use kitchensink_runtime::{BalancesCall, Runtime, RuntimeCall}; use sp_keyring::AccountKeyring; use substrate_api_client::{ - compose_extrinsic_offline, rpc::JsonrpseeClient, Api, AssetTipExtrinsicParams, SubmitExtrinsic, - UncheckedExtrinsicV4, + rpc::JsonrpseeClient, Api, AssetTipExtrinsicParams, GenericAddress, SubmitExtrinsic, }; #[tokio::main] @@ -44,15 +43,11 @@ async fn main() { let first_nonce = nonce; while nonce < first_nonce + 500 { // compose the extrinsic with all the element - #[allow(clippy::redundant_clone)] - let xt: UncheckedExtrinsicV4<_, _> = compose_extrinsic_offline!( - api.signer().unwrap().clone(), - RuntimeCall::Balances(BalancesCall::transfer { - dest: GenericAddress::Id(to.clone()), - value: 1_000_000 - }), - api.extrinsic_params(nonce) - ); + let call = RuntimeCall::Balances(BalancesCall::transfer { + dest: GenericAddress::Id(to.clone()), + value: 1_000_000, + }); + let xt = api.compose_extrinsic_offline(call, nonce); println!("sending extrinsic with nonce {}", nonce); let _tx_hash = api.submit_extrinsic(xt.hex_encode()).unwrap(); diff --git a/examples/examples/custom_nonce.rs b/examples/examples/custom_nonce.rs index dfb93ebea..14f21f944 100644 --- a/examples/examples/custom_nonce.rs +++ b/examples/examples/custom_nonce.rs @@ -20,8 +20,8 @@ use kitchensink_runtime::{BalancesCall, Runtime, RuntimeCall}; use sp_keyring::AccountKeyring; use sp_runtime::{generic::Era, MultiAddress}; use substrate_api_client::{ - compose_extrinsic_offline, rpc::JsonrpseeClient, Api, AssetTipExtrinsicParams, - AssetTipExtrinsicParamsBuilder, GetHeader, SubmitAndWatch, UncheckedExtrinsicV4, XtStatus, + rpc::JsonrpseeClient, Api, AssetTipExtrinsicParams, AssetTipExtrinsicParamsBuilder, GetHeader, + SubmitAndWatch, XtStatus, }; #[tokio::main] @@ -55,13 +55,8 @@ async fn main() { let to = MultiAddress::Id(AccountKeyring::Bob.to_account_id()); // Create an extrinsic that should get included in the future pool due to a nonce that is too high. - #[allow(clippy::redundant_clone)] - let xt: UncheckedExtrinsicV4<_, _> = compose_extrinsic_offline!( - api.signer().unwrap().clone(), - RuntimeCall::Balances(BalancesCall::transfer { dest: to.clone(), value: 42 }), - api.extrinsic_params(alice_nonce + 1) - ); - + let call = RuntimeCall::Balances(BalancesCall::transfer { dest: to, value: 42 }); + let xt = api.compose_extrinsic_offline(call, alice_nonce + 1); println!("[+] Composed Extrinsic:\n {:?}\n", xt); // Send and watch extrinsic until InBlock. From aa979cbb6287fcb8073cab44cad132cbea18d94d Mon Sep 17 00:00:00 2001 From: haerdib Date: Thu, 22 Dec 2022 14:00:40 +0100 Subject: [PATCH 5/7] remove unneccessary clippy allowance --- examples/examples/generic_extrinsic.rs | 3 +-- examples/examples/sudo.rs | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/examples/examples/generic_extrinsic.rs b/examples/examples/generic_extrinsic.rs index 6000bbe58..773f2b7bc 100644 --- a/examples/examples/generic_extrinsic.rs +++ b/examples/examples/generic_extrinsic.rs @@ -39,9 +39,8 @@ async fn main() { // call Balances::transfer // the names are given as strings - #[allow(clippy::redundant_clone)] let xt: UncheckedExtrinsicV4<_, _> = compose_extrinsic!( - api.clone(), + &api, "Balances", "transfer", GenericAddress::Id(to), diff --git a/examples/examples/sudo.rs b/examples/examples/sudo.rs index 9e5fe55b2..c842ce461 100644 --- a/examples/examples/sudo.rs +++ b/examples/examples/sudo.rs @@ -39,9 +39,8 @@ async fn main() { let to = AccountKeyring::Bob.to_account_id(); // this call can only be called by sudo - #[allow(clippy::redundant_clone)] let call = compose_call!( - api.metadata().clone(), + api.metadata(), "Balances", "set_balance", GenericAddress::Id(to), From 27729ce79696afc9c0a591f3d018220d472a1b2d Mon Sep 17 00:00:00 2001 From: haerdib Date: Thu, 22 Dec 2022 14:02:32 +0100 Subject: [PATCH 6/7] fix comment header --- src/extrinsic/offline_extrinsic.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/extrinsic/offline_extrinsic.rs b/src/extrinsic/offline_extrinsic.rs index e5f593d88..b88806c81 100644 --- a/src/extrinsic/offline_extrinsic.rs +++ b/src/extrinsic/offline_extrinsic.rs @@ -16,7 +16,7 @@ */ //! Helper function to easily create extrinsics offline (without getter calls to the node). -//! + use crate::Api; use ac_compose_macros::compose_extrinsic_offline; use ac_primitives::{ExtrinsicParams, FrameSystemConfig, UncheckedExtrinsicV4}; From 0638fbe2114f7aa314a07e94b64b6662d88f2dcb Mon Sep 17 00:00:00 2001 From: haerdib Date: Thu, 22 Dec 2022 14:10:12 +0100 Subject: [PATCH 7/7] fmt --- examples/examples/generic_extrinsic.rs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/examples/examples/generic_extrinsic.rs b/examples/examples/generic_extrinsic.rs index 773f2b7bc..0182498de 100644 --- a/examples/examples/generic_extrinsic.rs +++ b/examples/examples/generic_extrinsic.rs @@ -39,13 +39,8 @@ async fn main() { // call Balances::transfer // the names are given as strings - let xt: UncheckedExtrinsicV4<_, _> = compose_extrinsic!( - &api, - "Balances", - "transfer", - GenericAddress::Id(to), - Compact(42_u128) - ); + let xt: UncheckedExtrinsicV4<_, _> = + compose_extrinsic!(&api, "Balances", "transfer", GenericAddress::Id(to), Compact(42_u128)); println!("[+] Composed Extrinsic:\n {:?}\n", xt);