Skip to content

Commit

Permalink
feat: 🎸 add benchmark of buyback_vstoken_by_stable_pool
Browse files Browse the repository at this point in the history
  • Loading branch information
yooml committed Aug 9, 2023
1 parent d3152de commit 14dc32e
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 35 deletions.
8 changes: 3 additions & 5 deletions pallets/salp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,27 @@ sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false }
sp-arithmetic = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false }
orml-traits = { version = "0.4.1-dev", default-features = false }
orml-tokens = { version = "0.4.1-dev", default-features = false }
xcm-interface = { path = "../xcm-interface", default-features = false }
zenlink-protocol = { version = "*", default-features = false }
cumulus-primitives-core = { git = "https://github.com/paritytech/cumulus", branch = "polkadot-v0.9.42", default-features = false }
pallet-xcm = { git = "https://github.com/paritytech/polkadot", default-features = false , branch = "release-v0.9.42" }
xcm = { git = "https://github.com/paritytech/polkadot", default-features = false , branch = "release-v0.9.42" }
xcm-builder = { git = "https://github.com/paritytech/polkadot",default-features = false , branch = "release-v0.9.42" }
bifrost-stable-pool = { path = "../stable-pool", default-features = false }
nutsfinance-stable-asset = { version = "*", default-features = false }
bifrost-vtoken-minting = { path = "../../pallets/vtoken-minting", default-features = false }

[dev-dependencies]
pallet-multisig = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42" }
pallet-sudo = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42" }
pallet-collective = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42" }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42" }
orml-tokens = "0.4.1-dev"
orml-currencies = "0.4.1-dev"
orml-xtokens = "0.4.1-dev"
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42" }
bifrost-asset-registry = { path = "../asset-registry", default-features = false }
xcm-executor = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.42" }
nutsfinance-stable-asset = { version = "*", default-features = false }
bifrost-vtoken-minting = { path = "../../pallets/vtoken-minting", default-features = false }
bifrost-stable-pool = { path = "../../pallets/stable-pool", default-features = false }


[features]
Expand All @@ -66,7 +65,6 @@ std = [
"cumulus-primitives-core/std",
"pallet-xcm/std",
"xcm/std",
"bifrost-stable-pool/std",
]

runtime-benchmarks = [
Expand Down
93 changes: 88 additions & 5 deletions pallets/salp/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@

// Ensure we're `no_std` when compiling for Wasm.
#[cfg(feature = "runtime-benchmarks")]
use frame_benchmarking::v1::{account, benchmarks, whitelisted_caller};
pub use crate::{Pallet as Salp, *};
use bifrost_stable_pool::AtLeast64BitUnsignedOf;
use frame_benchmarking::v1::{account, benchmarks, whitelisted_caller, BenchmarkError};
use frame_support::assert_ok;
use frame_system::RawOrigin;
use node_primitives::ParaId;
use sp_runtime::{traits::Bounded, SaturatedConversion};
use node_primitives::{CurrencyId, ParaId, BNC, KSM, VKSM, VSKSM};
use sp_runtime::{
traits::{AccountIdConversion, Bounded, StaticLookup, UniqueSaturatedFrom},
SaturatedConversion,
};
use sp_std::prelude::*;

pub use crate::{Pallet as Salp, *};

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();
Expand All @@ -53,7 +56,27 @@ fn contribute_fund<T: Config>(who: &T::AccountId, index: ParaId) {
assert_ok!(Salp::<T>::contribute(RawOrigin::Signed(who.clone()).into(), index, value));
}

fn kusama_setup<
T: Config
+ bifrost_stable_pool::Config
+ nutsfinance_stable_asset::Config
+ orml_tokens::Config<CurrencyId = CurrencyId>
+ bifrost_vtoken_minting::Config,
>() -> Result<(), BenchmarkError> {
Ok(())
}

pub fn lookup_of_account<T: Config>(
who: T::AccountId,
) -> <<T as frame_system::Config>::Lookup as StaticLookup>::Source {
<T as frame_system::Config>::Lookup::unlookup(who)
}

benchmarks! {
where_clause {
where
T: Config + bifrost_stable_pool::Config + nutsfinance_stable_asset::Config + orml_tokens::Config<CurrencyId = CurrencyId> + bifrost_vtoken_minting::Config
}
contribute {
let fund_index = create_fund::<T>(1);
let caller: T::AccountId = whitelisted_caller();
Expand Down Expand Up @@ -339,5 +362,65 @@ benchmarks! {
buyback {
}: _(RawOrigin::Root, 100u32.into())

buyback_vstoken_by_stable_pool {
kusama_setup::<T>()?;
let caller: T::AccountId = whitelisted_caller();
let fee_account: T::AccountId = account("seed",1,1);
let buyback_account: T::AccountId = T::BuybackPalletId::get().into_account_truncating();
let pool_asset = BNC;

let amounts1: AtLeast64BitUnsignedOf<T> = 1_000_000_000_000u128.into();
let amounts: <T as nutsfinance_stable_asset::pallet::Config>::Balance = amounts1.into();
assert_ok!(bifrost_stable_pool::Pallet::<T>::create_pool(
RawOrigin::Root.into(),
pool_asset.into(),
vec![KSM.into(), VSKSM.into()],
vec![1u128.into(), 1u128.into()],
0u128.into(),
0u128.into(),
0u128.into(),
220u128.into(),
fee_account.clone(),
fee_account.clone(),
1000000000000u128.into()
));
assert_ok!(bifrost_stable_pool::Pallet::<T>::edit_token_rate(
RawOrigin::Root.into(),
0,
vec![(VSKSM.into(), (1u128.into(), 1u128.into())), (KSM.into(), (10u128.into(), 30u128.into()))]
));

assert_ok!(orml_tokens::Pallet::<T>::set_balance(
RawOrigin::Root.into(),
lookup_of_account::<T>(buyback_account.clone()),
KSM,
<T as orml_tokens::Config>::Balance::unique_saturated_from(1_000_000_000_000_000_000u128),
<T as orml_tokens::Config>::Balance::unique_saturated_from(0u128)
));
assert_ok!(orml_tokens::Pallet::<T>::set_balance(
RawOrigin::Root.into(),
lookup_of_account::<T>(caller.clone()),
KSM,
<T as orml_tokens::Config>::Balance::unique_saturated_from(1_000_000_000_000_000_000u128),
<T as orml_tokens::Config>::Balance::unique_saturated_from(0u128)
));
assert_ok!(orml_tokens::Pallet::<T>::set_balance(
RawOrigin::Root.into(),
lookup_of_account::<T>(caller.clone()),
VSKSM,
<T as orml_tokens::Config>::Balance::unique_saturated_from(1_000_000_000_000_000_000u128),
<T as orml_tokens::Config>::Balance::unique_saturated_from(0u128)
));
assert_eq!(
orml_tokens::Pallet::<T>::total_balance(KSM, &caller.clone()),
<T as orml_tokens::Config>::Balance::unique_saturated_from(1_000_000_000_000_000_000u128)
);
assert_ok!(bifrost_stable_pool::Pallet::<T>::add_liquidity(RawOrigin::Signed(caller.clone()).into(), 0, vec![amounts, amounts], amounts));
let minimum_mint_value = bifrost_vtoken_minting::BalanceOf::<T>::unique_saturated_from(0u128);
let token_amount = bifrost_vtoken_minting::BalanceOf::<T>::unique_saturated_from(1_000_000_000_000u128);
assert_ok!(bifrost_vtoken_minting::Pallet::<T>::set_minimum_mint(RawOrigin::Root.into(), KSM, minimum_mint_value));
assert_ok!(bifrost_vtoken_minting::Pallet::<T>::mint(RawOrigin::Signed(caller.clone()).into(), KSM, token_amount, BoundedVec::default()));
}: _(RawOrigin::Root, 0, KSM, 1_000_000_000u32.into())

impl_benchmark_test_suite!(Salp, crate::mock::new_test_ext(), crate::mock::Test);
}
2 changes: 1 addition & 1 deletion pallets/salp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1239,7 +1239,7 @@ pub mod pallet {
}

#[pallet::call_index(21)]
#[pallet::weight(T::WeightInfo::buyback())]
#[pallet::weight(T::WeightInfo::buyback_vstoken_by_stable_pool())]
pub fn buyback_vstoken_by_stable_pool(
origin: OriginFor<T>,
pool_id: StableAssetPoolId,
Expand Down
2 changes: 1 addition & 1 deletion pallets/salp/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ construct_runtime!(
AssetRegistry: bifrost_asset_registry::{Pallet, Call,Config<T>, Event<T>, Storage},
PolkadotXcm: pallet_xcm::{Pallet, Call, Storage, Event<T>, Origin, Config},
StableAsset: nutsfinance_stable_asset::{Pallet, Storage, Event<T>},
StablePool: bifrost_stable_pool,
StablePool: bifrost_stable_pool::{Pallet, Call, Storage},
VtokenMinting: bifrost_vtoken_minting::{Pallet, Call, Storage, Event<T>},
}
);
Expand Down
41 changes: 41 additions & 0 deletions pallets/salp/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ pub trait WeightInfo {
fn dissolve_refunded() -> Weight;
fn dissolve() -> Weight;
fn buyback() -> Weight;
fn buyback_vstoken_by_stable_pool() -> Weight;
}

/// Weights for bifrost_salp using the Bifrost node and recommended hardware.
Expand Down Expand Up @@ -371,6 +372,26 @@ impl<T: frame_system::Config> WeightInfo for BifrostWeight<T> {
Weight::from_parts(32_722_000, 499)
.saturating_add(T::DbWeight::get().reads(1_u64))
}
/// Storage: StableAsset Pools (r:1 w:1)
/// Proof Skipped: StableAsset Pools (max_values: None, max_size: None, mode: Measured)
/// Storage: Tokens Accounts (r:4 w:4)
/// Proof: Tokens Accounts (max_values: None, max_size: Some(118), added: 2593, mode: MaxEncodedLen)
/// Storage: StableAsset TokenRateCaches (r:2 w:0)
/// Proof Skipped: StableAsset TokenRateCaches (max_values: None, max_size: None, mode: Measured)
/// Storage: AssetRegistry CurrencyMetadatas (r:1 w:0)
/// Proof Skipped: AssetRegistry CurrencyMetadatas (max_values: None, max_size: None, mode: Measured)
/// Storage: System Account (r:2 w:1)
/// Proof: System Account (max_values: None, max_size: Some(128), added: 2603, mode: MaxEncodedLen)
fn buyback_vstoken_by_stable_pool() -> Weight {
// Proof Size summary in bytes:
// Measured: `2331`
// Estimated: `11362`
// Minimum execution time: 89_049_000 picoseconds.
Weight::from_parts(89_820_000, 0)
.saturating_add(Weight::from_parts(0, 11362))
.saturating_add(T::DbWeight::get().reads(10))
.saturating_add(T::DbWeight::get().writes(6))
}
}

// For backwards compatibility and tests
Expand Down Expand Up @@ -668,4 +689,24 @@ impl WeightInfo for () {
Weight::from_parts(32_722_000, 499)
.saturating_add(RocksDbWeight::get().reads(1_u64))
}
/// Storage: StableAsset Pools (r:1 w:1)
/// Proof Skipped: StableAsset Pools (max_values: None, max_size: None, mode: Measured)
/// Storage: Tokens Accounts (r:4 w:4)
/// Proof: Tokens Accounts (max_values: None, max_size: Some(118), added: 2593, mode: MaxEncodedLen)
/// Storage: StableAsset TokenRateCaches (r:2 w:0)
/// Proof Skipped: StableAsset TokenRateCaches (max_values: None, max_size: None, mode: Measured)
/// Storage: AssetRegistry CurrencyMetadatas (r:1 w:0)
/// Proof Skipped: AssetRegistry CurrencyMetadatas (max_values: None, max_size: None, mode: Measured)
/// Storage: System Account (r:2 w:1)
/// Proof: System Account (max_values: None, max_size: Some(128), added: 2603, mode: MaxEncodedLen)
fn buyback_vstoken_by_stable_pool() -> Weight {
// Proof Size summary in bytes:
// Measured: `2331`
// Estimated: `11362`
// Minimum execution time: 89_049_000 picoseconds.
Weight::from_parts(89_820_000, 0)
.saturating_add(Weight::from_parts(0, 11362))
.saturating_add(RocksDbWeight::get().reads(10))
.saturating_add(RocksDbWeight::get().writes(6))
}
}
7 changes: 1 addition & 6 deletions pallets/stable-pool/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,6 @@ benchmarks! {
&fee_account,
<T as nutsfinance_stable_asset::Config>::Balance::from(100_000_000_000u128.into())
)?;
// T::MultiCurrency::mint_into(
// VDOT.into(),
// &fee_account,
// <T as nutsfinance_stable_asset::Config>::Balance::from(100_000_000_000u128.into())
// )?;
let amounts = vec![<T as nutsfinance_stable_asset::Config>::Balance::from(10_000_000_000u128.into()), <T as nutsfinance_stable_asset::Config>::Balance::from(10_000_000_000u128.into())];
assert_ok!(StablePool::<T>::create_pool(
RawOrigin::Root.into(),
Expand All @@ -80,7 +75,7 @@ benchmarks! {
fee_account.clone(),
fee_account.clone(),
1000000000000000000u128.into()));
assert_ok!(StablePool::<T>::edit_token_rate(RawOrigin::Root.into(), 0, vec![(BNC.into(), (9u128.into(), 10u128.into()))]));
assert_ok!(StablePool::<T>::edit_token_rate(RawOrigin::Root.into(), 0, vec![(BNC.into(), (9u128.into(), 10u128.into())), (KSM.into(), (1u128.into(), 1u128.into()))]));
}: _(RawOrigin::Signed(fee_account), 0, amounts, <T as nutsfinance_stable_asset::Config>::Balance::zero())

swap {
Expand Down
29 changes: 14 additions & 15 deletions pallets/stable-pool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,23 +118,8 @@ pub mod pallet {

#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(10)]
#[pallet::weight(<T as pallet::Config>::WeightInfo::edit_token_rate())]
pub fn edit_token_rate(
origin: OriginFor<T>,
pool_id: StableAssetPoolId,
token_rate_info: Vec<(
AssetIdOf<T>,
(AtLeast64BitUnsignedOf<T>, AtLeast64BitUnsignedOf<T>),
)>,
) -> DispatchResult {
T::ControlOrigin::ensure_origin(origin)?;
nutsfinance_stable_asset::Pallet::<T>::set_token_rate(pool_id, token_rate_info)
}

#[pallet::call_index(0)]
#[pallet::weight(<T as pallet::Config>::WeightInfo::create_pool())]
#[transactional]
pub fn create_pool(
origin: OriginFor<T>,
pool_asset: AssetIdOf<T>,
Expand Down Expand Up @@ -305,6 +290,20 @@ pub mod pallet {
Ok(())
})
}

#[pallet::call_index(9)]
#[pallet::weight(<T as pallet::Config>::WeightInfo::edit_token_rate())]
pub fn edit_token_rate(
origin: OriginFor<T>,
pool_id: StableAssetPoolId,
token_rate_info: Vec<(
AssetIdOf<T>,
(AtLeast64BitUnsignedOf<T>, AtLeast64BitUnsignedOf<T>),
)>,
) -> DispatchResult {
T::ControlOrigin::ensure_origin(origin)?;
nutsfinance_stable_asset::Pallet::<T>::set_token_rate(pool_id, token_rate_info)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion pallets/vtoken-minting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub type CurrencyIdOf<T> = <<T as Config>::MultiCurrency as MultiCurrency<
<T as frame_system::Config>::AccountId,
>>::CurrencyId;

type BalanceOf<T> = <<T as Config>::MultiCurrency as MultiCurrency<AccountIdOf<T>>>::Balance;
pub type BalanceOf<T> = <<T as Config>::MultiCurrency as MultiCurrency<AccountIdOf<T>>>::Balance;

pub type UnlockId = u32;

Expand Down
1 change: 0 additions & 1 deletion runtime/bifrost-polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1686,7 +1686,6 @@ mod benches {
[bifrost_call_switchgear, CallSwitchgear]
[bifrost_vtoken_minting, VtokenMinting]
[bifrost_slp, Slp]
[bifrost_salp, Salp]
[bifrost_ve_minting, VeMinting]
[bifrost_slpx, Slpx]
);
Expand Down

0 comments on commit 14dc32e

Please sign in to comment.