From 3eef2bb4c19e993c89b68a6244046bcc4753d51d Mon Sep 17 00:00:00 2001 From: nuno Date: Sun, 12 Jun 2022 20:45:34 +0200 Subject: [PATCH 1/6] traits: add asset_registry::{Inspect, Mutate} --- traits/src/asset_registry.rs | 44 ++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/traits/src/asset_registry.rs b/traits/src/asset_registry.rs index 23b090e35..abefb0005 100644 --- a/traits/src/asset_registry.rs +++ b/traits/src/asset_registry.rs @@ -1,5 +1,7 @@ use frame_support::pallet_prelude::*; +use sp_runtime::DispatchResult; use xcm::latest::prelude::*; +use xcm::VersionedMultiLocation; pub trait WeightToFeeConverter { fn convert_weight_to_fee(location: &MultiLocation, weight: Weight) -> Option; @@ -15,3 +17,45 @@ pub trait AssetProcessor { Ok(()) } } + +/// Data describing the asset properties. +#[derive(scale_info::TypeInfo, Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug)] +pub struct AssetMetadata { + pub decimals: u32, + pub name: Vec, + pub symbol: Vec, + pub existential_deposit: Balance, + pub location: Option, + pub additional: CustomMetadata, +} + +pub trait Inspect { + /// AssetId type + type AssetId; + /// Balance type + type Balance; + /// Custom metadata type + type CustomMetadata: Parameter + Member + TypeInfo; + + fn asset_id(location: &MultiLocation) -> Option; + fn metadata(asset_id: &Self::AssetId) -> Option>; + fn metadata_by_location(location: &MultiLocation) -> Option>; + fn location(asset_id: &Self::AssetId) -> Result, DispatchError>; +} + +pub trait Mutate: Inspect { + fn register_asset( + asset_id: Option, + metadata: AssetMetadata, + ) -> DispatchResult; + + fn update_asset( + asset_id: Self::AssetId, + decimals: Option, + name: Option>, + symbol: Option>, + existential_deposit: Option, + location: Option>, + additional: Option, + ) -> DispatchResult; +} From 4b53b3c924dadc31588e0826743890f998987c05 Mon Sep 17 00:00:00 2001 From: nuno Date: Sun, 12 Jun 2022 20:52:11 +0200 Subject: [PATCH 2/6] Move to use traits::asset_registry::AssetMetadata The AssetMetadata type is needed by the Inspect and Mutate traits so it needs to be in a shared place. While it's fine for the pallet to depend on the traits::asset_registry, the inverse not so much, so we move the struct to the traits section and import it here. --- asset-registry/src/lib.rs | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/asset-registry/src/lib.rs b/asset-registry/src/lib.rs index a3fc951d6..41cbadc80 100644 --- a/asset-registry/src/lib.rs +++ b/asset-registry/src/lib.rs @@ -4,7 +4,7 @@ #![allow(clippy::large_enum_variant)] use frame_support::{pallet_prelude::*, traits::EnsureOriginWithArg, transactional}; use frame_system::pallet_prelude::*; -use orml_traits::asset_registry::AssetProcessor; +use orml_traits::asset_registry::{AssetMetadata, AssetProcessor}; use scale_info::TypeInfo; use sp_runtime::{ traits::{AtLeast32BitUnsigned, Member}, @@ -25,17 +25,6 @@ mod mock; #[cfg(test)] mod tests; -/// Data describing the asset properties. -#[derive(scale_info::TypeInfo, Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug)] -pub struct AssetMetadata { - pub decimals: u32, - pub name: Vec, - pub symbol: Vec, - pub existential_deposit: Balance, - pub location: Option, - pub additional: CustomMetadata, -} - #[frame_support::pallet] pub mod module { use super::*; From 5074049fff14f148dec9662cf8283a5b56aa711f Mon Sep 17 00:00:00 2001 From: nuno Date: Sun, 12 Jun 2022 20:55:29 +0200 Subject: [PATCH 3/6] asset-registry: Implement Inspect & Mutate --- asset-registry/src/impls.rs | 57 +++++++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/asset-registry/src/impls.rs b/asset-registry/src/impls.rs index cdc314434..351d095c9 100644 --- a/asset-registry/src/impls.rs +++ b/asset-registry/src/impls.rs @@ -1,7 +1,9 @@ -use crate::{module::*, AssetMetadata}; +use crate::module::*; use frame_support::{log, pallet_prelude::*, weights::constants::WEIGHT_PER_SECOND}; use orml_traits::{ - asset_registry::{AssetProcessor, FixedConversionRateProvider, WeightToFeeConverter}, + asset_registry::{ + AssetMetadata, AssetProcessor, FixedConversionRateProvider, Inspect, Mutate, WeightToFeeConverter, + }, GetByKey, }; use sp_runtime::FixedPointNumber; @@ -169,3 +171,54 @@ impl GetByKey for ExistentialDeposits { } } } + +impl Inspect for Pallet { + type AssetId = T::AssetId; + type Balance = T::Balance; + type CustomMetadata = T::CustomMetadata; + + fn asset_id(location: &MultiLocation) -> Option { + Pallet::::location_to_asset_id(location) + } + + fn metadata(id: &Self::AssetId) -> Option> { + Pallet::::metadata(id) + } + + fn metadata_by_location(location: &MultiLocation) -> Option> { + Pallet::::fetch_metadata_by_location(location) + } + + fn location(asset_id: &Self::AssetId) -> Result, DispatchError> { + Pallet::::multilocation(asset_id) + } +} + +impl Mutate for Pallet { + fn register_asset( + asset_id: Option, + metadata: AssetMetadata, + ) -> DispatchResult { + Pallet::::do_register_asset(metadata, asset_id) + } + + fn update_asset( + asset_id: Self::AssetId, + decimals: Option, + name: Option>, + symbol: Option>, + existential_deposit: Option, + location: Option>, + additional: Option, + ) -> DispatchResult { + Pallet::::do_update_asset( + asset_id, + decimals, + name, + symbol, + existential_deposit, + location, + additional, + ) + } +} From da9273a9930c3428ca47e8166af0b608ad4ca570 Mon Sep 17 00:00:00 2001 From: nuno Date: Mon, 13 Jun 2022 08:23:49 +0200 Subject: [PATCH 4/6] fixup: use sp_std::vec::Vec; --- traits/src/asset_registry.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/traits/src/asset_registry.rs b/traits/src/asset_registry.rs index abefb0005..a57c29cd7 100644 --- a/traits/src/asset_registry.rs +++ b/traits/src/asset_registry.rs @@ -1,5 +1,6 @@ use frame_support::pallet_prelude::*; use sp_runtime::DispatchResult; +use sp_std::vec::Vec; use xcm::latest::prelude::*; use xcm::VersionedMultiLocation; From 3745a1b07a068e781359888900c6ae4134eef419 Mon Sep 17 00:00:00 2001 From: nuno Date: Mon, 13 Jun 2022 13:05:56 +0200 Subject: [PATCH 5/6] review: clean up imports --- asset-registry/src/impls.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/asset-registry/src/impls.rs b/asset-registry/src/impls.rs index 351d095c9..1703ebea8 100644 --- a/asset-registry/src/impls.rs +++ b/asset-registry/src/impls.rs @@ -12,7 +12,8 @@ use sp_runtime::{ ArithmeticError, FixedU128, }; use sp_std::prelude::*; -use xcm::v2::prelude::*; +use xcm::latest::prelude::*; +use xcm::VersionedMultiLocation; use xcm_builder::TakeRevenue; use xcm_executor::{traits::WeightTrader, Assets}; @@ -208,7 +209,7 @@ impl Mutate for Pallet { name: Option>, symbol: Option>, existential_deposit: Option, - location: Option>, + location: Option>, additional: Option, ) -> DispatchResult { Pallet::::do_update_asset( From 1908d7f81165d5b4ce9fe62a5bd707af93049f36 Mon Sep 17 00:00:00 2001 From: nuno Date: Mon, 13 Jun 2022 14:37:27 +0200 Subject: [PATCH 6/6] asset-registry: pub use AssetMetadata Doing so makes logical sense and smoothes the update to this new version. --- asset-registry/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/asset-registry/src/lib.rs b/asset-registry/src/lib.rs index 41cbadc80..21150e8b2 100644 --- a/asset-registry/src/lib.rs +++ b/asset-registry/src/lib.rs @@ -4,7 +4,8 @@ #![allow(clippy::large_enum_variant)] use frame_support::{pallet_prelude::*, traits::EnsureOriginWithArg, transactional}; use frame_system::pallet_prelude::*; -use orml_traits::asset_registry::{AssetMetadata, AssetProcessor}; +pub use orml_traits::asset_registry::AssetMetadata; +use orml_traits::asset_registry::AssetProcessor; use scale_info::TypeInfo; use sp_runtime::{ traits::{AtLeast32BitUnsigned, Member},