forked from moonbeam-foundation/moonbeam
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4c8c956
commit 7a64944
Showing
7 changed files
with
1,621 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
[package] | ||
authors = {workspace = true} | ||
description = "A pallet to trade weight for XCM execution" | ||
edition = "2021" | ||
name = "pallet-xcm-weight-trader" | ||
version = "0.1.0" | ||
|
||
[dependencies] | ||
log = {workspace = true} | ||
|
||
# Substrate | ||
frame-support = {workspace = true} | ||
frame-system = {workspace = true} | ||
pallet-balances = {workspace = true} | ||
parity-scale-codec = {workspace = true} | ||
scale-info = {workspace = true, features = ["derive"]} | ||
sp-core = {workspace = true} | ||
sp-io = {workspace = true} | ||
sp-runtime = {workspace = true} | ||
sp-std = {workspace = true} | ||
|
||
# Polkadot | ||
xcm = { workspace = true } | ||
xcm-executor = { workspace = true } | ||
xcm-fee-payment-runtime-api = { workspace = true } | ||
|
||
# Benchmarks | ||
frame-benchmarking = {workspace = true, optional = true} | ||
|
||
[dev-dependencies] | ||
frame-benchmarking = {workspace = true, features = ["std"]} | ||
pallet-balances = {workspace = true, features = ["std", "insecure_zero_ed"]} | ||
sp-tracing = {workspace = true, features = ["std"] } | ||
|
||
[features] | ||
default = ["std"] | ||
runtime-benchmarks = [ | ||
"frame-benchmarking", | ||
"frame-system/runtime-benchmarks" | ||
] | ||
std = [ | ||
"frame-support/std", | ||
"frame-system/std", | ||
"pallet-balances/std", | ||
"scale-info/std", | ||
"sp-core/std", | ||
"sp-io/std", | ||
"sp-runtime/std", | ||
"sp-std/std", | ||
"xcm/std", | ||
"xcm-executor/std", | ||
"xcm-fee-payment-runtime-api/std", | ||
] | ||
try-runtime = ["frame-support/try-runtime"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
// Copyright 2024 Moonbeam foundation | ||
// This file is part of Moonbeam. | ||
|
||
// Moonbeam 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. | ||
|
||
// Moonbeam 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 Moonbeam. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
#![cfg(feature = "runtime-benchmarks")] | ||
|
||
use super::*; | ||
|
||
use frame_benchmarking::{v2::*, BenchmarkError}; | ||
use frame_support::traits::EnsureOrigin; | ||
use frame_system::EventRecord; | ||
|
||
fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) { | ||
let events = frame_system::Pallet::<T>::events(); | ||
let system_event: <T as frame_system::Config>::RuntimeEvent = generic_event.into(); | ||
// compare to the last event record | ||
let EventRecord { event, .. } = &events[events.len() - 1]; | ||
assert_eq!(event, &system_event); | ||
} | ||
|
||
fn setup_one_asset<T: Config>() -> Result<Location, BenchmarkError> { | ||
let origin = T::AddSupportedAssetOrigin::try_successful_origin() | ||
.map_err(|_| BenchmarkError::Weightless)?; | ||
|
||
let location = T::NotFilteredLocation::get(); | ||
|
||
Pallet::<T>::add_asset(origin, location.clone(), 1_000).expect("fail to setup asset"); | ||
|
||
Ok(location) | ||
} | ||
|
||
#[benchmarks] | ||
mod benchmarks { | ||
use super::*; | ||
|
||
#[benchmark] | ||
fn add_asset() -> Result<(), BenchmarkError> { | ||
let origin = T::AddSupportedAssetOrigin::try_successful_origin() | ||
.map_err(|_| BenchmarkError::Weightless)?; | ||
|
||
let location = T::NotFilteredLocation::get(); | ||
|
||
#[extrinsic_call] | ||
_(origin as T::RuntimeOrigin, location.clone(), 1_000); | ||
|
||
assert_last_event::<T>( | ||
Event::SupportedAssetAdded { | ||
location, | ||
relative_price: 1_000, | ||
} | ||
.into(), | ||
); | ||
Ok(()) | ||
} | ||
|
||
#[benchmark] | ||
fn edit_asset() -> Result<(), BenchmarkError> { | ||
// Setup one asset | ||
let location = setup_one_asset::<T>()?; | ||
|
||
let origin = T::EditSupportedAssetOrigin::try_successful_origin() | ||
.map_err(|_| BenchmarkError::Weightless)?; | ||
|
||
#[extrinsic_call] | ||
_(origin as T::RuntimeOrigin, location.clone(), 2_000); | ||
|
||
assert_last_event::<T>( | ||
Event::SupportedAssetEdited { | ||
location, | ||
relative_price: 2_000, | ||
} | ||
.into(), | ||
); | ||
Ok(()) | ||
} | ||
|
||
#[benchmark] | ||
fn resume_asset_support() -> Result<(), BenchmarkError> { | ||
// Setup one asset | ||
let location = setup_one_asset::<T>()?; | ||
let pause_origin = T::PauseSupportedAssetOrigin::try_successful_origin() | ||
.map_err(|_| BenchmarkError::Weightless)?; | ||
Pallet::<T>::pause_asset_support(pause_origin, location.clone()) | ||
.expect("fail to pause asset"); | ||
|
||
let origin = T::ResumeSupportedAssetOrigin::try_successful_origin() | ||
.map_err(|_| BenchmarkError::Weightless)?; | ||
|
||
#[extrinsic_call] | ||
_(origin as T::RuntimeOrigin, location.clone()); | ||
|
||
assert_last_event::<T>(Event::ResumeAssetSupport { location }.into()); | ||
Ok(()) | ||
} | ||
|
||
#[benchmark] | ||
fn pause_asset_support() -> Result<(), BenchmarkError> { | ||
// Setup one asset | ||
let location = setup_one_asset::<T>()?; | ||
|
||
let origin = T::PauseSupportedAssetOrigin::try_successful_origin() | ||
.map_err(|_| BenchmarkError::Weightless)?; | ||
|
||
#[extrinsic_call] | ||
_(origin as T::RuntimeOrigin, location.clone()); | ||
|
||
assert_last_event::<T>(Event::PauseAssetSupport { location }.into()); | ||
Ok(()) | ||
} | ||
|
||
#[benchmark] | ||
fn remove_asset() -> Result<(), BenchmarkError> { | ||
// Setup one asset | ||
let location = setup_one_asset::<T>()?; | ||
|
||
let origin = T::RemoveSupportedAssetOrigin::try_successful_origin() | ||
.map_err(|_| BenchmarkError::Weightless)?; | ||
|
||
#[extrinsic_call] | ||
_(origin as T::RuntimeOrigin, location.clone()); | ||
|
||
assert_last_event::<T>(Event::SupportedAssetRemoved { location }.into()); | ||
Ok(()) | ||
} | ||
|
||
impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Test,); | ||
} |
Oops, something went wrong.