forked from Moonsong-Labs/moonkit
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
foreign asset creator pallet (Moonsong-Labs#15)
* foreign asset creator pallet * add maybe equivalence * lock * Add benchmarks * benchmarks * at least 16 bit unsigned * update * add benchmarks * add weights· * fix tests
- Loading branch information
Showing
8 changed files
with
1,077 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
[package] | ||
name = "pallet-foreign-asset-creator" | ||
authors = { workspace = true } | ||
edition = "2021" | ||
version = "0.1.0" | ||
|
||
[dependencies] | ||
log = { workspace = true } | ||
serde = { workspace = true, optional = true } | ||
|
||
# Substrate | ||
frame-support = { workspace = true } | ||
frame-system = { workspace = true } | ||
parity-scale-codec = { workspace = true, features = [ "derive" ] } | ||
scale-info = { workspace = true, features = [ "derive" ] } | ||
sp-arithmetic = { workspace = true } | ||
sp-io = { workspace = true } | ||
sp-runtime = { workspace = true } | ||
sp-std = { workspace = true } | ||
|
||
# Polkadot | ||
staging-xcm = { workspace = true, optional = true } | ||
|
||
# Benchmarks | ||
frame-benchmarking = { workspace = true, optional = true } | ||
|
||
[dev-dependencies] | ||
pallet-balances = { workspace = true, features = [ "std" ] } | ||
pallet-assets = { workspace = true, features = [ "std" ] } | ||
sp-core = { workspace = true, features = [ "std" ] } | ||
|
||
[features] | ||
default = [ "std" ] | ||
std = [ | ||
"frame-benchmarking/std", | ||
"frame-support/std", | ||
"frame-system/std", | ||
"parity-scale-codec/std", | ||
"scale-info/std", | ||
"serde", | ||
"sp-arithmetic/std", | ||
"sp-io/std", | ||
"sp-runtime/std", | ||
"sp-std/std", | ||
"staging-xcm/std" | ||
] | ||
|
||
runtime-benchmarks = [ "frame-benchmarking", "staging-xcm"] | ||
try-runtime = [ "frame-support/try-runtime" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
// Copyright Moonsong Labs | ||
// This file is part of Moonkit. | ||
|
||
// Moonkit 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. | ||
|
||
// Moonkit 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 Moonkit. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
#![cfg(feature = "runtime-benchmarks")] | ||
|
||
use crate::{AssetId, Call, Config, Pallet}; | ||
use frame_benchmarking::{account, benchmarks, impl_benchmark_test_suite}; | ||
use frame_system::RawOrigin; | ||
use sp_arithmetic::traits::AtLeast16BitUnsigned; | ||
use staging_xcm::latest::prelude::*; | ||
benchmarks! { | ||
// This where clause allows us to create ForeignAssetTypes | ||
where_clause { where T::ForeignAsset: From<MultiLocation>, AssetId<T>: AtLeast16BitUnsigned } | ||
create_foreign_asset { | ||
const USER_SEED: u32 = 1; | ||
let manager = account("manager", 0, USER_SEED); | ||
let foreign_asset = T::ForeignAsset::default(); | ||
let amount = 1u32.into(); | ||
let asset_id: AssetId<T> = 1u16.into(); | ||
|
||
}: _(RawOrigin::Root, foreign_asset.clone(), asset_id.clone(), manager, true, amount) | ||
verify { | ||
assert_eq!(Pallet::<T>::foreign_asset_for_id(asset_id), Some(foreign_asset)); | ||
} | ||
|
||
change_existing_asset_type { | ||
// We make it dependent on the number of existing assets already | ||
let x in 5..100; | ||
const USER_SEED: u32 = 1; | ||
let manager: T::AccountId = account("manager", 0, USER_SEED); | ||
|
||
for i in 0..x { | ||
let foreign_asset: T::ForeignAsset = MultiLocation::new(0, X1(GeneralIndex(i as u128))).into(); | ||
let asset_id: AssetId<T> = (i as u16).into(); | ||
let amount = 1u32.into(); | ||
Pallet::<T>::create_foreign_asset( | ||
RawOrigin::Root.into(), | ||
foreign_asset.clone(), | ||
asset_id.clone(), | ||
manager.clone(), | ||
true, | ||
amount, | ||
)?; | ||
} | ||
|
||
let new_foreign_asset = T::ForeignAsset::default(); | ||
let asset_type_to_be_changed: T::ForeignAsset = MultiLocation::new( | ||
0, | ||
X1(GeneralIndex((x-1) as u128)) | ||
).into(); | ||
let asset_id_to_be_changed: AssetId<T> = ((x-1) as u16).into(); | ||
}: _(RawOrigin::Root, asset_id_to_be_changed.clone(), new_foreign_asset.clone()) | ||
verify { | ||
assert_eq!(Pallet::<T>::foreign_asset_for_id(asset_id_to_be_changed), Some(new_foreign_asset.clone())); | ||
} | ||
|
||
remove_existing_asset_type { | ||
// We make it dependent on the number of existing assets already | ||
let x in 5..100; | ||
const USER_SEED: u32 = 1; | ||
let manager: T::AccountId = account("manager", 0, USER_SEED); | ||
|
||
for i in 0..x { | ||
let foreign_asset: T::ForeignAsset = MultiLocation::new(0, X1(GeneralIndex(i as u128))).into(); | ||
let asset_id: AssetId<T> = (i as u16).into(); | ||
let amount = 1u32.into(); | ||
Pallet::<T>::create_foreign_asset( | ||
RawOrigin::Root.into(), | ||
foreign_asset.clone(), | ||
asset_id.clone(), | ||
manager.clone(), | ||
true, | ||
amount, | ||
)?; | ||
} | ||
|
||
let asset_id_to_be_removed: AssetId<T> = ((x-1) as u16).into(); | ||
}: _(RawOrigin::Root, asset_id_to_be_removed.clone()) | ||
verify { | ||
assert!(Pallet::<T>::foreign_asset_for_id(asset_id_to_be_removed).is_none()); | ||
} | ||
|
||
destroy_foreign_asset { | ||
// We make it dependent on the number of existing assets already | ||
let x in 5..100; | ||
const USER_SEED: u32 = 1; | ||
let manager: T::AccountId = account("manager", 0, USER_SEED); | ||
|
||
for i in 0..x { | ||
let foreign_asset: T::ForeignAsset = MultiLocation::new(0, X1(GeneralIndex(i as u128))).into(); | ||
let asset_id: AssetId<T> = (i as u16).into(); | ||
let amount = 1u32.into(); | ||
Pallet::<T>::create_foreign_asset( | ||
RawOrigin::Root.into(), | ||
foreign_asset.clone(), | ||
asset_id.clone(), | ||
manager.clone(), | ||
true, | ||
amount, | ||
)?; | ||
} | ||
|
||
let asset_id_to_be_destroyed: AssetId<T> = ((x-1) as u16).into(); | ||
}: _(RawOrigin::Root, asset_id_to_be_destroyed.clone()) | ||
verify { | ||
assert!(Pallet::<T>::foreign_asset_for_id(asset_id_to_be_destroyed).is_none()); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::mock::Test; | ||
use sp_io::TestExternalities; | ||
use sp_runtime::BuildStorage; | ||
|
||
pub fn new_test_ext() -> TestExternalities { | ||
let t = frame_system::GenesisConfig::<Test>::default() | ||
.build_storage() | ||
.unwrap(); | ||
TestExternalities::new(t) | ||
} | ||
} | ||
|
||
impl_benchmark_test_suite!( | ||
Pallet, | ||
crate::benchmarks::tests::new_test_ext(), | ||
crate::mock::Test | ||
); |
Oops, something went wrong.