diff --git a/Cargo.lock b/Cargo.lock index 21b7693f1..f53dd6a73 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8813,6 +8813,7 @@ name = "polimec-runtime" version = "0.8.0" dependencies = [ "array-bytes", + "assets-common", "cumulus-pallet-aura-ext", "cumulus-pallet-parachain-system", "cumulus-pallet-session-benchmarking", diff --git a/Cargo.toml b/Cargo.toml index 13f7e2fce..13ac2eb44 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -151,6 +151,7 @@ pallet-message-queue = { version = "38.0.0", default-features = false } sp-weights = { version = "31.0.0", default-features = false } # FRAME +assets-common = { version = "0.14.0", default-features = false } pallet-aura = { version = "34.0.0", default-features = false } pallet-balances = { version = "36.0.0", default-features = false } pallet-assets = { version = "36.0.0", default-features = false } diff --git a/runtimes/polimec/Cargo.toml b/runtimes/polimec/Cargo.toml index a3e8f153c..6bfe409b3 100644 --- a/runtimes/polimec/Cargo.toml +++ b/runtimes/polimec/Cargo.toml @@ -41,6 +41,7 @@ on-slash-vesting.workspace = true pallet-proxy-bonding.workspace = true # Substrate +assets-common.workspace = true frame-benchmarking = { workspace = true, optional = true } frame-executive.workspace = true frame-support.workspace = true @@ -117,6 +118,7 @@ default = [ "std" ] fast-mode = [ "shared-configuration/fast-mode" ] instant-mode = [ "shared-configuration/instant-mode" ] std = [ + "assets-common/std", "cumulus-pallet-aura-ext/std", "cumulus-pallet-parachain-system/std", "cumulus-pallet-session-benchmarking/std", @@ -194,6 +196,7 @@ std = [ ] runtime-benchmarks = [ + "assets-common/runtime-benchmarks", "cumulus-pallet-parachain-system/runtime-benchmarks", "cumulus-pallet-session-benchmarking/runtime-benchmarks", "cumulus-pallet-xcmp-queue/runtime-benchmarks", diff --git a/runtimes/polimec/src/custom_migrations/funding_holds.rs b/runtimes/polimec/src/custom_migrations/funding_holds.rs deleted file mode 100644 index f32b1a905..000000000 --- a/runtimes/polimec/src/custom_migrations/funding_holds.rs +++ /dev/null @@ -1,91 +0,0 @@ -use crate::{Balance, Funding, Runtime, RuntimeHoldReason}; -use frame_support::traits::{GetStorageVersion, OnRuntimeUpgrade, VariantCount, VariantCountOf}; -use pallet_balances::IdAmount; -use pallet_funding::ProjectId; -use parity_scale_codec::{Decode, Encode}; -use scale_info::TypeInfo; -use sp_core::{MaxEncodedLen, RuntimeDebug}; -use sp_runtime::BoundedVec; -use sp_std::vec::Vec; - -#[derive(Clone, Copy, Encode, Decode, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)] -pub enum OldFundingHoldReason { - Evaluation(ProjectId), - Participation(ProjectId), -} - -#[derive(Clone, Copy, Encode, Decode, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)] -pub enum OldRuntimeHoldReason { - #[codec(index = 25u8)] - ParachainStaking(pallet_parachain_staking::HoldReason), - - #[codec(index = 41u8)] - Democracy(pallet_democracy::HoldReason), - - #[codec(index = 44u8)] - Elections(pallet_elections_phragmen::HoldReason), - - #[codec(index = 45u8)] - Preimage(pallet_preimage::HoldReason), - - #[codec(index = 80u8)] - Funding(OldFundingHoldReason), -} - -impl VariantCount for OldRuntimeHoldReason { - const VARIANT_COUNT: u32 = 2 + 1 + 1 + 1 + 2; -} - -type OldIdAmount = IdAmount; -type NewIdAmount = IdAmount; -type OldHoldsItem = BoundedVec>; -type NewHoldsItem = BoundedVec>; - -pub struct FromFundingV4Migration; -impl OnRuntimeUpgrade for FromFundingV4Migration { - fn on_runtime_upgrade() -> frame_support::weights::Weight { - let on_chain_version = Funding::on_chain_storage_version(); - if on_chain_version != 4 { - log::warn!("Funding Holds migration can be removed. Skipping it now...",); - return ::DbWeight::get().reads(1) - } - let mut items = 0; - let mut translate = |_key, old_user_holds: OldHoldsItem| -> Option { - items += 1; - log::info!("Migrating hold {:?}", items); - let mut new_user_holds = Vec::new(); - for user_hold in old_user_holds.iter() { - let new_id = match user_hold.id { - OldRuntimeHoldReason::ParachainStaking(reason) => RuntimeHoldReason::ParachainStaking(reason), - OldRuntimeHoldReason::Democracy(reason) => RuntimeHoldReason::Democracy(reason), - OldRuntimeHoldReason::Elections(reason) => RuntimeHoldReason::Elections(reason), - OldRuntimeHoldReason::Preimage(reason) => RuntimeHoldReason::Preimage(reason), - OldRuntimeHoldReason::Funding(OldFundingHoldReason::Evaluation(_)) => - RuntimeHoldReason::Funding(pallet_funding::HoldReason::Evaluation), - OldRuntimeHoldReason::Funding(OldFundingHoldReason::Participation(_)) => - RuntimeHoldReason::Funding(pallet_funding::HoldReason::Participation), - }; - new_user_holds.push(IdAmount { id: new_id, amount: user_hold.amount }) - } - let output = NewHoldsItem::try_from(new_user_holds); - - debug_assert!(output.is_ok(), "Failed to convert holds"); - if let Err(err) = &output { - log::error!( - "Holds conversion failed with err {:?} for the following user holds: {:?}", - err, - old_user_holds - ); - } - // If we failed to convert the holds, we delete them - output.ok() - }; - - pallet_balances::Holds::::translate(|key, object: OldHoldsItem| translate(key, object)); - - log::info!("Number of users migrated: {}", items); - let weight = ::DbWeight::get().reads_writes(items, items); - log::info!("holds weight: {:?}", weight); - weight - } -} diff --git a/runtimes/polimec/src/custom_migrations/mod.rs b/runtimes/polimec/src/custom_migrations/mod.rs index 1d2de6e99..79925e649 100644 --- a/runtimes/polimec/src/custom_migrations/mod.rs +++ b/runtimes/polimec/src/custom_migrations/mod.rs @@ -14,7 +14,5 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -// the generated files do not pass clippy +// The generated files do not pass clippy. #![allow(clippy::all)] - -pub mod funding_holds; diff --git a/runtimes/polimec/src/lib.rs b/runtimes/polimec/src/lib.rs index 889e38e92..2a7e0cbdc 100644 --- a/runtimes/polimec/src/lib.rs +++ b/runtimes/polimec/src/lib.rs @@ -21,6 +21,7 @@ #[cfg(feature = "std")] include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); +use assets_common::fungible_conversion::{convert, convert_balance}; use core::ops::RangeInclusive; use cumulus_pallet_parachain_system::RelayNumberMonotonicallyIncreases; use cumulus_primitives_core::{AggregateMessageOrigin, ParaId}; @@ -1131,14 +1132,6 @@ ord_parameter_types! { pub const DispenserAdminAccount: AccountId = AccountId::from(hex_literal::hex!("d85a4f58eb7dba17bc436b16f394b242271237021f7880e1ccaf36cd9a616c99")); } -// #[test] -// fn ensure_admin_account_is_correct() { -// use frame_support::traits::SortedMembers; -// use sp_core::crypto::Ss58Codec; -// let acc = AccountId::from_ss58check("5BAimacvMnhBEoc2g7PaiuEhJwmMZejq6j1ZMCpDZMHGAogz").unwrap(); -// assert_eq!(acc, DispenserAdminAccount::sorted_members()[0]); -// } - impl pallet_dispenser::Config for Runtime { type AdminOrigin = EnsureSignedBy; type BlockNumberToBalance = ConvertInto; @@ -1308,6 +1301,34 @@ mod benches { } impl_runtime_apis! { + impl assets_common::runtime_api::FungiblesApi for Runtime{ + fn query_account_balances(account: AccountId) -> Result { + Ok([ + // collect pallet_balance + { + let balance = Balances::balance(&account); + if balance > 0 { + vec![convert_balance::(balance)?] + } else { + vec![] + } + }, + // collect pallet_assets (ContributionTokens) + convert::<_, _, _, _, xcm_config::ContributionTokensConvertedConcreteId>( + ContributionTokens::account_balances(account.clone()) + .iter() + .filter(|(_, balance)| balance > &0) + )?, + // collect pallet_assets (ForeignAssets) + convert::<_, _, _, _, xcm_config::ForeignAssetsConvertedConcreteId>( + ForeignAssets::account_balances(account) + .iter() + .filter(|(_, balance)| balance > &0) + )?, + ].concat().into()) + } + } + impl sp_consensus_aura::AuraApi for Runtime { fn slot_duration() -> sp_consensus_aura::SlotDuration { sp_consensus_aura::SlotDuration::from_millis(parachains_common::SLOT_DURATION) diff --git a/runtimes/polimec/src/xcm_config.rs b/runtimes/polimec/src/xcm_config.rs index 0f419d94c..788277385 100644 --- a/runtimes/polimec/src/xcm_config.rs +++ b/runtimes/polimec/src/xcm_config.rs @@ -15,14 +15,16 @@ // along with this program. If not, see . use super::{ - AccountId, AllPalletsWithSystem, AssetId as AssetIdPalletAssets, Balance, Balances, EnsureRoot, ForeignAssets, - Funding, ParachainInfo, ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, - ToTreasury, TreasuryAccount, Vec, WeightToFee, + AccountId, AllPalletsWithSystem, AssetId as AssetIdPalletAssets, Balance, Balances, ContributionTokens, EnsureRoot, + ForeignAssets, Funding, ParachainInfo, ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, + RuntimeOrigin, ToTreasury, TreasuryAccount, Vec, WeightToFee, }; use core::marker::PhantomData; use cumulus_primitives_core::ParaId; use frame_support::{ - ensure, parameter_types, + ensure, + pallet_prelude::*, + parameter_types, traits::{ConstU32, Contains, ContainsPair, Everything, Nothing, ProcessMessageError}, weights::Weight, }; @@ -74,6 +76,11 @@ parameter_types! { /// The check account that is allowed to mint assets locally. Used for PLMC teleport /// checking once enabled. pub LocalCheckAccount: (AccountId, MintLocation) = (CheckAccount::get(), MintLocation::Local); + pub ForeignAssetsPalletIndex: u8 = ::index() as u8; + pub ForeignAssetsPalletLocation: Location = PalletInstance(ForeignAssetsPalletIndex::get()).into(); + + pub ContributionTokensPalletIndex: u8 = ::index() as u8; + pub ContributionTokensPalletLocation: Location = PalletInstance(ContributionTokensPalletIndex::get()).into(); pub DotLocation: Location = RelayLocation::get(); pub UsdtLocation: Location = Location::new(1, [Parachain(1000), PalletInstance(50), GeneralIndex(1984)]); @@ -109,6 +116,14 @@ pub type FungibleTransactor = FungibleAdapter< LocalCheckAccount, >; +/// `AssetId`/`Balance` converter for `ForeignAssets`. +pub type ForeignAssetsConvertedConcreteId = + assets_common::TrustBackedAssetsConvertedConcreteId; + +/// `AssetId`/`Balance` converter for `ContributionTokens`. +pub type ContributionTokensConvertedConcreteId = + assets_common::TrustBackedAssetsConvertedConcreteId; + // The `AssetIdPalletAssets` ids that are supported by this chain. // Currently, we only support DOT (10), USDT (1984) and USDC (1337). pub struct SupportedAssets;