From adb5142a5d264f9a441a48c707fda9f224486f66 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Wed, 1 Sep 2021 12:11:45 +0200 Subject: [PATCH 1/3] feat: add MaybeTryFrom trait --- primitives/primitives/Cargo.toml | 1 - primitives/primitives/src/traits.rs | 32 +++++++++++++++++++++++++++++ primitives/primitives/src/types.rs | 2 ++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/primitives/primitives/Cargo.toml b/primitives/primitives/Cargo.toml index 60886e4aa9..12a6e6731a 100644 --- a/primitives/primitives/Cargo.toml +++ b/primitives/primitives/Cargo.toml @@ -26,7 +26,6 @@ std = [ 'frame-system/std', 'xcm/std', ] -# this feature is only for compilation now runtime-benchmarks = [] [package.metadata.docs.rs] diff --git a/primitives/primitives/src/traits.rs b/primitives/primitives/src/traits.rs index 640cf2f0a9..1589b90dc6 100644 --- a/primitives/primitives/src/traits.rs +++ b/primitives/primitives/src/traits.rs @@ -257,3 +257,35 @@ pub trait AssetRecorder { /// the nav from the caller's account fn remove_saft(who: AccountId, id: AssetId, units: Balance, nav: Balance) -> DispatchResult; } + +/// This is a helper trait used for constructing types in Runtime Benchmarks +pub trait MaybeTryFrom: Sized { + #[cfg(feature = "runtime-benchmarks")] + fn try_from(value: T) -> Option; +} + +#[cfg(feature = "runtime-benchmarks")] +impl MaybeTryFrom for crate::types::AssetId { + fn try_from(value: u8) -> Option { + frame_support::sp_std::convert::TryFrom::try_from(value).ok() + } +} + +#[cfg(not(feature = "runtime-benchmarks"))] +impl MaybeTryFrom for T {} + +#[cfg(test)] +mod tests { + use super::*; + use crate::AssetId; + + fn assert_maybe_from>() {} + + #[test] + fn maybe_from_works() { + assert_maybe_from::(); + + #[cfg(feature = "runtime-benchmarks")] + assert_eq!(Some(10 as AssetId), MaybeTryFrom::try_from(10u8)); + } +} diff --git a/primitives/primitives/src/types.rs b/primitives/primitives/src/types.rs index b0355fa599..689ca0384b 100644 --- a/primitives/primitives/src/types.rs +++ b/primitives/primitives/src/types.rs @@ -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::{ From db1f17c4ce1ec8ca55c571e3288034a4e1a3f60c Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Wed, 1 Sep 2021 13:06:39 +0200 Subject: [PATCH 2/3] feat: use MaybeTryFrom for runtime benchmarks --- pallets/asset-index/Cargo.toml | 1 + pallets/asset-index/src/benchmarking.rs | 22 ++++++++++++---------- pallets/asset-index/src/lib.rs | 9 +++------ pallets/price-feed/Cargo.toml | 1 + pallets/price-feed/src/benchmarking.rs | 12 ++++-------- pallets/price-feed/src/lib.rs | 4 ++-- pallets/price-feed/src/mock.rs | 4 ++-- pallets/remote-asset-manager/Cargo.toml | 1 + pallets/remote-asset-manager/src/lib.rs | 6 +++--- pallets/saft-registry/Cargo.toml | 1 + pallets/saft-registry/src/benchmarking.rs | 14 ++++++-------- pallets/saft-registry/src/lib.rs | 6 +++--- runtime/dev/Cargo.toml | 2 +- runtime/kusama/Cargo.toml | 2 +- runtime/polkadot/Cargo.toml | 2 +- 15 files changed, 42 insertions(+), 45 deletions(-) diff --git a/pallets/asset-index/Cargo.toml b/pallets/asset-index/Cargo.toml index 99fb23d71d..2b51eb5eb1 100644 --- a/pallets/asset-index/Cargo.toml +++ b/pallets/asset-index/Cargo.toml @@ -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] diff --git a/pallets/asset-index/src/benchmarking.rs b/pallets/asset-index/src/benchmarking.rs index 33a4125774..9a0883b325 100644 --- a/pallets/asset-index/src/benchmarking.rs +++ b/pallets/asset-index/src/benchmarking.rs @@ -8,13 +8,15 @@ 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; use orml_traits::MultiCurrency; use pallet_price_feed::{PriceFeed, PriceFeedBenchmarks}; -use primitives::{traits::NavProvider, AssetAvailability}; +use primitives::{ + traits::{MaybeTryFrom, NavProvider}, + AssetAvailability, +}; use xcm::v0::MultiLocation; use crate::Pallet as AssetIndex; @@ -35,7 +37,7 @@ fn whitelist_acc(acc: &T::AccountId) { benchmarks! { add_asset { - let asset_id = T::AssetId::try_from(1u8).ok().unwrap(); + let asset_id = T::AssetId::try_from(1u8).unwrap(); let origin = T::AdminOrigin::successful_origin(); let million = 1_000_000u32.into(); let location = MultiLocation::Null; @@ -58,7 +60,7 @@ benchmarks! { } complete_withdraw { - let asset_id = T::AssetId::try_from(1u8).ok().unwrap(); + let asset_id = T::AssetId::try_from(1u8).unwrap(); let units = 100_u32.into(); let tokens = 500_u32.into(); let admin = T::AdminOrigin::successful_origin(); @@ -98,7 +100,7 @@ benchmarks! { deposit { // ASSET_A_ID - let asset_id = T::AssetId::try_from(2u8).ok().unwrap(); + let asset_id = T::AssetId::try_from(2u8).unwrap(); let origin = T::AdminOrigin::successful_origin(); let depositor = whitelisted_account::("depositor", 0); let admin_deposit = 5u32.into(); @@ -126,7 +128,7 @@ benchmarks! { } remove_asset { - let asset_id = T::AssetId::try_from(1u8).ok().unwrap(); + let asset_id = T::AssetId::try_from(1u8).unwrap(); let origin = T::AdminOrigin::successful_origin(); let units: u32 = 100; let amount = 500u32.into(); @@ -149,7 +151,7 @@ benchmarks! { } register_asset { - let asset_id = T::AssetId::try_from(1u8).ok().unwrap(); + let asset_id = T::AssetId::try_from(1u8).unwrap(); let origin = T::AdminOrigin::successful_origin(); let availability = AssetAvailability::Saft; let call = Call::::register_asset( @@ -164,7 +166,7 @@ benchmarks! { } set_metadata { - let asset_id = T::AssetId::try_from(0u8).ok().unwrap(); + let asset_id = T::AssetId::try_from(0u8).unwrap(); let name = b"pint".to_vec(); let symbol = b"pint".to_vec(); let decimals = 8_u8; @@ -183,7 +185,7 @@ benchmarks! { } withdraw { - let asset_id = T::AssetId::try_from(1u8).ok().unwrap(); + let asset_id = T::AssetId::try_from(1u8).unwrap(); let units = 100_u32.into(); let tokens = 500_u32.into(); let admin = T::AdminOrigin::successful_origin(); @@ -217,7 +219,7 @@ benchmarks! { } unlock { - let asset_id = T::AssetId::try_from(1u8).ok().unwrap(); + let asset_id = T::AssetId::try_from(1u8).unwrap(); let origin = T::AdminOrigin::successful_origin(); let depositor = whitelisted_account::("depositor", 0); let amount = 500u32.into(); diff --git a/pallets/asset-index/src/lib.rs b/pallets/asset-index/src/lib.rs index 83c40bec1b..aa0b1ccb54 100644 --- a/pallets/asset-index/src/lib.rs +++ b/pallets/asset-index/src/lib.rs @@ -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, }; @@ -58,6 +54,7 @@ pub mod pallet { }; use crate::types::{AssetMetadata, AssetRedemption, AssetWithdrawal, IndexTokenLock, PendingRedemption}; + use primitives::traits::MaybeTryFrom; type AccountIdFor = ::AccountId; @@ -99,7 +96,7 @@ pub mod pallet { /// Type that handles cross chain transfers type RemoteAssetManager: RemoteAssetManager; /// Type used to identify assets - type AssetId: Parameter + Member + Copy + MaybeSerializeDeserialize + TryFrom; + type AssetId: Parameter + Member + Copy + MaybeSerializeDeserialize + MaybeTryFrom; /// The native asset id #[pallet::constant] diff --git a/pallets/price-feed/Cargo.toml b/pallets/price-feed/Cargo.toml index ece98a6675..9df058240b 100644 --- a/pallets/price-feed/Cargo.toml +++ b/pallets/price-feed/Cargo.toml @@ -44,6 +44,7 @@ runtime-benchmarks = [ 'frame-benchmarking', 'frame-support/runtime-benchmarks', 'pallet-chainlink-feed/runtime-benchmarks', + 'primitives/runtime-benchmarks', ] [package.metadata.docs.rs] diff --git a/pallets/price-feed/src/benchmarking.rs b/pallets/price-feed/src/benchmarking.rs index 50332e1789..608f1ab52e 100644 --- a/pallets/price-feed/src/benchmarking.rs +++ b/pallets/price-feed/src/benchmarking.rs @@ -5,18 +5,14 @@ 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 primitives::traits::MaybeTryFrom; 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::try_from(2u8).unwrap(); let origin = T::AdminOrigin::successful_origin(); let feed_id = 0u32.try_into().ok().unwrap(); let call = Call::::map_asset_price_feed( @@ -31,7 +27,7 @@ benchmarks! { } unmap_asset_price_feed { - let asset_id = T::AssetId::try_from(2u8).ok().unwrap(); + let asset_id = T::AssetId::try_from(2u8).unwrap(); let origin = T::AdminOrigin::successful_origin(); let feed_id = 0u32.try_into().ok().unwrap(); assert_ok!(PriceFeed::::map_asset_price_feed(origin.clone(), asset_id.clone(), feed_id)); diff --git a/pallets/price-feed/src/lib.rs b/pallets/price-feed/src/lib.rs index d8939ed94f..043e29613c 100644 --- a/pallets/price-feed/src/lib.rs +++ b/pallets/price-feed/src/lib.rs @@ -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::MaybeTryFrom; pub use primitives::{AssetPricePair, Price}; pub type FeedIdFor = ::FeedId; @@ -82,7 +82,7 @@ pub mod pallet { type SelfAssetId: Get; /// Type used to identify the assets. - type AssetId: Parameter + Member + MaybeSerializeDeserialize + TryFrom; + type AssetId: Parameter + Member + MaybeSerializeDeserialize + MaybeTryFrom; /// Type to keep track of timestamped values type Time: Time; diff --git a/pallets/price-feed/src/mock.rs b/pallets/price-feed/src/mock.rs index 067117c352..dc2c21fb63 100644 --- a/pallets/price-feed/src/mock.rs +++ b/pallets/price-feed/src/mock.rs @@ -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! { diff --git a/pallets/remote-asset-manager/Cargo.toml b/pallets/remote-asset-manager/Cargo.toml index cba028e240..75855fc1d4 100644 --- a/pallets/remote-asset-manager/Cargo.toml +++ b/pallets/remote-asset-manager/Cargo.toml @@ -110,6 +110,7 @@ runtime-benchmarks = [ 'frame-benchmarking', 'frame-support/runtime-benchmarks', 'frame-system/runtime-benchmarks', + 'primitives/runtime-benchmarks', ] [package.metadata.docs.rs] diff --git a/pallets/remote-asset-manager/src/lib.rs b/pallets/remote-asset-manager/src/lib.rs index 493f5ff164..89f9094455 100644 --- a/pallets/remote-asset-manager/src/lib.rs +++ b/pallets/remote-asset-manager/src/lib.rs @@ -28,7 +28,7 @@ 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, }; @@ -36,7 +36,7 @@ pub mod pallet { 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::{MaybeTryFrom, MultiAssetRegistry, RemoteAssetManager}; use xcm_calls::{ proxy::{ProxyCall, ProxyCallEncoder, ProxyConfig, ProxyParams, ProxyState, ProxyType, ProxyWeights}, staking::{ @@ -80,7 +80,7 @@ pub mod pallet { + Into; /// Asset Id that is used to identify different kinds of assets. - type AssetId: Parameter + Member + Copy + MaybeSerializeDeserialize + TryFrom; + type AssetId: Parameter + Member + Copy + MaybeSerializeDeserialize + MaybeTryFrom; /// Convert a `T::AssetId` to its relative `MultiLocation` identifier. type AssetIdConvert: Convert>; diff --git a/pallets/saft-registry/Cargo.toml b/pallets/saft-registry/Cargo.toml index 97953d8077..6943d85e07 100644 --- a/pallets/saft-registry/Cargo.toml +++ b/pallets/saft-registry/Cargo.toml @@ -59,4 +59,5 @@ runtime-benchmarks = [ 'frame-benchmarking', 'frame-support/runtime-benchmarks', 'pallet-asset-index/runtime-benchmarks', + 'primitives/runtime-benchmarks', ] diff --git a/pallets/saft-registry/src/benchmarking.rs b/pallets/saft-registry/src/benchmarking.rs index cf32c171ae..79fbd4d0af 100644 --- a/pallets/saft-registry/src/benchmarking.rs +++ b/pallets/saft-registry/src/benchmarking.rs @@ -4,10 +4,8 @@ #![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 primitives::traits::MaybeTryFrom; use xcm::v0::Junction; use crate::Pallet as SaftRegistry; @@ -18,7 +16,7 @@ const MAX_SAFT_RECORDS: u32 = 100; benchmarks! { add_saft { - let asset= T::AssetId::try_from(0u8).ok().unwrap(); + let asset= T::AssetId::try_from(0u8).unwrap(); let origin = T::AdminOrigin::successful_origin(); let call = Call::::add_saft( asset, @@ -35,7 +33,7 @@ benchmarks! { } remove_saft { - let asset= T::AssetId::try_from(0u8).ok().unwrap(); + let asset= T::AssetId::try_from(0u8).unwrap(); let origin = T::AdminOrigin::successful_origin(); assert_ok!(SaftRegistry::::add_saft(origin.clone(), asset, 100u32.into(), 20u32.into())); let call = Call::::remove_saft( @@ -50,7 +48,7 @@ benchmarks! { } report_nav { - let asset= T::AssetId::try_from(0u8).ok().unwrap(); + let asset= T::AssetId::try_from(0u8).unwrap(); let origin = T::AdminOrigin::successful_origin(); assert_ok!(SaftRegistry::::add_saft( origin.clone(), @@ -74,7 +72,7 @@ benchmarks! { convert_to_liquid { let nav = 1337u32; let units = 1234u32; - let asset= T::AssetId::try_from(0u8).ok().unwrap(); + let asset= T::AssetId::try_from(0u8).unwrap(); let origin = T::AdminOrigin::successful_origin(); // Create saft records for i in 0 .. MAX_SAFT_RECORDS { diff --git a/pallets/saft-registry/src/lib.rs b/pallets/saft-registry/src/lib.rs index 49e3b701da..03355a58a5 100644 --- a/pallets/saft-registry/src/lib.rs +++ b/pallets/saft-registry/src/lib.rs @@ -24,12 +24,12 @@ 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, MaybeTryFrom, SaftRegistry}, types::AssetAvailability, SAFTId, }; @@ -41,7 +41,7 @@ pub mod pallet { type AdminOrigin: EnsureOrigin; type AssetRecorder: AssetRecorder; type Balance: Parameter + Member + AtLeast32BitUnsigned + Default + Copy; - type AssetId: Parameter + Member + Copy + TryFrom; + type AssetId: Parameter + Member + Copy + MaybeTryFrom; type Event: From> + IsType<::Event>; /// The weight for this pallet's extrinsics. type WeightInfo: WeightInfo; diff --git a/runtime/dev/Cargo.toml b/runtime/dev/Cargo.toml index 8ea6c65bf5..d876915c7f 100644 --- a/runtime/dev/Cargo.toml +++ b/runtime/dev/Cargo.toml @@ -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', ] diff --git a/runtime/kusama/Cargo.toml b/runtime/kusama/Cargo.toml index 56acf498ae..8bd88ab5ed 100644 --- a/runtime/kusama/Cargo.toml +++ b/runtime/kusama/Cargo.toml @@ -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' ] diff --git a/runtime/polkadot/Cargo.toml b/runtime/polkadot/Cargo.toml index 50458e8382..f906ac0f33 100644 --- a/runtime/polkadot/Cargo.toml +++ b/runtime/polkadot/Cargo.toml @@ -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' ] From c240cd4f1c8f5a75a80e45b7985fa05f4b64c47e Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Thu, 2 Sep 2021 09:38:49 +0200 Subject: [PATCH 3/3] feat: implement helper trait --- pallets/asset-index/src/benchmarking.rs | 21 +++++++++------------ pallets/asset-index/src/lib.rs | 6 +++--- pallets/price-feed/src/benchmarking.rs | 5 ++--- pallets/price-feed/src/lib.rs | 8 +++++--- pallets/remote-asset-manager/src/lib.rs | 6 +++--- pallets/saft-registry/src/benchmarking.rs | 9 ++++----- pallets/saft-registry/src/lib.rs | 6 +++--- primitives/primitives/src/traits.rs | 20 ++++++++------------ 8 files changed, 37 insertions(+), 44 deletions(-) diff --git a/pallets/asset-index/src/benchmarking.rs b/pallets/asset-index/src/benchmarking.rs index 9a0883b325..9235d1dd79 100644 --- a/pallets/asset-index/src/benchmarking.rs +++ b/pallets/asset-index/src/benchmarking.rs @@ -13,10 +13,7 @@ use frame_support::{ use frame_system::RawOrigin; use orml_traits::MultiCurrency; use pallet_price_feed::{PriceFeed, PriceFeedBenchmarks}; -use primitives::{ - traits::{MaybeTryFrom, NavProvider}, - AssetAvailability, -}; +use primitives::{traits::NavProvider, AssetAvailability}; use xcm::v0::MultiLocation; use crate::Pallet as AssetIndex; @@ -37,7 +34,7 @@ fn whitelist_acc(acc: &T::AccountId) { benchmarks! { add_asset { - let asset_id = T::AssetId::try_from(1u8).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; @@ -60,7 +57,7 @@ benchmarks! { } complete_withdraw { - let asset_id = T::AssetId::try_from(1u8).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(); @@ -100,7 +97,7 @@ benchmarks! { deposit { // ASSET_A_ID - let asset_id = T::AssetId::try_from(2u8).unwrap(); + let asset_id : T::AssetId = T::try_convert(2u8).unwrap(); let origin = T::AdminOrigin::successful_origin(); let depositor = whitelisted_account::("depositor", 0); let admin_deposit = 5u32.into(); @@ -128,7 +125,7 @@ benchmarks! { } remove_asset { - let asset_id = T::AssetId::try_from(1u8).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(); @@ -151,7 +148,7 @@ benchmarks! { } register_asset { - let asset_id = T::AssetId::try_from(1u8).unwrap(); + let asset_id :T::AssetId = T::try_convert(1u8).unwrap(); let origin = T::AdminOrigin::successful_origin(); let availability = AssetAvailability::Saft; let call = Call::::register_asset( @@ -166,7 +163,7 @@ benchmarks! { } set_metadata { - let asset_id = T::AssetId::try_from(0u8).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; @@ -185,7 +182,7 @@ benchmarks! { } withdraw { - let asset_id = T::AssetId::try_from(1u8).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(); @@ -219,7 +216,7 @@ benchmarks! { } unlock { - let asset_id = T::AssetId::try_from(1u8).unwrap(); + let asset_id :T::AssetId = T::try_convert(1u8).unwrap(); let origin = T::AdminOrigin::successful_origin(); let depositor = whitelisted_account::("depositor", 0); let amount = 500u32.into(); diff --git a/pallets/asset-index/src/lib.rs b/pallets/asset-index/src/lib.rs index aa0b1ccb54..f165551f05 100644 --- a/pallets/asset-index/src/lib.rs +++ b/pallets/asset-index/src/lib.rs @@ -54,12 +54,12 @@ pub mod pallet { }; use crate::types::{AssetMetadata, AssetRedemption, AssetWithdrawal, IndexTokenLock, PendingRedemption}; - use primitives::traits::MaybeTryFrom; + use primitives::traits::MaybeAssetIdConvert; type AccountIdFor = ::AccountId; #[pallet::config] - pub trait Config: frame_system::Config { + pub trait Config: frame_system::Config + MaybeAssetIdConvert { /// Origin that is allowed to administer the index type AdminOrigin: EnsureOrigin; /// Currency implementation to use as the index token @@ -96,7 +96,7 @@ pub mod pallet { /// Type that handles cross chain transfers type RemoteAssetManager: RemoteAssetManager; /// Type used to identify assets - type AssetId: Parameter + Member + Copy + MaybeSerializeDeserialize + MaybeTryFrom; + type AssetId: Parameter + Member + Copy + MaybeSerializeDeserialize; /// The native asset id #[pallet::constant] diff --git a/pallets/price-feed/src/benchmarking.rs b/pallets/price-feed/src/benchmarking.rs index 608f1ab52e..7b859ddad3 100644 --- a/pallets/price-feed/src/benchmarking.rs +++ b/pallets/price-feed/src/benchmarking.rs @@ -6,13 +6,12 @@ use super::*; use frame_benchmarking::benchmarks; use frame_support::{assert_ok, dispatch::UnfilteredDispatchable, sp_std::convert::TryInto, traits::EnsureOrigin}; -use primitives::traits::MaybeTryFrom; use crate::Pallet as PriceFeed; benchmarks! { map_asset_price_feed { - let asset_id = T::AssetId::try_from(2u8).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::::map_asset_price_feed( @@ -27,7 +26,7 @@ benchmarks! { } unmap_asset_price_feed { - let asset_id = T::AssetId::try_from(2u8).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::::map_asset_price_feed(origin.clone(), asset_id.clone(), feed_id)); diff --git a/pallets/price-feed/src/lib.rs b/pallets/price-feed/src/lib.rs index 043e29613c..835f8f23f3 100644 --- a/pallets/price-feed/src/lib.rs +++ b/pallets/price-feed/src/lib.rs @@ -54,7 +54,7 @@ pub mod pallet { }; use frame_system::pallet_prelude::*; use pallet_chainlink_feed::{FeedInterface, FeedOracle, RoundData}; - use primitives::traits::MaybeTryFrom; + use primitives::traits::MaybeAssetIdConvert; pub use primitives::{AssetPricePair, Price}; pub type FeedIdFor = ::FeedId; @@ -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 + { /// The origin that is allowed to insert asset -> feed mappings type AdminOrigin: EnsureOrigin; @@ -82,7 +84,7 @@ pub mod pallet { type SelfAssetId: Get; /// Type used to identify the assets. - type AssetId: Parameter + Member + MaybeSerializeDeserialize + MaybeTryFrom; + type AssetId: Parameter + Member + MaybeSerializeDeserialize; /// Type to keep track of timestamped values type Time: Time; diff --git a/pallets/remote-asset-manager/src/lib.rs b/pallets/remote-asset-manager/src/lib.rs index 89f9094455..86c7e1c9fe 100644 --- a/pallets/remote-asset-manager/src/lib.rs +++ b/pallets/remote-asset-manager/src/lib.rs @@ -36,7 +36,7 @@ pub mod pallet { use orml_traits::{location::Parse, MultiCurrency, XcmTransfer}; use xcm::v0::{Error as XcmError, ExecuteXcm, MultiLocation, OriginKind, Result as XcmResult, SendXcm, Xcm}; - use primitives::traits::{MaybeTryFrom, MultiAssetRegistry, RemoteAssetManager}; + use primitives::traits::{MaybeAssetIdConvert, MultiAssetRegistry, RemoteAssetManager}; use xcm_calls::{ proxy::{ProxyCall, ProxyCallEncoder, ProxyConfig, ProxyParams, ProxyState, ProxyType, ProxyWeights}, staking::{ @@ -69,7 +69,7 @@ pub mod pallet { type PalletProxyCall = ProxyCall, ProxyType, ::BlockNumber>; #[pallet::config] - pub trait Config: frame_system::Config { + pub trait Config: frame_system::Config + MaybeAssetIdConvert { /// The balance type for cross chain transfers type Balance: Parameter + Member @@ -80,7 +80,7 @@ pub mod pallet { + Into; /// Asset Id that is used to identify different kinds of assets. - type AssetId: Parameter + Member + Copy + MaybeSerializeDeserialize + MaybeTryFrom; + type AssetId: Parameter + Member + Copy + MaybeSerializeDeserialize; /// Convert a `T::AssetId` to its relative `MultiLocation` identifier. type AssetIdConvert: Convert>; diff --git a/pallets/saft-registry/src/benchmarking.rs b/pallets/saft-registry/src/benchmarking.rs index 79fbd4d0af..bc1be048c1 100644 --- a/pallets/saft-registry/src/benchmarking.rs +++ b/pallets/saft-registry/src/benchmarking.rs @@ -5,7 +5,6 @@ use frame_benchmarking::benchmarks; use frame_support::{assert_ok, dispatch::UnfilteredDispatchable, sp_runtime::traits::Zero, traits::EnsureOrigin}; -use primitives::traits::MaybeTryFrom; use xcm::v0::Junction; use crate::Pallet as SaftRegistry; @@ -16,7 +15,7 @@ const MAX_SAFT_RECORDS: u32 = 100; benchmarks! { add_saft { - let asset= T::AssetId::try_from(0u8).unwrap(); + let asset: T::AssetId = T::try_convert(0u8).unwrap(); let origin = T::AdminOrigin::successful_origin(); let call = Call::::add_saft( asset, @@ -33,7 +32,7 @@ benchmarks! { } remove_saft { - let asset= T::AssetId::try_from(0u8).unwrap(); + let asset: T::AssetId = T::try_convert(0u8).unwrap(); let origin = T::AdminOrigin::successful_origin(); assert_ok!(SaftRegistry::::add_saft(origin.clone(), asset, 100u32.into(), 20u32.into())); let call = Call::::remove_saft( @@ -48,7 +47,7 @@ benchmarks! { } report_nav { - let asset= T::AssetId::try_from(0u8).unwrap(); + let asset: T::AssetId = T::try_convert(0u8).unwrap(); let origin = T::AdminOrigin::successful_origin(); assert_ok!(SaftRegistry::::add_saft( origin.clone(), @@ -72,7 +71,7 @@ benchmarks! { convert_to_liquid { let nav = 1337u32; let units = 1234u32; - let asset= T::AssetId::try_from(0u8).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 { diff --git a/pallets/saft-registry/src/lib.rs b/pallets/saft-registry/src/lib.rs index 03355a58a5..cf1f5379f7 100644 --- a/pallets/saft-registry/src/lib.rs +++ b/pallets/saft-registry/src/lib.rs @@ -29,19 +29,19 @@ pub mod pallet { }; use frame_system::pallet_prelude::*; use primitives::{ - traits::{AssetRecorder, MaybeTryFrom, 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 { // Origin that is allowed to manage the SAFTs type AdminOrigin: EnsureOrigin; type AssetRecorder: AssetRecorder; type Balance: Parameter + Member + AtLeast32BitUnsigned + Default + Copy; - type AssetId: Parameter + Member + Copy + MaybeTryFrom; + type AssetId: Parameter + Member + Copy; type Event: From> + IsType<::Event>; /// The weight for this pallet's extrinsics. type WeightInfo: WeightInfo; diff --git a/primitives/primitives/src/traits.rs b/primitives/primitives/src/traits.rs index 1589b90dc6..802f03509f 100644 --- a/primitives/primitives/src/traits.rs +++ b/primitives/primitives/src/traits.rs @@ -257,35 +257,31 @@ pub trait AssetRecorder { /// the nav from the caller's account fn remove_saft(who: AccountId, id: AssetId, units: Balance, nav: Balance) -> DispatchResult; } - -/// This is a helper trait used for constructing types in Runtime Benchmarks -pub trait MaybeTryFrom: Sized { +/// This is a helper trait only used for constructing `AssetId` types in Runtime Benchmarks +pub trait MaybeAssetIdConvert: Sized { #[cfg(feature = "runtime-benchmarks")] - fn try_from(value: T) -> Option; + fn try_convert(value: A) -> Option; } #[cfg(feature = "runtime-benchmarks")] -impl MaybeTryFrom for crate::types::AssetId { - fn try_from(value: u8) -> Option { +impl MaybeAssetIdConvert for T { + fn try_convert(value: u8) -> Option { frame_support::sp_std::convert::TryFrom::try_from(value).ok() } } #[cfg(not(feature = "runtime-benchmarks"))] -impl MaybeTryFrom for T {} +impl MaybeAssetIdConvert for T {} #[cfg(test)] mod tests { use super::*; use crate::AssetId; - fn assert_maybe_from>() {} + fn assert_maybe_from>() {} #[test] fn maybe_from_works() { - assert_maybe_from::(); - - #[cfg(feature = "runtime-benchmarks")] - assert_eq!(Some(10 as AssetId), MaybeTryFrom::try_from(10u8)); + assert_maybe_from::<()>(); } }