Skip to content

Commit 9bf1101

Browse files
asset-registry: And and Implement Inspect + Mutate traits (#767)
* traits: add asset_registry::{Inspect, Mutate} * 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: Implement Inspect & Mutate * fixup: use sp_std::vec::Vec; * review: clean up imports * asset-registry: pub use AssetMetadata Doing so makes logical sense and smoothes the update to this new version.
1 parent cc148a7 commit 9bf1101

3 files changed

Lines changed: 103 additions & 14 deletions

File tree

asset-registry/src/impls.rs

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
use crate::{module::*, AssetMetadata};
1+
use crate::module::*;
22
use frame_support::{log, pallet_prelude::*, weights::constants::WEIGHT_PER_SECOND};
33
use orml_traits::{
4-
asset_registry::{AssetProcessor, FixedConversionRateProvider, WeightToFeeConverter},
4+
asset_registry::{
5+
AssetMetadata, AssetProcessor, FixedConversionRateProvider, Inspect, Mutate, WeightToFeeConverter,
6+
},
57
GetByKey,
68
};
79
use sp_runtime::FixedPointNumber;
@@ -10,7 +12,8 @@ use sp_runtime::{
1012
ArithmeticError, FixedU128,
1113
};
1214
use sp_std::prelude::*;
13-
use xcm::v2::prelude::*;
15+
use xcm::latest::prelude::*;
16+
use xcm::VersionedMultiLocation;
1417
use xcm_builder::TakeRevenue;
1518
use xcm_executor::{traits::WeightTrader, Assets};
1619

@@ -169,3 +172,54 @@ impl<T: Config> GetByKey<T::AssetId, T::Balance> for ExistentialDeposits<T> {
169172
}
170173
}
171174
}
175+
176+
impl<T: Config> Inspect for Pallet<T> {
177+
type AssetId = T::AssetId;
178+
type Balance = T::Balance;
179+
type CustomMetadata = T::CustomMetadata;
180+
181+
fn asset_id(location: &MultiLocation) -> Option<Self::AssetId> {
182+
Pallet::<T>::location_to_asset_id(location)
183+
}
184+
185+
fn metadata(id: &Self::AssetId) -> Option<AssetMetadata<Self::Balance, Self::CustomMetadata>> {
186+
Pallet::<T>::metadata(id)
187+
}
188+
189+
fn metadata_by_location(location: &MultiLocation) -> Option<AssetMetadata<Self::Balance, Self::CustomMetadata>> {
190+
Pallet::<T>::fetch_metadata_by_location(location)
191+
}
192+
193+
fn location(asset_id: &Self::AssetId) -> Result<Option<MultiLocation>, DispatchError> {
194+
Pallet::<T>::multilocation(asset_id)
195+
}
196+
}
197+
198+
impl<T: Config> Mutate for Pallet<T> {
199+
fn register_asset(
200+
asset_id: Option<Self::AssetId>,
201+
metadata: AssetMetadata<Self::Balance, Self::CustomMetadata>,
202+
) -> DispatchResult {
203+
Pallet::<T>::do_register_asset(metadata, asset_id)
204+
}
205+
206+
fn update_asset(
207+
asset_id: Self::AssetId,
208+
decimals: Option<u32>,
209+
name: Option<Vec<u8>>,
210+
symbol: Option<Vec<u8>>,
211+
existential_deposit: Option<Self::Balance>,
212+
location: Option<Option<VersionedMultiLocation>>,
213+
additional: Option<Self::CustomMetadata>,
214+
) -> DispatchResult {
215+
Pallet::<T>::do_update_asset(
216+
asset_id,
217+
decimals,
218+
name,
219+
symbol,
220+
existential_deposit,
221+
location,
222+
additional,
223+
)
224+
}
225+
}

asset-registry/src/lib.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#![allow(clippy::large_enum_variant)]
55
use frame_support::{pallet_prelude::*, traits::EnsureOriginWithArg, transactional};
66
use frame_system::pallet_prelude::*;
7+
pub use orml_traits::asset_registry::AssetMetadata;
78
use orml_traits::asset_registry::AssetProcessor;
89
use scale_info::TypeInfo;
910
use sp_runtime::{
@@ -25,17 +26,6 @@ mod mock;
2526
#[cfg(test)]
2627
mod tests;
2728

28-
/// Data describing the asset properties.
29-
#[derive(scale_info::TypeInfo, Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug)]
30-
pub struct AssetMetadata<Balance, CustomMetadata: Parameter + Member + TypeInfo> {
31-
pub decimals: u32,
32-
pub name: Vec<u8>,
33-
pub symbol: Vec<u8>,
34-
pub existential_deposit: Balance,
35-
pub location: Option<VersionedMultiLocation>,
36-
pub additional: CustomMetadata,
37-
}
38-
3929
#[frame_support::pallet]
4030
pub mod module {
4131
use super::*;

traits/src/asset_registry.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use frame_support::pallet_prelude::*;
2+
use sp_runtime::DispatchResult;
3+
use sp_std::vec::Vec;
24
use xcm::latest::prelude::*;
5+
use xcm::VersionedMultiLocation;
36

47
pub trait WeightToFeeConverter {
58
fn convert_weight_to_fee(location: &MultiLocation, weight: Weight) -> Option<u128>;
@@ -15,3 +18,45 @@ pub trait AssetProcessor<AssetId, Metadata> {
1518
Ok(())
1619
}
1720
}
21+
22+
/// Data describing the asset properties.
23+
#[derive(scale_info::TypeInfo, Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug)]
24+
pub struct AssetMetadata<Balance, CustomMetadata: Parameter + Member + TypeInfo> {
25+
pub decimals: u32,
26+
pub name: Vec<u8>,
27+
pub symbol: Vec<u8>,
28+
pub existential_deposit: Balance,
29+
pub location: Option<VersionedMultiLocation>,
30+
pub additional: CustomMetadata,
31+
}
32+
33+
pub trait Inspect {
34+
/// AssetId type
35+
type AssetId;
36+
/// Balance type
37+
type Balance;
38+
/// Custom metadata type
39+
type CustomMetadata: Parameter + Member + TypeInfo;
40+
41+
fn asset_id(location: &MultiLocation) -> Option<Self::AssetId>;
42+
fn metadata(asset_id: &Self::AssetId) -> Option<AssetMetadata<Self::Balance, Self::CustomMetadata>>;
43+
fn metadata_by_location(location: &MultiLocation) -> Option<AssetMetadata<Self::Balance, Self::CustomMetadata>>;
44+
fn location(asset_id: &Self::AssetId) -> Result<Option<MultiLocation>, DispatchError>;
45+
}
46+
47+
pub trait Mutate: Inspect {
48+
fn register_asset(
49+
asset_id: Option<Self::AssetId>,
50+
metadata: AssetMetadata<Self::Balance, Self::CustomMetadata>,
51+
) -> DispatchResult;
52+
53+
fn update_asset(
54+
asset_id: Self::AssetId,
55+
decimals: Option<u32>,
56+
name: Option<Vec<u8>>,
57+
symbol: Option<Vec<u8>>,
58+
existential_deposit: Option<Self::Balance>,
59+
location: Option<Option<VersionedMultiLocation>>,
60+
additional: Option<Self::CustomMetadata>,
61+
) -> DispatchResult;
62+
}

0 commit comments

Comments
 (0)