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

Make UncheckedExtrinsicV4 generic #418

Merged
merged 6 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
23 changes: 4 additions & 19 deletions compose-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,8 @@ macro_rules! compose_extrinsic_offline {
($signer: expr,
$call: expr,
$params: expr) => {{
use $crate::{
primitives::{ExtrinsicParams, GenericAddress, SignedPayload, UncheckedExtrinsicV4},
sp_core::{crypto::Pair, Public},
sp_runtime::{generic::Era, traits::IdentifyAccount, MultiSigner},
use $crate::primitives::{
ExtrinsicParams, SignExtrinsic, SignedPayload, UncheckedExtrinsicV4,
};

let extra = $params.signed_extra();
Expand All @@ -76,14 +74,7 @@ macro_rules! compose_extrinsic_offline {

let signature = raw_payload.using_encoded(|payload| $signer.sign(payload));

let multi_signer: MultiSigner = $signer.public().into();

UncheckedExtrinsicV4::new_signed(
$call,
GenericAddress::from(multi_signer.into_account()),
signature.into(),
extra,
)
UncheckedExtrinsicV4::new_signed($call, $signer.extrinsic_address(), signature, extra)
}};
}

Expand All @@ -102,11 +93,8 @@ macro_rules! compose_extrinsic {
$call: expr
$(, $args: expr) *) => {
{
#[allow(unused_imports)] // For when extrinsic does not use Compact
use $crate::codec::Compact;
use $crate::log::debug;
use $crate::primitives::UncheckedExtrinsicV4;
use $crate::sp_runtime::generic::Era;

debug!("Composing generic extrinsic for module {:?} and call {:?}", $module, $call);
let call = $crate::compose_call!($api.metadata().clone(), $module, $call $(, ($args)) *);
Expand All @@ -117,10 +105,7 @@ macro_rules! compose_extrinsic {
$api.extrinsic_params($api.get_nonce().unwrap())
)
} else {
UncheckedExtrinsicV4 {
signature: None,
function: call.clone(),
}
UncheckedExtrinsicV4::new_unsigned(call.clone())
}
}
};
Expand Down
9 changes: 5 additions & 4 deletions examples/examples/batch_payout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
*/

use codec::{Decode, Encode};
use kitchensink_runtime::Runtime;
use kitchensink_runtime::{Runtime, Signature};
use pallet_staking::{ActiveEraInfo, Exposure};
use serde_json::Value;
use sp_keyring::AccountKeyring;
use sp_runtime::{app_crypto::Ss58Codec, AccountId32};
use substrate_api_client::{
rpc::JsonrpseeClient, Api, GetStorage, PlainTipExtrinsicParams, SubmitAndWatch, XtStatus,
rpc::JsonrpseeClient, Api, ExtrinsicSigner, GetStorage, PlainTipExtrinsicParams,
SubmitAndWatch, XtStatus,
};

const MAX_BATCHED_TRANSACTION: u32 = 9;
Expand Down Expand Up @@ -47,7 +48,7 @@ async fn main() {
let alice = AccountKeyring::Alice.pair();
let client = JsonrpseeClient::with_default_url().unwrap();
let mut api = Api::<_, _, PlainTipExtrinsicParams<Runtime>, Runtime>::new(client).unwrap();
api.set_signer(alice);
api.set_signer(ExtrinsicSigner::<_, Signature, Runtime>::new(alice));

// Give a valid validator account address, given one is westend chain validator account.
let account =
Expand Down Expand Up @@ -118,7 +119,7 @@ async fn main() {
pub fn get_last_reward(
account: &AccountId32,
api: &substrate_api_client::Api<
sp_core::sr25519::Pair,
ExtrinsicSigner<sp_core::sr25519::Pair, Signature, Runtime>,
JsonrpseeClient,
PlainTipExtrinsicParams<Runtime>,
Runtime,
Expand Down
22 changes: 17 additions & 5 deletions examples/examples/benchmark_bulk_xt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,24 @@
// run this against test node with
// > substrate-test-node --dev --execution native --ws-port 9979 -ltxpool=debug

use kitchensink_runtime::{BalancesCall, Runtime, RuntimeCall};
use kitchensink_runtime::{AccountId, BalancesCall, Runtime, RuntimeCall, Signature};
use sp_core::sr25519::Pair;
use sp_keyring::AccountKeyring;
use substrate_api_client::{
rpc::JsonrpseeClient, Api, AssetTipExtrinsicParams, GenericAddress, SubmitExtrinsic,
rpc::JsonrpseeClient, Api, AssetTipExtrinsicParams, ExtrinsicSigner as GenericExtrinsicSigner,
SignExtrinsic, SubmitExtrinsic,
};

// 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 makes
// the code better readable.
type ExtrinsicSigner = GenericExtrinsicSigner<Pair, Signature, Runtime>;

// To access the ExtrinsicAddress type of the Signer, we need to do 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() {
env_logger::init();
Expand All @@ -34,9 +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(signer);
api.set_signer(ExtrinsicSigner::new(signer));

let recipient = AccountKeyring::Bob.to_account_id();
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 All @@ -46,7 +58,7 @@ async fn main() {
while nonce < first_nonce + 500 {
// Compose a balance extrinsic.
let call = RuntimeCall::Balances(BalancesCall::transfer {
dest: GenericAddress::Id(recipient.clone()),
dest: recipient.clone(),
value: 1_000_000,
});
let xt = api.compose_extrinsic_offline(call, nonce);
Expand Down
8 changes: 4 additions & 4 deletions examples/examples/compose_extrinsic_offline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
//! This example shows how to use the compose_extrinsic_offline macro which generates an extrinsic
//! without asking the node for nonce and does not need to know the metadata

use kitchensink_runtime::{BalancesCall, Runtime, RuntimeCall};
use kitchensink_runtime::{BalancesCall, Runtime, RuntimeCall, Signature};
use sp_keyring::AccountKeyring;
use sp_runtime::{generic::Era, MultiAddress};
use substrate_api_client::{
rpc::JsonrpseeClient, Api, AssetTipExtrinsicParams, GenericAdditionalParams, GetHeader,
SubmitAndWatch, XtStatus,
rpc::JsonrpseeClient, Api, AssetTipExtrinsicParams, ExtrinsicSigner, GenericAdditionalParams,
GetHeader, SubmitAndWatch, XtStatus,
};

#[tokio::main]
Expand All @@ -38,7 +38,7 @@ 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(signer);
api.set_signer(ExtrinsicSigner::<_, Signature, Runtime>::new(signer));

// Information for Era for mortal transactions (online).
let last_finalized_header_hash = api.get_finalized_head().unwrap().unwrap();
Expand Down
8 changes: 4 additions & 4 deletions examples/examples/contract_instantiate_with_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
//! This example is community maintained and not CI tested, therefore it may not work as is.

use codec::Decode;
use kitchensink_runtime::Runtime;
use kitchensink_runtime::{AccountId, Runtime, Signature};
use sp_keyring::AccountKeyring;
use substrate_api_client::{
rpc::JsonrpseeClient, AccountId, Api, PlainTipExtrinsicParams, StaticEvent, SubmitAndWatch,
SubmitAndWatchUntilSuccess, XtStatus,
rpc::JsonrpseeClient, Api, ExtrinsicSigner, PlainTipExtrinsicParams, StaticEvent,
SubmitAndWatch, SubmitAndWatchUntilSuccess, SubscribeEvents, SubscribeFrameSystem, XtStatus,
};

#[allow(unused)]
Expand All @@ -45,7 +45,7 @@ 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::<_, _, PlainTipExtrinsicParams<Runtime>, Runtime>::new(client).unwrap();
api.set_signer(signer);
api.set_signer(ExtrinsicSigner::<_, Signature, Runtime>::new(signer));

println!("[+] Alice's Account Nonce is {}", api.get_nonce().unwrap());

Expand Down
8 changes: 4 additions & 4 deletions examples/examples/custom_nonce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
//! This example shows how to use the compose_extrinsic_offline macro which generates an extrinsic
//! without asking the node for nonce and does not need to know the metadata

use kitchensink_runtime::{BalancesCall, Runtime, RuntimeCall};
use kitchensink_runtime::{BalancesCall, Runtime, RuntimeCall, Signature};
use sp_keyring::AccountKeyring;
use sp_runtime::{generic::Era, MultiAddress};
use substrate_api_client::{
rpc::JsonrpseeClient, Api, AssetTipExtrinsicParams, Error, GenericAdditionalParams, GetHeader,
SubmitAndWatch, UnexpectedTxStatus, XtStatus,
rpc::JsonrpseeClient, Api, AssetTipExtrinsicParams, Error, ExtrinsicSigner,
GenericAdditionalParams, GetHeader, SubmitAndWatch, UnexpectedTxStatus, XtStatus,
};

#[tokio::main]
Expand All @@ -34,7 +34,7 @@ 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(signer);
api.set_signer(ExtrinsicSigner::<_, Signature, Runtime>::new(signer));

// Information for Era for mortal transactions.
let last_finalized_header_hash = api.get_finalized_head().unwrap().unwrap();
Expand Down
5 changes: 2 additions & 3 deletions examples/examples/event_callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use codec::Decode;
use kitchensink_runtime::Runtime;
use log::debug;
use sp_core::{sr25519, H256 as Hash};
use sp_core::H256 as Hash;
use substrate_api_client::{
rpc::{HandleSubscription, JsonrpseeClient},
Api, PlainTipExtrinsicParams, SubscribeFrameSystem,
Expand All @@ -35,8 +35,7 @@ async fn main() {

// Initialize the api.
let client = JsonrpseeClient::with_default_url().unwrap();
let api =
Api::<sr25519::Pair, _, PlainTipExtrinsicParams<Runtime>, Runtime>::new(client).unwrap();
let api = Api::<(), _, PlainTipExtrinsicParams<Runtime>, Runtime>::new(client).unwrap();

println!("Subscribe to events");
let mut subscription = api.subscribe_system_events().unwrap();
Expand Down
9 changes: 5 additions & 4 deletions examples/examples/event_error_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
*/

use codec::Decode;
use kitchensink_runtime::Runtime;
use kitchensink_runtime::{Runtime, Signature};
use sp_keyring::AccountKeyring;
use sp_runtime::{AccountId32 as AccountId, MultiAddress};
use substrate_api_client::{
rpc::JsonrpseeClient, Api, AssetTipExtrinsicParams, GetAccountInformation, StaticEvent,
SubmitAndWatchUntilSuccess,
rpc::JsonrpseeClient, Api, AssetTipExtrinsicParams, ExtrinsicSigner, GetAccountInformation,
Result, StaticEvent, SubmitAndWatch, SubmitAndWatchUntilSuccess, SubscribeEvents,
SubscribeFrameSystem, XtStatus,
};

#[derive(Decode)]
Expand All @@ -44,7 +45,7 @@ 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(alice_signer);
api.set_signer(ExtrinsicSigner::<_, Signature, Runtime>::new(alice_signer));

let alice = AccountKeyring::Alice.to_account_id();
let balance_of_alice = api.get_account_data(&alice).unwrap().unwrap().free;
Expand Down
10 changes: 5 additions & 5 deletions examples/examples/get_account_identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
//! Example to show how to get the account identity display name from the identity pallet.

use frame_support::traits::Currency;
use kitchensink_runtime::Runtime as KitchensinkRuntime;
use kitchensink_runtime::{Runtime as KitchensinkRuntime, Signature};
use pallet_identity::{Data, IdentityInfo, Registration};
use sp_core::{crypto::Pair, H256};
use sp_keyring::AccountKeyring;
use substrate_api_client::{
compose_extrinsic, rpc::JsonrpseeClient, Api, AssetTipExtrinsicParams, GetStorage,
SubmitAndWatch, UncheckedExtrinsicV4, XtStatus,
compose_extrinsic, rpc::JsonrpseeClient, Api, AssetTipExtrinsicParams, ExtrinsicSigner,
GetStorage, SubmitAndWatch, UncheckedExtrinsicV4, XtStatus,
};

type BalanceOf<T> = <<T as pallet_identity::Config>::Currency as Currency<
Expand All @@ -43,7 +43,7 @@ async fn main() {
let mut api =
Api::<_, _, AssetTipExtrinsicParams<KitchensinkRuntime>, KitchensinkRuntime>::new(client)
.unwrap();
api.set_signer(signer.clone());
api.set_signer(ExtrinsicSigner::<_, Signature, KitchensinkRuntime>::new(signer.clone()));

// Fill Identity storage.
let info = IdentityInfo::<MaxAdditionalFieldsOf<KitchensinkRuntime>> {
Expand All @@ -58,7 +58,7 @@ async fn main() {
twitter: Data::None,
};

let xt: UncheckedExtrinsicV4<_, _> =
let xt: UncheckedExtrinsicV4<_, _, _, _> =
compose_extrinsic!(&api, "Identity", "set_identity", Box::new(info.clone()));
println!("[+] Composed Extrinsic:\n {:?}\n", xt);

Expand Down
8 changes: 5 additions & 3 deletions examples/examples/get_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
//! Very simple example that shows how to get some simple storage values.

use frame_system::AccountInfo as GenericAccountInfo;
use kitchensink_runtime::Runtime;
use kitchensink_runtime::{Runtime, Signature};
use sp_keyring::AccountKeyring;
use substrate_api_client::{rpc::JsonrpseeClient, Api, GetStorage, PlainTipExtrinsicParams};
use substrate_api_client::{
rpc::JsonrpseeClient, Api, ExtrinsicSigner, GetStorage, PlainTipExtrinsicParams,
};

type IndexFor<T> = <T as frame_system::Config>::Index;
type AccountDataFor<T> = <T as frame_system::Config>::AccountData;
Expand Down Expand Up @@ -54,7 +56,7 @@ async fn main() {

// get Alice's AccountNonce with api.get_nonce()
let signer = AccountKeyring::Alice.pair();
api.set_signer(signer);
api.set_signer(ExtrinsicSigner::<_, Signature, Runtime>::new(signer));
println!("[+] Alice's Account Nonce is {}", api.get_nonce().unwrap());

// Get an vector of storage keys, numbering up to the given max keys and that start with the (optionally) given storage key prefix.
Expand Down
24 changes: 19 additions & 5 deletions examples/examples/sudo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,25 @@
//! module, whereas the desired module and call are supplied as a string.

use codec::Compact;
use kitchensink_runtime::Runtime;
use kitchensink_runtime::{AccountId, Runtime, Signature};
use sp_core::sr25519::Pair;
use sp_keyring::AccountKeyring;
use substrate_api_client::{
compose_call, compose_extrinsic, rpc::JsonrpseeClient, Api, AssetTipExtrinsicParams,
GenericAddress, GetAccountInformation, SubmitAndWatch, UncheckedExtrinsicV4, XtStatus,
ExtrinsicSigner as GenericExtrinsicSigner, GetAccountInformation, SignExtrinsic,
SubmitAndWatch, UncheckedExtrinsicV4, XtStatus,
};

// 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 makes
// the code better readable.
type ExtrinsicSigner = GenericExtrinsicSigner<Pair, Signature, Runtime>;

// To access the ExtrinsicAddress type of the Signer, we need to do 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() {
env_logger::init();
Expand All @@ -32,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(sudoer);
api.set_signer(ExtrinsicSigner::new(sudoer));

// Set the recipient of newly issued funds.
let recipient = AccountKeyring::Bob.to_account_id();
Expand All @@ -42,17 +54,19 @@ 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: ExtrinsicAddressOf<ExtrinsicSigner> =
recipient.clone().into();
let new_balance = recipient_balance + 100;
let call = compose_call!(
api.metadata(),
"Balances",
"set_balance",
GenericAddress::Id(recipient.clone()),
recipients_extrinsic_address,
Compact(new_balance),
Compact(new_balance)
);

let xt: UncheckedExtrinsicV4<_, _> = compose_extrinsic!(&api, "Sudo", "sudo", call);
let xt: UncheckedExtrinsicV4<_, _, _, _> = compose_extrinsic!(&api, "Sudo", "sudo", call);

// Send and watch extrinsic until in block.
let block_hash = api
Expand Down
8 changes: 4 additions & 4 deletions examples/examples/transfer_with_tungstenite_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@

//! Very simple example that shows how to use a predefined extrinsic from the extrinsic module.

use kitchensink_runtime::Runtime;
use kitchensink_runtime::{Runtime, Signature};
use sp_core::{
crypto::{Pair, Ss58Codec},
sr25519,
};
use sp_runtime::MultiAddress;
use substrate_api_client::{
rpc::TungsteniteRpcClient, Api, AssetTipExtrinsicParams, GetAccountInformation, SubmitAndWatch,
XtStatus,
rpc::TungsteniteRpcClient, Api, AssetTipExtrinsicParams, ExtrinsicSigner,
GetAccountInformation, SubmitAndWatch, XtStatus,
};

fn main() {
Expand All @@ -40,7 +40,7 @@ fn main() {
// Initialize api and set the signer (sender) that is used to sign the extrinsics.
let client = TungsteniteRpcClient::with_default_url(100);
let mut api = Api::<_, _, AssetTipExtrinsicParams<Runtime>, Runtime>::new(client).unwrap();
api.set_signer(alice.clone());
api.set_signer(ExtrinsicSigner::<_, Signature, Runtime>::new(alice.clone()));

// Retrieve bobs current balance.
let bob = sr25519::Public::from_ss58check("5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty")
Expand Down
Loading