Skip to content

Commit

Permalink
Clean up trait bounds (#541)
Browse files Browse the repository at this point in the history
* Code compile but examples and tests  not updated.
- Remove ExtrinsicParams from API. Add them to Config.
- Remove Runtime from API:FrameSystemConfig,BalancesConfig,ContractsConfig
- Add AccountData in primitives type
- Add Block to API, as this can't be easily configured (requires the Call from the runtime)
- Add config trait similar to subxt and a specific config for substrate node
- Get the block number from Header

Remove Signer
Add StakingBalance in config
Add a polkadot config
Example and test compile and run succesffully

Move Blocktype from API into config

Clean examples: all use kitchensing runtime

Clean code

move config into own folder

Clean after merge

Clippy

Remove unnecessary bounds

Header::Number must implement copy

Changes from review

Changes from review

Remove Signer from ExtrinsicSigner

Changes from Review:
- add type BlockNumber
- add trait file and move hasher and Header traits there

Changes from Review: make config generic over Call and use a own SubstrateBlock type
- Define a specific SubstrateExtrinsic type
- Implement Extrinsic,Serialize and Deserialize for UncheckedExtrinsicV4
- add unit tests

Use of OpaqueExtrinsic: no need of Call input in Config.
Add own OpaqueExtrinsic for no-std serialization

* Remove obsolete pallet configs.

* Changes from review

---------

Co-authored-by: echevrier <edith.chevrier@scs.ch>
  • Loading branch information
echevrier and echevrier committed May 4, 2023
1 parent e1c766c commit caf8d84
Show file tree
Hide file tree
Showing 52 changed files with 1,161 additions and 947 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions examples/examples/benchmark_bulk_xt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,23 @@
// run this against test node with
// > substrate-test-node --dev --execution native --ws-port 9979 -ltxpool=debug

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

// To test this example in CI, we run it against the Substrate kitchensink node. Therefore, we use the SubstrateKitchensinkConfig
// ! Careful: Most runtimes uses plain as tips, they need a polkadot config.

// 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>;
type ExtrinsicSigner = GenericExtrinsicSigner<SubstrateKitchensinkConfig>;

// 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
Expand All @@ -46,9 +48,7 @@ async fn main() {
// Initialize api and set the signer (sender) that is used to sign the extrinsics.
let signer = AccountKeyring::Alice.pair();
let client = JsonrpseeClient::with_default_url().unwrap();
// ! 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();
let mut api = Api::<SubstrateKitchensinkConfig, _>::new(client).unwrap();
api.set_signer(ExtrinsicSigner::new(signer));

let recipient: ExtrinsicAddressOf<ExtrinsicSigner> = AccountKeyring::Bob.to_account_id().into();
Expand Down
14 changes: 7 additions & 7 deletions examples/examples/compose_extrinsic_offline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@
//! 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, Signature};
use kitchensink_runtime::{BalancesCall, RuntimeCall};
use sp_keyring::AccountKeyring;
use sp_runtime::{generic::Era, MultiAddress};
use substrate_api_client::{
ac_primitives::{AssetTipExtrinsicParams, ExtrinsicSigner, GenericAdditionalParams},
ac_primitives::{ExtrinsicSigner, GenericAdditionalParams, SubstrateKitchensinkConfig},
rpc::JsonrpseeClient,
Api, GetChainInfo, SubmitAndWatch, XtStatus,
};

// To test this example in CI, we run it against the Substrate kitchensink node. Therefore, we use the SubstrateKitchensinkConfig
// ! Careful: Most runtimes uses plain as tips, they need a polkadot config.

#[tokio::main]
async fn main() {
env_logger::init();
Expand All @@ -35,11 +38,8 @@ async fn main() {
// Api::new(..) is not actually an offline call, but retrieves metadata and other information from the node.
// If this is not acceptable, use the Api::new_offline(..) function instead. There are no examples for this,
// because of the constantly changing substrate node. But check out our unit tests - there are Apis created with `new_offline`.
//
// ! 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(ExtrinsicSigner::<_, Signature, Runtime>::new(signer));
let mut api = Api::<SubstrateKitchensinkConfig, _>::new(client).unwrap();
api.set_signer(ExtrinsicSigner::<SubstrateKitchensinkConfig>::new(signer));

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

use codec::Decode;
use kitchensink_runtime::{AccountId, Runtime, Signature};
use kitchensink_runtime::AccountId;
use sp_keyring::AccountKeyring;
use substrate_api_client::{
ac_node_api::StaticEvent,
ac_primitives::{ExtrinsicSigner, PlainTipExtrinsicParams},
extrinsic::ContractsExtrinsics,
rpc::JsonrpseeClient,
Api, SubmitAndWatch, SubmitAndWatchUntilSuccess, XtStatus,
ac_compose_macros::primitives::SubstrateKitchensinkConfig, ac_node_api::StaticEvent,
ac_primitives::ExtrinsicSigner, extrinsic::ContractsExtrinsics, rpc::JsonrpseeClient, Api,
SubmitAndWatch, SubmitAndWatchUntilSuccess, XtStatus,
};

// To test this example in CI, we run it against the Substrate kitchensink node. Therefore, we use the SubstrateKitchensinkConfig
// ! Careful: Most runtimes uses plain as tips, they need a polkadot config.

#[allow(unused)]
#[derive(Decode)]
struct ContractInstantiatedEventArgs {
Expand All @@ -45,10 +46,8 @@ async fn main() {
// Initialize api and set the signer (sender) that is used to sign the extrinsics.
let signer = AccountKeyring::Alice.pair();
let client = JsonrpseeClient::with_default_url().unwrap();
// ! 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(ExtrinsicSigner::<_, Signature, Runtime>::new(signer));
let mut api = Api::<SubstrateKitchensinkConfig, _>::new(client).unwrap();
api.set_signer(ExtrinsicSigner::<_>::new(signer));

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

Expand Down
13 changes: 7 additions & 6 deletions examples/examples/custom_nonce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,27 @@
//! 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, Signature};
use kitchensink_runtime::{BalancesCall, RuntimeCall};
use sp_keyring::AccountKeyring;
use sp_runtime::{generic::Era, MultiAddress};
use substrate_api_client::{
ac_primitives::{AssetTipExtrinsicParams, ExtrinsicSigner, GenericAdditionalParams},
ac_primitives::{ExtrinsicSigner, GenericAdditionalParams, SubstrateKitchensinkConfig},
rpc::JsonrpseeClient,
Api, Error, GetChainInfo, SubmitAndWatch, UnexpectedTxStatus, XtStatus,
};

// To test this example in CI, we run it against the Substrate kitchensink node. Therefore, we use the SubstrateKitchensinkConfig.
// ! Careful: Most runtimes uses plain as tips, they need a polkadot config.

#[tokio::main]
async fn main() {
env_logger::init();

// Initialize api and set the signer (sender) that is used to sign the extrinsics.
let signer = AccountKeyring::Alice.pair();
let client = JsonrpseeClient::with_default_url().unwrap();
// ! 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(ExtrinsicSigner::<_, Signature, Runtime>::new(signer));
let mut api = Api::<SubstrateKitchensinkConfig, _>::new(client).unwrap();
api.set_signer(ExtrinsicSigner::<SubstrateKitchensinkConfig>::new(signer));

// Information for Era for mortal transactions.
let last_finalized_header_hash = api.get_finalized_head().unwrap().unwrap();
Expand Down
9 changes: 6 additions & 3 deletions examples/examples/event_callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,23 @@
use log::debug;
use sp_core::H256 as Hash;
use substrate_api_client::{
ac_primitives::PlainTipExtrinsicParams, rpc::JsonrpseeClient, Api, SubscribeEvents,
ac_primitives::SubstrateKitchensinkConfig, rpc::JsonrpseeClient, Api, SubscribeEvents,
};

// This module depends on the specific node runtime.
// Replace this crate by your own if you run a custom substrate node to get your custom events.
use kitchensink_runtime::{Runtime, RuntimeEvent};
use kitchensink_runtime::RuntimeEvent;

// To test this example in CI, we run it against the Substrate kitchensink node. Therefore, we use the SubstrateKitchensinkConfig
// ! Careful: Most runtimes uses plain as tips, they need a polkadot config.

#[tokio::main]
async fn main() {
env_logger::init();

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

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

use codec::Decode;
use kitchensink_runtime::{Runtime, Signature};
use sp_keyring::AccountKeyring;
use sp_runtime::{AccountId32 as AccountId, MultiAddress};
use substrate_api_client::{
ac_node_api::StaticEvent,
ac_primitives::{AssetTipExtrinsicParams, ExtrinsicSigner},
ac_primitives::{ExtrinsicSigner, SubstrateKitchensinkConfig},
extrinsic::BalancesExtrinsics,
rpc::JsonrpseeClient,
Api, GetAccountInformation, SubmitAndWatchUntilSuccess,
};

// To test this example in CI, we run it against the Substrate kitchensink node. Therefore, we use the SubstrateKitchensinkConfig
// ! Careful: Most runtimes uses plain as tips, they need a polkadot config.

#[derive(Decode)]
struct TransferEventArgs {
_from: AccountId,
Expand All @@ -44,10 +46,8 @@ async fn main() {
// Initialize api and set the signer (sender) that is used to sign the extrinsics.
let alice_signer = AccountKeyring::Alice.pair();
let client = JsonrpseeClient::with_default_url().unwrap();
// ! 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(ExtrinsicSigner::<_, Signature, Runtime>::new(alice_signer));
let mut api = Api::<SubstrateKitchensinkConfig, _>::new(client).unwrap();
api.set_signer(ExtrinsicSigner::<SubstrateKitchensinkConfig>::new(alice_signer));

let alice = AccountKeyring::Alice.to_account_id();
let balance_of_alice = api.get_account_data(&alice).unwrap().unwrap().free;
Expand Down
15 changes: 7 additions & 8 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, Signature};
use kitchensink_runtime::Runtime as KitchensinkRuntime;
use pallet_identity::{Data, IdentityInfo, Registration};
use sp_core::{crypto::Pair, H256};
use sp_keyring::AccountKeyring;
use substrate_api_client::{
ac_compose_macros::compose_extrinsic,
ac_primitives::{AssetTipExtrinsicParams, ExtrinsicSigner, UncheckedExtrinsicV4},
ac_primitives::{ExtrinsicSigner, SubstrateKitchensinkConfig, UncheckedExtrinsicV4},
rpc::JsonrpseeClient,
Api, GetStorage, SubmitAndWatch, XtStatus,
};
Expand All @@ -33,19 +33,18 @@ type BalanceOf<T> = <<T as pallet_identity::Config>::Currency as Currency<
type MaxRegistrarsOf<T> = <T as pallet_identity::Config>::MaxRegistrars;
type MaxAdditionalFieldsOf<T> = <T as pallet_identity::Config>::MaxAdditionalFields;

// To test this example in CI, we run it against the Substrate kitchensink node. Therefore, we use the SubstrateKitchensinkConfig
// ! Careful: Most runtimes uses plain as tips, they need a polkadot config.

#[tokio::main]
async fn main() {
env_logger::init();

// Create the node-api client and set the signer.
let client = JsonrpseeClient::with_default_url().unwrap();
let signer = AccountKeyring::Alice.pair();
// ! 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<KitchensinkRuntime>, KitchensinkRuntime>::new(client)
.unwrap();
api.set_signer(ExtrinsicSigner::<_, Signature, KitchensinkRuntime>::new(signer.clone()));
let mut api = Api::<SubstrateKitchensinkConfig, _>::new(client).unwrap();
api.set_signer(ExtrinsicSigner::<SubstrateKitchensinkConfig>::new(signer.clone()));

// Fill Identity storage.
let info = IdentityInfo::<MaxAdditionalFieldsOf<KitchensinkRuntime>> {
Expand Down
11 changes: 5 additions & 6 deletions examples/examples/get_blocks_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@
//! Very simple example that shows how to fetch chain information with async.
//! To compile this example for async you need to set the `--no-default-features` flag

use kitchensink_runtime::Runtime;
use sp_core::sr25519;
use substrate_api_client::{
ac_primitives::PlainTipExtrinsicParams,
ac_primitives::SubstrateKitchensinkConfig,
rpc::{HandleSubscription, JsonrpseeClient},
Api, GetChainInfo, SubscribeChain,
};
Expand All @@ -31,16 +29,17 @@ async fn main() {
println!("Please compile this example with `--no-default-features` for it to run properly.")
}

// To test this example in CI, we run it against the Substrate kitchensink node. Therefore, we use the SubstrateKitchensinkConfig
// ! Careful: Most runtimes uses plain as tips, they need a polkadot config.

#[cfg(not(feature = "sync-examples"))]
#[tokio::main]
async fn main() {
env_logger::init();

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

let (genesis_block, header_hash, signed_block) = futures::future::try_join3(
api.get_genesis_block(),
Expand Down
16 changes: 9 additions & 7 deletions examples/examples/get_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,28 @@
//! Very simple example that shows how to get some simple storage values.

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

type IndexFor<T> = <T as frame_system::Config>::Index;
type AccountDataFor<T> = <T as frame_system::Config>::AccountData;
// To test this example in CI, we run it against the Substrate kitchensink node. Therefore, we use the SubstrateKitchensinkConfig
// ! Careful: Most runtimes uses plain as tips, they need a polkadot config.

type AccountInfo = GenericAccountInfo<IndexFor<Runtime>, AccountDataFor<Runtime>>;
type AccountInfo = GenericAccountInfo<
<SubstrateKitchensinkConfig as Config>::Index,
<SubstrateKitchensinkConfig as Config>::AccountData,
>;

#[tokio::main]
async fn main() {
env_logger::init();

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

// get some plain storage value
let result: u128 = api.get_storage("Balances", "TotalIssuance", None).unwrap().unwrap();
Expand All @@ -58,7 +60,7 @@ async fn main() {

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

println!(
Expand Down
10 changes: 5 additions & 5 deletions examples/examples/print_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@
//! Very simple example that shows how to pretty print the metadata. Has proven to be a helpful
//! debugging tool.

use kitchensink_runtime::Runtime;
use sp_core::sr25519;
use substrate_api_client::{ac_primitives::PlainTipExtrinsicParams, rpc::JsonrpseeClient, Api};
use substrate_api_client::{ac_primitives::SubstrateKitchensinkConfig, rpc::JsonrpseeClient, Api};

// To test this example in CI, we run it against the Substrate kitchensink node. Therefore, we use the SubstrateKitchensinkConfig
// ! Careful: Most runtimes uses plain as tips, they need a polkadot config.

#[tokio::main]
async fn main() {
env_logger::init();

// Initialize the api, which retrieves the metadata from the node upon initialization.
let client = JsonrpseeClient::with_default_url().unwrap();
let mut api =
Api::<sr25519::Pair, _, PlainTipExtrinsicParams<Runtime>, Runtime>::new(client).unwrap();
let mut api = Api::<SubstrateKitchensinkConfig, _>::new(client).unwrap();

let meta = api.metadata().clone();

Expand Down
Loading

0 comments on commit caf8d84

Please sign in to comment.