diff --git a/examples/examples/benchmark_bulk_xt.rs b/examples/examples/benchmark_bulk_xt.rs index f810215c9..6780fbf08 100644 --- a/examples/examples/benchmark_bulk_xt.rs +++ b/examples/examples/benchmark_bulk_xt.rs @@ -22,8 +22,7 @@ use codec::Encode; 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] @@ -45,15 +44,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.encode()).unwrap(); diff --git a/examples/examples/compose_extrinsic_offline.rs b/examples/examples/compose_extrinsic_offline.rs index 3029d76c0..eacb5cd2d 100644 --- a/examples/examples/compose_extrinsic_offline.rs +++ b/examples/examples/compose_extrinsic_offline.rs @@ -21,8 +21,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] @@ -56,12 +56,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(), - 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); diff --git a/examples/examples/custom_nonce.rs b/examples/examples/custom_nonce.rs index 0f517a918..2471ff0fe 100644 --- a/examples/examples/custom_nonce.rs +++ b/examples/examples/custom_nonce.rs @@ -21,8 +21,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] @@ -56,13 +56,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. diff --git a/examples/examples/generic_extrinsic.rs b/examples/examples/generic_extrinsic.rs index bb8929905..46db9811e 100644 --- a/examples/examples/generic_extrinsic.rs +++ b/examples/examples/generic_extrinsic.rs @@ -40,14 +40,8 @@ async fn main() { // call Balances::transfer // the names are given as strings - #[allow(clippy::redundant_clone)] - let xt: UncheckedExtrinsicV4<_, _> = compose_extrinsic!( - api.clone(), - "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); diff --git a/examples/examples/sudo.rs b/examples/examples/sudo.rs index a8214fdfd..d7aa24a79 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), 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..b88806c81 --- /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 }, + } + } +}