Skip to content

Commit

Permalink
Make api and primitives generic over Runtime (#340)
Browse files Browse the repository at this point in the history
* add interface traits

add traits

add balanes impl

add Runtime trait

make api generic over Runtime

remove interfaces

fix clippy

fix staking feature

fix doc

make RuntimeVersion generic over Runtime

revert last commit

fix examples

* fix rebase changes

* add missing contracts

* amke staking generic over BalanceType

* cargo fmt

* make these types truly generic

* cargo fmt

* fix tests

* fix staking payout example

* add pallets trait

* add no_std traits

* remove obsoltete sp_std

* add missing re-export

* remove frame-support and frame-system from node-api

* add pallet contracts

* add pallets contracts

* add std impl for pallet traits

* add pallet staking

* remove pallet dependencies

* add tpyes.rs

* cargo fmt

* fix taplo

* remove substrate comment

* fix toml formatting

* fix taplo

* add links to the types where taken from substrate
  • Loading branch information
haerdib authored Dec 6, 2022
1 parent 7ff294c commit d540964
Show file tree
Hide file tree
Showing 45 changed files with 1,353 additions and 412 deletions.
426 changes: 293 additions & 133 deletions Cargo.lock

Large diffs are not rendered by default.

24 changes: 10 additions & 14 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,14 @@ codec = { package = "parity-scale-codec", version = "3.0.0", default-features =
hex = { version = "0.4.3", default-features = false, features = ["alloc"] }
log = { version = "0.4.14", default-features = false }
primitive-types = { version = "0.12.1", optional = true, features = ["codec"] }
serde = { version = "1.0.136", optional = true, features = ["derive"] }
serde_json = { version = "1.0.79", optional = true }
serde = { version = "1.0.136", default-features = false, features = ["derive"] }
serde_json = { version = "1.0.79", default-features = false }
thiserror = { version = "1.0.30", optional = true }
ws = { version = "0.9.2", optional = true, features = ["ssl"] }

# Substrate dependencies
frame-metadata = { default-features = false, git = "https://github.com/paritytech/frame-metadata", features = ["v14", "serde_full", "decode"] }
frame-support = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master" }
frame-system = { optional = true, git = "https://github.com/paritytech/substrate.git", branch = "master" }
pallet-balances = { optional = true, git = "https://github.com/paritytech/substrate.git", branch = "master" }
pallet-staking = { optional = true, git = "https://github.com/paritytech/substrate.git", branch = "master" }
pallet-transaction-payment = { optional = true, git = "https://github.com/paritytech/substrate.git", branch = "master" }
frame-support = { optional = true, git = "https://github.com/paritytech/substrate.git", branch = "master" }
sp-core = { default-features = false, features = ["full_crypto"], git = "https://github.com/paritytech/substrate.git", branch = "master" }
sp-rpc = { optional = true, git = "https://github.com/paritytech/substrate.git", branch = "master" }
sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master" }
Expand All @@ -45,10 +41,13 @@ ac-primitives = { path = "primitives", default-features = false }

[dev-dependencies]
env_logger = "0.9.0"
frame-system = { git = "https://github.com/paritytech/substrate.git", branch = "master" }
kitchensink-runtime = { git = "https://github.com/paritytech/substrate.git", branch = "master" }
pallet-balances = { git = "https://github.com/paritytech/substrate.git", branch = "master" }
pallet-identity = { git = "https://github.com/paritytech/substrate.git", branch = "master" }
pallet-staking = { git = "https://github.com/paritytech/substrate.git", branch = "master" }
sp-keyring = { git = "https://github.com/paritytech/substrate.git", branch = "master" }
wabt = "0.10.0"
pallet-identity = { git = "https://github.com/paritytech/substrate.git", branch = "master" }

[features]
default = ["std", "ws-client"]
Expand All @@ -63,14 +62,11 @@ std = [
"log/std",
"primitive-types",
"serde/std",
"serde_json",
"serde_json/std",
"thiserror",
# substrate
"frame-metadata/std",
"frame-support/std",
"frame-system",
"pallet-balances",
"pallet-transaction-payment/std",
"frame-support",
"sp-core/std",
"sp-rpc",
"sp-runtime/std",
Expand All @@ -83,4 +79,4 @@ std = [
"ac-primitives/std",
]
ws-client = ["ws"]
staking-xt = ["std", "pallet-staking"]
staking-xt = ["std"]
2 changes: 0 additions & 2 deletions compose-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ log = { version = "0.4.14", default-features = false }
# substrate
sp-core = { default-features = false, features = ["full_crypto"], git = "https://github.com/paritytech/substrate.git", branch = "master" }
sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master" }
sp-std = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master" }

# need to add this for the app_crypto macro
sp-application-crypto = { default-features = false, git = "https://github.com/paritytech/substrate.git", features = ["full_crypto"], branch = "master" }
Expand All @@ -28,7 +27,6 @@ std = [
# substrate
"sp-core/std",
"sp-runtime/std",
"sp-std/std",
"sp-application-crypto/std",
# local
"ac-primitives/std",
Expand Down
11 changes: 9 additions & 2 deletions examples/batch_payout.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#[cfg(feature = "staking-xt")]
use codec::{Decode, Encode};
#[cfg(feature = "staking-xt")]
use kitchensink_runtime::Runtime;
#[cfg(feature = "staking-xt")]
use pallet_staking::{ActiveEraInfo, Exposure};
#[cfg(feature = "staking-xt")]
use serde_json::Value;
Expand All @@ -17,7 +19,7 @@ fn main() {

let from = AccountKeyring::Alice.pair();
let client = WsRpcClient::new("ws://127.0.0.1:9944");
let mut api = Api::<_, _, PlainTipExtrinsicParams>::new(client).unwrap();
let mut api = Api::<_, _, PlainTipExtrinsicParams<Runtime>, Runtime>::new(client).unwrap();
api.set_signer(from);
let grace_period: GracePeriod = GracePeriod { enabled: false, eras: 0 };
let mut results: Vec<Value> = Vec::new();
Expand Down Expand Up @@ -86,7 +88,12 @@ fn main() {
#[cfg(feature = "staking-xt")]
pub fn get_last_reward(
validator_address: &str,
api: &substrate_api_client::Api<sp_core::sr25519::Pair, WsRpcClient, PlainTipExtrinsicParams>,
api: &substrate_api_client::Api<
sp_core::sr25519::Pair,
WsRpcClient,
PlainTipExtrinsicParams<Runtime>,
Runtime,
>,
) -> u32 {
let account = match AccountId32::from_ss58check(validator_address) {
Ok(address) => address,
Expand Down
6 changes: 2 additions & 4 deletions examples/benchmark_bulk_xt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// run this against test node with
// > substrate-test-node --dev --execution native --ws-port 9979 -ltxpool=debug

use kitchensink_runtime::{BalancesCall, RuntimeCall};
use kitchensink_runtime::{BalancesCall, Runtime, RuntimeCall};
use sp_keyring::AccountKeyring;
use substrate_api_client::{
compose_extrinsic_offline, rpc::WsRpcClient, Api, AssetTipExtrinsicParams,
Expand All @@ -31,11 +31,9 @@ fn main() {
// initialize api and set the signer (sender) that is used to sign the extrinsics
let from = AccountKeyring::Alice.pair();
let client = WsRpcClient::new("ws://127.0.0.1:9944");
let mut api = Api::<_, _, AssetTipExtrinsicParams>::new(client).unwrap();
let mut api = Api::<_, _, AssetTipExtrinsicParams<Runtime>, Runtime>::new(client).unwrap();
api.set_signer(from);

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

// define the recipient
let to = AccountKeyring::Bob.to_account_id();

Expand Down
6 changes: 3 additions & 3 deletions examples/compose_extrinsic_offline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//! without asking the node for nonce and does not need to know the metadata
use ac_primitives::AssetTipExtrinsicParamsBuilder;
use kitchensink_runtime::{BalancesCall, Header, RuntimeCall};
use kitchensink_runtime::{BalancesCall, Header, Runtime, RuntimeCall};
use sp_keyring::AccountKeyring;
use sp_runtime::{generic::Era, MultiAddress};
use substrate_api_client::{
Expand All @@ -32,14 +32,14 @@ fn main() {
let from = AccountKeyring::Alice.pair();
let client = WsRpcClient::new("ws://127.0.0.1:9944");

let mut api = Api::<_, _, AssetTipExtrinsicParams>::new(client).unwrap();
let mut api = Api::<_, _, AssetTipExtrinsicParams<Runtime>, Runtime>::new(client).unwrap();
api.set_signer(from);

// Information for Era for mortal transactions.
let head = api.get_finalized_head().unwrap().unwrap();
let h: Header = api.get_header(Some(head)).unwrap().unwrap();
let period = 5;
let tx_params = AssetTipExtrinsicParamsBuilder::new()
let tx_params = AssetTipExtrinsicParamsBuilder::<Runtime>::new()
.era(Era::mortal(period, h.number.into()), head)
.tip(0);

Expand Down
3 changes: 2 additions & 1 deletion examples/contract_instantiate_with_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
//! This example is community maintained and not CI tested, therefore it may not work as is.
use codec::Decode;
use kitchensink_runtime::Runtime;
use sp_keyring::AccountKeyring;
use std::sync::mpsc::channel;
use substrate_api_client::{
Expand All @@ -40,7 +41,7 @@ fn main() {
// initialize api and set the signer (sender) that is used to sign the extrinsics
let from = AccountKeyring::Alice.pair();
let client = WsRpcClient::new("ws://127.0.0.1:9944");
let mut api = Api::<_, _, PlainTipExtrinsicParams>::new(client).unwrap();
let mut api = Api::<_, _, PlainTipExtrinsicParams<Runtime>, Runtime>::new(client).unwrap();
api.set_signer(from);

println!("[+] Alice's Account Nonce is {}", api.get_nonce().unwrap());
Expand Down
6 changes: 3 additions & 3 deletions examples/custom_nonce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//! without asking the node for nonce and does not need to know the metadata
use ac_primitives::AssetTipExtrinsicParamsBuilder;
use kitchensink_runtime::{BalancesCall, Header, RuntimeCall};
use kitchensink_runtime::{BalancesCall, Header, Runtime, RuntimeCall};
use sp_keyring::AccountKeyring;
use sp_runtime::{generic::Era, MultiAddress};
use substrate_api_client::{
Expand All @@ -32,14 +32,14 @@ fn main() {
let from = AccountKeyring::Alice.pair();
let client = WsRpcClient::new("ws://127.0.0.1:9944");

let mut api = Api::<_, _, AssetTipExtrinsicParams>::new(client).unwrap();
let mut api = Api::<_, _, AssetTipExtrinsicParams<Runtime>, Runtime>::new(client).unwrap();
api.set_signer(from);

// Information for Era for mortal transactions.
let head = api.get_finalized_head().unwrap().unwrap();
let h: Header = api.get_header(Some(head)).unwrap().unwrap();
let period = 5;
let tx_params = AssetTipExtrinsicParamsBuilder::new()
let tx_params = AssetTipExtrinsicParamsBuilder::<Runtime>::new()
.era(Era::mortal(period, h.number.into()), head)
.tip(0);

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

///! Very simple example that shows how to subscribe to events.
use std::sync::mpsc::channel;
//! Very simple example that shows how to subscribe to events.
use codec::Decode;
use kitchensink_runtime::Runtime;
use log::debug;
use sp_core::{sr25519, H256 as Hash};
use std::sync::mpsc::channel;

// This module depends on node_runtime.
// To avoid dependency collisions, node_runtime has been removed from the substrate-api-client library.
Expand All @@ -31,7 +32,8 @@ fn main() {
env_logger::init();

let client = WsRpcClient::new("ws://127.0.0.1:9944");
let api = Api::<sr25519::Pair, _, AssetTipExtrinsicParams>::new(client).unwrap();
let api =
Api::<sr25519::Pair, _, AssetTipExtrinsicParams<Runtime>, Runtime>::new(client).unwrap();

println!("Subscribe to events");
let (events_in, events_out) = channel();
Expand Down
3 changes: 2 additions & 1 deletion examples/event_error_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ limitations under the License.
*/

use codec::Decode;
use kitchensink_runtime::Runtime;
use sp_core::crypto::Pair;
use sp_keyring::AccountKeyring;
use sp_runtime::{AccountId32 as AccountId, MultiAddress};
Expand Down Expand Up @@ -42,7 +43,7 @@ fn main() {
let from = AccountKeyring::Alice.pair();

let client = WsRpcClient::new("ws://127.0.0.1:9944");
let mut api = Api::<_, _, AssetTipExtrinsicParams>::new(client).unwrap();
let mut api = Api::<_, _, AssetTipExtrinsicParams<Runtime>, Runtime>::new(client).unwrap();
api.set_signer(from.clone());

let from_account_id = AccountKeyring::Alice.to_account_id();
Expand Down
3 changes: 2 additions & 1 deletion examples/generic_event_callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
//! implying no runtime needs to be imported
use codec::Decode;
use kitchensink_runtime::Runtime;
use sp_keyring::AccountKeyring;
use sp_runtime::{AccountId32 as AccountId, MultiAddress};
use std::{sync::mpsc::channel, thread};
Expand All @@ -41,7 +42,7 @@ fn main() {
// Initialize api and set the signer (sender) that is used to sign the extrinsics.
let alice = AccountKeyring::Alice.pair();
let client = WsRpcClient::new("ws://127.0.0.1:9944");
let mut api = Api::<_, _, AssetTipExtrinsicParams>::new(client).unwrap();
let mut api = Api::<_, _, AssetTipExtrinsicParams<Runtime>, Runtime>::new(client).unwrap();
api.set_signer(alice);

println!("Subscribe to events");
Expand Down
3 changes: 2 additions & 1 deletion examples/generic_extrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
//! This examples shows how to use the compose_extrinsic macro to create an extrinsic for any (custom)
//! module, whereas the desired module and call are supplied as a string.
use kitchensink_runtime::Runtime;
use sp_keyring::AccountKeyring;
use substrate_api_client::{
compose_extrinsic, rpc::WsRpcClient, Api, AssetTipExtrinsicParams, GenericAddress,
Expand All @@ -28,7 +29,7 @@ fn main() {
// initialize api and set the signer (sender) that is used to sign the extrinsics
let from = AccountKeyring::Alice.pair();
let client = WsRpcClient::new("ws://127.0.0.1:9944");
let mut api = Api::<_, _, AssetTipExtrinsicParams>::new(client).unwrap();
let mut api = Api::<_, _, AssetTipExtrinsicParams<Runtime>, Runtime>::new(client).unwrap();
api.set_signer(from);
// set the recipient
let to = AccountKeyring::Bob.to_account_id();
Expand Down
4 changes: 3 additions & 1 deletion examples/get_account_identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ fn main() {
// Create the node-api client and set the signer.
let client = WsRpcClient::new("ws://127.0.0.1:9944");
let alice = AccountKeyring::Alice.pair();
let mut api = Api::<_, _, AssetTipExtrinsicParams>::new(client).unwrap();
let mut api =
Api::<_, _, AssetTipExtrinsicParams<KitchensinkRuntime>, KitchensinkRuntime>::new(client)
.unwrap();
api.set_signer(alice.clone());

// Fill Identity storage
Expand Down
5 changes: 3 additions & 2 deletions examples/get_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//! Very simple example that shows how to pretty print the metadata. Has proven to be a helpful
//! debugging tool.
use kitchensink_runtime::{Block, Header};
use kitchensink_runtime::{Block, Header, Runtime};
use sp_core::sr25519;
use sp_runtime::generic::SignedBlock as SignedBlockG;
use std::sync::mpsc::channel;
Expand All @@ -28,7 +28,8 @@ fn main() {
env_logger::init();

let client = WsRpcClient::new("ws://127.0.0.1:9944");
let api = Api::<sr25519::Pair, _, AssetTipExtrinsicParams>::new(client).unwrap();
let api =
Api::<sr25519::Pair, _, AssetTipExtrinsicParams<Runtime>, Runtime>::new(client).unwrap();

let head = api.get_finalized_head().unwrap().unwrap();

Expand Down
7 changes: 5 additions & 2 deletions examples/get_existential_deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

///! Very simple example that shows how to get some simple storage values.
//! Very simple example that shows how to get some simple storage values.
use kitchensink_runtime::Runtime;
use sp_runtime::app_crypto::sp_core::sr25519;
use substrate_api_client::{rpc::WsRpcClient, Api, AssetTipExtrinsicParams};

fn main() {
env_logger::init();

let client = WsRpcClient::new("ws://127.0.0.1:9944");
let api = Api::<sr25519::Pair, _, AssetTipExtrinsicParams>::new(client).unwrap();
let api =
Api::<sr25519::Pair, _, AssetTipExtrinsicParams<Runtime>, Runtime>::new(client).unwrap();

// get existential deposit
let min_balance = api.get_existential_deposit().unwrap();
Expand Down
14 changes: 11 additions & 3 deletions examples/get_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,23 @@
limitations under the License.
*/

///! Very simple example that shows how to get some simple storage values.
//! Very simple example that shows how to get some simple storage values.
use frame_system::AccountInfo as GenericAccountInfo;
use kitchensink_runtime::Runtime;
use sp_keyring::AccountKeyring;
use substrate_api_client::{rpc::WsRpcClient, AccountInfo, Api, AssetTipExtrinsicParams};
use substrate_api_client::{rpc::WsRpcClient, Api, AssetTipExtrinsicParams};

type IndexFor<T> = <T as frame_system::Config>::Index;
type AccountDataFor<T> = <T as frame_system::Config>::AccountData;

type AccountInfo = GenericAccountInfo<IndexFor<Runtime>, AccountDataFor<Runtime>>;

fn main() {
env_logger::init();

let client = WsRpcClient::new("ws://127.0.0.1:9944");
let mut api = Api::<_, _, AssetTipExtrinsicParams>::new(client).unwrap();
let mut api = Api::<_, _, AssetTipExtrinsicParams<Runtime>, Runtime>::new(client).unwrap();

// get some plain storage value
let result: u128 = api.get_storage_value("Balances", "TotalIssuance", None).unwrap().unwrap();
Expand Down
10 changes: 6 additions & 4 deletions examples/print_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,19 @@
limitations under the License.
*/

///! Very simple example that shows how to pretty print the metadata. Has proven to be a helpful
///! debugging tool.
use sp_core::sr25519;
//! 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::{rpc::WsRpcClient, Api, AssetTipExtrinsicParams, Metadata};

fn main() {
env_logger::init();

let client = WsRpcClient::new("ws://127.0.0.1:9944");
let mut api = Api::<sr25519::Pair, _, AssetTipExtrinsicParams>::new(client).unwrap();
let mut api =
Api::<sr25519::Pair, _, AssetTipExtrinsicParams<Runtime>, Runtime>::new(client).unwrap();

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

Expand Down
Loading

0 comments on commit d540964

Please sign in to comment.