Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper methode api.compose_extrsinic_offline #394

Merged
merged 9 commits into from
Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions examples/examples/benchmark_bulk_xt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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();
Expand Down
12 changes: 4 additions & 8 deletions examples/examples/compose_extrinsic_offline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -55,12 +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(),
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);

Expand Down
13 changes: 4 additions & 9 deletions examples/examples/custom_nonce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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.
Expand Down
10 changes: 2 additions & 8 deletions examples/examples/generic_extrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +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(),
"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);

Expand Down
3 changes: 1 addition & 2 deletions examples/examples/sudo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions src/extrinsic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
47 changes: 47 additions & 0 deletions src/extrinsic/offline_extrinsic.rs
Original file line number Diff line number Diff line change
@@ -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<Signer, Client, Params, Runtime> Api<Signer, Client, Params, Runtime>
where
Signer: Pair,
MultiSignature: From<Signer::Signature>,
MultiSigner: From<Signer::Public>,
Params: ExtrinsicParams<Runtime::Index, Runtime::Hash>,
Runtime: FrameSystemConfig,
Runtime::AccountId: From<Signer::Public>,
{
/// Wrapper around the `compose_extrinsic_offline!` macro to be less verbose.
pub fn compose_extrinsic_offline<Call: Encode + Clone>(
&self,
call: Call,
nonce: Runtime::Index,
) -> UncheckedExtrinsicV4<Call, Params::SignedExtra> {
match self.signer() {
Some(signer) => compose_extrinsic_offline!(signer, call, self.extrinsic_params(nonce)),
None => UncheckedExtrinsicV4 { signature: None, function: call },
}
}
}