Skip to content

asset-registry: And and Implement Inspect + Mutate traits #767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 57 additions & 3 deletions asset-registry/src/impls.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,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};

Expand Down Expand Up @@ -169,3 +172,54 @@ impl<T: Config> GetByKey<T::AssetId, T::Balance> for ExistentialDeposits<T> {
}
}
}

impl<T: Config> Inspect for Pallet<T> {
type AssetId = T::AssetId;
type Balance = T::Balance;
type CustomMetadata = T::CustomMetadata;

fn asset_id(location: &MultiLocation) -> Option<Self::AssetId> {
Pallet::<T>::location_to_asset_id(location)
}

fn metadata(id: &Self::AssetId) -> Option<AssetMetadata<Self::Balance, Self::CustomMetadata>> {
Pallet::<T>::metadata(id)
}

fn metadata_by_location(location: &MultiLocation) -> Option<AssetMetadata<Self::Balance, Self::CustomMetadata>> {
Pallet::<T>::fetch_metadata_by_location(location)
}

fn location(asset_id: &Self::AssetId) -> Result<Option<MultiLocation>, DispatchError> {
Pallet::<T>::multilocation(asset_id)
}
}

impl<T: Config> Mutate for Pallet<T> {
fn register_asset(
asset_id: Option<Self::AssetId>,
metadata: AssetMetadata<Self::Balance, Self::CustomMetadata>,
) -> DispatchResult {
Pallet::<T>::do_register_asset(metadata, asset_id)
}

fn update_asset(
asset_id: Self::AssetId,
decimals: Option<u32>,
name: Option<Vec<u8>>,
symbol: Option<Vec<u8>>,
existential_deposit: Option<Self::Balance>,
location: Option<Option<VersionedMultiLocation>>,
additional: Option<Self::CustomMetadata>,
) -> DispatchResult {
Pallet::<T>::do_update_asset(
asset_id,
decimals,
name,
symbol,
existential_deposit,
location,
additional,
)
}
}
12 changes: 1 addition & 11 deletions asset-registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#![allow(clippy::large_enum_variant)]
use frame_support::{pallet_prelude::*, traits::EnsureOriginWithArg, transactional};
use frame_system::pallet_prelude::*;
pub use orml_traits::asset_registry::AssetMetadata;
use orml_traits::asset_registry::AssetProcessor;
use scale_info::TypeInfo;
use sp_runtime::{
Expand All @@ -25,17 +26,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<Balance, CustomMetadata: Parameter + Member + TypeInfo> {
pub decimals: u32,
pub name: Vec<u8>,
pub symbol: Vec<u8>,
pub existential_deposit: Balance,
pub location: Option<VersionedMultiLocation>,
pub additional: CustomMetadata,
}

#[frame_support::pallet]
pub mod module {
use super::*;
Expand Down
45 changes: 45 additions & 0 deletions traits/src/asset_registry.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use frame_support::pallet_prelude::*;
use sp_runtime::DispatchResult;
use sp_std::vec::Vec;
use xcm::latest::prelude::*;
use xcm::VersionedMultiLocation;

pub trait WeightToFeeConverter {
fn convert_weight_to_fee(location: &MultiLocation, weight: Weight) -> Option<u128>;
Expand All @@ -15,3 +18,45 @@ pub trait AssetProcessor<AssetId, Metadata> {
Ok(())
}
}

/// Data describing the asset properties.
#[derive(scale_info::TypeInfo, Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug)]
pub struct AssetMetadata<Balance, CustomMetadata: Parameter + Member + TypeInfo> {
pub decimals: u32,
pub name: Vec<u8>,
pub symbol: Vec<u8>,
pub existential_deposit: Balance,
pub location: Option<VersionedMultiLocation>,
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<Self::AssetId>;
fn metadata(asset_id: &Self::AssetId) -> Option<AssetMetadata<Self::Balance, Self::CustomMetadata>>;
fn metadata_by_location(location: &MultiLocation) -> Option<AssetMetadata<Self::Balance, Self::CustomMetadata>>;
fn location(asset_id: &Self::AssetId) -> Result<Option<MultiLocation>, DispatchError>;
}

pub trait Mutate: Inspect {
fn register_asset(
asset_id: Option<Self::AssetId>,
metadata: AssetMetadata<Self::Balance, Self::CustomMetadata>,
) -> DispatchResult;

fn update_asset(
asset_id: Self::AssetId,
decimals: Option<u32>,
name: Option<Vec<u8>>,
symbol: Option<Vec<u8>>,
existential_deposit: Option<Self::Balance>,
location: Option<Option<VersionedMultiLocation>>,
additional: Option<Self::CustomMetadata>,
) -> DispatchResult;
}