ac_primitives/config/
mod.rs

1// This file was taken from subxt (Parity Technologies (UK))
2// https://github.com/paritytech/subxt/
3// And was adapted by Supercomputing Systems AG and Integritee AG.
4//
5// Copyright 2019-2022 Parity Technologies (UK) Ltd, Supercomputing Systems AG and Integritee AG.
6// This file is licensed as Apache-2.0
7// see LICENSE for license details.
8
9//! The config used by the API.
10//!
11//! This file is mostly subxt:
12//! https://github.com/paritytech/subxt/blob/ce0a82e3227efb0eae131f025da5f839d9623e15/subxt/src/config/mod.rs
13
14use codec::{Decode, Encode, FullCodec};
15use core::{fmt::Debug, marker::PhantomData};
16use serde::{de::DeserializeOwned, Serialize};
17use sp_core::Pair;
18use sp_runtime::traits::{
19	AtLeast32Bit, AtLeast32BitUnsigned, Block, Hash as HashTrait, Header as HeaderTrait,
20	MaybeSerializeDeserialize,
21};
22
23use crate::{extrinsic_params, ExtrinsicSigner, SignExtrinsic};
24
25pub use asset_runtime_config::*;
26pub use default_runtime_config::*;
27pub use rococo_runtime_config::*;
28
29pub mod asset_runtime_config;
30pub mod default_runtime_config;
31pub mod rococo_runtime_config;
32
33/// Runtime types.
34pub trait Config {
35	/// Account index (aka nonce) type. This stores the number of previous
36	/// transactions associated with a sender account.
37	/// This type enforces the (de)serialization implementation
38	/// also in no-std mode (unlike substrates MaybeSerializeDeserialize).
39	type Index: Default + Debug + Copy + DeserializeOwned + AtLeast32Bit + Decode;
40
41	/// The block number type used by the runtime.
42	type BlockNumber: Debug
43		+ Copy
44		+ Encode
45		+ Default
46		+ Serialize
47		+ DeserializeOwned
48		+ core::hash::Hash
49		+ core::str::FromStr
50		+ Into<u64>
51		+ AtLeast32BitUnsigned;
52
53	/// The output of the `Hashing` function.
54	type Hash: Debug
55		+ Copy
56		+ Send
57		+ Sync
58		+ Decode
59		+ Default
60		+ AsRef<[u8]>
61		+ Serialize
62		+ DeserializeOwned
63		+ Encode
64		+ PartialEq;
65
66	/// The account ID type.
67	type AccountId: Debug
68		+ Clone
69		+ Encode
70		+ MaybeSerializeDeserialize
71		+ From<<Self::CryptoKey as Pair>::Public>;
72
73	/// The address type.
74	type Address: Debug + Clone + Encode + From<Self::AccountId>; //type Lookup: StaticLookup<Target = Self::AccountId>;
75
76	/// The signature type.
77	type Signature: Debug + Encode + From<<Self::CryptoKey as Pair>::Signature>;
78
79	/// The hashing system (algorithm) being used in the runtime (e.g. Blake2).
80	type Hasher: Debug + HashTrait<Output = Self::Hash>;
81
82	/// The block header.
83	type Header: Debug
84		+ HeaderTrait<Number = Self::BlockNumber, Hashing = Self::Hasher>
85		+ Send
86		+ DeserializeOwned;
87
88	/// The account data.
89	type AccountData: Debug + Clone + FullCodec;
90
91	/// This type defines the extrinsic extra and additional parameters.
92	type ExtrinsicParams: extrinsic_params::ExtrinsicParams<Self::Index, Self::Hash>;
93
94	/// The cryptographic PKI key pair type used to sign the extrinsic
95	type CryptoKey: Pair;
96
97	/// This extrinsic signer.
98	type ExtrinsicSigner: SignExtrinsic<Self::AccountId>;
99
100	/// The block type.
101	type Block: Block + DeserializeOwned;
102
103	/// The balance type.
104	type Balance: Debug
105		+ Decode
106		+ Encode
107		+ AtLeast32BitUnsigned
108		+ Default
109		+ Copy
110		+ Serialize
111		+ DeserializeOwned;
112
113	/// The currency type of the contract pallet.
114	type ContractCurrency: Debug
115		+ Decode
116		+ Encode
117		+ AtLeast32BitUnsigned
118		+ Default
119		+ Copy
120		+ Serialize
121		+ DeserializeOwned;
122
123	/// The balance type of the staking pallet.
124	type StakingBalance: Debug
125		+ Decode
126		+ Encode
127		+ AtLeast32BitUnsigned
128		+ Default
129		+ Copy
130		+ Serialize
131		+ DeserializeOwned;
132}
133
134/// Helper struct for fast Config creation with different Extrinsic Params than the original Config.
135///
136/// Take a type implementing [`Config`] (eg [`AssetRuntimeConfig`]), and some type which describes the
137/// additional and extra parameters to pass to an extrinsic (see [`ExtrinsicParams`]),
138/// and returns a type implementing [`Config`] with those new [`ExtrinsicParams`].
139///
140/// # Example
141///
142/// ```
143/// use ac_primitives::{ AssetRuntimeConfig, WithExtrinsicParams, PlainTipExtrinsicParams };
144///
145/// // This is how DefaultRuntimeConfig is implemented:
146/// type DefaultRuntimeConfig = WithExtrinsicParams<AssetRuntimeConfig, PlainTipExtrinsicParams<AssetRuntimeConfig>>;
147/// ```
148#[derive(Decode, Encode, Clone, Eq, PartialEq, Debug)]
149pub struct WithExtrinsicParams<T: Config, E: extrinsic_params::ExtrinsicParams<T::Index, T::Hash>> {
150	_marker: PhantomData<(T, E)>,
151}
152
153impl<T: Config, E: extrinsic_params::ExtrinsicParams<T::Index, T::Hash>> Config
154	for WithExtrinsicParams<T, E>
155{
156	type Index = T::Index;
157	type BlockNumber = T::BlockNumber;
158	type Hash = T::Hash;
159	type AccountId = T::AccountId;
160	type Address = T::Address;
161	type Signature = T::Signature;
162	type Hasher = T::Hasher;
163	type Header = T::Header;
164	type AccountData = T::AccountData;
165	type ExtrinsicParams = E;
166	type CryptoKey = T::CryptoKey;
167	type ExtrinsicSigner = ExtrinsicSigner<Self>;
168	type Block = T::Block;
169	type Balance = T::Balance;
170	type ContractCurrency = T::ContractCurrency;
171	type StakingBalance = T::StakingBalance;
172}
173
174/// Changes the Address type of the underlying Runtime Config. This allows to change the type of the Config without
175/// having to define all the other types as well.
176///
177/// # Example
178///
179/// ```
180/// use ac_primitives::{ DefaultRuntimeConfig, WithAddress, MultiAddress, AccountId32 };
181///
182/// type RococoRuntimeConfig = WithAddress<DefaultRuntimeConfig, MultiAddress<AccountId32, ()>>;
183/// ```
184#[derive(Decode, Encode, Clone, Eq, PartialEq, Debug)]
185pub struct WithAddress<T, A>
186where
187	T: Config,
188	A: Debug + Clone + Encode + From<T::AccountId>,
189{
190	_marker: PhantomData<(T, A)>,
191}
192
193impl<T, A> Config for WithAddress<T, A>
194where
195	T: Config,
196	A: Debug + Clone + Encode + From<T::AccountId>,
197{
198	type Index = T::Index;
199	type BlockNumber = T::BlockNumber;
200	type Hash = T::Hash;
201	type AccountId = T::AccountId;
202	type Address = A;
203	type Signature = T::Signature;
204	type Hasher = T::Hasher;
205	type Header = T::Header;
206	type AccountData = T::AccountData;
207	type ExtrinsicParams = T::ExtrinsicParams;
208	type CryptoKey = T::CryptoKey;
209	type ExtrinsicSigner = ExtrinsicSigner<Self>;
210	type Block = T::Block;
211	type Balance = T::Balance;
212	type ContractCurrency = T::ContractCurrency;
213	type StakingBalance = T::StakingBalance;
214}