Skip to content

Commit

Permalink
[foreign-asset-creator] add default created asset for benchmarking (#39)
Browse files Browse the repository at this point in the history
* add default created asset

* remove unwrap for expect

* wip
  • Loading branch information
girazoki committed Apr 25, 2024
1 parent 66edb0e commit 388aecc
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
48 changes: 46 additions & 2 deletions pallets/foreign-asset-creator/src/benchmarks.rs
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
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

0 comments on commit 388aecc

Please sign in to comment.