diff --git a/Cargo.lock b/Cargo.lock index a32cde2e3..6e1e7bd92 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -51,7 +51,7 @@ dependencies = [ [[package]] name = "ac-primitives" -version = "0.2.0" +version = "0.2.1" dependencies = [ "frame-system", "hex", diff --git a/examples/example_batch_payout.rs b/examples/example_batch_payout.rs index e196e8ee9..d6754e27e 100644 --- a/examples/example_batch_payout.rs +++ b/examples/example_batch_payout.rs @@ -11,9 +11,7 @@ use sp_keyring::AccountKeyring; #[cfg(feature = "staking-xt")] use sp_runtime::{app_crypto::Ss58Codec, AccountId32}; #[cfg(feature = "staking-xt")] -use substrate_api_client::{ - rpc::WsRpcClient, Api, BaseExtrinsicParams, PlainTip, PlainTipExtrinsicParams, XtStatus, -}; +use substrate_api_client::{rpc::WsRpcClient, Api, PlainTipExtrinsicParams, XtStatus}; #[cfg(feature = "staking-xt")] fn main() { @@ -104,11 +102,7 @@ pub fn get_node_url_from_cli() -> String { #[cfg(feature = "staking-xt")] pub fn get_last_reward( validator_address: &str, - api: &substrate_api_client::Api< - sp_core::sr25519::Pair, - WsRpcClient, - BaseExtrinsicParams, - >, + api: &substrate_api_client::Api, ) -> u32 { let api = api; let account = match AccountId32::from_ss58check(validator_address) { diff --git a/primitives/Cargo.toml b/primitives/Cargo.toml index 18021f7f2..0ecd85106 100644 --- a/primitives/Cargo.toml +++ b/primitives/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ac-primitives" -version = "0.2.0" +version = "0.2.1" authors = ["Supercomputing Systems AG "] license = "Apache-2.0" edition = "2021" @@ -12,9 +12,9 @@ hex = { version = "0.4.3", default-features = false, features = ["alloc"] } # substrate frame-system = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master" } pallet-balances = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master" } -sp-core = { version = "6.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master" } -sp-runtime = { version = "6.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master" } -sp-std = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master" } +sp-core = { default-features = false, 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" } [features] default = ["std"] diff --git a/primitives/src/extrinsic_params.rs b/primitives/src/extrinsic_params.rs index a08e919c5..abfba0825 100644 --- a/primitives/src/extrinsic_params.rs +++ b/primitives/src/extrinsic_params.rs @@ -1,3 +1,21 @@ +/* + Copyright 2019 Supercomputing Systems AG + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +*/ + +use crate::Balance; use codec::{Decode, Encode}; use core::marker::PhantomData; use sp_core::H256; @@ -5,33 +23,33 @@ use sp_runtime::{ generic::Era, traits::{BlakeTwo256, Hash}, }; -use sp_std::prelude::*; +use sp_std::{hash::Hash as HashTrait, prelude::*}; /// Default SignedExtra. /// Simple generic extra mirroring the SignedExtra currently used in extrinsics. #[derive(Decode, Encode, Copy, Clone, Eq, PartialEq, Debug)] -pub struct SubstrateDefaultSignedExtra { +pub struct SubstrateDefaultSignedExtra { pub era: Era, #[codec(compact)] - pub nonce: u32, + pub nonce: Index, pub tip: Tip, } -impl SubstrateDefaultSignedExtra { - pub fn new(era: Era, nonce: u32, tip: Tip) -> Self { +impl SubstrateDefaultSignedExtra { + pub fn new(era: Era, nonce: Index, tip: Tip) -> Self { Self { era, nonce, tip } } } /// Default AdditionalSigned fields of the respective SignedExtra fields. /// The Order is (CheckNonZeroSender, CheckSpecVersion, CheckTxVersion, CheckGenesis, Check::Era, CheckNonce, CheckWeight, transactionPayment::ChargeTransactionPayment). -pub type SubstrateDefaultAdditionalSigned = ((), u32, u32, H256, H256, (), (), ()); +pub type SubstrateDefaultAdditionalSigned = ((), u32, u32, Hash, Hash, (), (), ()); /// This trait allows you to configure the "signed extra" and /// "additional" parameters that are signed and used in transactions. /// see [`BaseExtrinsicParams`] for an implementation that is compatible with /// a Polkadot node. -pub trait ExtrinsicParams { +pub trait ExtrinsicParams { /// These parameters can be provided to the constructor along with /// some default parameters in order to help construct your [`ExtrinsicParams`] object. type OtherParams: Default + Clone; @@ -46,8 +64,8 @@ pub trait ExtrinsicParams { fn new( spec_version: u32, transaction_version: u32, - nonce: u32, - genesis_hash: H256, + nonce: Index, + genesis_hash: Hash, other_params: Self::OtherParams, ) -> Self; @@ -63,43 +81,43 @@ pub trait ExtrinsicParams { /// A struct representing the signed extra and additional parameters required /// to construct a transaction and pay in asset fees -pub type AssetTipExtrinsicParams = BaseExtrinsicParams; +pub type AssetTipExtrinsicParams = BaseExtrinsicParams, u32, H256>; /// A builder which leads to [`AssetTipExtrinsicParams`] being constructed. /// This is what you provide to methods like `sign_and_submit()`. -pub type AssetTipExtrinsicParamsBuilder = BaseExtrinsicParamsBuilder; +pub type AssetTipExtrinsicParamsBuilder = BaseExtrinsicParamsBuilder, H256>; /// A struct representing the signed extra and additional parameters required /// to construct a transaction and pay in token fees -pub type PlainTipExtrinsicParams = BaseExtrinsicParams; +pub type PlainTipExtrinsicParams = BaseExtrinsicParams, u32, H256>; /// A builder which leads to [`PlainTipExtrinsicParams`] being constructed. /// This is what you provide to methods like `sign_and_submit()`. -pub type PlainTipExtrinsicParamsBuilder = BaseExtrinsicParamsBuilder; +pub type PlainTipExtrinsicParamsBuilder = BaseExtrinsicParamsBuilder, H256>; /// An implementation of [`ExtrinsicParams`] that is suitable for constructing /// extrinsics that can be sent to a node with the same signed extra and additional /// parameters as a Polkadot/Substrate node. #[derive(Decode, Encode, Clone, Eq, PartialEq, Debug)] -pub struct BaseExtrinsicParams { +pub struct BaseExtrinsicParams { era: Era, - nonce: u32, + nonce: Index, tip: Tip, spec_version: u32, transaction_version: u32, - genesis_hash: H256, - mortality_checkpoint: H256, + genesis_hash: Hash, + mortality_checkpoint: Hash, marker: PhantomData<()>, } /// This builder allows you to provide the parameters that can be configured in order to /// construct a [`BaseExtrinsicParams`] value. #[derive(Decode, Encode, Copy, Clone, Eq, PartialEq, Debug)] -pub struct BaseExtrinsicParamsBuilder { +pub struct BaseExtrinsicParamsBuilder { era: Era, - mortality_checkpoint: Option, + mortality_checkpoint: Option, tip: Tip, } -impl BaseExtrinsicParamsBuilder { +impl BaseExtrinsicParamsBuilder { /// Instantiate the default set of [`BaseExtrinsicParamsBuilder`] pub fn new() -> Self { Self::default() @@ -110,7 +128,7 @@ impl BaseExtrinsicParamsBuilder { /// of time). The second argument is the block hash after which the transaction /// becomes valid, and must align with the era phase (see the [`Era::Mortal`] docs /// for more detail on that). - pub fn era(mut self, era: Era, checkpoint: H256) -> Self { + pub fn era(mut self, era: Era, checkpoint: Hash) -> Self { self.era = era; self.mortality_checkpoint = Some(checkpoint); self @@ -124,26 +142,29 @@ impl BaseExtrinsicParamsBuilder { } } -impl Default for BaseExtrinsicParamsBuilder { +impl Default for BaseExtrinsicParamsBuilder { fn default() -> Self { Self { era: Era::Immortal, mortality_checkpoint: None, tip: Tip::default() } } } -impl ExtrinsicParams for BaseExtrinsicParams +impl ExtrinsicParams for BaseExtrinsicParams where u128: From, - Tip: Copy + Default, + Tip: Copy + Default + Encode, + Index: Copy + Default + Encode, + Hash: HashTrait + Encode + Copy, + SubstrateDefaultSignedExtra: Encode, { - type OtherParams = BaseExtrinsicParamsBuilder; - type SignedExtra = SubstrateDefaultSignedExtra; - type AdditionalSigned = SubstrateDefaultAdditionalSigned; + type OtherParams = BaseExtrinsicParamsBuilder; + type SignedExtra = SubstrateDefaultSignedExtra; + type AdditionalSigned = SubstrateDefaultAdditionalSigned; fn new( spec_version: u32, transaction_version: u32, - nonce: u32, - genesis_hash: H256, + nonce: Index, + genesis_hash: Hash, other_params: Self::OtherParams, ) -> Self { BaseExtrinsicParams { @@ -207,41 +228,41 @@ where /// A tip payment. #[derive(Copy, Clone, Debug, Default, Decode, Encode, Eq, PartialEq)] -pub struct PlainTip { +pub struct PlainTip { #[codec(compact)] - tip: u128, + tip: Balance, } -impl PlainTip { +impl PlainTip { /// Create a new tip of the amount provided. - pub fn new(amount: u128) -> Self { + pub fn new(amount: Balance) -> Self { PlainTip { tip: amount } } } -impl From for PlainTip { - fn from(n: u128) -> Self { +impl From for PlainTip { + fn from(n: Balance) -> Self { PlainTip::new(n) } } -impl From for u128 { - fn from(tip: PlainTip) -> Self { +impl From> for Balance { + fn from(tip: PlainTip) -> Self { tip.tip } } /// A tip payment made in the form of a specific asset. #[derive(Copy, Clone, Debug, Default, Decode, Encode, Eq, PartialEq)] -pub struct AssetTip { +pub struct AssetTip { #[codec(compact)] - tip: u128, + tip: Balance, asset: Option, } -impl AssetTip { +impl AssetTip { /// Create a new tip of the amount provided. - pub fn new(amount: u128) -> Self { + pub fn new(amount: Balance) -> Self { AssetTip { tip: amount, asset: None } } @@ -253,14 +274,14 @@ impl AssetTip { } } -impl From for AssetTip { - fn from(n: u128) -> Self { +impl From for AssetTip { + fn from(n: Balance) -> Self { AssetTip::new(n) } } -impl From for u128 { - fn from(tip: AssetTip) -> Self { +impl From> for Balance { + fn from(tip: AssetTip) -> Self { tip.tip } } diff --git a/primitives/src/extrinsics.rs b/primitives/src/extrinsics.rs index 87956fcdf..8f983748a 100644 --- a/primitives/src/extrinsics.rs +++ b/primitives/src/extrinsics.rs @@ -165,7 +165,7 @@ mod tests { let tx_params = PlainTipExtrinsicParamsBuilder::new().era(Era::mortal(8, 0), Hash::from([0u8; 32])); - let default_extra = BaseExtrinsicParams::new(0, 0, 0, Hash::from([0u8; 32]), tx_params); + let default_extra = BaseExtrinsicParams::new(0, 0, 0u32, Hash::from([0u8; 32]), tx_params); let xt = UncheckedExtrinsicV4::new_signed( vec![1, 1, 1], account.into(), diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index e1f2d5abd..6b3c31cb1 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -1,3 +1,20 @@ +/* + Copyright 2019 Supercomputing Systems AG + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +*/ + #![cfg_attr(not(feature = "std"), no_std)] pub use extrinsic_params::*; @@ -11,12 +28,10 @@ pub type BlockNumber = u64; /// The timestamp moment type used in this runtime. pub type Moment = u64; /// Index of a transaction. -//fixme: make generic pub type Index = u32; pub type Hash = sp_core::H256; -//fixme: make generic pub type Balance = u128; pub use frame_system::AccountInfo as GenericAccountInfo; diff --git a/src/extrinsic/balances.rs b/src/extrinsic/balances.rs index 7e345175f..71e4032c4 100644 --- a/src/extrinsic/balances.rs +++ b/src/extrinsic/balances.rs @@ -17,7 +17,10 @@ //! Extrinsics for `pallet-balances`. -use crate::std::{Api, RpcClient}; +use crate::{ + std::{Api, RpcClient}, + Hash, Index, +}; use ac_compose_macros::compose_extrinsic; use ac_primitives::{Balance, CallIndex, ExtrinsicParams, GenericAddress, UncheckedExtrinsicV4}; use codec::Compact; @@ -41,7 +44,7 @@ where MultiSignature: From, MultiSigner: From, Client: RpcClient, - Params: ExtrinsicParams, + Params: ExtrinsicParams, { pub fn balance_transfer( &self, diff --git a/src/extrinsic/contract.rs b/src/extrinsic/contract.rs index 70960da24..04e7119aa 100644 --- a/src/extrinsic/contract.rs +++ b/src/extrinsic/contract.rs @@ -18,7 +18,10 @@ //! Extrinsics for `pallet-contract`. //! Contracts module is community maintained and not CI tested, therefore it may not work as is. -use crate::std::{Api, RpcClient}; +use crate::{ + std::{Api, RpcClient}, + Index, +}; use ac_compose_macros::compose_extrinsic; use ac_primitives::{Balance, CallIndex, ExtrinsicParams, GenericAddress, UncheckedExtrinsicV4}; use codec::Compact; @@ -61,7 +64,7 @@ where MultiSignature: From, MultiSigner: From, Client: RpcClient, - Params: ExtrinsicParams, + Params: ExtrinsicParams, { pub fn contract_put_code( &self, diff --git a/src/extrinsic/staking.rs b/src/extrinsic/staking.rs index 8eb4b1376..ca58a56a9 100644 --- a/src/extrinsic/staking.rs +++ b/src/extrinsic/staking.rs @@ -18,7 +18,7 @@ //! Extrinsics for `pallet-staking`. use super::common::*; -use crate::{Api, RpcClient}; +use crate::{Api, Hash, Index, RpcClient}; use ac_compose_macros::compose_extrinsic; use ac_primitives::{Balance, CallIndex, ExtrinsicParams, GenericAddress, UncheckedExtrinsicV4}; use codec::Compact; @@ -87,7 +87,7 @@ where MultiSignature: From, MultiSigner: From, Client: RpcClient, - Params: ExtrinsicParams, + Params: ExtrinsicParams, { /// Bond `value` amount to `controller` pub fn staking_bond( diff --git a/src/extrinsic/utility.rs b/src/extrinsic/utility.rs index e145ef890..40d3184e9 100644 --- a/src/extrinsic/utility.rs +++ b/src/extrinsic/utility.rs @@ -18,7 +18,7 @@ //! Extrinsics for `pallet-utility`. use super::common::Batch; -use crate::{Api, RpcClient}; +use crate::{Api, Hash, Index, RpcClient}; use ac_compose_macros::compose_extrinsic; use ac_primitives::{CallIndex, ExtrinsicParams, UncheckedExtrinsicV4}; use codec::Encode; @@ -39,7 +39,7 @@ where MultiSignature: From, MultiSigner: From, Client: RpcClient, - Params: ExtrinsicParams, + Params: ExtrinsicParams, { pub fn batch( &self, diff --git a/src/std/mod.rs b/src/std/mod.rs index f2dcfaa9c..33a8bb038 100644 --- a/src/std/mod.rs +++ b/src/std/mod.rs @@ -25,6 +25,7 @@ pub mod rpc; use std::convert::{TryFrom, TryInto}; +use crate::Index; use codec::{Decode, Encode}; use log::{debug, info}; use pallet_transaction_payment::{InclusionFee, RuntimeDispatchInfo}; @@ -104,7 +105,7 @@ pub trait RpcClient { pub struct Api where Client: RpcClient, - Params: ExtrinsicParams, + Params: ExtrinsicParams, { pub signer: Option

, pub genesis_hash: Hash, @@ -120,7 +121,7 @@ where MultiSignature: From, MultiSigner: From, Client: RpcClient, - Params: ExtrinsicParams, + Params: ExtrinsicParams, { pub fn signer_account(&self) -> Option { let pair = self.signer.as_ref()?; @@ -141,7 +142,7 @@ where impl Api where Client: RpcClient, - Params: ExtrinsicParams, + Params: ExtrinsicParams, { pub fn new(client: Client) -> ApiResult { let genesis_hash = Self::_get_genesis_hash(&client)?; @@ -217,7 +218,7 @@ where pub fn extrinsic_params(&self, nonce: u32) -> Params { let extrinsic_params_builder = self.extrinsic_params_builder.clone().unwrap_or_default(); - ::new( + >::new( self.runtime_version.spec_version, self.runtime_version.transaction_version, nonce, diff --git a/src/std/rpc/ws_client/mod.rs b/src/std/rpc/ws_client/mod.rs index 6f23599c7..ce84a3ab6 100644 --- a/src/std/rpc/ws_client/mod.rs +++ b/src/std/rpc/ws_client/mod.rs @@ -22,7 +22,7 @@ use crate::{ error::Error, json_req, rpc::RpcClientError, Api, ApiResult, FromHexString, RpcClient as RpcClientTrait, XtStatus, }, - utils, + utils, Hash, Index, }; use ac_node_api::DispatchError; use ac_primitives::ExtrinsicParams; @@ -88,7 +88,7 @@ pub trait Subscriber { impl Api where - Params: ExtrinsicParams, + Params: ExtrinsicParams, { pub fn default_with_url(url: &str) -> ApiResult { let client = WsRpcClient::new(url); @@ -101,7 +101,7 @@ where P: Pair, MultiSignature: From, Client: RpcClientTrait + Subscriber, - Params: ExtrinsicParams, + Params: ExtrinsicParams, { pub fn subscribe_events(&self, sender: ThreadOut) -> ApiResult<()> { debug!("subscribing to events");