Skip to content

Commit

Permalink
Improve asset manager (#472)
Browse files Browse the repository at this point in the history
* improve asset manager

Signed-off-by: Shumo Chu <shumo.chu@protonmail.com>
  • Loading branch information
stechu committed Apr 9, 2022
1 parent 9e5d146 commit 8191baa
Show file tree
Hide file tree
Showing 23 changed files with 1,046 additions and 483 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion node/src/chain_specs/dolphin.rs
Expand Up @@ -17,7 +17,9 @@
use super::*;
use crate::command::DOLPHIN_PARACHAIN_ID;

use dolphin_runtime::{CouncilConfig, DemocracyConfig, GenesisConfig, TechnicalCommitteeConfig};
use dolphin_runtime::{
AssetManagerConfig, CouncilConfig, DemocracyConfig, GenesisConfig, TechnicalCommitteeConfig,
};
use manta_primitives::helpers::{get_account_id_from_seed, get_collator_keys_from_seed};

/// Specialized `ChainSpec` for the normal parachain runtime.
Expand Down Expand Up @@ -203,6 +205,7 @@ fn dolphin_dev_genesis(
members: endowed_accounts.iter().take(1).cloned().collect(),
phantom: Default::default(),
},
asset_manager: Default::default(),
council_membership: Default::default(),
technical_membership: Default::default(),
aura_ext: Default::default(),
Expand Down
10 changes: 6 additions & 4 deletions pallets/asset-manager/Cargo.toml
Expand Up @@ -17,14 +17,15 @@ frame-support = { git = "https://github.com/paritytech/substrate", branch = "pol
frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16", default-features = false }
frame-benchmarking = { git = 'https://github.com/paritytech/substrate.git', branch = "polkadot-v0.9.16", default-features = false, optional = true }
manta-primitives = { path = '../../primitives', default-features = false}
xcm = { git = 'https://github.com/paritytech/polkadot.git', default-features = false, branch = "release-v0.9.16", optional = true}
manta-primitives = { path = "../../primitives", default-features = false}

[dev-dependencies]
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16" }
pallet-balances = { git = 'https://github.com/paritytech/substrate.git', branch = "polkadot-v0.9.16" }
pallet-assets = { git = 'https://github.com/paritytech/substrate.git', branch = "polkadot-v0.9.16" }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16" }
xcm = { git = 'https://github.com/paritytech/polkadot.git', branch = "release-v0.9.16" }
xcm = { git = 'https://github.com/paritytech/polkadot.git', default-features = false, branch = "release-v0.9.16"}

[features]
default = ["std"]
Expand All @@ -36,15 +37,16 @@ std = [
"frame-system/std",
"sp-std/std",
"manta-primitives/std",
"frame-benchmarking/std",
]
try-runtime = [
"frame-support/try-runtime",
]

runtime-benchmarks = [
"frame-benchmarking",
'frame-benchmarking',
'frame-support/runtime-benchmarks',
'frame-system/runtime-benchmarks',
'manta-primitives/runtime-benchmarks',
'xcm',
]

136 changes: 136 additions & 0 deletions pallets/asset-manager/src/benchmarking.rs
@@ -0,0 +1,136 @@
// Copyright 2020-2022 Manta Network.
// This file is part of Manta.
//
// Manta is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Manta is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Manta. If not, see <http://www.gnu.org/licenses/>.

#![cfg(feature = "runtime-benchmarks")]

use crate::{Call, Config, Event, Pallet};
use frame_benchmarking::{benchmarks, impl_benchmark_test_suite, whitelisted_caller};
use frame_support::traits::Get;
use frame_system::{EventRecord, RawOrigin};
use xcm::latest::prelude::*;

use manta_primitives::assets::{AssetConfig, UnitsToWeightRatio};

fn assert_last_event<T: Config>(generic_event: <T as Config>::Event) {
let events = frame_system::Pallet::<T>::events();
let system_event: <T as frame_system::Config>::Event = generic_event.into();
// compare to the last event record
let EventRecord { event, .. } = &events[events.len() - 1];
assert_eq!(event, &system_event);
}

benchmarks! {
where_clause { where <T::AssetConfig as AssetConfig<T>>::AssetLocation: From<MultiLocation> }
register_asset {
let location = <T::AssetConfig as AssetConfig<T>>::AssetLocation::default();
let metadata = <T::AssetConfig as AssetConfig<T>>::AssetRegistrarMetadata::default();

}: _(RawOrigin::Root, location.clone(), metadata.clone())
verify {
assert_eq!(Pallet::<T>::asset_id_location(<T::AssetConfig as AssetConfig<T>>::NativeAssetId::get()), Some(location));
}

set_units_per_second {
let start = <T::AssetConfig as AssetConfig<T>>::NativeAssetId::get();
let end = start + 1000;
for i in start..end {

let location: MultiLocation = MultiLocation::new(0, X1(Parachain(i)));
let location = <T::AssetConfig as AssetConfig<T>>::AssetLocation::from(location.clone());
let metadata = <T::AssetConfig as AssetConfig<T>>::AssetRegistrarMetadata::default();

Pallet::<T>::register_asset(RawOrigin::Root.into(), location.clone(), metadata.clone())?;
Pallet::<T>::set_units_per_second(RawOrigin::Root.into(), i, 0)?;
}

// does not really matter what we register, as long as it is different than the previous
let location = <T::AssetConfig as AssetConfig<T>>::AssetLocation::default();
let metadata = <T::AssetConfig as AssetConfig<T>>::AssetRegistrarMetadata::default();
let amount = 10;
Pallet::<T>::register_asset(RawOrigin::Root.into(), location.clone(), metadata.clone())?;

}: _(RawOrigin::Root, end, amount)
verify {
assert_eq!(Pallet::<T>::get_units_per_second(end), Some(amount));
}

update_asset_location {
let start = <T::AssetConfig as AssetConfig<T>>::NativeAssetId::get();
let end = start + 1000;
for i in start..end {

let location: MultiLocation = MultiLocation::new(0, X1(Parachain(i)));
let location = <T::AssetConfig as AssetConfig<T>>::AssetLocation::from(location.clone());
let metadata = <T::AssetConfig as AssetConfig<T>>::AssetRegistrarMetadata::default();

Pallet::<T>::register_asset(RawOrigin::Root.into(), location.clone(), metadata.clone())?;
}

// does not really matter what we register, as long as it is different than the previous
let location = <T::AssetConfig as AssetConfig<T>>::AssetLocation::default();
let metadata = <T::AssetConfig as AssetConfig<T>>::AssetRegistrarMetadata::default();
Pallet::<T>::register_asset(RawOrigin::Root.into(), location.clone(), metadata.clone())?;
let new_location = <T::AssetConfig as AssetConfig<T>>::AssetLocation::from(MultiLocation::new(0, X1(Parachain(end))));
}: _(RawOrigin::Root, end, new_location.clone())
verify {
assert_eq!(Pallet::<T>::asset_id_location(end), Some(new_location));
}

update_asset_metadata {
let start = <T::AssetConfig as AssetConfig<T>>::NativeAssetId::get();
let end = start + 1000;
for i in start..end {

let location: MultiLocation = MultiLocation::new(0, X1(Parachain(i)));
let location = <T::AssetConfig as AssetConfig<T>>::AssetLocation::from(location.clone());
let metadata = <T::AssetConfig as AssetConfig<T>>::AssetRegistrarMetadata::default();

Pallet::<T>::register_asset(RawOrigin::Root.into(), location.clone(), metadata.clone())?;
}

// does not really matter what we register, as long as it is different than the previous
let location = <T::AssetConfig as AssetConfig<T>>::AssetLocation::default();
let metadata = <T::AssetConfig as AssetConfig<T>>::AssetRegistrarMetadata::default();
Pallet::<T>::register_asset(RawOrigin::Root.into(), location.clone(), metadata.clone())?;
}: _(RawOrigin::Root, end, metadata.clone())
verify {
assert_last_event::<T>(Event::AssetMetadataUpdated { asset_id: end, metadata }.into());
}

mint_asset {
let start = <T::AssetConfig as AssetConfig<T>>::NativeAssetId::get();
let end = start + 1000;
for i in start..end {

let location = <T::AssetConfig as AssetConfig<T>>::AssetLocation::from(MultiLocation::new(0, X1(Parachain(i))));
let metadata = <T::AssetConfig as AssetConfig<T>>::AssetRegistrarMetadata::default();

Pallet::<T>::register_asset(RawOrigin::Root.into(), location.clone(), metadata.clone())?;
}

let beneficiary: T::AccountId = whitelisted_caller();
let amount = 100;
// does not really matter what we register, as long as it is different than the previous
let location = <T::AssetConfig as AssetConfig<T>>::AssetLocation::default();
let metadata = <T::AssetConfig as AssetConfig<T>>::AssetRegistrarMetadata::default();
Pallet::<T>::register_asset(RawOrigin::Root.into(), location.clone(), metadata.clone())?;
}: _(RawOrigin::Root, end, beneficiary.clone(), amount)
verify {
assert_last_event::<T>(Event::AssetMinted { asset_id: end, beneficiary, amount }.into());
}
}

impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Runtime);

0 comments on commit 8191baa

Please sign in to comment.