Skip to content

Commit

Permalink
Update WithExtrinsicParams to use ExtrinsicSigner<Self> (#698)
Browse files Browse the repository at this point in the history
* wWithExtrinsicParams now uses ExtrinsicSigner directly

* simplify examples

* fix clippy

* fix clippy

* add test

* move test to cargo test

* fix clippy

* move test to chain atest

* add comment
  • Loading branch information
haerdib authored Dec 22, 2023
1 parent 9ee2cc8 commit 724f7c3
Show file tree
Hide file tree
Showing 20 changed files with 47 additions and 58 deletions.
2 changes: 1 addition & 1 deletion examples/examples/benchmark_bulk_xt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async fn main() {
let signer = AccountKeyring::Alice.pair();
let client = JsonrpseeClient::with_default_url().unwrap();
let mut api = Api::<AssetRuntimeConfig, _>::new(client).unwrap();
api.set_signer(ExtrinsicSigner::new(signer));
api.set_signer(signer.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
Expand Down
4 changes: 2 additions & 2 deletions examples/examples/check_extrinsic_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use sp_keyring::AccountKeyring;
use substrate_api_client::{
ac_node_api::EventDetails,
ac_primitives::{AssetRuntimeConfig, Config, ExtrinsicSigner},
ac_primitives::{AssetRuntimeConfig, Config},
extrinsic::BalancesExtrinsics,
rpc::JsonrpseeClient,
Api, GetAccountInformation, SubmitAndWatch, TransactionStatus, XtStatus,
Expand All @@ -37,7 +37,7 @@ async fn main() {
let alice_signer = AccountKeyring::Alice.pair();
let client = JsonrpseeClient::with_default_url().unwrap();
let mut api = Api::<AssetRuntimeConfig, _>::new(client).unwrap();
api.set_signer(ExtrinsicSigner::<AssetRuntimeConfig>::new(alice_signer));
api.set_signer(alice_signer.into());

let alice = AccountKeyring::Alice.to_account_id();
let balance_of_alice = api.get_account_data(&alice).unwrap().unwrap().free;
Expand Down
5 changes: 2 additions & 3 deletions examples/examples/contract_instantiate_with_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ use kitchensink_runtime::AccountId;
use sp_keyring::AccountKeyring;
use substrate_api_client::{
ac_compose_macros::primitives::AssetRuntimeConfig, ac_node_api::StaticEvent,
ac_primitives::ExtrinsicSigner, extrinsic::ContractsExtrinsics, rpc::JsonrpseeClient, Api,
SubmitAndWatch, XtStatus,
extrinsic::ContractsExtrinsics, rpc::JsonrpseeClient, Api, SubmitAndWatch, XtStatus,
};

// To test this example with CI we run it against the Substrate kitchensink node, which uses the asset pallet.
Expand Down Expand Up @@ -49,7 +48,7 @@ async fn main() {
let signer = AccountKeyring::Alice.pair();
let client = JsonrpseeClient::with_default_url().unwrap();
let mut api = Api::<AssetRuntimeConfig, _>::new(client).unwrap();
api.set_signer(ExtrinsicSigner::<_>::new(signer));
api.set_signer(signer.into());

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

Expand Down
4 changes: 2 additions & 2 deletions examples/examples/custom_nonce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use kitchensink_runtime::{BalancesCall, RuntimeCall};
use sp_keyring::AccountKeyring;
use sp_runtime::{generic::Era, MultiAddress};
use substrate_api_client::{
ac_primitives::{AssetRuntimeConfig, ExtrinsicSigner, GenericAdditionalParams},
ac_primitives::{AssetRuntimeConfig, GenericAdditionalParams},
rpc::JsonrpseeClient,
Api, Error, GetChainInfo, SubmitAndWatch, UnexpectedTxStatus, XtStatus,
};
Expand All @@ -38,7 +38,7 @@ async fn main() {
let signer = AccountKeyring::Alice.pair();
let client = JsonrpseeClient::with_default_url().unwrap();
let mut api = Api::<AssetRuntimeConfig, _>::new(client).unwrap();
api.set_signer(ExtrinsicSigner::<AssetRuntimeConfig>::new(signer));
api.set_signer(signer.into());

// Information for Era for mortal transactions.
let last_finalized_header_hash = api.get_finalized_head().unwrap().unwrap();
Expand Down
4 changes: 2 additions & 2 deletions examples/examples/get_account_identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use sp_core::{crypto::Pair, H256};
use sp_keyring::AccountKeyring;
use substrate_api_client::{
ac_compose_macros::compose_extrinsic,
ac_primitives::{AssetRuntimeConfig, ExtrinsicSigner, UncheckedExtrinsicV4},
ac_primitives::{AssetRuntimeConfig, UncheckedExtrinsicV4},
rpc::JsonrpseeClient,
Api, GetStorage, SubmitAndWatch, XtStatus,
};
Expand All @@ -46,7 +46,7 @@ async fn main() {
let client = JsonrpseeClient::with_default_url().unwrap();
let signer = AccountKeyring::Alice.pair();
let mut api = Api::<AssetRuntimeConfig, _>::new(client).unwrap();
api.set_signer(ExtrinsicSigner::<AssetRuntimeConfig>::new(signer.clone()));
api.set_signer(signer.clone().into());

// Fill Identity storage.
let info = IdentityInfo::<MaxAdditionalFields> {
Expand Down
4 changes: 2 additions & 2 deletions examples/examples/get_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use kitchensink_runtime::AccountId;
use pallet_staking::Exposure;
use sp_keyring::AccountKeyring;
use substrate_api_client::{
ac_primitives::{AssetRuntimeConfig, Config, ExtrinsicSigner},
ac_primitives::{AssetRuntimeConfig, Config},
rpc::JsonrpseeClient,
Api, GetAccountInformation, GetStorage,
};
Expand Down Expand Up @@ -66,7 +66,7 @@ async fn main() {

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

println!(
Expand Down
5 changes: 2 additions & 3 deletions examples/examples/runtime_update_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use sp_keyring::AccountKeyring;
use sp_weights::Weight;
use substrate_api_client::{
ac_compose_macros::{compose_call, compose_extrinsic},
ac_primitives::{AssetRuntimeConfig, Config, ExtrinsicSigner as GenericExtrinsicSigner},
ac_primitives::{AssetRuntimeConfig, Config},
api_client::UpdateRuntime,
rpc::JsonrpseeClient,
rpc_api::RuntimeUpdateDetector,
Expand All @@ -25,7 +25,6 @@ use substrate_api_client::{
use tokio::select;
use tokio_util::sync::CancellationToken;

type ExtrinsicSigner = GenericExtrinsicSigner<AssetRuntimeConfig>;
type Hash = <AssetRuntimeConfig as Config>::Hash;

#[cfg(feature = "sync-examples")]
Expand Down Expand Up @@ -64,7 +63,7 @@ async fn main() {
let client = JsonrpseeClient::with_default_url().unwrap();
let mut api = Api::<AssetRuntimeConfig, _>::new(client).await.unwrap();
let sudoer = AccountKeyring::Alice.pair();
api.set_signer(ExtrinsicSigner::new(sudoer));
api.set_signer(sudoer.into());

let subscription = api.subscribe_events().await.unwrap();
let mut update_detector: RuntimeUpdateDetector<Hash, JsonrpseeClient> =
Expand Down
5 changes: 2 additions & 3 deletions examples/examples/runtime_update_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ use sp_weights::Weight;
use std::{sync::Arc, thread};
use substrate_api_client::{
ac_compose_macros::{compose_call, compose_extrinsic},
ac_primitives::{AssetRuntimeConfig, Config, ExtrinsicSigner as GenericExtrinsicSigner},
ac_primitives::{AssetRuntimeConfig, Config},
api_client::UpdateRuntime,
rpc::JsonrpseeClient,
rpc_api::RuntimeUpdateDetector,
Api, SubmitAndWatch, SubscribeEvents, XtStatus,
};

type ExtrinsicSigner = GenericExtrinsicSigner<AssetRuntimeConfig>;
type Hash = <AssetRuntimeConfig as Config>::Hash;

#[cfg(not(feature = "sync-examples"))]
Expand Down Expand Up @@ -65,7 +64,7 @@ async fn main() {
let client = JsonrpseeClient::with_default_url().unwrap();
let mut api = Api::<AssetRuntimeConfig, _>::new(client).unwrap();
let sudoer = AccountKeyring::Alice.pair();
api.set_signer(ExtrinsicSigner::new(sudoer));
api.set_signer(sudoer.into());

let subscription = api.subscribe_events().unwrap();
let cancellation = Arc::new(AtomicBool::new(false));
Expand Down
4 changes: 2 additions & 2 deletions examples/examples/staking_batch_payout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use pallet_staking::{ActiveEraInfo, Exposure};
use sp_keyring::AccountKeyring;
use sp_runtime::{app_crypto::Ss58Codec, AccountId32};
use substrate_api_client::{
ac_primitives::{AssetRuntimeConfig, ExtrinsicSigner},
ac_primitives::AssetRuntimeConfig,
extrinsic::{StakingExtrinsics, UtilityExtrinsics},
rpc::JsonrpseeClient,
Api, GetStorage, SubmitAndWatch, XtStatus,
Expand Down Expand Up @@ -55,7 +55,7 @@ async fn main() {
let alice = AccountKeyring::Alice.pair();
let client = JsonrpseeClient::with_default_url().unwrap();
let mut api = Api::<AssetRuntimeConfig, _>::new(client).unwrap();
api.set_signer(ExtrinsicSigner::<AssetRuntimeConfig>::new(alice));
api.set_signer(alice.into());

// Give a valid validator account address. In the kitchinsink runtime, this is Alice.
let validator_account = AccountKeyring::Alice.to_account_id();
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/sudo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async fn main() {
let sudoer = AccountKeyring::Alice.pair();
let client = JsonrpseeClient::with_default_url().unwrap();
let mut api = Api::<AssetRuntimeConfig, _>::new(client).unwrap();
api.set_signer(ExtrinsicSigner::new(sudoer));
api.set_signer(sudoer.into());

// Set the recipient of newly issued funds.
let recipient = AccountKeyring::Bob.to_account_id();
Expand Down
6 changes: 2 additions & 4 deletions examples/examples/transfer_with_tungstenite_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ use sp_core::{
};
use sp_runtime::MultiAddress;
use substrate_api_client::{
ac_primitives::{AssetRuntimeConfig, ExtrinsicSigner},
extrinsic::BalancesExtrinsics,
rpc::TungsteniteRpcClient,
ac_primitives::AssetRuntimeConfig, extrinsic::BalancesExtrinsics, rpc::TungsteniteRpcClient,
Api, GetAccountInformation, SubmitAndWatch, XtStatus,
};

Expand All @@ -46,7 +44,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::<AssetRuntimeConfig, _>::new(client).unwrap();
api.set_signer(ExtrinsicSigner::<AssetRuntimeConfig>::new(alice.clone()));
api.set_signer(alice.clone().into());

// Retrieve bobs current balance.
let bob = sr25519::Public::from_ss58check("5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty")
Expand Down
8 changes: 3 additions & 5 deletions examples/examples/transfer_with_ws_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ use sp_core::{
};
use sp_runtime::MultiAddress;
use substrate_api_client::{
ac_primitives::{AssetRuntimeConfig, ExtrinsicSigner},
extrinsic::BalancesExtrinsics,
rpc::WsRpcClient,
Api, GetAccountInformation, SubmitAndWatch, XtStatus,
ac_primitives::AssetRuntimeConfig, extrinsic::BalancesExtrinsics, rpc::WsRpcClient, Api,
GetAccountInformation, SubmitAndWatch, XtStatus,
};

// To test this example with CI we run it against the Substrate kitchensink node, which uses the asset pallet.
Expand All @@ -46,7 +44,7 @@ fn main() {
// Initialize api and set the signer (sender) that is used to sign the extrinsics.
let client = WsRpcClient::with_default_url();
let mut api = Api::<AssetRuntimeConfig, _>::new(client).unwrap();
api.set_signer(ExtrinsicSigner::<AssetRuntimeConfig>::new(alice.clone()));
api.set_signer(alice.clone().into());

// Retrieve bobs current balance.
let bob = sr25519::Public::from_ss58check("5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty")
Expand Down
4 changes: 2 additions & 2 deletions primitives/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use sp_runtime::traits::{
MaybeSerializeDeserialize,
};

use crate::{extrinsic_params, SignExtrinsic};
use crate::{extrinsic_params, ExtrinsicSigner, SignExtrinsic};

pub use asset_runtime_config::*;
pub use default_runtime_config::*;
Expand Down Expand Up @@ -160,7 +160,7 @@ impl<T: Config, E: extrinsic_params::ExtrinsicParams<T::Index, T::Hash>> Config
type AccountData = T::AccountData;
type ExtrinsicParams = E;
type CryptoKey = T::CryptoKey;
type ExtrinsicSigner = T::ExtrinsicSigner;
type ExtrinsicSigner = ExtrinsicSigner<Self>;
type Block = T::Block;
type Balance = T::Balance;
type ContractCurrency = T::ContractCurrency;
Expand Down
2 changes: 1 addition & 1 deletion testing/examples/author_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async fn main() {
let alice_pair = AccountKeyring::Alice.pair();
let mut api = Api::<AssetRuntimeConfig, _>::new(client).unwrap();

api.set_signer(ExtrinsicSigner::new(alice_pair));
api.set_signer(alice_pair.into());

let bob: ExtrinsicAddressOf<ExtrinsicSigner> = AccountKeyring::Bob.to_account_id().into();

Expand Down
10 changes: 7 additions & 3 deletions testing/examples/chain_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
limitations under the License.
*/

//! Tests for the chain rpc interface functions.
//! Tests for the chain rpc interface functions, including testing the DefaultRuntimeConfig
//! and Signer generation for the DefaultRuntimeConfig.
use sp_keyring::AccountKeyring;
use substrate_api_client::{
ac_primitives::AssetRuntimeConfig,
ac_primitives::{DefaultRuntimeConfig, ExtrinsicSigner},
rpc::{HandleSubscription, JsonrpseeClient},
Api, GetChainInfo, SubscribeChain,
};
Expand All @@ -25,7 +27,9 @@ use substrate_api_client::{
async fn main() {
// Setup
let client = JsonrpseeClient::with_default_url().unwrap();
let api = Api::<AssetRuntimeConfig, _>::new(client).unwrap();
let mut api = Api::<DefaultRuntimeConfig, _>::new(client).unwrap();
let signer = AccountKeyring::Alice.pair();
api.set_signer(ExtrinsicSigner::<DefaultRuntimeConfig>::new(signer));

// GetChainInfo
let finalized_header_hash = api.get_finalized_head().unwrap().unwrap();
Expand Down
10 changes: 4 additions & 6 deletions testing/examples/dispatch_errors_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@
use sp_keyring::AccountKeyring;
use sp_runtime::MultiAddress;
use substrate_api_client::{
ac_primitives::{AssetRuntimeConfig, ExtrinsicSigner},
extrinsic::BalancesExtrinsics,
rpc::JsonrpseeClient,
Api, GetAccountInformation, SubmitAndWatch, XtStatus,
ac_primitives::AssetRuntimeConfig, extrinsic::BalancesExtrinsics, rpc::JsonrpseeClient, Api,
GetAccountInformation, SubmitAndWatch, XtStatus,
};

#[tokio::main]
Expand All @@ -45,7 +43,7 @@ async fn main() {
println!("[+] One's Free Balance is {}\n", balance_of_one);

//BadOrigin
api.set_signer(ExtrinsicSigner::<AssetRuntimeConfig>::new(bob_signer));
api.set_signer(bob_signer.into());
//Can only be called by root
let xt = api.balance_force_set_balance(MultiAddress::Id(alice.clone()), 10);

Expand All @@ -55,7 +53,7 @@ async fn main() {
println!("[+] BadOrigin error: Bob can't force set balance");

//BelowMinimum
api.set_signer(ExtrinsicSigner::<AssetRuntimeConfig>::new(alice_signer));
api.set_signer(alice_signer.into());
let xt = api.balance_transfer_allow_death(MultiAddress::Id(one.clone()), 999999);
let result = api.submit_and_watch_extrinsic_until(xt, XtStatus::InBlock);
assert!(result.is_err());
Expand Down
4 changes: 2 additions & 2 deletions testing/examples/events_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use kitchensink_runtime::RuntimeEvent;
use sp_keyring::AccountKeyring;
use substrate_api_client::{
ac_node_api::{EventDetails, StaticEvent},
ac_primitives::{AssetRuntimeConfig, Config, ExtrinsicSigner},
ac_primitives::{AssetRuntimeConfig, Config},
extrinsic::BalancesExtrinsics,
rpc::JsonrpseeClient,
Api, FetchEvents, GetChainInfo, SubmitAndWatch, SubscribeEvents, XtStatus,
Expand All @@ -46,7 +46,7 @@ async fn main() {
let client = JsonrpseeClient::with_default_url().unwrap();
let alice_pair = AccountKeyring::Alice.pair();
let mut api = Api::<AssetRuntimeConfig, _>::new(client).unwrap();
api.set_signer(ExtrinsicSigner::<AssetRuntimeConfig>::new(alice_pair));
api.set_signer(alice_pair.into());

let bob = AccountKeyring::Bob.to_account_id();

Expand Down
8 changes: 3 additions & 5 deletions testing/examples/frame_system_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ use codec::Decode;
use frame_support::dispatch::DispatchInfo;
use sp_keyring::AccountKeyring;
use substrate_api_client::{
ac_node_api::StaticEvent,
ac_primitives::{AssetRuntimeConfig, ExtrinsicSigner},
rpc::JsonrpseeClient,
Api, GetAccountInformation, SystemApi,
ac_node_api::StaticEvent, ac_primitives::AssetRuntimeConfig, rpc::JsonrpseeClient, Api,
GetAccountInformation, SystemApi,
};

/// Check out frame_system::Event::ExtrinsicSuccess:
Expand All @@ -42,7 +40,7 @@ async fn main() {
let client = JsonrpseeClient::with_default_url().unwrap();
let alice_pair = AccountKeyring::Alice.pair();
let mut api = Api::<AssetRuntimeConfig, _>::new(client).unwrap();
api.set_signer(ExtrinsicSigner::new(alice_pair));
api.set_signer(alice_pair.into());

let alice = AccountKeyring::Alice.to_account_id();

Expand Down
6 changes: 2 additions & 4 deletions testing/examples/tungstenite_client_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ use sp_core::{
};
use sp_runtime::MultiAddress;
use substrate_api_client::{
ac_primitives::{AssetRuntimeConfig, ExtrinsicSigner},
extrinsic::BalancesExtrinsics,
rpc::TungsteniteRpcClient,
ac_primitives::AssetRuntimeConfig, extrinsic::BalancesExtrinsics, rpc::TungsteniteRpcClient,
Api, GetAccountInformation, SubmitAndWatch, XtStatus,
};

Expand All @@ -34,7 +32,7 @@ fn main() {
.unwrap();
let client = TungsteniteRpcClient::with_default_url(100);
let mut api = Api::<AssetRuntimeConfig, _>::new(client).unwrap();
api.set_signer(ExtrinsicSigner::<AssetRuntimeConfig>::new(alice.clone()));
api.set_signer(alice.clone().into());

let bob = sr25519::Public::from_ss58check("5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty")
.unwrap();
Expand Down
8 changes: 3 additions & 5 deletions testing/examples/ws_client_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ use sp_core::{
};
use sp_runtime::MultiAddress;
use substrate_api_client::{
ac_primitives::{AssetRuntimeConfig, ExtrinsicSigner},
extrinsic::BalancesExtrinsics,
rpc::WsRpcClient,
Api, GetAccountInformation, SubmitAndWatch, XtStatus,
ac_primitives::AssetRuntimeConfig, extrinsic::BalancesExtrinsics, rpc::WsRpcClient, Api,
GetAccountInformation, SubmitAndWatch, XtStatus,
};

fn main() {
Expand All @@ -34,7 +32,7 @@ fn main() {
.unwrap();
let client = WsRpcClient::with_default_url();
let mut api = Api::<AssetRuntimeConfig, _>::new(client).unwrap();
api.set_signer(ExtrinsicSigner::<AssetRuntimeConfig>::new(alice.clone()));
api.set_signer(alice.clone().into());

let bob = sr25519::Public::from_ss58check("5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty")
.unwrap();
Expand Down

0 comments on commit 724f7c3

Please sign in to comment.