Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions pallets/asset-index/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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::<T>("depositor", 0);
let admin_deposit = 5u32.into();
assert_ok!(AssetIndex::<T>::add_asset(origin, asset_id, 100u32.into(),MultiLocation::Null,admin_deposit
));
let units = 1_000u32.into();

assert_ok!(AssetIndex::<T>::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::<T>::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::<T>::index_token_balance(&depositor).into(), received);
let nav = AssetIndex::<T>::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::<T>::index_token_balance(&depositor).into(), received);
}

remove_asset {
Expand Down
6 changes: 6 additions & 0 deletions pallets/asset-index/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -106,6 +108,10 @@ pub mod pallet {
/// The types that provides the necessary asset price pairs
type PriceFeed: PriceFeed<Self::AssetId>;

#[cfg(feature = "runtime-benchmarks")]
/// The type that provides benchmark features of pallet_price_feed
type PriceFeedBenchmarks: PriceFeedBenchmarks<Self::AccountId, Self::AssetId>;

/// The type registry that stores all NAV for non liquid assets
type SaftRegistry: SaftRegistry<Self::AssetId, Self::Balance>;

Expand Down
15 changes: 13 additions & 2 deletions pallets/asset-index/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -213,6 +217,13 @@ impl MockPriceFeed {
}
}

#[cfg(feature = "runtime-benchmarks")]
impl PriceFeedBenchmarks<AccountId, AssetId> for MockPriceFeed {
fn create_feed(_caller: AccountId, _asset_id: AssetId) -> DispatchResultWithPostInfo {
Ok(().into())
}
}

impl PriceFeed<AssetId> for MockPriceFeed {
// mock price supposed to return the price pair with the same `quote` price, like USD
fn get_price(asset: AssetId) -> Result<Price, DispatchError> {
Expand Down
46 changes: 45 additions & 1 deletion pallets/price-feed/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -260,6 +265,45 @@ pub mod pallet {
}
}

#[cfg(feature = "runtime-benchmarks")]
impl<T: Config> PriceFeedBenchmarks<T::AccountId, T::AssetId> for Pallet<T> {
fn create_feed(
caller: <T as frame_system::Config>::AccountId,
asset_id: T::AssetId,
) -> DispatchResultWithPostInfo {
use frame_benchmarking::vec;

pallet_chainlink_feed::Pallet::<T>::set_feed_creator(
<frame_system::Origin<T>>::Signed(pallet_chainlink_feed::Pallet::<T>::pallet_admin()).into(),
caller.clone(),
)?;

pallet_chainlink_feed::Pallet::<T>::create_feed(
<frame_system::Origin<T>>::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 = <pallet_chainlink_feed::FeedCounter<T>>::get() - 1.into();
AssetFeeds::<T>::insert(&asset_id, feed_id);
pallet_chainlink_feed::Pallet::<T>::submit(
<frame_system::Origin<T>>::Signed(caller.clone()).into(),
feed_id,
1_u32.into(),
42.into(),
)?;
Ok(().into())
}
}

Comment on lines +268 to +306
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is essentially a mock?
I'm rather against adding this. Can you extend on why this is necessary, we should fix this upstream if this is a problem of the chainlink pallet.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I'm thinking of fixing it in the chainlink repo and open another issue for this

impl<T: Config> pallet_chainlink_feed::traits::OnAnswerHandler<T> for Pallet<T> {
fn on_answer(feed_id: FeedIdFor<T>, _: RoundData<T::BlockNumber, FeedValueFor<T>>) {
LatestAnswerTimestamp::<T>::insert(feed_id, T::Time::now());
Expand Down
7 changes: 6 additions & 1 deletion pallets/price-feed/src/traits.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -15,3 +15,8 @@ pub trait PriceFeed<AssetId> {
/// `base/quote`
fn get_relative_price_pair(base: AssetId, quote: AssetId) -> Result<AssetPricePair<AssetId>, DispatchError>;
}

#[cfg(feature = "runtime-benchmarks")]
pub trait PriceFeedBenchmarks<AccountId, AssetId> {
fn create_feed(caller: AccountId, asset_id: AssetId) -> DispatchResultWithPostInfo;
}
13 changes: 10 additions & 3 deletions pallets/remote-asset-manager/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -471,6 +471,13 @@ pub mod para {
}
}

#[cfg(feature = "runtime-benchmarks")]
impl PriceFeedBenchmarks<AccountId, AssetId> 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;
Expand Down
15 changes: 13 additions & 2 deletions pallets/saft-registry/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -179,6 +183,13 @@ impl PriceFeed<AssetId> for MockPriceFeed {
}
}

#[cfg(feature = "runtime-benchmarks")]
impl PriceFeedBenchmarks<AccountId, AssetId> 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()
Expand Down
3 changes: 3 additions & 0 deletions runtime/dev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion runtime/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,8 @@ impl pallet_committee::Config for Runtime {
type ProposalSubmissionPeriod = ProposalSubmissionPeriod;
type VotingPeriod = VotingPeriod;
type MinCouncilVotes = MinCouncilVotes;
type ProposalSubmissionOrigin = EnsureSigned<AccountId>;
type ProposalExecutionOrigin = EnsureMember<Self>;
type ProposalSubmissionOrigin = EnsureSigned<AccountId>;
type ApprovedByCommitteeOrigin = GovernanceOrigin<AccountId, Runtime>;
type Event = Event;
type WeightInfo = weights::pallet_committee::WeightInfo<Runtime>;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions runtime/polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down