From 9b008587f33df6c5efea5b2976ad540f2131e19a Mon Sep 17 00:00:00 2001 From: haerdib Date: Tue, 10 Jan 2023 14:53:19 +0100 Subject: [PATCH] define new type for <.. as Trait> --- examples/examples/benchmark_bulk_xt.rs | 19 +++++++++++++------ examples/examples/sudo.rs | 19 ++++++++++++++----- testing/examples/author_tests.rs | 20 ++++++++++++++------ 3 files changed, 41 insertions(+), 17 deletions(-) diff --git a/examples/examples/benchmark_bulk_xt.rs b/examples/examples/benchmark_bulk_xt.rs index 776d000f0..efc902177 100644 --- a/examples/examples/benchmark_bulk_xt.rs +++ b/examples/examples/benchmark_bulk_xt.rs @@ -22,11 +22,19 @@ use kitchensink_runtime::{AccountId, BalancesCall, Runtime, RuntimeCall, Signatu use sp_core::sr25519::Pair; use sp_keyring::AccountKeyring; use substrate_api_client::{ - rpc::JsonrpseeClient, Api, AssetTipExtrinsicParams, ExtrinsicSigner, SignExtrinsic, - SubmitExtrinsic, + rpc::JsonrpseeClient, Api, AssetTipExtrinsicParams, ExtrinsicSigner as GenericExtrinsicSigner, + SignExtrinsic, SubmitExtrinsic, }; -type MyExtrinsicSigner = ExtrinsicSigner; +// Define an extrinsic signer type which sets the generic types of the `GenericExtrinsicSigner`. +// This way, the types don't have to be reassigned with every usage of this type and make +// the code better readable. +type ExtrinsicSigner = GenericExtrinsicSigner; + +// To access the ExtrinsicAddress type of the Signer, we need to this via the trait `SignExtrinsic`. +// For better code readability, we define a simple type here and, at the same time, assign the +// AccountId type of the `SignExtrinsic` trait. +type ExtrinsicAddressOf = >::ExtrinsicAddress; #[tokio::main] async fn main() { @@ -38,10 +46,9 @@ async fn main() { // ! Careful: AssetTipExtrinsicParams is used here, because the substrate kitchensink runtime uses assets as tips. But for most // runtimes, the PlainTipExtrinsicParams needs to be used. let mut api = Api::<_, _, AssetTipExtrinsicParams, Runtime>::new(client).unwrap(); - api.set_signer(MyExtrinsicSigner::new(signer)); + api.set_signer(ExtrinsicSigner::new(signer)); - let recipient: >::ExtrinsicAddress = - AccountKeyring::Bob.to_account_id().into(); + let recipient: ExtrinsicAddressOf = AccountKeyring::Bob.to_account_id().into(); // We use a manual nonce input here, because otherwise the api retrieves the nonce via getter and needs // to wait for the response of the node (and the actual execution of the previous extrinsic). // But because we want to spam the node with extrinsic, we simple monotonically increase the nonce, without diff --git a/examples/examples/sudo.rs b/examples/examples/sudo.rs index edeb903d6..0039bb06e 100644 --- a/examples/examples/sudo.rs +++ b/examples/examples/sudo.rs @@ -22,11 +22,19 @@ use sp_core::sr25519::Pair; use sp_keyring::AccountKeyring; use substrate_api_client::{ compose_call, compose_extrinsic, rpc::JsonrpseeClient, Api, AssetTipExtrinsicParams, - ExtrinsicSigner, GetAccountInformation, SignExtrinsic, SubmitAndWatch, UncheckedExtrinsicV4, - XtStatus, + ExtrinsicSigner as GenericExtrinsicSigner, GetAccountInformation, SignExtrinsic, + SubmitAndWatch, UncheckedExtrinsicV4, XtStatus, }; -type MyExtrinsicSigner = ExtrinsicSigner; +// Define an extrinsic signer type which sets the generic types of the `GenericExtrinsicSigner`. +// This way, the types don't have to be reassigned with every usage of this type and make +// the code better readable. +type ExtrinsicSigner = GenericExtrinsicSigner; + +// To access the ExtrinsicAddress type of the ExtrinsicSigner, we need to access the trait `SignExtrinsic`. +// As this is very verbose, we define a simple type here and, at the same time, assign the +// AccountId type of the `SignExtrinsic` trait. +type ExtrinsicAddressOf = >::ExtrinsicAddress; #[tokio::main] async fn main() { @@ -36,7 +44,7 @@ async fn main() { let sudoer = AccountKeyring::Alice.pair(); let client = JsonrpseeClient::with_default_url().unwrap(); let mut api = Api::<_, _, AssetTipExtrinsicParams, Runtime>::new(client).unwrap(); - api.set_signer(MyExtrinsicSigner::new(sudoer)); + api.set_signer(ExtrinsicSigner::new(sudoer)); // Set the recipient of newly issued funds. let recipient = AccountKeyring::Bob.to_account_id(); @@ -46,7 +54,8 @@ async fn main() { println!("[+] Recipients's Free Balance is now {}\n", recipient_balance); // Compose a call that should only be executable via Sudo. - let recipients_extrinsic_address: >::ExtrinsicAddress = recipient.clone().into(); + let recipients_extrinsic_address: ExtrinsicAddressOf = + recipient.clone().into(); let new_balance = recipient_balance + 100; let call = compose_call!( api.metadata(), diff --git a/testing/examples/author_tests.rs b/testing/examples/author_tests.rs index 4cd71975a..fa1063859 100644 --- a/testing/examples/author_tests.rs +++ b/testing/examples/author_tests.rs @@ -21,11 +21,20 @@ use sp_keyring::AccountKeyring; use std::{thread, time::Duration}; use substrate_api_client::{ rpc::{HandleSubscription, JsonrpseeClient}, - Api, AssetTipExtrinsicParams, EventDetails, ExtrinsicSigner, SignExtrinsic, SubmitAndWatch, - SubmitAndWatchUntilSuccess, SubmitExtrinsic, TransactionStatus, XtStatus, + Api, AssetTipExtrinsicParams, EventDetails, ExtrinsicSigner as GenericExtrinsicSigner, + SignExtrinsic, SubmitAndWatch, SubmitAndWatchUntilSuccess, SubmitExtrinsic, TransactionStatus, + XtStatus, }; -type MyExtrinsicSigner = ExtrinsicSigner; +// Define an extrinsic signer type which sets the generic types of the `GenericExtrinsicSigner`. +// This way, the types don't have to be reassigned with every usage of this type and make +// the code better readable. +type ExtrinsicSigner = GenericExtrinsicSigner; + +// To access the ExtrinsicAddress type of the ExtrinsicSigner, we need to access the trait `SignExtrinsic`. +// As this is very verbose, we define a simple type here and, at the same time, assign the +// AccountId type of the `SignExtrinsic` trait. +type ExtrinsicAddressOf = >::ExtrinsicAddress; #[tokio::main] async fn main() { @@ -33,10 +42,9 @@ async fn main() { let client = JsonrpseeClient::with_default_url().unwrap(); let alice_pair = AccountKeyring::Alice.pair(); let mut api = Api::<_, _, AssetTipExtrinsicParams, Runtime>::new(client).unwrap(); - api.set_signer(MyExtrinsicSigner::new(alice_pair)); + api.set_signer(ExtrinsicSigner::new(alice_pair)); - let bob: >::ExtrinsicAddress = - AccountKeyring::Bob.to_account_id().into(); + let bob: ExtrinsicAddressOf = AccountKeyring::Bob.to_account_id().into(); // Submit extrinisc. let xt0 = api.balance_transfer(bob.clone().into(), 1000);