Skip to content
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

foreign asset creator pallet #15

Merged
merged 11 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from 10 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
53 changes: 50 additions & 3 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ pallet-utility = { git = "https://github.com/paritytech/polkadot-sdk", branch =
pallet-whitelist = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.3.0", default-features = false }
sp-api = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.3.0", default-features = false }
sp-application-crypto = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.3.0", default-features = false }
sp-arithmetic = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.3.0", default-features = false }
sp-block-builder = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.3.0", default-features = false }
sp-consensus-babe = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.3.0", default-features = false }
sp-consensus-slots = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.3.0", default-features = false }
Expand Down
49 changes: 49 additions & 0 deletions pallets/foreign-asset-creator/Cargo.toml
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" ]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now try-runtime is a separate binary, so we don't need this rust feature anymore

141 changes: 141 additions & 0 deletions pallets/foreign-asset-creator/src/benchmarks.rs
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
);
Loading
Loading