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
1 change: 1 addition & 0 deletions pallets/asset-index/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ runtime-benchmarks = [
'frame-support/runtime-benchmarks',
'pallet-price-feed/runtime-benchmarks',
# 'pallet-chainlink-feed/runtime-benchmarks',
'primitives/runtime-benchmarks',
]

[package.metadata.docs.rs]
Expand Down
17 changes: 8 additions & 9 deletions pallets/asset-index/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use frame_support::{
assert_ok,
dispatch::UnfilteredDispatchable,
sp_runtime::{traits::AccountIdConversion, FixedPointNumber},
sp_std::convert::TryFrom,
traits::{Currency as _, EnsureOrigin, Get},
};
use frame_system::RawOrigin;
Expand All @@ -35,7 +34,7 @@ fn whitelist_acc<T: Config>(acc: &T::AccountId) {

benchmarks! {
add_asset {
let asset_id = T::AssetId::try_from(1u8).ok().unwrap();
let asset_id :T::AssetId = T::try_convert(1u8).unwrap();
let origin = T::AdminOrigin::successful_origin();
let million = 1_000_000u32.into();
let location = MultiLocation::Null;
Expand All @@ -58,7 +57,7 @@ benchmarks! {
}

complete_withdraw {
let asset_id = T::AssetId::try_from(1u8).ok().unwrap();
let asset_id : T::AssetId = T::try_convert(1u8).unwrap();
let units = 100_u32.into();
let tokens = 500_u32.into();
let admin = T::AdminOrigin::successful_origin();
Expand Down Expand Up @@ -98,7 +97,7 @@ benchmarks! {

deposit {
// ASSET_A_ID
let asset_id = T::AssetId::try_from(2u8).ok().unwrap();
let asset_id : T::AssetId = T::try_convert(2u8).unwrap();
let origin = T::AdminOrigin::successful_origin();
let depositor = whitelisted_account::<T>("depositor", 0);
let admin_deposit = 5u32.into();
Expand Down Expand Up @@ -126,7 +125,7 @@ benchmarks! {
}

remove_asset {
let asset_id = T::AssetId::try_from(1u8).ok().unwrap();
let asset_id :T::AssetId = T::try_convert(1u8).unwrap();
let origin = T::AdminOrigin::successful_origin();
let units: u32 = 100;
let amount = 500u32.into();
Expand All @@ -149,7 +148,7 @@ benchmarks! {
}

register_asset {
let asset_id = T::AssetId::try_from(1u8).ok().unwrap();
let asset_id :T::AssetId = T::try_convert(1u8).unwrap();
let origin = T::AdminOrigin::successful_origin();
let availability = AssetAvailability::Saft;
let call = Call::<T>::register_asset(
Expand All @@ -164,7 +163,7 @@ benchmarks! {
}

set_metadata {
let asset_id = T::AssetId::try_from(0u8).ok().unwrap();
let asset_id :T::AssetId = T::try_convert(0u8).unwrap();
let name = b"pint".to_vec();
let symbol = b"pint".to_vec();
let decimals = 8_u8;
Expand All @@ -183,7 +182,7 @@ benchmarks! {
}

withdraw {
let asset_id = T::AssetId::try_from(1u8).ok().unwrap();
let asset_id :T::AssetId = T::try_convert(1u8).unwrap();
let units = 100_u32.into();
let tokens = 500_u32.into();
let admin = T::AdminOrigin::successful_origin();
Expand Down Expand Up @@ -217,7 +216,7 @@ benchmarks! {
}

unlock {
let asset_id = T::AssetId::try_from(1u8).ok().unwrap();
let asset_id :T::AssetId = T::try_convert(1u8).unwrap();
let origin = T::AdminOrigin::successful_origin();
let depositor = whitelisted_account::<T>("depositor", 0);
let amount = 500u32.into();
Expand Down
11 changes: 4 additions & 7 deletions pallets/asset-index/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ pub mod pallet {
traits::{AccountIdConversion, AtLeast32BitUnsigned, CheckedAdd, CheckedDiv, CheckedSub, Saturating, Zero},
ArithmeticError, FixedPointNumber,
},
sp_std::{
convert::{TryFrom, TryInto},
prelude::*,
result::Result,
},
sp_std::{convert::TryInto, prelude::*, result::Result},
traits::{Currency, ExistenceRequirement, Get, LockIdentifier, LockableCurrency, WithdrawReasons},
transactional, PalletId,
};
Expand All @@ -58,11 +54,12 @@ pub mod pallet {
};

use crate::types::{AssetMetadata, AssetRedemption, AssetWithdrawal, IndexTokenLock, PendingRedemption};
use primitives::traits::MaybeAssetIdConvert;

type AccountIdFor<T> = <T as frame_system::Config>::AccountId;

#[pallet::config]
pub trait Config: frame_system::Config {
pub trait Config: frame_system::Config + MaybeAssetIdConvert<u8, Self::AssetId> {
/// Origin that is allowed to administer the index
type AdminOrigin: EnsureOrigin<Self::Origin>;
/// Currency implementation to use as the index token
Expand Down Expand Up @@ -99,7 +96,7 @@ pub mod pallet {
/// Type that handles cross chain transfers
type RemoteAssetManager: RemoteAssetManager<Self::AccountId, Self::AssetId, Self::Balance>;
/// Type used to identify assets
type AssetId: Parameter + Member + Copy + MaybeSerializeDeserialize + TryFrom<u8>;
type AssetId: Parameter + Member + Copy + MaybeSerializeDeserialize;

/// Restricts how many deposits can be active
#[pallet::constant]
Expand Down
1 change: 1 addition & 0 deletions pallets/price-feed/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ runtime-benchmarks = [
'frame-benchmarking',
'frame-support/runtime-benchmarks',
'pallet-chainlink-feed/runtime-benchmarks',
'primitives/runtime-benchmarks',
]

[package.metadata.docs.rs]
Expand Down
11 changes: 3 additions & 8 deletions pallets/price-feed/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,13 @@

use super::*;
use frame_benchmarking::benchmarks;
use frame_support::{
assert_ok,
dispatch::UnfilteredDispatchable,
sp_std::convert::{TryFrom, TryInto},
traits::EnsureOrigin,
};
use frame_support::{assert_ok, dispatch::UnfilteredDispatchable, sp_std::convert::TryInto, traits::EnsureOrigin};

use crate::Pallet as PriceFeed;

benchmarks! {
map_asset_price_feed {
let asset_id = T::AssetId::try_from(2u8).ok().unwrap();
let asset_id :T::AssetId = T::try_convert(2u8).unwrap();
let origin = T::AdminOrigin::successful_origin();
let feed_id = 0u32.try_into().ok().unwrap();
let call = Call::<T>::map_asset_price_feed(
Expand All @@ -31,7 +26,7 @@ benchmarks! {
}

unmap_asset_price_feed {
let asset_id = T::AssetId::try_from(2u8).ok().unwrap();
let asset_id :T::AssetId = T::try_convert(2u8).unwrap();
let origin = T::AdminOrigin::successful_origin();
let feed_id = 0u32.try_into().ok().unwrap();
assert_ok!(PriceFeed::<T>::map_asset_price_feed(origin.clone(), asset_id.clone(), feed_id));
Expand Down
8 changes: 5 additions & 3 deletions pallets/price-feed/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ pub mod pallet {
use frame_support::{
pallet_prelude::*,
sp_runtime::{traits::CheckedDiv, FixedPointNumber, FixedPointOperand},
sp_std::convert::TryFrom,
traits::{Get, Time},
};
use frame_system::pallet_prelude::*;
use pallet_chainlink_feed::{FeedInterface, FeedOracle, RoundData};
use primitives::traits::MaybeAssetIdConvert;
pub use primitives::{AssetPricePair, Price};

pub type FeedIdFor<T> = <T as pallet_chainlink_feed::Config>::FeedId;
Expand All @@ -73,7 +73,9 @@ pub mod pallet {
/// (`quote`/`asset`) from the oracle, its price is given by
/// means of the asset pair `(base / quote)`. (e.g. DOT/PINT)
#[pallet::config]
pub trait Config: frame_system::Config + pallet_chainlink_feed::Config {
pub trait Config:
frame_system::Config + pallet_chainlink_feed::Config + MaybeAssetIdConvert<u8, Self::AssetId>
{
/// The origin that is allowed to insert asset -> feed mappings
type AdminOrigin: EnsureOrigin<Self::Origin>;

Expand All @@ -82,7 +84,7 @@ pub mod pallet {
type SelfAssetId: Get<Self::AssetId>;

/// Type used to identify the assets.
type AssetId: Parameter + Member + MaybeSerializeDeserialize + TryFrom<u8>;
type AssetId: Parameter + Member + MaybeSerializeDeserialize;

/// Type to keep track of timestamped values
type Time: Time;
Expand Down
4 changes: 2 additions & 2 deletions pallets/price-feed/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ impl pallet_chainlink_feed::Config for Test {
type WeightInfo = ();
}

pub(crate) type AssetId = u64;
pub(crate) type AssetId = u32;
pub(crate) const ADMIN_ACCOUNT_ID: AccountId = 88;

parameter_types! {
pub const PINTAssetId: AssetId = 1u64;
pub const PINTAssetId: AssetId = 1u32;
}

ord_parameter_types! {
Expand Down
1 change: 1 addition & 0 deletions pallets/remote-asset-manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ runtime-benchmarks = [
'frame-benchmarking',
'frame-support/runtime-benchmarks',
'frame-system/runtime-benchmarks',
'primitives/runtime-benchmarks',
]

[package.metadata.docs.rs]
Expand Down
8 changes: 4 additions & 4 deletions pallets/remote-asset-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ pub mod pallet {
dispatch::DispatchResultWithPostInfo,
pallet_prelude::*,
sp_runtime::traits::{AccountIdConversion, AtLeast32BitUnsigned, Convert, Saturating, StaticLookup, Zero},
sp_std::{self, convert::TryFrom, mem, prelude::*},
sp_std::{self, mem, prelude::*},
traits::Get,
transactional,
};
use frame_system::pallet_prelude::*;
use orml_traits::{location::Parse, MultiCurrency, XcmTransfer};
use xcm::v0::{Error as XcmError, ExecuteXcm, MultiLocation, OriginKind, Result as XcmResult, SendXcm, Xcm};

use primitives::traits::{MultiAssetRegistry, RemoteAssetManager};
use primitives::traits::{MaybeAssetIdConvert, MultiAssetRegistry, RemoteAssetManager};
use xcm_calls::{
proxy::{ProxyCall, ProxyCallEncoder, ProxyConfig, ProxyParams, ProxyState, ProxyType, ProxyWeights},
staking::{
Expand Down Expand Up @@ -69,7 +69,7 @@ pub mod pallet {
type PalletProxyCall<T> = ProxyCall<AccountIdFor<T>, ProxyType, <T as frame_system::Config>::BlockNumber>;

#[pallet::config]
pub trait Config: frame_system::Config {
pub trait Config: frame_system::Config + MaybeAssetIdConvert<u8, Self::AssetId> {
/// The balance type for cross chain transfers
type Balance: Parameter
+ Member
Expand All @@ -80,7 +80,7 @@ pub mod pallet {
+ Into<u128>;

/// Asset Id that is used to identify different kinds of assets.
type AssetId: Parameter + Member + Copy + MaybeSerializeDeserialize + TryFrom<u8>;
type AssetId: Parameter + Member + Copy + MaybeSerializeDeserialize;

/// Convert a `T::AssetId` to its relative `MultiLocation` identifier.
type AssetIdConvert: Convert<Self::AssetId, Option<MultiLocation>>;
Expand Down
1 change: 1 addition & 0 deletions pallets/saft-registry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ runtime-benchmarks = [
'frame-benchmarking',
'frame-support/runtime-benchmarks',
'pallet-asset-index/runtime-benchmarks',
'primitives/runtime-benchmarks',
]
13 changes: 5 additions & 8 deletions pallets/saft-registry/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
#![cfg(feature = "runtime-benchmarks")]

use frame_benchmarking::benchmarks;
use frame_support::{
assert_ok, dispatch::UnfilteredDispatchable, sp_runtime::traits::Zero, sp_std::convert::TryFrom,
traits::EnsureOrigin,
};
use frame_support::{assert_ok, dispatch::UnfilteredDispatchable, sp_runtime::traits::Zero, traits::EnsureOrigin};
use xcm::v0::Junction;

use crate::Pallet as SaftRegistry;
Expand All @@ -18,7 +15,7 @@ const MAX_SAFT_RECORDS: u32 = 100;

benchmarks! {
add_saft {
let asset= T::AssetId::try_from(0u8).ok().unwrap();
let asset: T::AssetId = T::try_convert(0u8).unwrap();
let origin = T::AdminOrigin::successful_origin();
let call = Call::<T>::add_saft(
asset,
Expand All @@ -35,7 +32,7 @@ benchmarks! {
}

remove_saft {
let asset= T::AssetId::try_from(0u8).ok().unwrap();
let asset: T::AssetId = T::try_convert(0u8).unwrap();
let origin = T::AdminOrigin::successful_origin();
assert_ok!(SaftRegistry::<T>::add_saft(origin.clone(), asset, 100u32.into(), 20u32.into()));
let call = Call::<T>::remove_saft(
Expand All @@ -50,7 +47,7 @@ benchmarks! {
}

report_nav {
let asset= T::AssetId::try_from(0u8).ok().unwrap();
let asset: T::AssetId = T::try_convert(0u8).unwrap();
let origin = T::AdminOrigin::successful_origin();
assert_ok!(SaftRegistry::<T>::add_saft(
origin.clone(),
Expand All @@ -74,7 +71,7 @@ benchmarks! {
convert_to_liquid {
let nav = 1337u32;
let units = 1234u32;
let asset= T::AssetId::try_from(0u8).ok().unwrap();
let asset:T::AssetId = T::try_convert(0u8).unwrap();
let origin = T::AdminOrigin::successful_origin();
// Create saft records
for i in 0 .. MAX_SAFT_RECORDS {
Expand Down
8 changes: 4 additions & 4 deletions pallets/saft-registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,24 @@ pub mod pallet {
traits::{AtLeast32BitUnsigned, CheckedAdd, One, Saturating, Zero},
ArithmeticError,
},
sp_std::{self, convert::TryFrom, prelude::*, result::Result},
sp_std::{self, prelude::*, result::Result},
transactional,
};
use frame_system::pallet_prelude::*;
use primitives::{
traits::{AssetRecorder, SaftRegistry},
traits::{AssetRecorder, MaybeAssetIdConvert, SaftRegistry},
types::AssetAvailability,
SAFTId,
};
use xcm::v0::MultiLocation;

#[pallet::config]
pub trait Config: frame_system::Config {
pub trait Config: frame_system::Config + MaybeAssetIdConvert<u8, Self::AssetId> {
// Origin that is allowed to manage the SAFTs
type AdminOrigin: EnsureOrigin<Self::Origin>;
type AssetRecorder: AssetRecorder<Self::AccountId, Self::AssetId, Self::Balance>;
type Balance: Parameter + Member + AtLeast32BitUnsigned + Default + Copy;
type AssetId: Parameter + Member + Copy + TryFrom<u8>;
type AssetId: Parameter + Member + Copy;
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
/// The weight for this pallet's extrinsics.
type WeightInfo: WeightInfo;
Expand Down
1 change: 0 additions & 1 deletion primitives/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ std = [
'frame-system/std',
'xcm/std',
]
# this feature is only for compilation now
runtime-benchmarks = []

[package.metadata.docs.rs]
Expand Down
29 changes: 29 additions & 0 deletions primitives/primitives/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,32 @@ impl<BlockNumber, Balance: AtLeast32BitUnsigned> RedemptionFee<BlockNumber, Bala
Balance::zero()
}
}

/// This is a helper trait only used for constructing `AssetId` types in Runtime Benchmarks
pub trait MaybeAssetIdConvert<A, B>: Sized {
#[cfg(feature = "runtime-benchmarks")]
fn try_convert(value: A) -> Option<B>;
}

#[cfg(feature = "runtime-benchmarks")]
impl<T> MaybeAssetIdConvert<u8, crate::types::AssetId> for T {
fn try_convert(value: u8) -> Option<crate::types::AssetId> {
frame_support::sp_std::convert::TryFrom::try_from(value).ok()
}
}

#[cfg(not(feature = "runtime-benchmarks"))]
impl<T, A, B> MaybeAssetIdConvert<A, B> for T {}

#[cfg(test)]
mod tests {
use super::*;
use crate::AssetId;

fn assert_maybe_from<T: MaybeAssetIdConvert<u8, AssetId>>() {}

#[test]
fn maybe_from_works() {
assert_maybe_from::<()>();
}
}
2 changes: 2 additions & 0 deletions primitives/primitives/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright 2021 ChainSafe Systems
// SPDX-License-Identifier: LGPL-3.0-only

//! Shareable PINT types

use frame_support::{
pallet_prelude::*,
sp_runtime::{
Expand Down
2 changes: 1 addition & 1 deletion runtime/dev/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@ runtime-benchmarks = [
'pallet-price-feed/runtime-benchmarks',
'pallet-local-treasury/runtime-benchmarks',
'pallet-balances/runtime-benchmarks',
'pallet-timestamp/runtime-benchmarks',
'pallet-committee/runtime-benchmarks',
'pallet-xcm/runtime-benchmarks',
'pallet-saft-registry/runtime-benchmarks',
'pallet-remote-asset-manager/runtime-benchmarks',
'pallet-collective/runtime-benchmarks',
'primitives/runtime-benchmarks',

'orml-tokens/runtime-benchmarks',
]
Expand Down
Loading