From aa12c5d81ff410d9e2380f97ec9f384eafbafe19 Mon Sep 17 00:00:00 2001 From: ferrell-code Date: Tue, 2 Aug 2022 16:28:14 -0400 Subject: [PATCH] Refactor Benchmarks and update calls that use `T::Swap` (Honzon and Cdp Treasury) (#2303) * refactor inject utility * refactor currency id in benchmarks * refactor fixes * update honzon and cdp treasury benches * Update runtime/mandala/src/benchmarking/utils.rs Co-authored-by: Xiliang Chen * use STAKING for prices benchmark * decrease ED a little * update to use correct ED * cargo run --quiet --profile=production --color=never --bin=acala --features=runtime-benchmarks --features=with-acala-runtime --features=on-chain-release-build -- benchmark pallet --chain=acala-dev --steps=50 --repeat=20 --pallet=module_honzon --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --template=./templates/runtime-weight-template.hbs --output=./runtime/acala/src/weights/ * cargo run --quiet --profile=production --color=never --bin=acala --features=runtime-benchmarks --features=with-acala-runtime --features=on-chain-release-build -- benchmark pallet --chain=acala-dev --steps=50 --repeat=20 --pallet=module_cdp_treasury --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --template=./templates/runtime-weight-template.hbs --output=./runtime/acala/src/weights/ * cargo run --quiet --profile=production --color=never --bin=acala --features=runtime-benchmarks --features=with-acala-runtime --features=on-chain-release-build -- benchmark pallet --chain=acala-dev --steps=50 --repeat=20 --pallet=module_prices --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --template=./templates/runtime-weight-template.hbs --output=./runtime/acala/src/weights/ Co-authored-by: Xiliang Chen Co-authored-by: Acala Benchmarking Bot --- .../acala/src/weights/module_cdp_treasury.rs | 35 ++-- runtime/acala/src/weights/module_honzon.rs | 50 +++--- runtime/acala/src/weights/module_prices.rs | 11 +- .../src/benchmarking/asset_registry.rs | 6 +- runtime/mandala/src/benchmarking/auction.rs | 10 +- .../src/benchmarking/auction_manager.rs | 10 +- .../mandala/src/benchmarking/cdp_engine.rs | 43 +---- .../mandala/src/benchmarking/cdp_treasury.rs | 19 +-- .../mandala/src/benchmarking/currencies.rs | 10 +- runtime/mandala/src/benchmarking/dex.rs | 47 +---- .../mandala/src/benchmarking/dex_oracle.rs | 48 +----- runtime/mandala/src/benchmarking/earning.rs | 9 +- .../src/benchmarking/emergency_shutdown.rs | 6 +- runtime/mandala/src/benchmarking/evm.rs | 6 +- .../mandala/src/benchmarking/evm_accounts.rs | 6 +- runtime/mandala/src/benchmarking/homa.rs | 19 +-- runtime/mandala/src/benchmarking/honzon.rs | 66 +------ .../mandala/src/benchmarking/incentives.rs | 19 +-- .../src/benchmarking/nominees_election.rs | 6 +- .../benchmarking/nutsfinance_stable_asset.rs | 75 +++----- runtime/mandala/src/benchmarking/prices.rs | 6 +- runtime/mandala/src/benchmarking/tokens.rs | 6 +- .../src/benchmarking/transaction_payment.rs | 102 ++++------- runtime/mandala/src/benchmarking/utils.rs | 161 +++++++++++++++++- runtime/mandala/src/benchmarking/vesting.rs | 8 +- 25 files changed, 324 insertions(+), 460 deletions(-) diff --git a/runtime/acala/src/weights/module_cdp_treasury.rs b/runtime/acala/src/weights/module_cdp_treasury.rs index 7c669a65cc..f275a11cbb 100644 --- a/runtime/acala/src/weights/module_cdp_treasury.rs +++ b/runtime/acala/src/weights/module_cdp_treasury.rs @@ -19,7 +19,7 @@ //! Autogenerated weights for module_cdp_treasury //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2022-07-25, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2022-08-01, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("acala-dev"), DB CACHE: 1024 // Executed Command: @@ -29,7 +29,7 @@ // --chain=acala-dev // --steps=50 // --repeat=20 -// --pallet=* +// --pallet=module_cdp_treasury // --extrinsic=* // --execution=wasm // --wasm-execution=compiled @@ -58,31 +58,36 @@ impl module_cdp_treasury::WeightInfo for WeightInfo // Storage: Auction AuctionEndTime (r:0 w:1) // Storage: Auction Auctions (r:0 w:1) fn auction_collateral(b: u32, ) -> Weight { - (39_476_000 as Weight) - // Standard Error: 79_000 - .saturating_add((11_045_000 as Weight).saturating_mul(b as Weight)) + (35_898_000 as Weight) + // Standard Error: 22_000 + .saturating_add((11_084_000 as Weight).saturating_mul(b as Weight)) .saturating_add(T::DbWeight::get().reads(7 as Weight)) .saturating_add(T::DbWeight::get().writes(7 as Weight)) .saturating_add(T::DbWeight::get().writes((3 as Weight).saturating_mul(b as Weight))) } // Storage: unknown [0x3a7472616e73616374696f6e5f6c6576656c3a] (r:1 w:1) - // Storage: Tokens Accounts (r:4 w:4) + // Storage: Tokens Accounts (r:8 w:8) // Storage: AuctionManager TotalCollateralInAuction (r:1 w:0) - // Storage: Dex TradingPairStatuses (r:3 w:0) - // Storage: Dex LiquidityPool (r:1 w:1) - // Storage: StableAsset Pools (r:1 w:0) + // Storage: Dex TradingPairStatuses (r:5 w:0) + // Storage: Dex LiquidityPool (r:5 w:1) + // Storage: StableAsset Pools (r:2 w:1) // Storage: AggregatedDex AggregatedSwapPaths (r:1 w:0) - // Storage: System Account (r:2 w:1) + // Storage: Homa TotalStakingBonded (r:1 w:0) + // Storage: Homa ToBondPool (r:1 w:0) + // Storage: Tokens TotalIssuance (r:2 w:1) + // Storage: Homa TotalVoidLiquid (r:1 w:0) + // Storage: System Account (r:3 w:1) // Storage: EvmAccounts EvmAddresses (r:1 w:0) + // Storage: AssetRegistry AssetMetadatas (r:1 w:0) fn exchange_collateral_to_stable() -> Weight { - (123_137_000 as Weight) - .saturating_add(T::DbWeight::get().reads(15 as Weight)) - .saturating_add(T::DbWeight::get().writes(7 as Weight)) + (333_621_000 as Weight) + .saturating_add(T::DbWeight::get().reads(33 as Weight)) + .saturating_add(T::DbWeight::get().writes(13 as Weight)) } // Storage: unknown [0x3a7472616e73616374696f6e5f6c6576656c3a] (r:1 w:1) // Storage: CdpTreasury ExpectedCollateralAuctionSize (r:0 w:1) fn set_expected_collateral_auction_size() -> Weight { - (13_748_000 as Weight) + (13_320_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(2 as Weight)) } @@ -90,7 +95,7 @@ impl module_cdp_treasury::WeightInfo for WeightInfo // Storage: Tokens Accounts (r:2 w:2) // Storage: System Account (r:2 w:1) fn extract_surplus_to_treasury() -> Weight { - (40_323_000 as Weight) + (40_013_000 as Weight) .saturating_add(T::DbWeight::get().reads(5 as Weight)) .saturating_add(T::DbWeight::get().writes(4 as Weight)) } diff --git a/runtime/acala/src/weights/module_honzon.rs b/runtime/acala/src/weights/module_honzon.rs index 817dc2b170..1f76ccd4c1 100644 --- a/runtime/acala/src/weights/module_honzon.rs +++ b/runtime/acala/src/weights/module_honzon.rs @@ -19,7 +19,7 @@ //! Autogenerated weights for module_honzon //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2022-07-25, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2022-08-01, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("acala-dev"), DB CACHE: 1024 // Executed Command: @@ -29,7 +29,7 @@ // --chain=acala-dev // --steps=50 // --repeat=20 -// --pallet=* +// --pallet=module_honzon // --extrinsic=* // --execution=wasm // --wasm-execution=compiled @@ -51,7 +51,7 @@ impl module_honzon::WeightInfo for WeightInfo { // Storage: Honzon Authorization (r:1 w:1) // Storage: Balances Reserves (r:1 w:1) fn authorize() -> Weight { - (29_314_000 as Weight) + (29_590_000 as Weight) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } @@ -59,7 +59,7 @@ impl module_honzon::WeightInfo for WeightInfo { // Storage: Honzon Authorization (r:1 w:1) // Storage: Balances Reserves (r:1 w:1) fn unauthorize() -> Weight { - (31_004_000 as Weight) + (31_399_000 as Weight) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } @@ -67,9 +67,9 @@ impl module_honzon::WeightInfo for WeightInfo { // Storage: Balances Reserves (r:1 w:0) // Storage: Honzon Authorization (r:0 w:1) fn unauthorize_all(c: u32, ) -> Weight { - (21_015_000 as Weight) - // Standard Error: 364_000 - .saturating_add((6_209_000 as Weight).saturating_mul(c as Weight)) + (22_300_000 as Weight) + // Standard Error: 398_000 + .saturating_add((4_857_000 as Weight).saturating_mul(c as Weight)) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) .saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(c as Weight))) @@ -89,7 +89,7 @@ impl module_honzon::WeightInfo for WeightInfo { // Storage: AcalaOracle Values (r:1 w:0) // Storage: AssetRegistry AssetMetadatas (r:2 w:0) fn adjust_loan() -> Weight { - (114_367_000 as Weight) + (104_230_000 as Weight) .saturating_add(T::DbWeight::get().reads(16 as Weight)) .saturating_add(T::DbWeight::get().writes(8 as Weight)) } @@ -107,7 +107,7 @@ impl module_honzon::WeightInfo for WeightInfo { // Storage: System Account (r:1 w:1) // Storage: Loans TotalPositions (r:1 w:1) fn transfer_loan_from() -> Weight { - (85_827_000 as Weight) + (86_873_000 as Weight) .saturating_add(T::DbWeight::get().reads(17 as Weight)) .saturating_add(T::DbWeight::get().writes(8 as Weight)) } @@ -136,7 +136,7 @@ impl module_honzon::WeightInfo for WeightInfo { // Storage: StableAsset Pools (r:1 w:0) // Storage: AggregatedDex AggregatedSwapPaths (r:1 w:0) fn close_loan_has_debit_by_dex() -> Weight { - (256_405_000 as Weight) + (257_598_000 as Weight) .saturating_add(T::DbWeight::get().reads(37 as Weight)) .saturating_add(T::DbWeight::get().writes(16 as Weight)) } @@ -147,7 +147,7 @@ impl module_honzon::WeightInfo for WeightInfo { // Storage: System Account (r:2 w:1) // Storage: Dex TradingPairStatuses (r:5 w:0) // Storage: Dex LiquidityPool (r:5 w:2) - // Storage: StableAsset Pools (r:1 w:0) + // Storage: StableAsset Pools (r:2 w:0) // Storage: AggregatedDex AggregatedSwapPaths (r:1 w:0) // Storage: CdpEngine DebitExchangeRate (r:1 w:0) // Storage: Loans Positions (r:1 w:1) @@ -158,28 +158,32 @@ impl module_honzon::WeightInfo for WeightInfo { // Storage: AcalaOracle Values (r:1 w:0) // Storage: AssetRegistry AssetMetadatas (r:2 w:0) fn expand_position_collateral() -> Weight { - (213_798_000 as Weight) - .saturating_add(T::DbWeight::get().reads(31 as Weight)) + (227_767_000 as Weight) + .saturating_add(T::DbWeight::get().reads(32 as Weight)) .saturating_add(T::DbWeight::get().writes(13 as Weight)) } // Storage: unknown [0x3a7472616e73616374696f6e5f6c6576656c3a] (r:1 w:1) // Storage: CdpEngine CollateralParams (r:1 w:0) // Storage: Loans Positions (r:1 w:1) // Storage: Dex TradingPairStatuses (r:5 w:0) - // Storage: Dex LiquidityPool (r:5 w:2) - // Storage: StableAsset Pools (r:1 w:0) + // Storage: Dex LiquidityPool (r:5 w:1) + // Storage: StableAsset Pools (r:2 w:1) // Storage: AggregatedDex AggregatedSwapPaths (r:1 w:0) - // Storage: Tokens Accounts (r:5 w:5) - // Storage: System Account (r:2 w:1) + // Storage: Homa TotalStakingBonded (r:1 w:0) + // Storage: Homa ToBondPool (r:1 w:0) + // Storage: Tokens TotalIssuance (r:3 w:2) + // Storage: Homa TotalVoidLiquid (r:1 w:0) + // Storage: Tokens Accounts (r:9 w:9) + // Storage: System Account (r:3 w:1) + // Storage: AssetRegistry AssetMetadatas (r:1 w:0) // Storage: CdpEngine DebitExchangeRate (r:1 w:0) // Storage: Rewards SharesAndWithdrawnRewards (r:1 w:1) // Storage: Rewards PoolInfos (r:1 w:1) // Storage: Loans TotalPositions (r:1 w:1) - // Storage: Tokens TotalIssuance (r:1 w:1) fn shrink_position_debit() -> Weight { - (208_443_000 as Weight) - .saturating_add(T::DbWeight::get().reads(27 as Weight)) - .saturating_add(T::DbWeight::get().writes(14 as Weight)) + (365_182_000 as Weight) + .saturating_add(T::DbWeight::get().reads(39 as Weight)) + .saturating_add(T::DbWeight::get().writes(19 as Weight)) } // Storage: unknown [0x3a7472616e73616374696f6e5f6c6576656c3a] (r:1 w:1) // Storage: Tokens Accounts (r:1 w:1) @@ -195,7 +199,7 @@ impl module_honzon::WeightInfo for WeightInfo { // Storage: Homa ToBondPool (r:1 w:0) // Storage: Homa TotalVoidLiquid (r:1 w:0) fn transfer_debit() -> Weight { - (134_399_000 as Weight) + (217_935_000 as Weight) .saturating_add(T::DbWeight::get().reads(21 as Weight)) .saturating_add(T::DbWeight::get().writes(7 as Weight)) } @@ -209,7 +213,7 @@ impl module_honzon::WeightInfo for WeightInfo { // Storage: Homa TotalVoidLiquid (r:1 w:0) // Storage: CdpEngine DebitExchangeRate (r:1 w:0) fn precompile_get_current_collateral_ratio() -> Weight { - (32_267_000 as Weight) + (32_496_000 as Weight) .saturating_add(T::DbWeight::get().reads(11 as Weight)) } } diff --git a/runtime/acala/src/weights/module_prices.rs b/runtime/acala/src/weights/module_prices.rs index e8231958e3..b935148b24 100644 --- a/runtime/acala/src/weights/module_prices.rs +++ b/runtime/acala/src/weights/module_prices.rs @@ -19,7 +19,7 @@ //! Autogenerated weights for module_prices //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2022-07-25, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2022-08-01, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("acala-dev"), DB CACHE: 1024 // Executed Command: @@ -29,7 +29,7 @@ // --chain=acala-dev // --steps=50 // --repeat=20 -// --pallet=* +// --pallet=module_prices // --extrinsic=* // --execution=wasm // --wasm-execution=compiled @@ -48,17 +48,18 @@ use sp_std::marker::PhantomData; pub struct WeightInfo(PhantomData); impl module_prices::WeightInfo for WeightInfo { // Storage: unknown [0x3a7472616e73616374696f6e5f6c6576656c3a] (r:1 w:1) + // Storage: AcalaOracle Values (r:1 w:0) // Storage: AssetRegistry AssetMetadatas (r:1 w:0) // Storage: Prices LockedPrice (r:0 w:1) fn lock_price() -> Weight { - (19_442_000 as Weight) - .saturating_add(T::DbWeight::get().reads(2 as Weight)) + (21_712_000 as Weight) + .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(2 as Weight)) } // Storage: unknown [0x3a7472616e73616374696f6e5f6c6576656c3a] (r:1 w:1) // Storage: Prices LockedPrice (r:1 w:1) fn unlock_price() -> Weight { - (16_525_000 as Weight) + (17_165_000 as Weight) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().writes(2 as Weight)) } diff --git a/runtime/mandala/src/benchmarking/asset_registry.rs b/runtime/mandala/src/benchmarking/asset_registry.rs index 84dba506da..e7a758e175 100644 --- a/runtime/mandala/src/benchmarking/asset_registry.rs +++ b/runtime/mandala/src/benchmarking/asset_registry.rs @@ -16,9 +16,9 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use crate::{AccountId, AssetRegistry, CurrencyId, GetNativeCurrencyId, Origin, Runtime, EVM}; +use crate::{AccountId, AssetRegistry, CurrencyId, Origin, Runtime, EVM}; -use super::utils::{dollar, set_balance}; +use super::utils::{dollar, set_balance, NATIVE}; use frame_support::assert_ok; use frame_system::RawOrigin; use module_evm::EvmAddress; @@ -28,8 +28,6 @@ use primitives::{currency::AssetMetadata, TokenSymbol}; use sp_std::{boxed::Box, str::FromStr, vec}; use xcm::{v1::MultiLocation, VersionedMultiLocation}; -const NATIVE: CurrencyId = GetNativeCurrencyId::get(); - pub fn alice() -> AccountId { ::AddressMapping::get_account_id(&alice_evm_addr()) } diff --git a/runtime/mandala/src/benchmarking/auction.rs b/runtime/mandala/src/benchmarking/auction.rs index ee03b56874..1f6d560e24 100644 --- a/runtime/mandala/src/benchmarking/auction.rs +++ b/runtime/mandala/src/benchmarking/auction.rs @@ -16,12 +16,9 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use crate::{ - AccountId, Auction, AuctionId, AuctionManager, AuctionTimeToClose, CdpTreasury, CurrencyId, GetStableCurrencyId, - GetStakingCurrencyId, Runtime, System, -}; +use crate::{AccountId, Auction, AuctionId, AuctionManager, AuctionTimeToClose, CdpTreasury, Runtime, System}; -use super::utils::{dollar, set_balance}; +use super::utils::{dollar, set_balance, STABLECOIN, STAKING}; use frame_benchmarking::{account, whitelisted_caller}; use frame_support::traits::OnFinalize; use frame_system::RawOrigin; @@ -33,9 +30,6 @@ const SEED: u32 = 0; const MAX_DOLLARS: u32 = 1000; const MAX_AUCTION_ID: u32 = 100; -const STABLECOIN: CurrencyId = GetStableCurrencyId::get(); -const STAKING: CurrencyId = GetStakingCurrencyId::get(); - runtime_benchmarks! { { Runtime, orml_auction } diff --git a/runtime/mandala/src/benchmarking/auction_manager.rs b/runtime/mandala/src/benchmarking/auction_manager.rs index 7266d39304..cea48d7faf 100644 --- a/runtime/mandala/src/benchmarking/auction_manager.rs +++ b/runtime/mandala/src/benchmarking/auction_manager.rs @@ -16,12 +16,9 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use crate::{ - AccountId, AuctionId, AuctionManager, CdpTreasury, Currencies, CurrencyId, EmergencyShutdown, GetStableCurrencyId, - GetStakingCurrencyId, Price, Runtime, -}; +use crate::{AccountId, AuctionId, AuctionManager, CdpTreasury, Currencies, EmergencyShutdown, Price, Runtime}; -use super::utils::{dollar, feed_price}; +use super::utils::{dollar, feed_price, STABLECOIN, STAKING}; use frame_benchmarking::account; use frame_system::RawOrigin; use module_support::{AuctionManager as AuctionManagerTrait, CDPTreasury}; @@ -32,9 +29,6 @@ use sp_std::vec; const SEED: u32 = 0; -const STABLECOIN: CurrencyId = GetStableCurrencyId::get(); -const STAKING: CurrencyId = GetStakingCurrencyId::get(); - runtime_benchmarks! { { Runtime, module_auction_manager } diff --git a/runtime/mandala/src/benchmarking/cdp_engine.rs b/runtime/mandala/src/benchmarking/cdp_engine.rs index c38fc47322..b85543791c 100644 --- a/runtime/mandala/src/benchmarking/cdp_engine.rs +++ b/runtime/mandala/src/benchmarking/cdp_engine.rs @@ -17,15 +17,14 @@ // along with this program. If not, see . use crate::{ - AccountId, Address, Amount, Balance, CdpEngine, CdpTreasury, CurrencyId, DefaultDebitExchangeRate, Dex, - EmergencyShutdown, ExistentialDeposits, GetLiquidCurrencyId, GetNativeCurrencyId, GetStableCurrencyId, - GetStakingCurrencyId, MinimumDebitValue, NativeTokenExistentialDeposit, Price, Rate, Ratio, Runtime, Timestamp, + AccountId, Address, Amount, CdpEngine, CdpTreasury, CurrencyId, DefaultDebitExchangeRate, Dex, EmergencyShutdown, + ExistentialDeposits, MinimumDebitValue, NativeTokenExistentialDeposit, Price, Rate, Ratio, Runtime, Timestamp, H160, MILLISECS_PER_BLOCK, }; use super::{ get_benchmarking_collateral_currency_ids, - utils::{dollar, feed_price, set_balance}, + utils::{dollar, feed_price, inject_liquidity, set_balance, LIQUID, NATIVE, STABLECOIN, STAKING}, }; use frame_benchmarking::account; use frame_support::traits::{Get, OnInitialize}; @@ -41,36 +40,6 @@ use sp_std::prelude::*; const SEED: u32 = 0; -const STABLECOIN: CurrencyId = GetStableCurrencyId::get(); -const STAKING: CurrencyId = GetStakingCurrencyId::get(); -const LIQUID: CurrencyId = GetLiquidCurrencyId::get(); - -fn inject_liquidity( - maker: AccountId, - currency_id_a: CurrencyId, - currency_id_b: CurrencyId, - amount_a: Balance, - amount_b: Balance, -) -> Result<(), &'static str> { - // set balance - set_balance(currency_id_a, &maker, amount_a.unique_saturated_into()); - set_balance(currency_id_b, &maker, amount_b.unique_saturated_into()); - - let _ = Dex::enable_trading_pair(RawOrigin::Root.into(), currency_id_a, currency_id_b); - - Dex::add_liquidity( - RawOrigin::Signed(maker.clone()).into(), - currency_id_a, - currency_id_b, - amount_a, - amount_b, - Default::default(), - false, - )?; - - Ok(()) -} - runtime_benchmarks! { { Runtime, module_cdp_engine } @@ -101,7 +70,7 @@ runtime_benchmarks! { } let collateral_amount = Price::saturating_from_rational(dollar(currency_id), dollar(STABLECOIN)).saturating_mul_int(collateral_value); - let ed = if currency_id == GetNativeCurrencyId::get() { + let ed = if currency_id == NATIVE { NativeTokenExistentialDeposit::get() } else { ExistentialDeposits::get(¤cy_id) @@ -205,8 +174,8 @@ runtime_benchmarks! { let collateral_price = Price::one(); // 1 USD set_balance(LIQUID, &owner, (10 * collateral_amount) + ExistentialDeposits::get(&LIQUID)); - inject_liquidity(funder.clone(), LIQUID, STAKING, 10_000 * dollar(LIQUID), 10_000 * dollar(STAKING))?; - inject_liquidity(funder, STAKING, STABLECOIN, 10_000 * dollar(STAKING), 10_000 * dollar(STABLECOIN))?; + inject_liquidity(funder.clone(), LIQUID, STAKING, 10_000 * dollar(LIQUID), 10_000 * dollar(STAKING), false)?; + inject_liquidity(funder, STAKING, STABLECOIN, 10_000 * dollar(STAKING), 10_000 * dollar(STABLECOIN), false)?; // feed price feed_price(vec![(STAKING, collateral_price)])?; diff --git a/runtime/mandala/src/benchmarking/cdp_treasury.rs b/runtime/mandala/src/benchmarking/cdp_treasury.rs index fa71e9f153..6e4b2f89c5 100644 --- a/runtime/mandala/src/benchmarking/cdp_treasury.rs +++ b/runtime/mandala/src/benchmarking/cdp_treasury.rs @@ -16,9 +16,9 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use crate::{AccountId, CdpTreasury, Currencies, CurrencyId, Dex, GetStableCurrencyId, GetStakingCurrencyId, Runtime}; +use crate::{AccountId, CdpTreasury, Currencies, Runtime}; -use super::utils::{dollar, set_balance}; +use super::utils::{dollar, initialize_swap_pools, set_balance, STABLECOIN, STAKING}; use frame_benchmarking::whitelisted_caller; use frame_support::traits::Get; use frame_system::RawOrigin; @@ -26,9 +26,6 @@ use module_support::{CDPTreasury, SwapLimit}; use orml_benchmarking::runtime_benchmarks; use orml_traits::MultiCurrency; -const STABLECOIN: CurrencyId = GetStableCurrencyId::get(); -const STAKING: CurrencyId = GetStakingCurrencyId::get(); - runtime_benchmarks! { { Runtime, module_cdp_treasury } @@ -45,16 +42,8 @@ runtime_benchmarks! { let caller: AccountId = whitelisted_caller(); set_balance(STABLECOIN, &caller, 1000 * dollar(STABLECOIN)); set_balance(STAKING, &caller, 1000 * dollar(STAKING)); - let _ = Dex::enable_trading_pair(RawOrigin::Root.into(), STABLECOIN, STAKING); - Dex::add_liquidity( - RawOrigin::Signed(caller.clone()).into(), - STABLECOIN, - STAKING, - 1000 * dollar(STABLECOIN), - 100 * dollar(STAKING), - 0, - false, - )?; + initialize_swap_pools(caller.clone())?; + CdpTreasury::deposit_collateral(&caller, STAKING, 100 * dollar(STAKING))?; }: _(RawOrigin::Root, STAKING, SwapLimit::ExactSupply(100 * dollar(STAKING), 0)) diff --git a/runtime/mandala/src/benchmarking/currencies.rs b/runtime/mandala/src/benchmarking/currencies.rs index 1d2828b70b..651565b2dd 100644 --- a/runtime/mandala/src/benchmarking/currencies.rs +++ b/runtime/mandala/src/benchmarking/currencies.rs @@ -16,11 +16,8 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use super::utils::{dollar, lookup_of_account, set_balance}; -use crate::{ - AccountId, Amount, Balance, Currencies, CurrencyId, GetNativeCurrencyId, GetStakingCurrencyId, - NativeTokenExistentialDeposit, Runtime, Tokens, TreasuryPalletId, -}; +use super::utils::{dollar, lookup_of_account, set_balance, NATIVE, STAKING}; +use crate::{AccountId, Amount, Balance, Currencies, NativeTokenExistentialDeposit, Runtime, Tokens, TreasuryPalletId}; use sp_std::prelude::*; @@ -33,9 +30,6 @@ use orml_traits::MultiCurrency; const SEED: u32 = 0; -const NATIVE: CurrencyId = GetNativeCurrencyId::get(); -const STAKING: CurrencyId = GetStakingCurrencyId::get(); - runtime_benchmarks! { { Runtime, module_currencies } diff --git a/runtime/mandala/src/benchmarking/dex.rs b/runtime/mandala/src/benchmarking/dex.rs index ae31024cb1..f3b48acac4 100644 --- a/runtime/mandala/src/benchmarking/dex.rs +++ b/runtime/mandala/src/benchmarking/dex.rs @@ -16,11 +16,8 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use super::utils::dollar; -use crate::{ - AccountId, Balance, Currencies, CurrencyId, Dex, Event, ExtendedProvisioningBlocks, GetLiquidCurrencyId, - GetNativeCurrencyId, GetStableCurrencyId, GetStakingCurrencyId, Runtime, System, -}; +use super::utils::{dollar, inject_liquidity, LIQUID, NATIVE, STABLECOIN, STAKING}; +use crate::{AccountId, Currencies, CurrencyId, Dex, Event, ExtendedProvisioningBlocks, Runtime, System}; use frame_benchmarking::{account, whitelisted_caller}; use frame_system::RawOrigin; use module_dex::TradingPairStatus; @@ -33,52 +30,12 @@ use sp_std::prelude::*; const SEED: u32 = 0; -const NATIVE: CurrencyId = GetNativeCurrencyId::get(); -const STABLECOIN: CurrencyId = GetStableCurrencyId::get(); -const LIQUID: CurrencyId = GetLiquidCurrencyId::get(); -const STAKING: CurrencyId = GetStakingCurrencyId::get(); - const CURRENCY_LIST: [CurrencyId; 7] = [NATIVE, STABLECOIN, LIQUID, STAKING, BNC, VSKSM, RENBTC]; fn assert_last_event(generic_event: Event) { System::assert_last_event(generic_event.into()); } -fn inject_liquidity( - maker: AccountId, - currency_id_a: CurrencyId, - currency_id_b: CurrencyId, - max_amount_a: Balance, - max_amount_b: Balance, - deposit: bool, -) -> Result<(), &'static str> { - // set balance - >::update_balance( - currency_id_a, - &maker, - max_amount_a.unique_saturated_into(), - )?; - >::update_balance( - currency_id_b, - &maker, - max_amount_b.unique_saturated_into(), - )?; - - let _ = Dex::enable_trading_pair(RawOrigin::Root.into(), currency_id_a, currency_id_b); - - Dex::add_liquidity( - RawOrigin::Signed(maker.clone()).into(), - currency_id_a, - currency_id_b, - max_amount_a, - max_amount_b, - Default::default(), - deposit, - )?; - - Ok(()) -} - runtime_benchmarks! { { Runtime, module_dex } diff --git a/runtime/mandala/src/benchmarking/dex_oracle.rs b/runtime/mandala/src/benchmarking/dex_oracle.rs index ff50988cfd..eb019f88f3 100644 --- a/runtime/mandala/src/benchmarking/dex_oracle.rs +++ b/runtime/mandala/src/benchmarking/dex_oracle.rs @@ -16,59 +16,15 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use super::utils::dollar; -use crate::{ - AccountId, Balance, Currencies, CurrencyId, Dex, DexOracle, GetNativeCurrencyId, GetStableCurrencyId, - GetStakingCurrencyId, Runtime, Timestamp, -}; +use super::utils::{dollar, inject_liquidity, NATIVE, STABLECOIN, STAKING}; +use crate::{AccountId, DexOracle, Runtime, Timestamp}; use frame_benchmarking::whitelisted_caller; use frame_support::traits::OnInitialize; use frame_system::RawOrigin; use orml_benchmarking::runtime_benchmarks; -use orml_traits::MultiCurrencyExtended; use primitives::TradingPair; -use sp_runtime::traits::UniqueSaturatedInto; use sp_std::prelude::*; -const NATIVE: CurrencyId = GetNativeCurrencyId::get(); -const STABLECOIN: CurrencyId = GetStableCurrencyId::get(); -const STAKING: CurrencyId = GetStakingCurrencyId::get(); - -fn inject_liquidity( - maker: AccountId, - currency_id_a: CurrencyId, - currency_id_b: CurrencyId, - max_amount_a: Balance, - max_amount_b: Balance, - deposit: bool, -) -> Result<(), &'static str> { - // set balance - >::update_balance( - currency_id_a, - &maker, - max_amount_a.unique_saturated_into(), - )?; - >::update_balance( - currency_id_b, - &maker, - max_amount_b.unique_saturated_into(), - )?; - - let _ = Dex::enable_trading_pair(RawOrigin::Root.into(), currency_id_a, currency_id_b); - - Dex::add_liquidity( - RawOrigin::Signed(maker.clone()).into(), - currency_id_a, - currency_id_b, - max_amount_a, - max_amount_b, - Default::default(), - deposit, - )?; - - Ok(()) -} - runtime_benchmarks! { { Runtime, module_dex_oracle } diff --git a/runtime/mandala/src/benchmarking/earning.rs b/runtime/mandala/src/benchmarking/earning.rs index 41d72d2a0b..e5690ae47b 100644 --- a/runtime/mandala/src/benchmarking/earning.rs +++ b/runtime/mandala/src/benchmarking/earning.rs @@ -16,17 +16,12 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use super::utils::{dollar, set_balance}; -use crate::{ - AccountId, CurrencyId, DispatchResult, Earning, Get, GetNativeCurrencyId, NativeTokenExistentialDeposit, Origin, - Runtime, System, -}; +use super::utils::{dollar, set_balance, NATIVE}; +use crate::{AccountId, DispatchResult, Earning, Get, NativeTokenExistentialDeposit, Origin, Runtime, System}; use frame_benchmarking::whitelisted_caller; use frame_system::RawOrigin; use orml_benchmarking::runtime_benchmarks; -const NATIVE: CurrencyId = GetNativeCurrencyId::get(); - fn make_max_unbonding_chunk(who: AccountId) -> DispatchResult { System::set_block_number(0); set_balance(NATIVE, &who, 100 * dollar(NATIVE)); diff --git a/runtime/mandala/src/benchmarking/emergency_shutdown.rs b/runtime/mandala/src/benchmarking/emergency_shutdown.rs index 2b45e2a0af..7bb90a9305 100644 --- a/runtime/mandala/src/benchmarking/emergency_shutdown.rs +++ b/runtime/mandala/src/benchmarking/emergency_shutdown.rs @@ -16,11 +16,11 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use crate::{AccountId, CdpTreasury, CurrencyId, EmergencyShutdown, GetStableCurrencyId, Price, Runtime}; +use crate::{AccountId, CdpTreasury, CurrencyId, EmergencyShutdown, Price, Runtime}; use super::{ get_benchmarking_collateral_currency_ids, - utils::{dollar, feed_price, set_balance}, + utils::{dollar, feed_price, set_balance, STABLECOIN}, }; use frame_benchmarking::{account, whitelisted_caller}; use frame_system::RawOrigin; @@ -31,8 +31,6 @@ use sp_std::vec; const SEED: u32 = 0; -const STABLECOIN: CurrencyId = GetStableCurrencyId::get(); - runtime_benchmarks! { { Runtime, module_emergency_shutdown } diff --git a/runtime/mandala/src/benchmarking/evm.rs b/runtime/mandala/src/benchmarking/evm.rs index 3a21d70084..5f28fb5e00 100644 --- a/runtime/mandala/src/benchmarking/evm.rs +++ b/runtime/mandala/src/benchmarking/evm.rs @@ -16,9 +16,9 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use crate::{AccountId, CurrencyId, Event, EvmAccounts, GetNativeCurrencyId, Origin, Runtime, System, EVM}; +use crate::{AccountId, Event, EvmAccounts, Origin, Runtime, System, EVM}; -use super::utils::{dollar, set_balance}; +use super::utils::{dollar, set_balance, NATIVE}; use frame_support::dispatch::DispatchError; use frame_system::RawOrigin; use module_evm::MaxCodeSize; @@ -28,8 +28,6 @@ use sp_core::{H160, H256}; use sp_io::hashing::keccak_256; use sp_std::{str::FromStr, vec}; -const NATIVE: CurrencyId = GetNativeCurrencyId::get(); - fn contract_addr() -> H160 { H160::from_str("0x5e0b4bfa0b55932a3587e648c3552a6515ba56b1").unwrap() } diff --git a/runtime/mandala/src/benchmarking/evm_accounts.rs b/runtime/mandala/src/benchmarking/evm_accounts.rs index 672469bf0e..db8f687eea 100644 --- a/runtime/mandala/src/benchmarking/evm_accounts.rs +++ b/runtime/mandala/src/benchmarking/evm_accounts.rs @@ -16,9 +16,9 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use crate::{AccountId, CurrencyId, EvmAccounts, GetNativeCurrencyId, Runtime}; +use crate::{AccountId, EvmAccounts, Runtime}; -use super::utils::{dollar, set_balance}; +use super::utils::{dollar, set_balance, NATIVE}; use frame_benchmarking::{account, whitelisted_caller}; use frame_system::RawOrigin; use orml_benchmarking::runtime_benchmarks; @@ -26,8 +26,6 @@ use sp_io::hashing::keccak_256; const SEED: u32 = 0; -const NATIVE: CurrencyId = GetNativeCurrencyId::get(); - fn alice() -> libsecp256k1::SecretKey { libsecp256k1::SecretKey::parse(&keccak_256(b"Alice")).unwrap() } diff --git a/runtime/mandala/src/benchmarking/homa.rs b/runtime/mandala/src/benchmarking/homa.rs index e9dbe502af..f4c661ccff 100644 --- a/runtime/mandala/src/benchmarking/homa.rs +++ b/runtime/mandala/src/benchmarking/homa.rs @@ -17,11 +17,10 @@ // along with this program. If not, see . use crate::{ - AccountId, ActiveSubAccountsIndexList, Balance, Currencies, GetLiquidCurrencyId, GetStakingCurrencyId, Homa, Rate, - RelaychainBlockNumberProvider, Runtime, + AccountId, ActiveSubAccountsIndexList, Balance, Currencies, Homa, Rate, RelaychainBlockNumberProvider, Runtime, }; -use super::utils::set_balance; +use super::utils::{set_balance, LIQUID, STAKING}; use frame_benchmarking::{account, whitelisted_caller}; use frame_support::traits::OnInitialize; use frame_system::RawOrigin; @@ -46,8 +45,8 @@ runtime_benchmarks! { let redeemer: AccountId = account("redeemer", 0, SEED); let sub_account_index = ActiveSubAccountsIndexList::get().first().unwrap().clone(); - set_balance(GetStakingCurrencyId::get(), &minter, 1_000_000_000_000_000); - set_balance(GetLiquidCurrencyId::get(), &redeemer, 1_000_000_000_000_000 * 10); + set_balance(STAKING, &minter, 1_000_000_000_000_000); + set_balance(LIQUID, &redeemer, 1_000_000_000_000_000 * 10); Homa::reset_ledgers( RawOrigin::Root.into(), vec![(sub_account_index, Some(1_000_000_000_000_000), Some(vec![UnlockChunk{value: 1_000_000_000_000, era: 10}]))] @@ -80,14 +79,14 @@ runtime_benchmarks! { None, None, )?; - set_balance(GetStakingCurrencyId::get(), &caller, amount * 2); + set_balance(STAKING, &caller, amount * 2); }: _(RawOrigin::Signed(caller), amount) request_redeem { let caller: AccountId = whitelisted_caller(); let amount = 10_000_000_000_000; - set_balance(GetLiquidCurrencyId::get(), &caller, amount * 2); + set_balance(LIQUID, &caller, amount * 2); }: _(RawOrigin::Signed(caller), amount, true) fast_match_redeems { @@ -96,7 +95,7 @@ runtime_benchmarks! { let minter: AccountId = account("minter", 0, SEED); let mint_amount = 1_000_000_000_000_000; - set_balance(GetStakingCurrencyId::get(), &minter, mint_amount * 2); + set_balance(STAKING, &minter, mint_amount * 2); Homa::update_homa_params( RawOrigin::Root.into(), Some(mint_amount * 10), @@ -110,7 +109,7 @@ runtime_benchmarks! { let redeem_amount = 10_000_000_000_000; for i in 0 .. n { let redeemer = account("redeemer", i, SEED); - >::transfer(GetLiquidCurrencyId::get(), &minter, &redeemer, redeem_amount * 2)?; + >::transfer(LIQUID, &minter, &redeemer, redeem_amount * 2)?; Homa::request_redeem(RawOrigin::Signed(redeemer.clone()).into(), redeem_amount, true)?; redeem_request_list.push(redeemer); } @@ -122,7 +121,7 @@ runtime_benchmarks! { let redeption_amount = 1_000_000_000_000; module_homa::Unbondings::::insert(&redeemer, 1, redeption_amount); - set_balance(GetStakingCurrencyId::get(), &Homa::account_id(), redeption_amount); + set_balance(STAKING, &Homa::account_id(), redeption_amount); module_homa::UnclaimedRedemption::::put(redeption_amount); Homa::reset_current_era(RawOrigin::Root.into(), 1)?; }: _(RawOrigin::Signed(caller), redeemer) diff --git a/runtime/mandala/src/benchmarking/honzon.rs b/runtime/mandala/src/benchmarking/honzon.rs index 00875834b3..58759555b2 100644 --- a/runtime/mandala/src/benchmarking/honzon.rs +++ b/runtime/mandala/src/benchmarking/honzon.rs @@ -17,21 +17,21 @@ // along with this program. If not, see . use crate::{ - AccountId, Amount, Balance, CdpEngine, Currencies, CurrencyId, DepositPerAuthorization, Dex, ExistentialDeposits, - GetLiquidCurrencyId, GetNativeCurrencyId, GetStableCurrencyId, GetStakingCurrencyId, Honzon, Price, Rate, Ratio, + AccountId, Amount, CdpEngine, CurrencyId, DepositPerAuthorization, ExistentialDeposits, Honzon, Price, Rate, Ratio, Runtime, }; use super::{ get_benchmarking_collateral_currency_ids, - utils::{dollar, feed_price, set_balance}, + utils::{ + dollar, feed_price, initialize_swap_pools, inject_liquidity, set_balance, LIQUID, NATIVE, STABLECOIN, STAKING, + }, }; use frame_benchmarking::{account, whitelisted_caller}; use frame_system::RawOrigin; use module_support::HonzonManager; use orml_benchmarking::runtime_benchmarks; -use orml_traits::{Change, GetByKey, MultiCurrencyExtended}; -use runtime_common::LCDOT; +use orml_traits::{Change, GetByKey}; use sp_runtime::{ traits::{AccountIdLookup, One, StaticLookup, UniqueSaturatedInto}, FixedPointNumber, @@ -40,46 +40,6 @@ use sp_std::prelude::*; const SEED: u32 = 0; -const NATIVE: CurrencyId = GetNativeCurrencyId::get(); -const STABLECOIN: CurrencyId = GetStableCurrencyId::get(); -const STAKING: CurrencyId = GetStakingCurrencyId::get(); -const LIQUID: CurrencyId = GetLiquidCurrencyId::get(); - -fn inject_liquidity( - maker: AccountId, - currency_id_a: CurrencyId, - currency_id_b: CurrencyId, - max_amount_a: Balance, - max_amount_b: Balance, - deposit: bool, -) -> Result<(), &'static str> { - // set balance - >::update_balance( - currency_id_a, - &maker, - max_amount_a.unique_saturated_into(), - )?; - >::update_balance( - currency_id_b, - &maker, - max_amount_b.unique_saturated_into(), - )?; - - let _ = Dex::enable_trading_pair(RawOrigin::Root.into(), currency_id_a, currency_id_b); - - Dex::add_liquidity( - RawOrigin::Signed(maker.clone()).into(), - currency_id_a, - currency_id_b, - max_amount_a, - max_amount_b, - Default::default(), - deposit, - )?; - - Ok(()) -} - runtime_benchmarks! { { Runtime, module_honzon } @@ -254,13 +214,7 @@ runtime_benchmarks! { // set balance and inject liquidity for trading path set_balance(currency_id, &sender, (10 * collateral_amount) + ExistentialDeposits::get(¤cy_id)); - inject_liquidity(maker.clone(), LIQUID, STABLECOIN, 10_000 * dollar(LIQUID), 10_000 * dollar(STABLECOIN), false)?; - inject_liquidity(maker.clone(), currency_id, LIQUID, 10_000 * dollar(currency_id), 10_000 * dollar(LIQUID), false)?; - - // purposly inject too little liquidity to have failed path, still reads dexs to check for viable swap paths - inject_liquidity(maker.clone(), STAKING, STABLECOIN, 10 * dollar(STAKING), 10 * dollar(STABLECOIN), false)?; - inject_liquidity(maker.clone(), LCDOT, STABLECOIN, dollar(LCDOT), dollar(STABLECOIN), false)?; - inject_liquidity(maker.clone(), LCDOT, STAKING, dollar(LCDOT), dollar(STAKING), false)?; + initialize_swap_pools(maker)?; feed_price(vec![(currency_id, Price::one())])?; @@ -296,13 +250,7 @@ runtime_benchmarks! { // set balance and inject liquidity for trading path set_balance(currency_id, &sender, (10 * collateral_amount) + ExistentialDeposits::get(¤cy_id)); - inject_liquidity(maker.clone(), LIQUID, STABLECOIN, 10_000 * dollar(LIQUID), 10_000 * dollar(STABLECOIN), false)?; - inject_liquidity(maker.clone(), currency_id, LIQUID, 10_000 * dollar(currency_id), 10_000 * dollar(LIQUID), false)?; - - // purposly inject too little liquidity to have failed path, still reads dexs to check for viable swap paths - inject_liquidity(maker.clone(), STAKING, STABLECOIN, 10 * dollar(STAKING), 10 * dollar(STABLECOIN), false)?; - inject_liquidity(maker.clone(), LCDOT, STABLECOIN, dollar(LCDOT), dollar(STABLECOIN), false)?; - inject_liquidity(maker.clone(), LCDOT, STAKING, dollar(LCDOT), dollar(STAKING), false)?; + initialize_swap_pools(maker)?; feed_price(vec![(currency_id, Price::one())])?; diff --git a/runtime/mandala/src/benchmarking/incentives.rs b/runtime/mandala/src/benchmarking/incentives.rs index 178b017782..cf23a11026 100644 --- a/runtime/mandala/src/benchmarking/incentives.rs +++ b/runtime/mandala/src/benchmarking/incentives.rs @@ -16,14 +16,11 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use crate::{ - AccountId, AccumulatePeriod, Currencies, CurrencyId, GetNativeCurrencyId, GetStableCurrencyId, - GetStakingCurrencyId, Incentives, Rate, Rewards, Runtime, System, -}; +use crate::{AccountId, AccumulatePeriod, Currencies, CurrencyId, Incentives, Rate, Rewards, Runtime, System}; use super::{ get_benchmarking_collateral_currency_ids, - utils::{dollar, set_balance}, + utils::{dollar, set_balance, NATIVE, STABLECOIN, STAKING}, }; use frame_benchmarking::{account, whitelisted_caller, BenchmarkError}; use frame_support::traits::OnInitialize; @@ -35,10 +32,6 @@ use sp_std::prelude::*; const SEED: u32 = 0; -const NATIVE: CurrencyId = GetNativeCurrencyId::get(); -const STAKING: CurrencyId = GetStakingCurrencyId::get(); -const STABLECOIN: CurrencyId = GetStableCurrencyId::get(); - runtime_benchmarks! { { Runtime, module_incentives } @@ -83,11 +76,10 @@ runtime_benchmarks! { claim_rewards { let caller: AccountId = whitelisted_caller(); let pool_id = PoolId::Loans(STAKING); - let native_currency_id = GetNativeCurrencyId::get(); Rewards::add_share(&caller, &pool_id, 100); - Currencies::deposit(native_currency_id, &Incentives::account_id(), 80 * dollar(native_currency_id))?; - Rewards::accumulate_reward(&pool_id, native_currency_id, 80 * dollar(native_currency_id))?; + Currencies::deposit(NATIVE, &Incentives::account_id(), 80 * dollar(NATIVE))?; + Rewards::accumulate_reward(&pool_id, NATIVE, 80 * dollar(NATIVE))?; }: _(RawOrigin::Signed(caller), pool_id) update_incentive_rewards { @@ -106,14 +98,13 @@ runtime_benchmarks! { let currency_ids = get_benchmarking_collateral_currency_ids(); let caller: AccountId = account("caller", 0, SEED); let mut updates = vec![]; - let base_currency_id = GetStableCurrencyId::get(); for i in 0 .. c { let currency_id = currency_ids[i as usize]; if matches!(currency_id, CurrencyId::StableAssetPoolToken(_)) { continue; } - if let Some(lp_share_currency_id) = CurrencyId::join_dex_share_currency_id(currency_id, base_currency_id) { + if let Some(lp_share_currency_id) = CurrencyId::join_dex_share_currency_id(currency_id, STABLECOIN) { updates.push((PoolId::Dex(lp_share_currency_id), Rate::default())); } else { return Err(BenchmarkError::Stop("invalid currency id")); diff --git a/runtime/mandala/src/benchmarking/nominees_election.rs b/runtime/mandala/src/benchmarking/nominees_election.rs index 4c9fe70f42..381c20e26d 100644 --- a/runtime/mandala/src/benchmarking/nominees_election.rs +++ b/runtime/mandala/src/benchmarking/nominees_election.rs @@ -16,9 +16,9 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use crate::{AccountId, CurrencyId, GetLiquidCurrencyId, MinCouncilBondThreshold, NomineesElection, Runtime}; +use crate::{AccountId, MinCouncilBondThreshold, NomineesElection, Runtime}; -use super::utils::set_balance; +use super::utils::{set_balance, LIQUID}; use frame_benchmarking::{account, whitelisted_caller}; use frame_support::traits::Get; use frame_system::RawOrigin; @@ -28,8 +28,6 @@ use sp_std::prelude::*; const SEED: u32 = 0; -const LIQUID: CurrencyId = GetLiquidCurrencyId::get(); - runtime_benchmarks! { { Runtime, module_nominees_election } diff --git a/runtime/mandala/src/benchmarking/nutsfinance_stable_asset.rs b/runtime/mandala/src/benchmarking/nutsfinance_stable_asset.rs index 519c754a5c..6f28137fb4 100644 --- a/runtime/mandala/src/benchmarking/nutsfinance_stable_asset.rs +++ b/runtime/mandala/src/benchmarking/nutsfinance_stable_asset.rs @@ -16,15 +16,15 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use crate::{AccountId, AssetRegistry, DispatchResult, Runtime, StableAsset}; +use crate::{AccountId, Runtime, StableAsset}; -use super::utils::set_balance_fungibles; +use super::utils::{create_stable_pools, register_stable_asset, set_balance_fungibles}; use frame_benchmarking::{account, whitelisted_caller}; use frame_support::traits::Get; use frame_system::RawOrigin; use orml_benchmarking::runtime_benchmarks; use primitives::{ - currency::{AssetMetadata, CurrencyId, AUSD, BNC, LDOT, VSKSM}, + currency::{CurrencyId, AUSD, BNC, LDOT, VSKSM}, DexShare, TokenSymbol, }; use sp_std::prelude::*; @@ -38,41 +38,6 @@ const CURRENCY_LIST: [CurrencyId; 5] = [ VSKSM, ]; -fn register_stable_asset() -> DispatchResult { - let asset_metadata = AssetMetadata { - name: b"Token Name".to_vec(), - symbol: b"TN".to_vec(), - decimals: 12, - minimal_balance: 1, - }; - AssetRegistry::register_stable_asset(RawOrigin::Root.into(), Box::new(asset_metadata.clone())) -} - -fn create_pools(assets: Vec, precisions: Vec) -> DispatchResult { - let pool_asset = CurrencyId::StableAssetPoolToken(0); - let mint_fee = 10000000u128; - let swap_fee = 20000000u128; - let redeem_fee = 50000000u128; - let intial_a = 10000u128; - let fee_recipient: AccountId = account("fee", 0, SEED); - let yield_recipient: AccountId = account("yield", 1, SEED); - - register_stable_asset()?; - StableAsset::create_pool( - RawOrigin::Root.into(), - pool_asset, - assets, - precisions, - mint_fee, - swap_fee, - redeem_fee, - intial_a, - fee_recipient, - yield_recipient, - 1000000000000000000u128, - ) -} - runtime_benchmarks! { { Runtime, nutsfinance_stable_asset } @@ -92,14 +57,14 @@ runtime_benchmarks! { modify_a { let assets = vec![LDOT, AUSD]; let precisions = vec![1u128, 1u128]; - create_pools(assets, precisions)?; + create_stable_pools(assets, precisions)?; let pool_id = StableAsset::pool_count() - 1; }: _(RawOrigin::Root, pool_id, 1000u128, 2629112370) modify_fees { let assets = vec![LDOT, AUSD]; let precisions = vec![1u128, 1u128]; - create_pools(assets, precisions)?; + create_stable_pools(assets, precisions)?; let pool_id = StableAsset::pool_count() - 1; }: _(RawOrigin::Root, pool_id, Some(100u128), Some(200u128), Some(300u128)) @@ -114,12 +79,12 @@ runtime_benchmarks! { let multiple: u128 = (i + 1).into(); assets.push(CURRENCY_LIST[i_idx]); precisions.push(1u128); - mint_args.push(10000000000u128 * multiple); + mint_args.push(10000000000000u128 * multiple); } for asset in &CURRENCY_LIST { - set_balance_fungibles(*asset, &tester, 200000000000u128); + set_balance_fungibles(*asset, &tester, 200000000000000u128); } - create_pools(assets, precisions)?; + create_stable_pools(assets, precisions)?; let pool_id = StableAsset::pool_count() - 1; }: _(RawOrigin::Signed(tester), pool_id, mint_args, 0u128) @@ -134,12 +99,12 @@ runtime_benchmarks! { let multiple: u128 = (i + 1).into(); assets.push(CURRENCY_LIST[i_idx]); precisions.push(1u128); - mint_args.push(10000000000u128 * multiple); + mint_args.push(10000000000000u128 * multiple); } for asset in &CURRENCY_LIST { - set_balance_fungibles(*asset, &tester, 200000000000u128); + set_balance_fungibles(*asset, &tester, 200000000000000u128); } - create_pools(assets, precisions)?; + create_stable_pools(assets, precisions)?; let pool_id = StableAsset::pool_count() - 1; StableAsset::mint(RawOrigin::Signed(tester.clone()).into(), pool_id, mint_args, 0u128)?; }: _(RawOrigin::Signed(tester), pool_id, 0, 1, 5000000u128, 0u128, u) @@ -156,13 +121,13 @@ runtime_benchmarks! { let multiple: u128 = (i + 1).into(); assets.push(CURRENCY_LIST[i_idx]); precisions.push(1u128); - mint_args.push(10000000000u128 * multiple); + mint_args.push(10000000000000u128 * multiple); redeem_args.push(0u128); } for asset in &CURRENCY_LIST { - set_balance_fungibles(*asset, &tester, 200000000000u128); + set_balance_fungibles(*asset, &tester, 200000000000000u128); } - create_pools(assets, precisions)?; + create_stable_pools(assets, precisions)?; let pool_id = StableAsset::pool_count() - 1; StableAsset::mint(RawOrigin::Signed(tester.clone()).into(), pool_id, mint_args, 0u128)?; }: _(RawOrigin::Signed(tester), pool_id, 100000000u128, redeem_args) @@ -179,12 +144,12 @@ runtime_benchmarks! { let multiple: u128 = (i + 1).into(); assets.push(CURRENCY_LIST[i_idx]); precisions.push(1u128); - mint_args.push(10000000000u128 * multiple); + mint_args.push(10000000000000u128 * multiple); } for asset in &CURRENCY_LIST { - set_balance_fungibles(*asset, &tester, 200000000000u128); + set_balance_fungibles(*asset, &tester, 200000000000000u128); } - create_pools(assets, precisions)?; + create_stable_pools(assets, precisions)?; let pool_id = StableAsset::pool_count() - 1; StableAsset::mint(RawOrigin::Signed(tester.clone()).into(), pool_id, mint_args, 0u128)?; }: _(RawOrigin::Signed(tester), pool_id, 100000000u128, 0u32, 0u128, u) @@ -201,13 +166,13 @@ runtime_benchmarks! { let multiple: u128 = (i + 1).into(); assets.push(CURRENCY_LIST[i_idx]); precisions.push(1u128); - mint_args.push(10000000000u128 * multiple); + mint_args.push(10000000000000u128 * multiple); redeem_args.push(500000u128); } for asset in &CURRENCY_LIST { - set_balance_fungibles(*asset, &tester, 200000000000u128); + set_balance_fungibles(*asset, &tester, 200000000000000u128); } - create_pools(assets, precisions)?; + create_stable_pools(assets, precisions)?; let pool_id = StableAsset::pool_count() - 1; StableAsset::mint(RawOrigin::Signed(tester.clone()).into(), pool_id, mint_args, 0u128)?; }: _(RawOrigin::Signed(tester), pool_id, redeem_args, 1100000000000000000u128) diff --git a/runtime/mandala/src/benchmarking/prices.rs b/runtime/mandala/src/benchmarking/prices.rs index 208d3d6969..c8655b0205 100644 --- a/runtime/mandala/src/benchmarking/prices.rs +++ b/runtime/mandala/src/benchmarking/prices.rs @@ -16,15 +16,13 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use crate::{CurrencyId, GetStableCurrencyId, Origin, Prices, Runtime}; +use crate::{Origin, Prices, Runtime}; -use super::utils::{dollar, feed_price}; +use super::utils::{dollar, feed_price, STAKING}; use frame_system::RawOrigin; use orml_benchmarking::runtime_benchmarks; use sp_std::vec; -const STAKING: CurrencyId = GetStableCurrencyId::get(); - runtime_benchmarks! { { Runtime, module_prices } diff --git a/runtime/mandala/src/benchmarking/tokens.rs b/runtime/mandala/src/benchmarking/tokens.rs index 0f207aa02d..9ee38aee15 100644 --- a/runtime/mandala/src/benchmarking/tokens.rs +++ b/runtime/mandala/src/benchmarking/tokens.rs @@ -16,8 +16,8 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use super::utils::{dollar, lookup_of_account, set_balance as update_balance}; -use crate::{AccountId, Balance, CurrencyId, GetStableCurrencyId, Runtime, Tokens}; +use super::utils::{dollar, lookup_of_account, set_balance as update_balance, STABLECOIN}; +use crate::{AccountId, Balance, Runtime, Tokens}; use sp_std::prelude::*; @@ -29,8 +29,6 @@ use orml_traits::MultiCurrency; const SEED: u32 = 0; -const STABLECOIN: CurrencyId = GetStableCurrencyId::get(); - runtime_benchmarks! { { Runtime, orml_tokens } diff --git a/runtime/mandala/src/benchmarking/transaction_payment.rs b/runtime/mandala/src/benchmarking/transaction_payment.rs index a35a0c7c8c..4b10c72415 100644 --- a/runtime/mandala/src/benchmarking/transaction_payment.rs +++ b/runtime/mandala/src/benchmarking/transaction_payment.rs @@ -16,11 +16,10 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use super::utils::{dollar, set_balance}; +use super::utils::{dollar, inject_liquidity, set_balance, LIQUID, NATIVE, STABLECOIN, STAKING}; use crate::{ - AccountId, AssetRegistry, Balance, Currencies, CurrencyId, Dex, Event, GetLiquidCurrencyId, GetNativeCurrencyId, - GetStableCurrencyId, GetStakingCurrencyId, NativeTokenExistentialDeposit, Origin, Runtime, StableAsset, System, - TransactionPayment, TreasuryPalletId, + AccountId, AssetRegistry, Balance, Currencies, CurrencyId, Dex, Event, NativeTokenExistentialDeposit, Origin, + Runtime, StableAsset, System, TransactionPayment, TreasuryPalletId, }; use frame_benchmarking::{account, whitelisted_caller}; use frame_support::{assert_ok, traits::OnFinalize}; @@ -29,52 +28,21 @@ use module_support::{AggregatedSwapPath, DEXManager, Ratio, SwapLimit}; use orml_benchmarking::runtime_benchmarks; use orml_traits::MultiCurrency; use primitives::currency::AssetMetadata; -use sp_runtime::traits::{AccountIdConversion, One, UniqueSaturatedInto}; +use sp_runtime::traits::{AccountIdConversion, One}; use sp_std::prelude::*; const SEED: u32 = 0; -const STAKINGCOIN: CurrencyId = GetStakingCurrencyId::get(); -const STABLECOIN: CurrencyId = GetStableCurrencyId::get(); -const NATIVECOIN: CurrencyId = GetNativeCurrencyId::get(); -const LIQUIDCOIN: CurrencyId = GetLiquidCurrencyId::get(); - fn assert_has_event(generic_event: Event) { System::assert_has_event(generic_event.into()); } -fn inject_liquidity( - maker: AccountId, - currency_id_a: CurrencyId, - currency_id_b: CurrencyId, - max_amount_a: Balance, - max_amount_b: Balance, -) -> Result<(), &'static str> { - // set balance - set_balance(currency_id_a, &maker, max_amount_a.unique_saturated_into()); - set_balance(currency_id_b, &maker, max_amount_b.unique_saturated_into()); - - let _ = Dex::enable_trading_pair(RawOrigin::Root.into(), currency_id_a, currency_id_b); - - Dex::add_liquidity( - RawOrigin::Signed(maker.clone()).into(), - currency_id_a, - currency_id_b, - max_amount_a, - max_amount_b, - Default::default(), - false, - )?; - - Ok(()) -} - fn enable_fee_pool() -> (AccountId, Balance, Balance, Balance) { let funder: AccountId = account("funder", 0, SEED); let treasury_account: AccountId = TreasuryPalletId::get().into_account_truncating(); let sub_account: AccountId = ::PalletId::get().into_sub_account_truncating(STABLECOIN); - let native_ed: Balance = >::minimum_balance(NATIVECOIN); + let native_ed: Balance = >::minimum_balance(NATIVE); let stable_ed: Balance = >::minimum_balance(STABLECOIN); let pool_size: Balance = native_ed * 50; let swap_threshold: Balance = native_ed * 2; @@ -82,38 +50,39 @@ fn enable_fee_pool() -> (AccountId, Balance, Balance, Balance) { inject_liquidity( funder.clone(), STABLECOIN, - NATIVECOIN, + NATIVE, 1_000 * dollar(STABLECOIN), - 10_000 * dollar(NATIVECOIN), + 10_000 * dollar(NATIVE), + false, ) .unwrap(); assert!(Dex::get_swap_amount( - &vec![STABLECOIN, NATIVECOIN], + &vec![STABLECOIN, NATIVE], SwapLimit::ExactTarget(Balance::MAX, native_ed) ) .is_some()); assert_eq!( - Dex::get_liquidity_pool(STABLECOIN, NATIVECOIN), - (1_000 * dollar(STABLECOIN), 10_000 * dollar(NATIVECOIN)) + Dex::get_liquidity_pool(STABLECOIN, NATIVE), + (1_000 * dollar(STABLECOIN), 10_000 * dollar(NATIVE)) ); - set_balance(NATIVECOIN, &treasury_account, pool_size * 10); + set_balance(NATIVE, &treasury_account, pool_size * 10); set_balance(STABLECOIN, &treasury_account, stable_ed * 10); (sub_account, stable_ed, pool_size, swap_threshold) } fn enable_stable_asset() { let funder: AccountId = account("funder", 0, SEED); - set_balance(STAKINGCOIN, &funder, 1000 * dollar(STAKINGCOIN)); - set_balance(LIQUIDCOIN, &funder, 1000 * dollar(LIQUIDCOIN)); - set_balance(NATIVECOIN, &funder, 1000 * dollar(NATIVECOIN)); + set_balance(STAKING, &funder, 1000 * dollar(STAKING)); + set_balance(LIQUID, &funder, 1000 * dollar(LIQUID)); + set_balance(NATIVE, &funder, 1000 * dollar(NATIVE)); // create stable asset pool let pool_asset = CurrencyId::StableAssetPoolToken(0); assert_ok!(StableAsset::create_pool( Origin::root(), pool_asset, - vec![STAKINGCOIN, LIQUIDCOIN], + vec![STAKING, LIQUID], vec![1u128, 1u128], 10_000_000u128, 20_000_000u128, @@ -136,16 +105,17 @@ fn enable_stable_asset() { assert_ok!(StableAsset::mint( Origin::signed(funder.clone()), 0, - vec![100 * dollar(STAKINGCOIN), 100 * dollar(LIQUIDCOIN)], + vec![100 * dollar(STAKING), 100 * dollar(LIQUID)], 0u128 )); inject_liquidity( funder.clone(), - LIQUIDCOIN, - NATIVECOIN, - 100 * dollar(LIQUIDCOIN), - 100 * dollar(NATIVECOIN), + LIQUID, + NATIVE, + 100 * dollar(LIQUID), + 100 * dollar(NATIVE), + false, ) .unwrap(); } @@ -155,10 +125,10 @@ runtime_benchmarks! { set_alternative_fee_swap_path { let caller: AccountId = whitelisted_caller(); - set_balance(NATIVECOIN, &caller, NativeTokenExistentialDeposit::get()); - }: _(RawOrigin::Signed(caller.clone()), Some(vec![STABLECOIN, NATIVECOIN])) + set_balance(NATIVE, &caller, NativeTokenExistentialDeposit::get()); + }: _(RawOrigin::Signed(caller.clone()), Some(vec![STABLECOIN, NATIVE])) verify { - assert_eq!(TransactionPayment::alternative_fee_swap_path(&caller).unwrap().into_inner(), vec![STABLECOIN, NATIVECOIN]); + assert_eq!(TransactionPayment::alternative_fee_swap_path(&caller).unwrap().into_inner(), vec![STABLECOIN, NATIVE]); } enable_charge_fee_pool { @@ -169,7 +139,7 @@ runtime_benchmarks! { assert_eq!(TransactionPayment::pool_size(STABLECOIN), pool_size); assert!(TransactionPayment::token_exchange_rate(STABLECOIN).is_some()); assert_eq!(>::free_balance(STABLECOIN, &sub_account), stable_ed); - assert_eq!(>::free_balance(NATIVECOIN, &sub_account), pool_size); + assert_eq!(>::free_balance(NATIVE, &sub_account), pool_size); assert_has_event(module_transaction_payment::Event::ChargeFeePoolEnabled { sub_account, currency_id: STABLECOIN, @@ -182,11 +152,11 @@ runtime_benchmarks! { disable_charge_fee_pool { let treasury_account: AccountId = TreasuryPalletId::get().into_account_truncating(); let sub_account: AccountId = ::PalletId::get().into_sub_account_truncating(STABLECOIN); - let native_ed: Balance = >::minimum_balance(NATIVECOIN); + let native_ed: Balance = >::minimum_balance(NATIVE); let stable_ed: Balance = >::minimum_balance(STABLECOIN); let pool_size: Balance = native_ed * 50; - set_balance(NATIVECOIN, &sub_account, native_ed * 10); + set_balance(NATIVE, &sub_account, native_ed * 10); set_balance(STABLECOIN, &sub_account, stable_ed * 10); module_transaction_payment::TokenExchangeRate::::insert(STABLECOIN, Ratio::one()); @@ -205,14 +175,14 @@ runtime_benchmarks! { System::set_block_number(1); let funder: AccountId = account("funder", 0, SEED); - inject_liquidity(funder.clone(), STABLECOIN, NATIVECOIN, 100 * dollar(STABLECOIN), 100 * dollar(NATIVECOIN))?; + inject_liquidity(funder.clone(), STABLECOIN, NATIVE, 100 * dollar(STABLECOIN), 100 * dollar(NATIVE), false)?; let caller: AccountId = whitelisted_caller(); let call = Box::new(frame_system::Call::remark { remark: vec![] }.into()); set_balance(STABLECOIN, &caller, 100 * dollar(STABLECOIN)); - set_balance(NATIVECOIN, &caller, 100 * dollar(NATIVECOIN)); + set_balance(NATIVE, &caller, 100 * dollar(NATIVE)); - let fee_swap_path: Vec = vec![STABLECOIN, NATIVECOIN]; + let fee_swap_path: Vec = vec![STABLECOIN, NATIVE]; }: _(RawOrigin::Signed(caller), fee_swap_path.clone(), call) with_fee_currency { @@ -221,7 +191,7 @@ runtime_benchmarks! { let caller: AccountId = whitelisted_caller(); let call = Box::new(frame_system::Call::remark { remark: vec![] }.into()); set_balance(STABLECOIN, &caller, 100 * dollar(STABLECOIN)); - set_balance(NATIVECOIN, &caller, 100 * dollar(NATIVECOIN)); + set_balance(NATIVE, &caller, 100 * dollar(NATIVE)); let (sub_account, stable_ed, pool_size, swap_threshold) = enable_fee_pool(); TransactionPayment::enable_charge_fee_pool(RawOrigin::Root.into(), STABLECOIN, pool_size, swap_threshold).unwrap(); @@ -241,15 +211,15 @@ runtime_benchmarks! { let caller: AccountId = whitelisted_caller(); let call = Box::new(frame_system::Call::remark { remark: vec![] }.into()); - set_balance(STAKINGCOIN, &caller, 100 * dollar(STAKINGCOIN)); - set_balance(NATIVECOIN, &caller, 100 * dollar(NATIVECOIN)); + set_balance(STAKING, &caller, 100 * dollar(STAKING)); + set_balance(NATIVE, &caller, 100 * dollar(NATIVE)); enable_stable_asset(); - // Taiga(STAKINGCOIN, LIQUIDCOIN), Dex(LIQUIDCOIN, NATIVECOIN) + // Taiga(STAKING, LIQUID), Dex(LIQUID, NATIVE) let fee_aggregated_path = vec![ AggregatedSwapPath::::Taiga(0, 0, 1), - AggregatedSwapPath::::Dex(vec![LIQUIDCOIN, NATIVECOIN]), + AggregatedSwapPath::::Dex(vec![LIQUID, NATIVE]), ]; }: _(RawOrigin::Signed(caller.clone()), fee_aggregated_path, call) diff --git a/runtime/mandala/src/benchmarking/utils.rs b/runtime/mandala/src/benchmarking/utils.rs index 185234f1c0..29c24addbd 100644 --- a/runtime/mandala/src/benchmarking/utils.rs +++ b/runtime/mandala/src/benchmarking/utils.rs @@ -17,24 +17,33 @@ // along with this program. If not, see . use crate::{ - AcalaOracle, AccountId, AssetRegistry, Balance, Currencies, CurrencyId, ExistentialDeposits, GetNativeCurrencyId, - MinimumCount, NativeTokenExistentialDeposit, OperatorMembershipAcala, Origin, Price, Runtime, + AcalaOracle, AccountId, AggregatedDex, AssetRegistry, Balance, Currencies, CurrencyId, Dex, ExistentialDeposits, + GetLiquidCurrencyId, GetNativeCurrencyId, GetStableCurrencyId, GetStakingCurrencyId, MinimumCount, + NativeTokenExistentialDeposit, OperatorMembershipAcala, Origin, Price, Runtime, StableAsset, }; use frame_benchmarking::account; use frame_support::traits::tokens::fungibles; use frame_support::{assert_ok, traits::Contains}; use frame_system::RawOrigin; -use module_support::Erc20InfoMapping; +use module_support::{AggregatedSwapPath, Erc20InfoMapping}; use orml_traits::{GetByKey, MultiCurrencyExtended}; use primitives::currency::AssetMetadata; -use runtime_common::TokenInfo; +use runtime_common::{TokenInfo, LCDOT}; use sp_runtime::{ - traits::{SaturatedConversion, StaticLookup}, + traits::{SaturatedConversion, StaticLookup, UniqueSaturatedInto}, DispatchResult, }; use sp_std::prelude::*; +pub type SwapPath = AggregatedSwapPath; + +pub const NATIVE: CurrencyId = GetNativeCurrencyId::get(); +pub const STABLECOIN: CurrencyId = GetStableCurrencyId::get(); +pub const LIQUID: CurrencyId = GetLiquidCurrencyId::get(); +pub const STAKING: CurrencyId = GetStakingCurrencyId::get(); +const SEED: u32 = 0; + pub fn lookup_of_account(who: AccountId) -> <::Lookup as StaticLookup>::Source { ::Lookup::unlookup(who) } @@ -98,6 +107,148 @@ pub fn dollar(currency_id: CurrencyId) -> Balance { } } +pub fn inject_liquidity( + maker: AccountId, + currency_id_a: CurrencyId, + currency_id_b: CurrencyId, + max_amount_a: Balance, + max_amount_b: Balance, + deposit: bool, +) -> Result<(), &'static str> { + // set balance + >::update_balance( + currency_id_a, + &maker, + max_amount_a.unique_saturated_into(), + )?; + >::update_balance( + currency_id_b, + &maker, + max_amount_b.unique_saturated_into(), + )?; + + let _ = Dex::enable_trading_pair(RawOrigin::Root.into(), currency_id_a, currency_id_b); + + Dex::add_liquidity( + RawOrigin::Signed(maker.clone()).into(), + currency_id_a, + currency_id_b, + max_amount_a, + max_amount_b, + Default::default(), + deposit, + )?; + Ok(()) +} + +pub fn register_stable_asset() -> DispatchResult { + let asset_metadata = AssetMetadata { + name: b"Token Name".to_vec(), + symbol: b"TN".to_vec(), + decimals: 12, + minimal_balance: 1_000_000_000, + }; + AssetRegistry::register_stable_asset(RawOrigin::Root.into(), Box::new(asset_metadata.clone())) +} + +pub fn create_stable_pools(assets: Vec, precisions: Vec) -> DispatchResult { + let pool_asset = CurrencyId::StableAssetPoolToken(0); + let mint_fee = 10000000u128; + let swap_fee = 20000000u128; + let redeem_fee = 50000000u128; + let intial_a = 10000u128; + let fee_recipient: AccountId = account("fee", 0, SEED); + let yield_recipient: AccountId = account("yield", 1, SEED); + + register_stable_asset()?; + StableAsset::create_pool( + RawOrigin::Root.into(), + pool_asset, + assets, + precisions, + mint_fee, + swap_fee, + redeem_fee, + intial_a, + fee_recipient, + yield_recipient, + 1000000000000000000u128, + )?; + + Ok(()) +} + +/// Initializes all pools used in AggregatedDex `Swap` for trading to stablecoin +pub fn initialize_swap_pools(maker: AccountId) -> Result<(), &'static str> { + // Inject liquidity into all possible `AlternativeSwapPathJointList` + inject_liquidity( + maker.clone(), + LIQUID, + STABLECOIN, + 10_000 * dollar(LIQUID), + 10_000 * dollar(STABLECOIN), + false, + )?; + inject_liquidity( + maker.clone(), + STAKING, + LIQUID, + 10_000 * dollar(STAKING), + 10_000 * dollar(LIQUID), + false, + )?; + + // purposly inject too little liquidity to have failed path, still reads dexs to check for viable + // swap paths + inject_liquidity( + maker.clone(), + STAKING, + STABLECOIN, + 10 * dollar(STAKING), + 10 * dollar(STABLECOIN), + false, + )?; + inject_liquidity( + maker.clone(), + LCDOT, + STABLECOIN, + dollar(LCDOT), + dollar(STABLECOIN), + false, + )?; + inject_liquidity(maker.clone(), LCDOT, STAKING, dollar(LCDOT), dollar(STAKING), false)?; + + // Add and initialize stable pools, is manually added with changes to runtime + let assets_1 = vec![STAKING, LIQUID]; + create_stable_pools(assets_1.clone(), vec![1, 1])?; + for asset in assets_1 { + >::update_balance(asset, &maker, 1_000_000_000_000_000)?; + } + StableAsset::mint( + RawOrigin::Signed(maker.clone()).into(), + 0, + vec![1_000_000_000_000, 1_000_000_000_000], + 0, + )?; + + // Adds `AggregatedSwapPaths`, also mirrors runtimes state + AggregatedDex::update_aggregated_swap_paths( + RawOrigin::Root.into(), + vec![ + ( + (STAKING, STABLECOIN), + Some(vec![SwapPath::Taiga(0, 0, 1), SwapPath::Dex(vec![LIQUID, STABLECOIN])]), + ), + ( + (LIQUID, STABLECOIN), + Some(vec![SwapPath::Taiga(0, 1, 0), SwapPath::Dex(vec![STAKING, STABLECOIN])]), + ), + ], + )?; + + Ok(()) +} + #[cfg(test)] pub mod tests { pub fn new_test_ext() -> sp_io::TestExternalities { diff --git a/runtime/mandala/src/benchmarking/vesting.rs b/runtime/mandala/src/benchmarking/vesting.rs index 26a7257301..0b693818c2 100644 --- a/runtime/mandala/src/benchmarking/vesting.rs +++ b/runtime/mandala/src/benchmarking/vesting.rs @@ -18,11 +18,9 @@ use super::{ get_vesting_account, - utils::{lookup_of_account, set_balance}, -}; -use crate::{ - dollar, AccountId, Balance, BlockNumber, Currencies, CurrencyId, GetNativeCurrencyId, Runtime, System, Vesting, + utils::{lookup_of_account, set_balance, NATIVE}, }; +use crate::{dollar, AccountId, Balance, BlockNumber, Currencies, Runtime, System, Vesting}; use sp_std::prelude::*; @@ -38,8 +36,6 @@ pub type Schedule = VestingSchedule; const SEED: u32 = 0; -const NATIVE: CurrencyId = GetNativeCurrencyId::get(); - runtime_benchmarks! { { Runtime, orml_vesting }