Skip to content

Commit

Permalink
added xcm-weight-trader pallet
Browse files Browse the repository at this point in the history
  • Loading branch information
ozgunozerk committed Sep 19, 2024
1 parent 4c8c956 commit 7a64944
Show file tree
Hide file tree
Showing 7 changed files with 1,621 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ members = [
"pallets/precompile-benchmarks",
"pallets/proxy-genesis-companion",
"pallets/xcm-transactor",
"pallets/xcm-weight-trader",
"precompiles/balances-erc20",
"precompiles/batch",
"precompiles/call-permit",
Expand Down Expand Up @@ -105,6 +106,7 @@ pallet-precompile-benchmarks = { path = "pallets/precompile-benchmarks", default
pallet-proxy-genesis-companion = { path = "pallets/proxy-genesis-companion", default-features = false }
pallet-xcm-transactor = { path = "pallets/xcm-transactor", default-features = false }
pallet-moonbeam-lazy-migrations = { path = "pallets/moonbeam-lazy-migrations", default-features = false }
pallet-xcm-weight-trader = { path = "pallets/xcm-weight-trader", default-features = false }
precompile-utils = { path = "precompiles/utils", default-features = false }
xcm-primitives = { path = "primitives/xcm", default-features = false }

Expand Down
54 changes: 54 additions & 0 deletions pallets/xcm-weight-trader/Cargo.toml
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"]
139 changes: 139 additions & 0 deletions pallets/xcm-weight-trader/src/benchmarking.rs
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,);
}
Loading

0 comments on commit 7a64944

Please sign in to comment.