referrerpolicy=no-referrer-when-downgrade

pallet_asset_rate/
benchmarking.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! The crate's benchmarks.
19
20use super::*;
21use crate::{pallet as pallet_asset_rate, Pallet as AssetRate};
22
23use codec::Encode;
24use frame_benchmarking::v2::*;
25use frame_support::assert_ok;
26use frame_system::RawOrigin;
27use sp_core::crypto::FromEntropy;
28
29/// Trait describing the factory function for the `AssetKind` parameter.
30pub trait AssetKindFactory<AssetKind> {
31	fn create_asset_kind(seed: u32) -> AssetKind;
32}
33impl<AssetKind> AssetKindFactory<AssetKind> for ()
34where
35	AssetKind: FromEntropy,
36{
37	fn create_asset_kind(seed: u32) -> AssetKind {
38		AssetKind::from_entropy(&mut seed.encode().as_slice()).unwrap()
39	}
40}
41
42const SEED: u32 = 1;
43
44fn default_conversion_rate() -> FixedU128 {
45	FixedU128::from_u32(1u32)
46}
47
48#[benchmarks]
49mod benchmarks {
50	use super::*;
51
52	#[benchmark]
53	fn create() -> Result<(), BenchmarkError> {
54		let asset_kind: T::AssetKind = T::BenchmarkHelper::create_asset_kind(SEED);
55		#[extrinsic_call]
56		_(RawOrigin::Root, Box::new(asset_kind.clone()), default_conversion_rate());
57
58		assert_eq!(
59			pallet_asset_rate::ConversionRateToNative::<T>::get(asset_kind),
60			Some(default_conversion_rate())
61		);
62		Ok(())
63	}
64
65	#[benchmark]
66	fn update() -> Result<(), BenchmarkError> {
67		let asset_kind: T::AssetKind = T::BenchmarkHelper::create_asset_kind(SEED);
68		assert_ok!(AssetRate::<T>::create(
69			RawOrigin::Root.into(),
70			Box::new(asset_kind.clone()),
71			default_conversion_rate()
72		));
73
74		#[extrinsic_call]
75		_(RawOrigin::Root, Box::new(asset_kind.clone()), FixedU128::from_u32(2));
76
77		assert_eq!(
78			pallet_asset_rate::ConversionRateToNative::<T>::get(asset_kind),
79			Some(FixedU128::from_u32(2))
80		);
81		Ok(())
82	}
83
84	#[benchmark]
85	fn remove() -> Result<(), BenchmarkError> {
86		let asset_kind: T::AssetKind = T::BenchmarkHelper::create_asset_kind(SEED);
87		assert_ok!(AssetRate::<T>::create(
88			RawOrigin::Root.into(),
89			Box::new(asset_kind.clone()),
90			default_conversion_rate()
91		));
92
93		#[extrinsic_call]
94		_(RawOrigin::Root, Box::new(asset_kind.clone()));
95
96		assert!(pallet_asset_rate::ConversionRateToNative::<T>::get(asset_kind).is_none());
97		Ok(())
98	}
99
100	impl_benchmark_test_suite! { AssetRate, crate::mock::new_test_ext(), crate::mock::Test }
101}