diff --git a/pallets/asset-index/src/benchmarking.rs b/pallets/asset-index/src/benchmarking.rs index 75a0089f84..6c569aba0e 100644 --- a/pallets/asset-index/src/benchmarking.rs +++ b/pallets/asset-index/src/benchmarking.rs @@ -12,7 +12,7 @@ use frame_support::{ }; use frame_system::RawOrigin; use orml_traits::MultiCurrency; -use pallet_price_feed::PriceFeed; +use pallet_price_feed::{PriceFeed, PriceFeedBenchmarks}; use primitives::{traits::NavProvider, AssetAvailability}; use xcm::v0::MultiLocation; @@ -96,24 +96,31 @@ benchmarks! { } deposit { - // ASSET_A_ID - let asset_id = 1_u32.into(); + let asset_id = 2_u32.into(); let origin = T::AdminOrigin::successful_origin(); let depositor = whitelisted_account::("depositor", 0); let admin_deposit = 5u32.into(); - assert_ok!(AssetIndex::::add_asset(origin, asset_id, 100u32.into(),MultiLocation::Null,admin_deposit - )); let units = 1_000u32.into(); + + assert_ok!(AssetIndex::::add_asset( + origin, + asset_id, + 100u32.into(), + MultiLocation::Null, + admin_deposit, + )); + + T::PriceFeedBenchmarks::create_feed(Default::default(), asset_id).unwrap(); assert_ok!(T::Currency::deposit(asset_id, &depositor, units)); - let nav = AssetIndex::::nav().unwrap(); }: _( RawOrigin::Signed(depositor.clone()), asset_id, units ) verify { - let deposit_value = T::PriceFeed::get_price(asset_id).unwrap().checked_mul_int(units.into()).unwrap(); - let received = nav.reciprocal().unwrap().saturating_mul_int(deposit_value); - assert_eq!(AssetIndex::::index_token_balance(&depositor).into(), received); + let nav = AssetIndex::::nav().unwrap(); + let deposit_value = T::PriceFeed::get_price(asset_id).unwrap().checked_mul_int(units.into()).unwrap(); + let received = nav.reciprocal().unwrap().saturating_mul_int(deposit_value).saturating_add(1u128); + assert_eq!(AssetIndex::::index_token_balance(&depositor).into(), received); } remove_asset { diff --git a/pallets/asset-index/src/lib.rs b/pallets/asset-index/src/lib.rs index a8a67b0906..998f5393e6 100644 --- a/pallets/asset-index/src/lib.rs +++ b/pallets/asset-index/src/lib.rs @@ -44,6 +44,8 @@ pub mod pallet { use sp_core::U256; use xcm::v0::MultiLocation; + #[cfg(feature = "runtime-benchmarks")] + use pallet_price_feed::PriceFeedBenchmarks; use pallet_price_feed::{AssetPricePair, Price, PriceFeed}; use primitives::{ fee::{BaseFee, FeeRate}, @@ -106,6 +108,10 @@ pub mod pallet { /// The types that provides the necessary asset price pairs type PriceFeed: PriceFeed; + #[cfg(feature = "runtime-benchmarks")] + /// The type that provides benchmark features of pallet_price_feed + type PriceFeedBenchmarks: PriceFeedBenchmarks; + /// The type registry that stores all NAV for non liquid assets type SaftRegistry: SaftRegistry; diff --git a/pallets/asset-index/src/mock.rs b/pallets/asset-index/src/mock.rs index 6d5769f13a..dc82b25852 100644 --- a/pallets/asset-index/src/mock.rs +++ b/pallets/asset-index/src/mock.rs @@ -6,13 +6,15 @@ use crate as pallet_asset_index; use frame_support::{ - ord_parameter_types, parameter_types, + ord_parameter_types, + pallet_prelude::DispatchResultWithPostInfo, + parameter_types, traits::{GenesisBuild, LockIdentifier, StorageMapShim}, PalletId, }; use frame_system as system; use orml_traits::parameter_type_with_key; -use pallet_price_feed::PriceFeed; +use pallet_price_feed::{PriceFeed, PriceFeedBenchmarks}; use primitives::{fee::FeeRate, traits::RemoteAssetManager, AssetPricePair, Price}; use sp_core::H256; use sp_std::cell::RefCell; @@ -167,6 +169,8 @@ impl pallet_asset_index::Config for Test { type SelfAssetId = PINTAssetId; type Currency = Currency; type PriceFeed = MockPriceFeed; + #[cfg(feature = "runtime-benchmarks")] + type PriceFeedBenchmarks = MockPriceFeed; type SaftRegistry = SaftRegistry; type BaseWithdrawalFee = BaseWithdrawalFee; type TreasuryPalletId = TreasuryPalletId; @@ -213,6 +217,13 @@ impl MockPriceFeed { } } +#[cfg(feature = "runtime-benchmarks")] +impl PriceFeedBenchmarks for MockPriceFeed { + fn create_feed(_caller: AccountId, _asset_id: AssetId) -> DispatchResultWithPostInfo { + Ok(().into()) + } +} + impl PriceFeed for MockPriceFeed { // mock price supposed to return the price pair with the same `quote` price, like USD fn get_price(asset: AssetId) -> Result { diff --git a/pallets/price-feed/src/lib.rs b/pallets/price-feed/src/lib.rs index c94fb99299..21085fc386 100644 --- a/pallets/price-feed/src/lib.rs +++ b/pallets/price-feed/src/lib.rs @@ -39,9 +39,14 @@ mod types; // this is requires as the #[pallet::event] proc macro generates code that violates this lint #[allow(clippy::unused_unit)] pub mod pallet { - pub use crate::{traits::PriceFeed, types::TimestampedValue}; + #[cfg(feature = "runtime-benchmarks")] + pub use crate::traits::PriceFeedBenchmarks; + #[cfg(feature = "runtime-benchmarks")] + use frame_benchmarking::Zero; #[cfg(feature = "std")] use frame_support::traits::GenesisBuild; + + pub use crate::{traits::PriceFeed, types::TimestampedValue}; use frame_support::{ pallet_prelude::*, sp_runtime::{traits::CheckedDiv, FixedPointNumber, FixedPointOperand}, @@ -260,6 +265,45 @@ pub mod pallet { } } + #[cfg(feature = "runtime-benchmarks")] + impl PriceFeedBenchmarks for Pallet { + fn create_feed( + caller: ::AccountId, + asset_id: T::AssetId, + ) -> DispatchResultWithPostInfo { + use frame_benchmarking::vec; + + pallet_chainlink_feed::Pallet::::set_feed_creator( + >::Signed(pallet_chainlink_feed::Pallet::::pallet_admin()).into(), + caller.clone(), + )?; + + pallet_chainlink_feed::Pallet::::create_feed( + >::Signed(caller.clone()).into(), + 100u32.into(), + Zero::zero(), + (1u8.into(), 100u8.into()), + 1u8.into(), + 8u8, + vec![1; T::StringLimit::get() as usize], + Zero::zero(), + vec![(caller.clone(), caller.clone())], + None, + None, + )?; + + let feed_id = >::get() - 1.into(); + AssetFeeds::::insert(&asset_id, feed_id); + pallet_chainlink_feed::Pallet::::submit( + >::Signed(caller.clone()).into(), + feed_id, + 1_u32.into(), + 42.into(), + )?; + Ok(().into()) + } + } + impl pallet_chainlink_feed::traits::OnAnswerHandler for Pallet { fn on_answer(feed_id: FeedIdFor, _: RoundData>) { LatestAnswerTimestamp::::insert(feed_id, T::Time::now()); diff --git a/pallets/price-feed/src/traits.rs b/pallets/price-feed/src/traits.rs index b9740aaf51..b64533bd67 100644 --- a/pallets/price-feed/src/traits.rs +++ b/pallets/price-feed/src/traits.rs @@ -1,7 +1,7 @@ // Copyright 2021 ChainSafe Systems // SPDX-License-Identifier: LGPL-3.0-only -use frame_support::dispatch::DispatchError; +use frame_support::dispatch::{DispatchError, DispatchResultWithPostInfo}; use primitives::{AssetPricePair, Price}; /// An interface to access price data @@ -15,3 +15,8 @@ pub trait PriceFeed { /// `base/quote` fn get_relative_price_pair(base: AssetId, quote: AssetId) -> Result, DispatchError>; } + +#[cfg(feature = "runtime-benchmarks")] +pub trait PriceFeedBenchmarks { + fn create_feed(caller: AccountId, asset_id: AssetId) -> DispatchResultWithPostInfo; +} diff --git a/pallets/remote-asset-manager/src/mock.rs b/pallets/remote-asset-manager/src/mock.rs index 64d29b76d1..c8a7d7d849 100644 --- a/pallets/remote-asset-manager/src/mock.rs +++ b/pallets/remote-asset-manager/src/mock.rs @@ -52,7 +52,6 @@ pub use xcm_test_support::{relay, types::*, Relay}; pub const ALICE: AccountId = AccountId::new([0u8; 32]); pub const ADMIN_ACCOUNT: AccountId = AccountId::new([1u8; 32]); -// pub const EMPTY_ACCOUNT: AccountId = AccountId::new([3u8; 32]); pub const INITIAL_BALANCE: Balance = 10_000; pub const PARA_ID: u32 = 1u32; pub const STATEMINT_PARA_ID: u32 = 200u32; @@ -187,10 +186,10 @@ pub mod para { *, }; use codec::Decode; - use frame_support::dispatch::DispatchError; + use frame_support::dispatch::{DispatchError, DispatchResultWithPostInfo}; use orml_currencies::BasicCurrencyAdapter; use orml_xcm_support::{IsNativeConcrete, MultiCurrencyAdapter}; - use pallet_price_feed::{AssetPricePair, Price, PriceFeed}; + use pallet_price_feed::{AssetPricePair, Price, PriceFeed, PriceFeedBenchmarks}; use sp_runtime::traits::Convert; use xcm::v0::MultiAsset; @@ -446,6 +445,7 @@ pub mod para { type RemoteAssetManager = RemoteAssetManager; type Currency = Currency; type PriceFeed = MockPriceFeed; + type PriceFeedBenchmarks = MockPriceFeed; type SaftRegistry = SaftRegistry; type TreasuryPalletId = TreasuryPalletId; type StringLimit = StringLimit; @@ -471,6 +471,13 @@ pub mod para { } } + #[cfg(feature = "runtime-benchmarks")] + impl PriceFeedBenchmarks for MockPriceFeed { + fn create_feed(_caller: AccountId, _asset_id: AssetId) -> DispatchResultWithPostInfo { + Ok(().into()) + } + } + impl pallet_remote_asset_manager::Config for Runtime { type Balance = Balance; type AssetId = AssetId; diff --git a/pallets/saft-registry/src/mock.rs b/pallets/saft-registry/src/mock.rs index 99dbace6ce..d9d940baea 100644 --- a/pallets/saft-registry/src/mock.rs +++ b/pallets/saft-registry/src/mock.rs @@ -7,13 +7,15 @@ use crate as pallet_saft_registry; use core::cell::RefCell; use frame_support::{ - assert_ok, ord_parameter_types, parameter_types, + assert_ok, ord_parameter_types, + pallet_prelude::DispatchResultWithPostInfo, + parameter_types, traits::{LockIdentifier, StorageMapShim}, PalletId, }; use frame_system as system; use orml_traits::parameter_type_with_key; -use pallet_price_feed::{AssetPricePair, Price, PriceFeed}; +use pallet_price_feed::{AssetPricePair, Price, PriceFeed, PriceFeedBenchmarks}; use primitives::traits::RemoteAssetManager; use xcm::v0::MultiLocation; @@ -132,6 +134,8 @@ impl pallet_asset_index::Config for Test { type SelfAssetId = PINTAssetId; type Currency = Currency; type PriceFeed = MockPriceFeed; + #[cfg(feature = "runtime-benchmarks")] + type PriceFeedBenchmarks = MockPriceFeed; type SaftRegistry = SaftRegistry; type BaseWithdrawalFee = BaseWithdrawalFee; type TreasuryPalletId = TreasuryPalletId; @@ -179,6 +183,13 @@ impl PriceFeed for MockPriceFeed { } } +#[cfg(feature = "runtime-benchmarks")] +impl PriceFeedBenchmarks for MockPriceFeed { + fn create_feed(_caller: AccountId, _asset_id: AssetId) -> DispatchResultWithPostInfo { + Ok(().into()) + } +} + parameter_type_with_key! { pub ExistentialDeposits: |_asset_id: AssetId| -> Balance { Zero::zero() diff --git a/runtime/dev/src/lib.rs b/runtime/dev/src/lib.rs index ae3a4aa9ea..8376aeab84 100644 --- a/runtime/dev/src/lib.rs +++ b/runtime/dev/src/lib.rs @@ -56,6 +56,7 @@ use xcm_executor::XcmExecutor; use frame_support::traits::Everything; use pallet_committee::EnsureMember; + pub use pint_runtime_common::{constants::*, types::GovernanceOrigin, weights}; use primitives::traits::MultiAssetRegistry; pub use primitives::*; @@ -467,6 +468,8 @@ impl pallet_asset_index::Config for Runtime { type SelfAssetId = PINTAssetId; type Currency = Currencies; type PriceFeed = PriceFeed; + #[cfg(feature = "runtime-benchmarks")] + type PriceFeedBenchmarks = PriceFeed; type SaftRegistry = SaftRegistry; type BaseWithdrawalFee = BaseWithdrawalFee; type TreasuryPalletId = TreasuryPalletId; diff --git a/runtime/kusama/src/lib.rs b/runtime/kusama/src/lib.rs index e7d6ed9cf1..86af752a4d 100644 --- a/runtime/kusama/src/lib.rs +++ b/runtime/kusama/src/lib.rs @@ -420,8 +420,8 @@ impl pallet_committee::Config for Runtime { type ProposalSubmissionPeriod = ProposalSubmissionPeriod; type VotingPeriod = VotingPeriod; type MinCouncilVotes = MinCouncilVotes; - type ProposalSubmissionOrigin = EnsureSigned; type ProposalExecutionOrigin = EnsureMember; + type ProposalSubmissionOrigin = EnsureSigned; type ApprovedByCommitteeOrigin = GovernanceOrigin; type Event = Event; type WeightInfo = weights::pallet_committee::WeightInfo; @@ -466,6 +466,8 @@ impl pallet_asset_index::Config for Runtime { type SelfAssetId = PINTAssetId; type Currency = Currencies; type PriceFeed = PriceFeed; + #[cfg(feature = "runtime-benchmarks")] + type PriceFeedBenchmarks = PriceFeed; type SaftRegistry = SaftRegistry; type BaseWithdrawalFee = BaseWithdrawalFee; type TreasuryPalletId = TreasuryPalletId; diff --git a/runtime/polkadot/src/lib.rs b/runtime/polkadot/src/lib.rs index 44c860ae7e..53c1fa6f38 100644 --- a/runtime/polkadot/src/lib.rs +++ b/runtime/polkadot/src/lib.rs @@ -467,6 +467,8 @@ impl pallet_asset_index::Config for Runtime { type SelfAssetId = PINTAssetId; type Currency = Currencies; type PriceFeed = PriceFeed; + #[cfg(feature = "runtime-benchmarks")] + type PriceFeedBenchmarks = PriceFeed; type SaftRegistry = SaftRegistry; type BaseWithdrawalFee = BaseWithdrawalFee; type TreasuryPalletId = TreasuryPalletId;