diff --git a/frame/dutch-auction/src/benchmarking.rs b/frame/dutch-auction/src/benchmarking.rs index f52943453ef..b574c5c357c 100644 --- a/frame/dutch-auction/src/benchmarking.rs +++ b/frame/dutch-auction/src/benchmarking.rs @@ -1,11 +1,14 @@ use super::*; -use crate::{mock::currency::CurrencyId, Pallet as DutchAuction}; -use codec::{Decode, Encode}; +use crate::Pallet as DutchAuction; +use codec::Decode; use composable_traits::defi::{CurrencyPair, DeFiComposableConfig, Ratio, Sell, Take}; use frame_benchmarking::{benchmarks, impl_benchmark_test_suite, whitelisted_caller}; -use frame_support::traits::{fungibles::Mutate, Hooks}; +use frame_support::traits::{fungibles::Mutate, Currency, Get, Hooks}; use frame_system::{pallet_prelude::BlockNumberFor, RawOrigin}; -use sp_runtime::FixedPointNumber; +use sp_runtime::{ + traits::{AccountIdConversion, Saturating}, + FixedPointNumber, +}; use sp_std::prelude::*; // meaningless sell of 1 to 1 @@ -36,6 +39,19 @@ where ) } +fn mint_native_tokens(account_id: &T::AccountId) +where + T: Config, + ::MultiCurrency: + Mutate, +{ + let treasury = &T::PalletId::get().into_account(); + let native_token_amount = ::NativeCurrency::minimum_balance() + .saturating_mul(1_000_000_000u32.into()); + ::NativeCurrency::make_free_balance_be(&treasury, native_token_amount); + ::NativeCurrency::make_free_balance_be(account_id, native_token_amount); +} + benchmarks! { where_clause { where @@ -47,6 +63,7 @@ benchmarks! { let account_id : T::AccountId = whitelisted_caller(); let caller = RawOrigin::Signed(account_id.clone()); let amount: T::Balance = 1_000_000_000_000_u64.into(); + mint_native_tokens::(&account_id); ::MultiCurrency::mint_into(sell.pair.base, &account_id, amount).unwrap(); }: _( caller, @@ -54,24 +71,17 @@ benchmarks! { <_>::default() ) take { - let x in 1..2^16; let sell = sell_identity::(); let account_id : T::AccountId = whitelisted_caller(); let caller = RawOrigin::Signed(account_id.clone()); let amount: T::Balance = 1_000_000_000_000_u64.into(); - - let encoded = CurrencyId::PICA.encode(); - let native_asset_id = T::MayBeAssetId::decode(&mut &encoded[..]).unwrap(); - ::MultiCurrency::mint_into(native_asset_id, &account_id, amount).unwrap(); - + mint_native_tokens::(&account_id); ::MultiCurrency::mint_into(sell.pair.base, &account_id, amount).unwrap(); ::MultiCurrency::mint_into(sell.pair.quote, &account_id, amount).unwrap(); DutchAuction::::ask(caller.clone().into(), sell, <_>::default()).unwrap(); let order_id = OrdersIndex::::get(); let take_order = take_identity::(); - for i in 0..x { - DutchAuction::::take(caller.clone().into(), order_id, take_order.clone()).unwrap(); - } + DutchAuction::::take(caller.clone().into(), order_id, take_order.clone()).unwrap(); }: _( caller, order_id, @@ -82,12 +92,7 @@ benchmarks! { let account_id : T::AccountId = whitelisted_caller(); let caller = RawOrigin::Signed(account_id.clone()); let amount: T::Balance = 1_000_000_000_000_u64.into(); - - let encoded = CurrencyId::PICA.encode(); - let native_asset_id = T::MayBeAssetId::decode(&mut &encoded[..]).unwrap(); - ::MultiCurrency::mint_into(native_asset_id, &account_id, amount).unwrap(); - - + mint_native_tokens::(&account_id); ::MultiCurrency::mint_into(sell.pair.base, &account_id, amount).unwrap(); DutchAuction::::ask(caller.clone().into(), sell, <_>::default()).unwrap(); let order_id = OrdersIndex::::get(); @@ -100,12 +105,7 @@ benchmarks! { let account_id : T::AccountId = whitelisted_caller(); let caller = RawOrigin::Signed(account_id.clone()); let amount: T::Balance = 1_000_000_000_000_u64.into(); - - let encoded = CurrencyId::PICA.encode(); - let native_asset_id = T::MayBeAssetId::decode(&mut &encoded[..]).unwrap(); - ::MultiCurrency::mint_into(native_asset_id, &account_id, amount).unwrap(); - - + mint_native_tokens::(&account_id); ::MultiCurrency::mint_into(sell.pair.base, &account_id, amount).unwrap(); ::MultiCurrency::mint_into(sell.pair.quote, &account_id, amount).unwrap(); DutchAuction::::ask(caller.clone().into(), sell, <_>::default()).unwrap(); @@ -115,7 +115,6 @@ benchmarks! { } : { as Hooks>>::on_finalize(T::BlockNumber::default()) } - } impl_benchmark_test_suite!( diff --git a/frame/dutch-auction/src/lib.rs b/frame/dutch-auction/src/lib.rs index 3a122905955..c0656ed6f18 100644 --- a/frame/dutch-auction/src/lib.rs +++ b/frame/dutch-auction/src/lib.rs @@ -79,6 +79,8 @@ pub mod pallet { math::WrappingNext, time::{TimeReleaseFunction, Timestamp}, }; + #[cfg(feature = "runtime-benchmarks")] + use frame_support::traits::Currency; use frame_support::{ pallet_prelude::*, traits::{tokens::fungible::Transfer as NativeTransfer, IsType, UnixTime}, @@ -99,6 +101,7 @@ pub mod pallet { #[pallet::config] #[pallet::disable_frame_system_supertrait_check] + #[cfg(not(feature = "runtime-benchmarks"))] pub trait Config: DeFiComposableConfig + frame_system::Config { type Event: From> + IsType<::Event>; type UnixTime: UnixTime; @@ -119,6 +122,27 @@ pub mod pallet { /// Convert a weight value into a deductible fee based on the currency type. type WeightToFee: WeightToFeePolynomial; } + #[cfg(feature = "runtime-benchmarks")] + pub trait Config: DeFiComposableConfig + frame_system::Config { + type Event: From> + IsType<::Event>; + type UnixTime: UnixTime; + type OrderId: OrderIdLike + WrappingNext + Zero; + type MultiCurrency: MultiCurrency< + Self::AccountId, + CurrencyId = Self::MayBeAssetId, + Balance = ::Balance, + > + MultiReservableCurrency< + Self::AccountId, + CurrencyId = Self::MayBeAssetId, + Balance = ::Balance, + >; + type WeightInfo: WeightInfo; + type PalletId: Get; + type NativeCurrency: NativeTransfer + + Currency; + /// Convert a weight value into a deductible fee based on the currency type. + type WeightToFee: WeightToFeePolynomial; + } #[derive(Encode, Decode, Default, TypeInfo, Clone, Debug, PartialEq)] pub struct SellOrder { @@ -225,7 +249,7 @@ pub mod pallet { } /// adds take to list, does not execute take immediately - #[pallet::weight(T::WeightInfo::take(42))] // FIXME: need to update benchmark and weight for this extrinsic + #[pallet::weight(T::WeightInfo::take())] pub fn take( origin: OriginFor, order_id: T::OrderId, @@ -249,7 +273,12 @@ pub mod pallet { // pollute account system let treasury = &T::PalletId::get().into_account(); T::MultiCurrency::unreserve(order.order.pair.base, &who, order.order.take.amount); - T::NativeCurrency::transfer(treasury, &order.from_to, order.context.deposit, true)?; + >::transfer( + treasury, + &order.from_to, + order.context.deposit, + true, + )?; >::remove(order_id); Self::deposit_event(Event::OrderRemoved { order_id }); @@ -273,7 +302,9 @@ pub mod pallet { }); let treasury = &T::PalletId::get().into_account(); let deposit = T::WeightToFee::calc(&T::WeightInfo::liquidate()); - T::NativeCurrency::transfer(from_to, treasury, deposit, true)?; + >::transfer( + from_to, treasury, deposit, true, + )?; let now = T::UnixTime::now().as_secs(); let order = SellOf:: { from_to: from_to.clone(), diff --git a/frame/dutch-auction/src/weights.rs b/frame/dutch-auction/src/weights.rs index b5a3a301f51..d033c7052ba 100644 --- a/frame/dutch-auction/src/weights.rs +++ b/frame/dutch-auction/src/weights.rs @@ -1,10 +1,11 @@ #![allow(unused_parens, unused_imports, clippy::unnecessary_cast)] + use frame_support::{pallet_prelude::Weight, traits::Get, weights::constants::RocksDbWeight}; use sp_std::marker::PhantomData; pub trait WeightInfo { fn ask() -> Weight; - fn take(_x: u32) -> Weight; + fn take() -> Weight; fn liquidate() -> Weight; fn known_overhead_for_on_finalize() -> Weight; } @@ -13,35 +14,37 @@ pub trait WeightInfo { pub struct SubstrateWeight(PhantomData); impl WeightInfo for SubstrateWeight { // Storage: DutchAuction OrdersIndex (r:1 w:1) + // Storage: System Account (r:1 w:1) // Storage: Timestamp Now (r:1 w:0) // Storage: Tokens Accounts (r:1 w:1) // Storage: DutchAuction SellOrders (r:0 w:1) fn ask() -> Weight { - (164_004_000 as Weight) - .saturating_add(T::DbWeight::get().reads(3 as Weight)) - .saturating_add(T::DbWeight::get().writes(3 as Weight)) + (185_212_000 as Weight) + .saturating_add(T::DbWeight::get().reads(4 as Weight)) + .saturating_add(T::DbWeight::get().writes(4 as Weight)) } // Storage: DutchAuction SellOrders (r:1 w:0) // Storage: Timestamp Now (r:1 w:0) // Storage: Tokens Accounts (r:1 w:1) // Storage: DutchAuction Takes (r:1 w:1) - fn take(_x: u32) -> Weight { - (158_860_000 as Weight) + fn take() -> Weight { + (140_074_000 as Weight) .saturating_add(T::DbWeight::get().reads(4 as Weight)) .saturating_add(T::DbWeight::get().writes(2 as Weight)) } // Storage: DutchAuction SellOrders (r:1 w:1) // Storage: Tokens Accounts (r:1 w:1) + // Storage: System Account (r:1 w:1) fn liquidate() -> Weight { - (105_812_000 as Weight) - .saturating_add(T::DbWeight::get().reads(2 as Weight)) - .saturating_add(T::DbWeight::get().writes(2 as Weight)) + (150_867_000 as Weight) + .saturating_add(T::DbWeight::get().reads(3 as Weight)) + .saturating_add(T::DbWeight::get().writes(3 as Weight)) } // Storage: DutchAuction Takes (r:2 w:1) // Storage: DutchAuction SellOrders (r:1 w:1) // Storage: Tokens Accounts (r:2 w:2) fn known_overhead_for_on_finalize() -> Weight { - (287_359_000 as Weight) + (195_022_000 as Weight) .saturating_add(T::DbWeight::get().reads(5 as Weight)) .saturating_add(T::DbWeight::get().writes(4 as Weight)) } @@ -49,35 +52,37 @@ impl WeightInfo for SubstrateWeight { impl WeightInfo for () { // Storage: DutchAuction OrdersIndex (r:1 w:1) + // Storage: System Account (r:1 w:1) // Storage: Timestamp Now (r:1 w:0) // Storage: Tokens Accounts (r:1 w:1) // Storage: DutchAuction SellOrders (r:0 w:1) fn ask() -> Weight { - (164_004_000 as Weight) - .saturating_add(RocksDbWeight::get().reads(3 as Weight)) - .saturating_add(RocksDbWeight::get().writes(3 as Weight)) + (185_212_000 as Weight) + .saturating_add(RocksDbWeight::get().reads(4 as Weight)) + .saturating_add(RocksDbWeight::get().writes(4 as Weight)) } // Storage: DutchAuction SellOrders (r:1 w:0) // Storage: Timestamp Now (r:1 w:0) // Storage: Tokens Accounts (r:1 w:1) // Storage: DutchAuction Takes (r:1 w:1) - fn take(_x: u32) -> Weight { - (158_860_000 as Weight) + fn take() -> Weight { + (140_074_000 as Weight) .saturating_add(RocksDbWeight::get().reads(4 as Weight)) .saturating_add(RocksDbWeight::get().writes(2 as Weight)) } // Storage: DutchAuction SellOrders (r:1 w:1) // Storage: Tokens Accounts (r:1 w:1) + // Storage: System Account (r:1 w:1) fn liquidate() -> Weight { - (105_812_000 as Weight) - .saturating_add(RocksDbWeight::get().reads(2 as Weight)) - .saturating_add(RocksDbWeight::get().writes(2 as Weight)) + (150_867_000 as Weight) + .saturating_add(RocksDbWeight::get().reads(3 as Weight)) + .saturating_add(RocksDbWeight::get().writes(3 as Weight)) } // Storage: DutchAuction Takes (r:2 w:1) // Storage: DutchAuction SellOrders (r:1 w:1) // Storage: Tokens Accounts (r:2 w:2) fn known_overhead_for_on_finalize() -> Weight { - (287_359_000 as Weight) + (195_022_000 as Weight) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) .saturating_add(RocksDbWeight::get().writes(4 as Weight)) } diff --git a/frame/lending/src/mocks/mod.rs b/frame/lending/src/mocks/mod.rs index 26baa6a9c90..2cc01102b07 100644 --- a/frame/lending/src/mocks/mod.rs +++ b/frame/lending/src/mocks/mod.rs @@ -312,7 +312,7 @@ impl pallet_dutch_auction::weights::WeightInfo for DutchAuctionsMocks { 0 } - fn take(_x: u32) -> frame_support::dispatch::Weight { + fn take() -> frame_support::dispatch::Weight { 0 } diff --git a/runtime/dali/src/weights/dutch_auction.rs b/runtime/dali/src/weights/dutch_auction.rs index 49055412d43..54a377306ca 100644 --- a/runtime/dali/src/weights/dutch_auction.rs +++ b/runtime/dali/src/weights/dutch_auction.rs @@ -1,7 +1,8 @@ + //! Autogenerated weights for `dutch_auction` //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2022-01-11, STEPS: `5`, REPEAT: 2, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2022-01-18, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dali-dev"), DB CACHE: 128 // Executed Command: @@ -12,8 +13,8 @@ // --wasm-execution=compiled // --pallet=dutch_auction // --extrinsic=* -// --steps=5 -// --repeat=2 +// --steps=50 +// --repeat=20 // --raw // --output=./runtime/dali/src/weights @@ -28,35 +29,37 @@ use frame_support::{traits::Get, weights::Weight}; pub struct WeightInfo(PhantomData); impl dutch_auction::WeightInfo for WeightInfo { // Storage: DutchAuction OrdersIndex (r:1 w:1) + // Storage: System Account (r:1 w:1) // Storage: Timestamp Now (r:1 w:0) // Storage: Tokens Accounts (r:1 w:1) // Storage: DutchAuction SellOrders (r:0 w:1) fn ask() -> Weight { - (164_004_000 as Weight) - .saturating_add(T::DbWeight::get().reads(3 as Weight)) - .saturating_add(T::DbWeight::get().writes(3 as Weight)) + (185_212_000 as Weight) + .saturating_add(T::DbWeight::get().reads(4 as Weight)) + .saturating_add(T::DbWeight::get().writes(4 as Weight)) } // Storage: DutchAuction SellOrders (r:1 w:0) // Storage: Timestamp Now (r:1 w:0) // Storage: Tokens Accounts (r:1 w:1) // Storage: DutchAuction Takes (r:1 w:1) - fn take(_x: u32, ) -> Weight { - (158_860_000 as Weight) + fn take() -> Weight { + (140_074_000 as Weight) .saturating_add(T::DbWeight::get().reads(4 as Weight)) .saturating_add(T::DbWeight::get().writes(2 as Weight)) } // Storage: DutchAuction SellOrders (r:1 w:1) // Storage: Tokens Accounts (r:1 w:1) + // Storage: System Account (r:1 w:1) fn liquidate() -> Weight { - (105_812_000 as Weight) - .saturating_add(T::DbWeight::get().reads(2 as Weight)) - .saturating_add(T::DbWeight::get().writes(2 as Weight)) + (150_867_000 as Weight) + .saturating_add(T::DbWeight::get().reads(3 as Weight)) + .saturating_add(T::DbWeight::get().writes(3 as Weight)) } // Storage: DutchAuction Takes (r:2 w:1) // Storage: DutchAuction SellOrders (r:1 w:1) // Storage: Tokens Accounts (r:2 w:2) fn known_overhead_for_on_finalize() -> Weight { - (287_359_000 as Weight) + (195_022_000 as Weight) .saturating_add(T::DbWeight::get().reads(5 as Weight)) .saturating_add(T::DbWeight::get().writes(4 as Weight)) }