Skip to content

Commit

Permalink
define new type for <.. as Trait>
Browse files Browse the repository at this point in the history
  • Loading branch information
haerdib committed Jan 10, 2023
1 parent da78380 commit 9b00858
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
19 changes: 13 additions & 6 deletions examples/examples/benchmark_bulk_xt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Pair, Signature, Runtime>;
// 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<Pair, Signature, Runtime>;

// 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<Signer> = <Signer as SignExtrinsic<AccountId>>::ExtrinsicAddress;

#[tokio::main]
async fn main() {
Expand All @@ -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>, Runtime>::new(client).unwrap();
api.set_signer(MyExtrinsicSigner::new(signer));
api.set_signer(ExtrinsicSigner::new(signer));

let recipient: <MyExtrinsicSigner as SignExtrinsic<AccountId>>::ExtrinsicAddress =
AccountKeyring::Bob.to_account_id().into();
let recipient: ExtrinsicAddressOf<ExtrinsicSigner> = 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
Expand Down
19 changes: 14 additions & 5 deletions examples/examples/sudo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Pair, Signature, Runtime>;
// 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<Pair, Signature, Runtime>;

// 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<Signer> = <Signer as SignExtrinsic<AccountId>>::ExtrinsicAddress;

#[tokio::main]
async fn main() {
Expand All @@ -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>, 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();
Expand All @@ -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: <MyExtrinsicSigner as SignExtrinsic<AccountId>>::ExtrinsicAddress = recipient.clone().into();
let recipients_extrinsic_address: ExtrinsicAddressOf<ExtrinsicSigner> =
recipient.clone().into();
let new_balance = recipient_balance + 100;
let call = compose_call!(
api.metadata(),
Expand Down
20 changes: 14 additions & 6 deletions testing/examples/author_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,30 @@ 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<Pair, Signature, Runtime>;
// 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<Pair, Signature, Runtime>;

// 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<Signer> = <Signer as SignExtrinsic<AccountId>>::ExtrinsicAddress;

#[tokio::main]
async fn main() {
// Setup
let client = JsonrpseeClient::with_default_url().unwrap();
let alice_pair = AccountKeyring::Alice.pair();
let mut api = Api::<_, _, AssetTipExtrinsicParams<Runtime>, Runtime>::new(client).unwrap();
api.set_signer(MyExtrinsicSigner::new(alice_pair));
api.set_signer(ExtrinsicSigner::new(alice_pair));

let bob: <MyExtrinsicSigner as SignExtrinsic<AccountId>>::ExtrinsicAddress =
AccountKeyring::Bob.to_account_id().into();
let bob: ExtrinsicAddressOf<ExtrinsicSigner> = AccountKeyring::Bob.to_account_id().into();

// Submit extrinisc.
let xt0 = api.balance_transfer(bob.clone().into(), 1000);
Expand Down

0 comments on commit 9b00858

Please sign in to comment.