Skip to content
This repository has been archived by the owner on May 30, 2023. It is now read-only.

Commit

Permalink
Merge pull request #209 from galacticcouncil/feat/add-xcm-rate-limit-…
Browse files Browse the repository at this point in the history
…to-registry

feat: add xcm rate limit to registry and remove locked
  • Loading branch information
mrq1911 committed May 29, 2023
2 parents d319127 + 3bc5c9a commit 90e9669
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 42 deletions.
18 changes: 10 additions & 8 deletions asset-registry/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ benchmarks! {
decimals: 100,
};

}: _(RawOrigin::Root, name.clone(), AssetType::Token, ed, None, Some(metadata), Some(Default::default()))
}: _(RawOrigin::Root, name.clone(), AssetType::Token, ed, None, Some(metadata), Some(Default::default()), None)
verify {
let bname = crate::Pallet::<T>::to_bounded_name(name).unwrap();
assert!(crate::Pallet::<T>::asset_ids(bname).is_some());
Expand All @@ -48,13 +48,15 @@ benchmarks! {
let name = b"NAME".to_vec();
let ed = T::Balance::from(1_000_000u32);
let asset_id = T::AssetId::from(10u8);
let _ = crate::Pallet::<T>::register(RawOrigin::Root.into(), name, AssetType::Token, ed, Some(asset_id),None,None);
let _ = crate::Pallet::<T>::register(RawOrigin::Root.into(), name, AssetType::Token, ed, Some(asset_id), None, None, None);

let new_name= vec![1; T::StringLimit::get() as usize];

let new_ed = T::Balance::from(2_000_000u32);

}: _(RawOrigin::Root, asset_id, new_name.clone(), AssetType::PoolShare(T::AssetId::from(10u8),T::AssetId::from(20u8)), Some(new_ed))
let rate_limit = T::Balance::from(10_000_000u32);

}: _(RawOrigin::Root, asset_id, new_name.clone(), AssetType::PoolShare(T::AssetId::from(10u8),T::AssetId::from(20u8)), Some(new_ed), Some(rate_limit))
verify {
let bname = crate::Pallet::<T>::to_bounded_name(new_name).unwrap();
assert_eq!(crate::Pallet::<T>::asset_ids(&bname), Some(asset_id));
Expand All @@ -66,12 +68,12 @@ benchmarks! {

let expected = AssetDetails{
asset_type: AssetType::PoolShare(T::AssetId::from(10u8), T::AssetId::from(20u8)),
locked: false,
existential_deposit: new_ed,
name: bname,};
name: bname,
xcm_rate_limit: Some(rate_limit),
};

assert_eq!(stored.asset_type, expected.asset_type);
assert_eq!(stored.locked, expected.locked);
assert_eq!(stored.existential_deposit, expected.existential_deposit);
assert_eq!(stored.name.to_vec(), expected.name.to_vec());
}
Expand All @@ -80,7 +82,7 @@ benchmarks! {
let name = b"NAME".to_vec();
let bname = crate::Pallet::<T>::to_bounded_name(name.clone()).unwrap();
let ed = T::Balance::from(1_000_000u32);
let _ = crate::Pallet::<T>::register(RawOrigin::Root.into(), name, AssetType::Token, ed, None, None, None);
let _ = crate::Pallet::<T>::register(RawOrigin::Root.into(), name, AssetType::Token, ed, None, None, None, None);

let asset_id = crate::Pallet::<T>::asset_ids(bname).unwrap();

Expand Down Expand Up @@ -109,7 +111,7 @@ benchmarks! {
let name = b"NAME".to_vec();
let ed = T::Balance::from(1_000_000u32);
let asset_id = T::AssetId::from(10u8);
let _ = crate::Pallet::<T>::register(RawOrigin::Root.into(), name.clone(), AssetType::Token, ed, Some(asset_id), None, None);
let _ = crate::Pallet::<T>::register(RawOrigin::Root.into(), name.clone(), AssetType::Token, ed, Some(asset_id), None, None, None);

}: _(RawOrigin::Root, asset_id, Default::default())
verify {
Expand Down
32 changes: 27 additions & 5 deletions asset-registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ mod mock;
mod tests;

mod benchmarking;
pub mod migration;
mod types;
pub mod weights;

Expand Down Expand Up @@ -194,7 +195,8 @@ pub mod pallet {
name: native_asset_name,
asset_type: AssetType::Token,
existential_deposit: self.native_existential_deposit,
locked: false,

xcm_rate_limit: None,
};

Assets::<T>::insert(T::NativeAssetId::get(), details);
Expand All @@ -203,7 +205,7 @@ pub mod pallet {
let bounded_name = Pallet::<T>::to_bounded_name(name.to_vec())
.map_err(|_| panic!("Invalid asset name!"))
.unwrap();
let _ = Pallet::<T>::register_asset(bounded_name, AssetType::Token, *ed, *id)
let _ = Pallet::<T>::register_asset(bounded_name, AssetType::Token, *ed, *id, None)
.map_err(|_| panic!("Failed to register asset"));
})
}
Expand All @@ -224,6 +226,8 @@ pub mod pallet {
asset_id: T::AssetId,
asset_name: BoundedVec<u8, T::StringLimit>,
asset_type: AssetType<T::AssetId>,
existential_deposit: T::Balance,
xcm_rate_limit: Option<T::Balance>,
},

/// Metadata set for an asset.
Expand Down Expand Up @@ -251,6 +255,7 @@ pub mod pallet {
/// Adds mapping between `name` and assigned `asset_id` so asset id can be retrieved by name too (Note: this approach is used in AMM implementation (xyk))
///
/// Emits 'Registered` event when successful.
#[allow(clippy::too_many_arguments)]
#[pallet::call_index(0)]
#[pallet::weight(<T as Config>::WeightInfo::register())]
pub fn register(
Expand All @@ -261,6 +266,7 @@ pub mod pallet {
asset_id: Option<T::AssetId>,
metadata: Option<Metadata>,
location: Option<T::AssetNativeLocation>,
xcm_rate_limit: Option<T::Balance>,
) -> DispatchResult {
T::RegistryOrigin::ensure_origin(origin)?;

Expand All @@ -271,7 +277,8 @@ pub mod pallet {
Error::<T>::AssetAlreadyRegistered
);

let asset_id = Self::register_asset(bounded_name, asset_type, existential_deposit, asset_id)?;
let asset_id =
Self::register_asset(bounded_name, asset_type, existential_deposit, asset_id, xcm_rate_limit)?;

if let Some(meta) = metadata {
let symbol = Self::to_bounded_name(meta.symbol)?;
Expand Down Expand Up @@ -317,6 +324,7 @@ pub mod pallet {
name: Vec<u8>,
asset_type: AssetType<T::AssetId>,
existential_deposit: Option<T::Balance>,
xcm_rate_limit: Option<T::Balance>,
) -> DispatchResult {
T::RegistryOrigin::ensure_origin(origin)?;

Expand All @@ -340,11 +348,14 @@ pub mod pallet {
detail.name = bounded_name.clone();
detail.asset_type = asset_type;
detail.existential_deposit = existential_deposit.unwrap_or(detail.existential_deposit);
detail.xcm_rate_limit = xcm_rate_limit;

Self::deposit_event(Event::Updated {
asset_id,
asset_name: bounded_name,
asset_type,
existential_deposit: detail.existential_deposit,
xcm_rate_limit: detail.xcm_rate_limit,
});

Ok(())
Expand Down Expand Up @@ -442,6 +453,7 @@ impl<T: Config> Pallet<T> {
asset_type: AssetType<T::AssetId>,
existential_deposit: T::Balance,
selected_asset_id: Option<T::AssetId>,
xcm_rate_limit: Option<T::Balance>,
) -> Result<T::AssetId, DispatchError> {
let asset_id = if let Some(selected_id) = selected_asset_id {
ensure!(
Expand Down Expand Up @@ -484,7 +496,7 @@ impl<T: Config> Pallet<T> {
name: name.clone(),
asset_type,
existential_deposit,
locked: false,
xcm_rate_limit,
};

// Store the details
Expand Down Expand Up @@ -513,7 +525,7 @@ impl<T: Config> Pallet<T> {
if let Some(asset_id) = AssetIds::<T>::get(&bounded_name) {
Ok(asset_id)
} else {
Self::register_asset(bounded_name, asset_type, existential_deposit, asset_id)
Self::register_asset(bounded_name, asset_type, existential_deposit, asset_id, None)
}
}

Expand Down Expand Up @@ -581,3 +593,13 @@ impl<T: Config> GetByKey<T::AssetId, T::Balance> for Pallet<T> {
}
}
}

/// Allows querying the XCM rate limit for an asset by its id.
pub struct XcmRateLimitsInRegistry<T>(PhantomData<T>);
/// Allows querying the XCM rate limit for an asset by its id.
/// Both a unknown asset and an unset rate limit will return `None`.
impl<T: Config> GetByKey<T::AssetId, Option<T::Balance>> for XcmRateLimitsInRegistry<T> {
fn get(k: &T::AssetId) -> Option<T::Balance> {
Pallet::<T>::assets(k).and_then(|details| details.xcm_rate_limit)
}
}
91 changes: 91 additions & 0 deletions asset-registry/src/migration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// This file is part of pallet-asset-registry

// Copyright (C) 2020-2023 Intergalactic, Limited (GIB).
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License..

use crate::{AssetDetails, AssetType, Assets, Config, Pallet};
use frame_support::{
log,
traits::{Get, StorageVersion},
weights::Weight,
};

///
pub mod v1 {
use super::*;
use codec::{Decode, Encode};
use scale_info::TypeInfo;
use sp_core::RuntimeDebug;

#[derive(Encode, Decode, Eq, PartialEq, Copy, Clone, RuntimeDebug, TypeInfo)]
pub struct OldAssetDetails<AssetId, Balance, BoundedString> {
/// The name of this asset. Limited in length by `StringLimit`.
pub(super) name: BoundedString,

pub(super) asset_type: AssetType<AssetId>,

pub(super) existential_deposit: Balance,

pub(super) locked: bool,
}

pub fn pre_migrate<T: Config>() {
assert_eq!(StorageVersion::get::<Pallet<T>>(), 0, "Storage version too high.");

log::info!(
target: "runtime::asset-registry",
"Asset Registry migration: PRE checks successful!"
);
}

pub fn migrate<T: Config>() -> Weight {
log::info!(
target: "runtime::asset-registry",
"Running migration to v1 for Asset Registry"
);

let mut i = 0;
Assets::<T>::translate(
|_key,
OldAssetDetails {
name,
asset_type,
existential_deposit,
locked: _,
}| {
i += 1;
Some(AssetDetails {
name,
asset_type,
existential_deposit,
xcm_rate_limit: None,
})
},
);

StorageVersion::new(1).put::<Pallet<T>>();

T::DbWeight::get().reads_writes(i, i)
}

pub fn post_migrate<T: Config>() {
assert_eq!(StorageVersion::get::<Pallet<T>>(), 1, "Unexpected storage version.");

log::info!(
target: "runtime::asset-registry",
"Asset Registry migration: POST checks successful!"
);
}
}
2 changes: 2 additions & 0 deletions asset-registry/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ use crate::{self as asset_registry, Config};
pub type AssetId = u32;
pub type Balance = u128;

pub const UNIT: Balance = 1_000_000_000_000;

type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;

Expand Down
Loading

0 comments on commit 90e9669

Please sign in to comment.