Skip to content

Commit

Permalink
treasury-convert-benchmark (#1232)
Browse files Browse the repository at this point in the history
  • Loading branch information
herryho authored Apr 23, 2024
1 parent 1fbf122 commit b041b5a
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pallets/slp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ bifrost-vtoken-minting = { workspace = true }
bifrost-asset-registry = { workspace = true }
bifrost-parachain-staking = { workspace = true }
bifrost-stable-pool = { workspace = true }
bifrost-stable-asset = { workspace = true }

[dev-dependencies]
hex = "0.4.3"
Expand Down
154 changes: 151 additions & 3 deletions pallets/slp/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,112 @@
#![cfg(feature = "runtime-benchmarks")]

use crate::*;
use bifrost_primitives::{DOT, VDOT, VKSM};
use frame_benchmarking::v2::*;
use frame_support::assert_ok;
use sp_runtime::traits::UniqueSaturatedFrom;
use frame_support::{assert_ok, PalletId};
use frame_system::RawOrigin as SystemOrigin;
use sp_runtime::traits::{AccountIdConversion, StaticLookup, UniqueSaturatedFrom};

const DELEGATOR1: MultiLocation =
MultiLocation { parents: 1, interior: X1(AccountId32 { network: None, id: [1u8; 32] }) };
const DELEGATOR2: MultiLocation =
MultiLocation { parents: 1, interior: X1(AccountId32 { network: None, id: [2u8; 32] }) };

type TokenBalanceOf<T> = <<T as pallet::Config>::MultiCurrency as orml_traits::MultiCurrency<
<T as frame_system::Config>::AccountId,
>>::Balance;

pub fn unit(d: u128) -> u128 {
d.saturating_mul(10_u128.pow(12))
}

fn init_stable_asset_pool<
T: Config
+ bifrost_stable_pool::Config
+ pallet_balances::Config<Balance = u128>
+ bifrost_stable_asset::Config,
>() -> Result<(), BenchmarkError> {
let caller: AccountIdOf<T> = whitelisted_caller();
let account_id = T::Lookup::unlookup(caller.clone());
pallet_balances::Pallet::<T>::force_set_balance(
SystemOrigin::Root.into(),
account_id,
10_000_000_000_000_u128,
)
.unwrap();

<T as bifrost_stable_pool::Config>::MultiCurrency::deposit(
DOT.into(),
&caller,
<T as bifrost_stable_asset::Config>::Balance::from(unit(1_000_000).into()),
)?;
<T as bifrost_stable_pool::Config>::MultiCurrency::deposit(
VDOT.into(),
&caller,
<T as bifrost_stable_asset::Config>::Balance::from(unit(1_000_000).into()),
)?;
let fee_account: AccountIdOf<T> = account("caller", 2, 2);
pallet_balances::Pallet::<T>::force_set_balance(
SystemOrigin::Root.into(),
T::Lookup::unlookup(caller.clone()),
10_000_000_000_000_u128,
)
.unwrap();
pallet_balances::Pallet::<T>::force_set_balance(
SystemOrigin::Root.into(),
T::Lookup::unlookup(fee_account.clone()),
10_000_000_000_000_u128,
)
.unwrap();

let coin0 = DOT;
let coin1 = VDOT;
let amounts = vec![
<T as bifrost_stable_asset::Config>::Balance::from(unit(100u128).into()),
<T as bifrost_stable_asset::Config>::Balance::from(unit(100u128).into()),
];

let origin = <T as Config>::ControlOrigin::try_successful_origin()
.map_err(|_| BenchmarkError::Weightless)?;

assert_ok!(bifrost_stable_pool::Pallet::<T>::create_pool(
origin.clone() as <T as frame_system::Config>::RuntimeOrigin,
vec![coin0.into(), coin1.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(
origin.clone() as <T as frame_system::Config>::RuntimeOrigin,
0,
vec![
(DOT.into(), (1u128.into(), 1u128.into())),
(VDOT.into(), (90_000_000u128.into(), 100_000_000u128.into()))
]
));
assert_ok!(bifrost_stable_pool::Pallet::<T>::add_liquidity(
SystemOrigin::Signed(caller.clone()).into(),
0,
amounts,
<T as bifrost_stable_asset::Config>::Balance::zero()
));

let treasury_account = PalletId(*b"bf/trsry").into_account_truncating();
// deposit some VDOT to the treasury account
<T as bifrost_stable_pool::Config>::MultiCurrency::deposit(
VDOT.into(),
&treasury_account,
<T as bifrost_stable_asset::Config>::Balance::from(unit(1_000_000).into()),
)?;

Ok(())
}

pub fn set_mins_and_maxs<T: Config>(origin: <T as frame_system::Config>::RuntimeOrigin) {
let mins_and_maxs = MinimumsMaximums {
delegator_bonded_minimum: 0u32.into(),
Expand Down Expand Up @@ -97,7 +194,9 @@ pub fn init_ongoing_time<T: Config>(origin: <T as frame_system::Config>::Runtime
assert_ok!(Pallet::<T>::set_currency_delays(origin.clone(), KSM, Some(delay)));
}

#[benchmarks(where T: Config + orml_tokens::Config<CurrencyId = CurrencyId> + bifrost_vtoken_minting::Config)]
#[benchmarks(where T: Config + orml_tokens::Config<CurrencyId = CurrencyId> + bifrost_vtoken_minting::Config+ bifrost_stable_pool::Config+ pallet_balances::Config<Balance=u128> + bifrost_asset_registry::Config)]
// #[benchmarks(where T: Config + bifrost_stable_pool::Config +
// pallet_balances::Config<Balance=u128>)]
mod benchmarks {
use super::*;
use crate::primitives::{PhalaLedger, SubstrateValidatorsByDelegatorUpdateEntry};
Expand Down Expand Up @@ -1083,6 +1182,55 @@ mod benchmarks {
Ok(())
}

#[benchmark]
fn convert_treasury_vtoken() -> Result<(), BenchmarkError> {
let origin = <T as Config>::ControlOrigin::try_successful_origin()
.map_err(|_| BenchmarkError::Weightless)?;

init_stable_asset_pool::<T>()?;

let treasury_account = PalletId(*b"bf/trsry").into_account_truncating();
assert_eq!(
<T as Config>::MultiCurrency::free_balance(VDOT, &treasury_account),
TokenBalanceOf::<T>::unique_saturated_from(1_000_0000_0000_0000_000u128)
);
assert_eq!(
<T as Config>::MultiCurrency::free_balance(DOT, &treasury_account),
Zero::zero()
);

let metadata = AssetMetadata {
name: b"DOT Native Token".to_vec(),
symbol: b"DOT".to_vec(),
decimals: 10,
minimal_balance: Zero::zero(),
};
// register DOT in registry pallet
assert_ok!(bifrost_asset_registry::Pallet::<T>::register_token_metadata(
origin.clone(),
Box::new(metadata.clone())
));

#[extrinsic_call]
_(
origin as <T as frame_system::Config>::RuntimeOrigin,
VDOT,
TokenBalanceOf::<T>::from(10u32),
);

// get the VDOT balance of treasury account
assert_eq!(
<T as Config>::MultiCurrency::free_balance(VDOT, &treasury_account),
TokenBalanceOf::<T>::unique_saturated_from(999_999_999_999_999_990u128)
);
assert_eq!(
<T as Config>::MultiCurrency::free_balance(DOT, &treasury_account),
TokenBalanceOf::<T>::from(10u32)
);

Ok(())
}

#[benchmark]
fn clean_outdated_validator_boost_list() -> Result<(), BenchmarkError> {
let origin = <T as Config>::ControlOrigin::try_successful_origin()
Expand Down
45 changes: 42 additions & 3 deletions pallets/slp/src/mocks/mock_kusama.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ use frame_support::{
construct_runtime, ord_parameter_types,
pallet_prelude::Get,
parameter_types,
traits::{Everything, Nothing, ProcessMessageError},
traits::{ConstU128, ConstU32, Everything, Nothing, ProcessMessageError},
PalletId,
};
use frame_system::{EnsureRoot, EnsureSignedBy};
use hex_literal::hex;
use orml_traits::{location::RelativeReserveProvider, parameter_type_with_key};
use parity_scale_codec::{Decode, Encode};
use sp_core::{bounded::BoundedVec, hashing::blake2_256, ConstU32, H256};
use sp_core::{bounded::BoundedVec, hashing::blake2_256, H256};
pub use sp_runtime::{testing::Header, Perbill};
use sp_runtime::{
traits::{AccountIdConversion, Convert, TrailingZeroInput},
Expand Down Expand Up @@ -74,9 +74,48 @@ construct_runtime!(
ParachainStaking: bifrost_parachain_staking,
Utility: pallet_utility,
PolkadotXcm: pallet_xcm,
StableAsset: bifrost_stable_asset,
StablePool: bifrost_stable_pool,
}
);

impl bifrost_stable_pool::Config for Runtime {
type WeightInfo = ();
type ControlOrigin = EnsureSignedBy<One, AccountId>;
type CurrencyId = CurrencyId;
type MultiCurrency = Currencies;
type StableAsset = StableAsset;
type VtokenMinting = VtokenMinting;
type CurrencyIdConversion = AssetIdMaps<Runtime>;
type CurrencyIdRegister = AssetIdMaps<Runtime>;
}

pub struct EnsurePoolAssetId;
impl bifrost_stable_asset::traits::ValidateAssetId<CurrencyId> for EnsurePoolAssetId {
fn validate(_: CurrencyId) -> bool {
true
}
}
parameter_types! {
pub const StableAssetPalletId: PalletId = PalletId(*b"nuts/sta");
}

impl bifrost_stable_asset::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type AssetId = CurrencyId;
type Balance = Balance;
type Assets = Currencies;
type PalletId = StableAssetPalletId;
type AtLeast64BitUnsigned = u128;
type FeePrecision = ConstU128<10_000_000_000>;
type APrecision = ConstU128<100>;
type PoolAssetLimit = ConstU32<5>;
type SwapExactOverAmount = ConstU128<100>;
type WeightInfo = ();
type ListingOrigin = EnsureSignedBy<One, AccountId>;
type EnsurePoolAssetId = EnsurePoolAssetId;
}

parameter_types! {
pub const NativeCurrencyId: CurrencyId = BNC;
pub const RelayCurrencyId: CurrencyId = KSM;
Expand Down Expand Up @@ -516,7 +555,7 @@ impl Config for Runtime {
type MaxLengthLimit = MaxLengthLimit;
type XcmWeightAndFeeHandler = XcmDestWeightAndFee;
type ChannelCommission = ();
type StablePoolHandler = ();
type StablePoolHandler = StablePool;
type AssetIdMaps = AssetIdMaps<Runtime>;
type TreasuryAccount = BifrostTreasuryAccount;
}
Expand Down

0 comments on commit b041b5a

Please sign in to comment.