Skip to content

Commit

Permalink
chore: add missing dex-stable benchmarks
Browse files Browse the repository at this point in the history
Signed-off-by: Gregory Hill <gregorydhill@outlook.com>
  • Loading branch information
gregdhill committed Jul 10, 2023
1 parent e685d39 commit e6a5c63
Show file tree
Hide file tree
Showing 8 changed files with 665 additions and 315 deletions.
4 changes: 3 additions & 1 deletion crates/dex-stable/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ frame-system = { git = "https://github.com/paritytech/substrate", default-featur
sp-runtime = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.31" }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false }
sp-arithmetic = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false }
pallet-timestamp = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false }

# Orml dependencies
orml-traits = { git = "https://github.com/open-web3-stack/open-runtime-module-library", rev = "dc39cfddefb10ef0de23655e2c3dcdab66a19404", default-features = false }
Expand All @@ -27,7 +28,6 @@ orml-tokens = { git = "https://github.com/open-web3-stack/open-runtime-module-li
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31" }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false }
pallet-timestamp = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31"}

[features]
default = ["std"]
Expand All @@ -42,6 +42,7 @@ std = [
"sp-runtime/std",
"sp-std/std",
"sp-arithmetic/std",
"pallet-timestamp/std",
"orml-tokens/std",
]

Expand All @@ -50,5 +51,6 @@ runtime-benchmarks = [
"frame-system/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"pallet-timestamp/runtime-benchmarks"
]
try-runtime = [ "frame-support/try-runtime" ]
99 changes: 98 additions & 1 deletion crates/dex-stable/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

use super::*;
use crate::Pallet as StablePallet;
use frame_system::Pallet as System;
use pallet_timestamp::Pallet as Timestamp;

use frame_benchmarking::v2::*;
use frame_support::assert_ok;
Expand All @@ -17,6 +19,10 @@ const INITIAL_A_VALUE: Balance = 50;
const SWAP_FEE: Balance = 10000000;
const ADMIN_FEE: Balance = 0;

fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
System::<T>::assert_last_event(generic_event.into());
}

pub fn lookup_of_account<T: Config>(
who: T::AccountId,
) -> <<T as frame_system::Config>::Lookup as StaticLookup>::Source {
Expand Down Expand Up @@ -127,7 +133,13 @@ fn setup_meta_pool_and_add_liquidity<T: Config>(
meta_pool_id
}

#[benchmarks(where T: Config, T::CurrencyId: From<u32>)]
#[benchmarks(
where
T: Config,
T::CurrencyId: From<u32>,
T: pallet_timestamp::Config,
)]
pub mod benchmarks {
use super::*;

Expand Down Expand Up @@ -415,6 +427,91 @@ pub mod benchmarks {
);
}

#[benchmark]
pub fn update_fee_receiver() {
let caller: T::AccountId = whitelisted_caller();
let base_pool_id = setup_base_pool::<T>(caller.clone(), base_currencies::<T>(T::PoolCurrencyLimit::get()));

#[extrinsic_call]
_(
RawOrigin::Root,
base_pool_id.clone(),
lookup_of_account::<T>(caller.clone()).into(),
);

assert_last_event::<T>(
Event::UpdateAdminFeeReceiver {
pool_id: base_pool_id,
admin_fee_receiver: caller,
}
.into(),
);
}

#[benchmark]
pub fn set_swap_fee() {
let caller: T::AccountId = whitelisted_caller();
let base_pool_id = setup_base_pool::<T>(caller.clone(), base_currencies::<T>(T::PoolCurrencyLimit::get()));
let new_swap_fee = SWAP_FEE * 2;

#[extrinsic_call]
_(RawOrigin::Root, base_pool_id, new_swap_fee);

assert_last_event::<T>(
Event::NewSwapFee {
pool_id: base_pool_id,
new_swap_fee,
}
.into(),
);
}

#[benchmark]
pub fn set_admin_fee() {
let caller: T::AccountId = whitelisted_caller();
let base_pool_id = setup_base_pool::<T>(caller.clone(), base_currencies::<T>(T::PoolCurrencyLimit::get()));
let new_admin_fee = MAX_ADMIN_FEE;

#[extrinsic_call]
_(RawOrigin::Root, base_pool_id, new_admin_fee);

assert_last_event::<T>(
Event::NewAdminFee {
pool_id: base_pool_id,
new_admin_fee,
}
.into(),
);
}

#[benchmark]
pub fn ramp_a() {
let caller: T::AccountId = whitelisted_caller();
let base_pool_id = setup_base_pool::<T>(caller.clone(), base_currencies::<T>(T::PoolCurrencyLimit::get()));

Timestamp::<T>::set_timestamp(Timestamp::<T>::get() + (DAY * 1000).into());

#[extrinsic_call]
_(RawOrigin::Root, base_pool_id, 100, (MIN_RAMP_TIME * 2).into());
}

#[benchmark]
pub fn stop_ramp_a() {
let caller: T::AccountId = whitelisted_caller();
let base_pool_id = setup_base_pool::<T>(caller.clone(), base_currencies::<T>(T::PoolCurrencyLimit::get()));

Timestamp::<T>::set_timestamp(Timestamp::<T>::get() + (DAY * 1000).into());
assert_ok!(StablePallet::<T>::ramp_a(
RawOrigin::Root.into(),
base_pool_id,
100,
(MIN_RAMP_TIME * 2).into()
));

#[extrinsic_call]
_(RawOrigin::Root, base_pool_id);
}

#[benchmark]
pub fn withdraw_admin_fee() {
let caller: T::AccountId = whitelisted_caller();
Expand Down
Loading

0 comments on commit e6a5c63

Please sign in to comment.