Skip to content

Commit

Permalink
foreign asset creator pallet (Moonsong-Labs#15)
Browse files Browse the repository at this point in the history
* 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
girazoki committed Jan 9, 2024
1 parent 8c0ae75 commit 3cc7df4
Show file tree
Hide file tree
Showing 8 changed files with 1,077 additions and 0 deletions.
37 changes: 37 additions & 0 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 @@ -88,6 +88,7 @@ pallet-utility = { git = "https://github.com/moondance-labs/polkadot-sdk", branc
pallet-whitelist = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-v1.3.0", default-features = false }
sp-api = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-v1.3.0", default-features = false }
sp-application-crypto = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-v1.3.0", default-features = false }
sp-arithmetic = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-v1.3.0", default-features = false }
sp-block-builder = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-v1.3.0", default-features = false }
sp-consensus-babe = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-v1.3.0", default-features = false }
sp-consensus-slots = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-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" ]
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

0 comments on commit 3cc7df4

Please sign in to comment.