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] add default created asset for benchmarking #39

Merged
merged 3 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
48 changes: 46 additions & 2 deletions pallets/foreign-asset-creator/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,55 @@

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

use crate::{AssetId, Call, Config, Pallet};
use frame_benchmarking::{account, benchmarks, impl_benchmark_test_suite};
use crate::{AssetBalance, AssetId, Call, Config, Pallet};
use frame_benchmarking::{
account, benchmarks, impl_benchmark_test_suite, whitelisted_caller, BenchmarkError,
};
use frame_support::traits::{fungibles, fungibles::Mutate, EnsureOrigin};
use frame_system::RawOrigin;
use sp_arithmetic::traits::AtLeast16BitUnsigned;
use staging_xcm::latest::prelude::*;

#[allow(dead_code)]
pub fn create_default_minted_asset<T: Config>(
amount: AssetBalance<T>,
receiver: T::AccountId,
) -> (AssetId<T>, T::ForeignAsset)
where
T::ForeignAsset: From<Location>,
T::Fungibles: fungibles::Mutate<T::AccountId>,
AssetId<T>: AtLeast16BitUnsigned,
{
let (asset_id, foreign_asset) = create_default_asset::<T>(true);

assert!(T::Fungibles::mint_into(asset_id.clone(), &receiver, amount).is_ok());
(asset_id, foreign_asset)
}

#[allow(dead_code)]
fn create_default_asset<T: Config>(is_sufficient: bool) -> (AssetId<T>, T::ForeignAsset)
where
T::ForeignAsset: From<Location>,
AssetId<T>: AtLeast16BitUnsigned,
{
let asset_id: AssetId<T> = 1u16.into();
let foreign_asset: T::ForeignAsset = Location::parent().into();
let admin: T::AccountId = whitelisted_caller();
let origin = T::ForeignAssetCreatorOrigin::try_successful_origin()
.map_err(|_| BenchmarkError::Weightless)
.expect("Not able to generate an appropriate origin to disptach the call");
assert!(Pallet::<T>::create_foreign_asset(
origin,
foreign_asset.clone(),
asset_id.clone(),
admin,
is_sufficient,
1u32.into(),
)
.is_ok());
(asset_id, foreign_asset)
}

benchmarks! {
// This where clause allows us to create ForeignAssetTypes
where_clause { where T::ForeignAsset: From<Location>, AssetId<T>: AtLeast16BitUnsigned }
Expand Down
2 changes: 1 addition & 1 deletion pallets/foreign-asset-creator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub use pallet::*;
pub mod weights;
pub use weights::WeightInfo;
#[cfg(any(test, feature = "runtime-benchmarks"))]
mod benchmarks;
pub mod benchmarks;
#[cfg(test)]
pub mod mock;
#[cfg(test)]
Expand Down
Loading