From 324789c88b276fb7aec2b9e150d37bab959ae6a3 Mon Sep 17 00:00:00 2001 From: Sergej Date: Sat, 13 Apr 2024 11:01:46 +0200 Subject: [PATCH] Add asset-registry --- Cargo.lock | 64 ++++++++++++++++++++++++++++++++ Cargo.toml | 4 ++ runtime/primitives/Cargo.toml | 4 ++ runtime/primitives/src/assets.rs | 18 +++++++++ runtime/primitives/src/lib.rs | 2 + runtime/regionx/Cargo.toml | 7 ++++ runtime/regionx/src/impls.rs | 30 +++++++++++++++ runtime/regionx/src/lib.rs | 52 ++++++++++++++++++-------- 8 files changed, 165 insertions(+), 16 deletions(-) create mode 100644 runtime/primitives/src/assets.rs create mode 100644 runtime/regionx/src/impls.rs diff --git a/Cargo.lock b/Cargo.lock index bf6aa5c7..aee179dd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5892,6 +5892,66 @@ dependencies = [ "num-traits", ] +[[package]] +name = "orml-asset-registry" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e98494d6c579cbfa0a3fd3b1b274468baf4aa8ba74ce152a4ae575d8dec021b" +dependencies = [ + "frame-support", + "frame-system", + "log", + "orml-traits", + "pallet-xcm", + "parity-scale-codec", + "polkadot-runtime-common", + "scale-info", + "serde", + "sp-io", + "sp-runtime", + "sp-std", + "staging-xcm", + "staging-xcm-builder", + "staging-xcm-executor", +] + +[[package]] +name = "orml-traits" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1f4617d5262e9a8f3b5e7ab5961e8d0a3d4e25ef0c9d34e6ff87766c406898b" +dependencies = [ + "frame-support", + "impl-trait-for-tuples", + "num-traits", + "orml-utilities", + "parity-scale-codec", + "paste", + "scale-info", + "serde", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", + "staging-xcm", +] + +[[package]] +name = "orml-utilities" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1756d8ca7c433fdb538ff14bbb60a343fc6cf7b0684af4a73889f89de364e18f" +dependencies = [ + "frame-support", + "parity-scale-codec", + "scale-info", + "serde", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", +] + [[package]] name = "pallet-asset-rate" version = "7.0.0" @@ -9235,6 +9295,8 @@ dependencies = [ name = "regionx-primitives" version = "0.1.0" dependencies = [ + "parity-scale-codec", + "scale-info", "sp-core", "sp-runtime", ] @@ -9265,6 +9327,8 @@ dependencies = [ "ismp-parachain-runtime-api", "ismp-runtime-api", "log", + "orml-asset-registry", + "orml-traits", "pallet-assets", "pallet-aura", "pallet-authorship", diff --git a/Cargo.toml b/Cargo.toml index 64fbfa1f..d93c8354 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -128,6 +128,10 @@ cumulus-client-collator = "0.7.0" substrate-wasm-builder = { version = "17.0.0" } substrate-build-script-utils = "11.0.0" +# Orml +orml-asset-registry = { version = "0.7.0", default-features = false } +orml-traits = { version = "0.7.0", default-features = false } + # Polytope Labs ismp = { git="https://github.com/Szegoo/hyperbridge.git", branch="fix-try-runtime", default-features = false } pallet-ismp = { git="https://github.com/Szegoo/hyperbridge.git", branch="fix-try-runtime", default-features = false } diff --git a/runtime/primitives/Cargo.toml b/runtime/primitives/Cargo.toml index fa33f0b3..3e47f8fd 100644 --- a/runtime/primitives/Cargo.toml +++ b/runtime/primitives/Cargo.toml @@ -7,6 +7,8 @@ homepage.workspace = true repository.workspace = true [dependencies] +parity-scale-codec = { workspace = true } +scale-info = { workspace = true } sp-core = { workspace = true, default-features = false } sp-runtime = { workspace = true, default-features = false } @@ -14,6 +16,8 @@ sp-runtime = { workspace = true, default-features = false } [features] default = ["std"] std = [ + "parity-scale-codec/std", + "scale-info/std", "sp-core/std", "sp-runtime/std", ] diff --git a/runtime/primitives/src/assets.rs b/runtime/primitives/src/assets.rs new file mode 100644 index 00000000..2c79115f --- /dev/null +++ b/runtime/primitives/src/assets.rs @@ -0,0 +1,18 @@ +use parity_scale_codec::{Decode, Encode, MaxEncodedLen}; +use scale_info::TypeInfo; + +#[derive( + Clone, + Copy, + Default, + PartialOrd, + Ord, + PartialEq, + Eq, + Debug, + Encode, + Decode, + TypeInfo, + MaxEncodedLen, +)] +pub struct CustomMetadata; diff --git a/runtime/primitives/src/lib.rs b/runtime/primitives/src/lib.rs index b0117db2..54059396 100644 --- a/runtime/primitives/src/lib.rs +++ b/runtime/primitives/src/lib.rs @@ -15,6 +15,8 @@ #![cfg_attr(not(feature = "std"), no_std)] +pub mod assets; + use sp_runtime::{ generic, traits::{BlakeTwo256, IdentifyAccount, Verify}, diff --git a/runtime/regionx/Cargo.toml b/runtime/regionx/Cargo.toml index b9d2bdc3..54f90845 100644 --- a/runtime/regionx/Cargo.toml +++ b/runtime/regionx/Cargo.toml @@ -29,6 +29,10 @@ ismp-parachain = { workspace = true, default-features = false } ismp-runtime-api = { workspace = true, default-features = false } ismp-parachain-runtime-api = { workspace = true, default-features = false } +# Orml +orml-asset-registry = { workspace = true } +orml-traits = { workspace = true } + # Substrate frame-benchmarking = { workspace = true, optional = true } frame-executive = { workspace = true } @@ -107,6 +111,7 @@ std = [ "frame-system/std", "frame-try-runtime?/std", "log/std", + "orml-asset-registry/std", "pallet-assets/std", "pallet-aura/std", "pallet-authorship/std", @@ -154,6 +159,7 @@ runtime-benchmarks = [ "frame-system-benchmarking/runtime-benchmarks", "frame-system/runtime-benchmarks", "hex-literal", + "orml-asset-registry/runtime-benchmarks", "pallet-assets/runtime-benchmarks", "pallet-balances/runtime-benchmarks", "pallet-collator-selection/runtime-benchmarks", @@ -179,6 +185,7 @@ try-runtime = [ "frame-support/try-runtime", "frame-system/try-runtime", "frame-try-runtime/try-runtime", + "orml-asset-registry/try-runtime", "pallet-ismp/try-runtime", "ismp-parachain/try-runtime", "pallet-assets/try-runtime", diff --git a/runtime/regionx/src/impls.rs b/runtime/regionx/src/impls.rs new file mode 100644 index 00000000..55f0edca --- /dev/null +++ b/runtime/regionx/src/impls.rs @@ -0,0 +1,30 @@ +use crate::{AssetId, Runtime}; +use orml_asset_registry::DefaultAssetMetadata; +use orml_traits::asset_registry::AssetProcessor; +use parity_scale_codec::{Decode, Encode, MaxEncodedLen}; +use scale_info::TypeInfo; +use sp_runtime::DispatchError; + +#[derive( + Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Debug, Encode, Decode, TypeInfo, MaxEncodedLen, +)] +pub struct CustomAssetProcessor; + +impl AssetProcessor> for CustomAssetProcessor { + fn pre_register( + id: Option, + metadata: DefaultAssetMetadata, + ) -> Result<(AssetId, DefaultAssetMetadata), DispatchError> { + match id { + Some(id) => Ok((id, metadata)), + None => Err(DispatchError::Other("asset-registry: AssetId is required")), + } + } + + fn post_register( + _id: AssetId, + _metadata: DefaultAssetMetadata, + ) -> Result<(), DispatchError> { + Ok(()) + } +} diff --git a/runtime/regionx/src/lib.rs b/runtime/regionx/src/lib.rs index 1a1e9296..bf0961b7 100644 --- a/runtime/regionx/src/lib.rs +++ b/runtime/regionx/src/lib.rs @@ -26,8 +26,11 @@ extern crate alloc; mod weights; pub mod xcm_config; +mod impls; mod ismp; +use impls::*; + use cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases; use cumulus_primitives_core::{AggregateMessageOrigin, ParaId}; use frame_support::traits::TransformOrigin; @@ -93,7 +96,8 @@ use weights::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight}; use xcm::latest::prelude::BodyId; use regionx_primitives::{ - AccountId, Address, Balance, BlockNumber, Hash, Header, Nonce, Signature, + assets::CustomMetadata, AccountId, Address, Balance, BlockNumber, Hash, Header, Nonce, + Signature, }; pub type Block = generic::Block; @@ -228,6 +232,8 @@ const MAXIMUM_BLOCK_WEIGHT: Weight = Weight::from_parts( cumulus_primitives_core::relay_chain::MAX_POV_SIZE as u64, ); +type AssetId = u32; + /// The version information used to identify this runtime when compiled natively. #[cfg(feature = "std")] pub fn native_version() -> NativeVersion { @@ -366,11 +372,11 @@ parameter_types! { impl pallet_assets::Config for Runtime { type RuntimeEvent = RuntimeEvent; type Balance = Balance; - type AssetId = u32; - type AssetIdParameter = parity_scale_codec::Compact; + type AssetId = AssetId; + type AssetIdParameter = parity_scale_codec::Compact; type Currency = Balances; // TODO after https://github.com/RegionX-Labs/RegionX-Node/issues/72: - // Allow only TC to create an asset as well. + // Allow only TC to create an asset. type CreateOrigin = AsEnsureOriginWithArg>; type ForceOrigin = EnsureRoot; type AssetDeposit = AssetDeposit; @@ -389,6 +395,19 @@ impl pallet_assets::Config for Runtime { type BenchmarkHelper = (); } +impl orml_asset_registry::Config for Runtime { + type AssetId = AssetId; + type AssetProcessor = CustomAssetProcessor; + // TODO after https://github.com/RegionX-Labs/RegionX-Node/issues/72: + // Allow TC to register an asset. + type AuthorityOrigin = EnsureRoot; + type Balance = Balance; + type CustomMetadata = CustomMetadata; + type StringLimit = AssetsStringLimit; + type RuntimeEvent = RuntimeEvent; + type WeightInfo = (); +} + parameter_types! { /// Relay Chain `TransactionByteFee` / 10 pub const TransactionByteFee: Balance = 10 * MICROM4X; @@ -548,26 +567,27 @@ construct_runtime!( Balances: pallet_balances = 10, TransactionPayment: pallet_transaction_payment = 11, Assets: pallet_assets = 12, + OrmlAssetRegistry: orml_asset_registry = 13, // Governance - Sudo: pallet_sudo = 15, + Sudo: pallet_sudo = 20, // Collator support. The order of these 4 are important and shall not change. - Authorship: pallet_authorship = 20, - CollatorSelection: pallet_collator_selection = 21, - Session: pallet_session = 22, - Aura: pallet_aura = 23, - AuraExt: cumulus_pallet_aura_ext = 24, + Authorship: pallet_authorship = 30, + CollatorSelection: pallet_collator_selection = 31, + Session: pallet_session = 32, + Aura: pallet_aura = 33, + AuraExt: cumulus_pallet_aura_ext = 34, // XCM helpers. - XcmpQueue: cumulus_pallet_xcmp_queue = 30, - PolkadotXcm: pallet_xcm = 31, - CumulusXcm: cumulus_pallet_xcm = 32, - MessageQueue: pallet_message_queue = 33, + XcmpQueue: cumulus_pallet_xcmp_queue = 40, + PolkadotXcm: pallet_xcm = 41, + CumulusXcm: cumulus_pallet_xcm = 42, + MessageQueue: pallet_message_queue = 43, // ISMP - Ismp: pallet_ismp = 40, - IsmpParachain: ismp_parachain = 41, + Ismp: pallet_ismp = 50, + IsmpParachain: ismp_parachain = 51, } );