From 855f102141d3229e498967b1e4c7d7edb1c3dc5d Mon Sep 17 00:00:00 2001 From: valentinfernandez1 <33705477+valentinfernandez1@users.noreply.github.com> Date: Tue, 6 Jun 2023 09:03:16 -0300 Subject: [PATCH] Query account balances using `accounts_common` (#202) * query_account_balances added * fmt * Added assets-common dependency from Cumulus --------- Co-authored-by: Steve Degosserie --- Cargo.lock | 17 +++++++++++++++ Cargo.toml | 1 + primitives/xcm/src/lib.rs | 2 +- runtime/trappist/Cargo.toml | 2 ++ runtime/trappist/src/lib.rs | 33 ++++++++++++++++++++++++++++-- runtime/trappist/src/xcm_config.rs | 17 ++++++++------- 6 files changed, 60 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a08e7d74..71dac2c7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -412,6 +412,22 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b34d609dfbaf33d6889b2b7106d3ca345eacad44200913df5ba02bfd31d2ba9" +[[package]] +name = "assets-common" +version = "0.1.0" +source = "git+https://github.com/paritytech/cumulus?branch=polkadot-v0.9.40#e05c8d7f71734ed71188337c6cb0d30715f6320f" +dependencies = [ + "frame-support", + "parachains-common", + "parity-scale-codec", + "sp-api", + "sp-std", + "substrate-wasm-builder", + "xcm", + "xcm-builder", + "xcm-executor", +] + [[package]] name = "async-channel" version = "1.8.0" @@ -12363,6 +12379,7 @@ dependencies = [ name = "trappist-runtime" version = "1.0.0" dependencies = [ + "assets-common", "cumulus-pallet-aura-ext", "cumulus-pallet-dmp-queue", "cumulus-pallet-parachain-system", diff --git a/Cargo.toml b/Cargo.toml index b4d1d5a7..bf8244ff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -149,6 +149,7 @@ cumulus-primitives-parachain-inherent = { git = "https://github.com/paritytech/c cumulus-relay-chain-interface = { git = "https://github.com/paritytech/cumulus", branch = "polkadot-v0.9.40" } # Cumulus runtime dependencies +assets-common = { git = "https://github.com/paritytech/cumulus", branch = "polkadot-v0.9.40", default-features = false } cumulus-pallet-aura-ext = { git = "https://github.com/paritytech/cumulus", branch = "polkadot-v0.9.40", default-features = false } cumulus-pallet-dmp-queue = { git = "https://github.com/paritytech/cumulus", branch = "polkadot-v0.9.40", default-features = false } cumulus-pallet-parachain-system = { git = "https://github.com/paritytech/cumulus", branch = "polkadot-v0.9.40", default-features = false } diff --git a/primitives/xcm/src/lib.rs b/primitives/xcm/src/lib.rs index 0390746d..0a60e2cb 100644 --- a/primitives/xcm/src/lib.rs +++ b/primitives/xcm/src/lib.rs @@ -6,7 +6,7 @@ use frame_support::{ weights::Weight, }; use sp_runtime::DispatchResult; -use sp_std::{borrow::Borrow, marker::PhantomData, vec::Vec}; +use sp_std::{borrow::Borrow, marker::PhantomData}; use xcm::{ latest::{ AssetId::Concrete, Fungibility::Fungible, Junctions::Here, MultiAsset, MultiLocation, diff --git a/runtime/trappist/Cargo.toml b/runtime/trappist/Cargo.toml index 748b0008..72aeddc1 100644 --- a/runtime/trappist/Cargo.toml +++ b/runtime/trappist/Cargo.toml @@ -70,6 +70,7 @@ pallet-utility = { workspace = true } pallet-treasury = { workspace = true } # Cumulus dependencies +assets-common = { workspace = true } cumulus-pallet-aura-ext = { workspace = true } cumulus-pallet-dmp-queue = { workspace = true } cumulus-pallet-parachain-system = { workspace = true } @@ -156,6 +157,7 @@ std = [ "pallet-uniques/std", "pallet-utility/std", "pallet-xcm/std", + "assets-common/std", "cumulus-pallet-aura-ext/std", "cumulus-pallet-parachain-system/std", "cumulus-pallet-xcm/std", diff --git a/runtime/trappist/src/lib.rs b/runtime/trappist/src/lib.rs index 7b41e736..8e7e8a95 100644 --- a/runtime/trappist/src/lib.rs +++ b/runtime/trappist/src/lib.rs @@ -29,7 +29,7 @@ pub mod xcm_config; pub use common::AssetIdForTrustBackedAssets; use cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases; -use cumulus_primitives_core::BodyId; +use cumulus_primitives_core::{BodyId, MultiAsset}; use sp_api::impl_runtime_apis; use sp_core::{crypto::KeyTypeId, ConstU8, OpaqueMetadata}; #[cfg(any(feature = "std", test))] @@ -75,7 +75,9 @@ pub use parachains_common::{ use impls::{DealWithFees, LockdownDmpHandler, RuntimeBlackListedCalls, XcmExecutionManager}; -use xcm_config::{CollatorSelectionUpdateOrigin, RelayLocation}; +use xcm_config::{ + CollatorSelectionUpdateOrigin, RelayLocation, TrustBackedAssetsConvertedConcreteId, +}; // Polkadot imports use pallet_xcm::{EnsureXcm, IsMajorityOfBody}; @@ -907,6 +909,33 @@ impl_runtime_apis! { } } + impl assets_common::runtime_api::FungiblesApi< + Block, + AccountId, + > for Runtime + { + fn query_account_balances(account: AccountId) -> Result, assets_common::runtime_api::FungiblesAccessError> { + use assets_common::fungible_conversion::{convert, convert_balance}; + Ok([ + // collect pallet_balance + { + let balance = Balances::free_balance(account.clone()); + if balance > 0 { + vec![convert_balance::(balance)?] + } else { + vec![] + } + }, + // collect pallet_assets (TrustBackedAssets) + convert::<_, _, _, _, TrustBackedAssetsConvertedConcreteId>( + Assets::account_balances(account.clone()) + .iter() + .filter(|(_, balance)| balance > &0) + )? + ].concat().into()) + } + } + impl pallet_contracts::ContractsApi for Runtime { fn call( origin: AccountId, diff --git a/runtime/trappist/src/xcm_config.rs b/runtime/trappist/src/xcm_config.rs index 3af2b35c..6976a18e 100644 --- a/runtime/trappist/src/xcm_config.rs +++ b/runtime/trappist/src/xcm_config.rs @@ -46,9 +46,9 @@ use xcm::latest::{prelude::*, Fungibility::Fungible, MultiAsset, MultiLocation}; use xcm_builder::{ AccountId32Aliases, AllowKnownQueryResponses, AllowSubscriptionsFrom, AllowTopLevelPaidExecutionFrom, AllowUnpaidExecutionFrom, AsPrefixedGeneralIndex, - ConvertedConcreteId, CurrencyAdapter, EnsureXcmOrigin, FixedRateOfFungible, FungiblesAdapter, - IsConcrete, MintLocation, NativeAsset, NoChecking, ParentAsSuperuser, ParentIsPreset, - RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia, + ConvertedConcreteId, CurrencyAdapter, EnsureXcmOrigin, FixedRateOfFungible, FixedWeightBounds, + FungiblesAdapter, IsConcrete, MintLocation, NativeAsset, NoChecking, ParentAsSuperuser, + ParentIsPreset, RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia, SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit, UsingComponents, WeightInfoBounds, }; @@ -90,6 +90,10 @@ pub type LocationToAccountId = ( AccountId32Aliases, ); +/// `AssetId/Balancer` converter for `TrustBackedAssets` +pub type TrustBackedAssetsConvertedConcreteId = + assets_common::TrustBackedAssetsConvertedConcreteId; + /// Means for transacting the native currency on this chain. pub type LocalAssetTransactor = CurrencyAdapter< // Use this currency: @@ -109,12 +113,7 @@ pub type LocalFungiblesTransactor = FungiblesAdapter< // Use this fungibles implementation: Assets, // Use this currency when it is a fungible asset matching the given location or name: - ConvertedConcreteId< - AssetIdForTrustBackedAssets, - Balance, - AsPrefixedGeneralIndex, - JustTry, - >, + TrustBackedAssetsConvertedConcreteId, // Convert an XCM MultiLocation into a local account id: LocationToAccountId, // Our chain's account ID type (we can't get away without mentioning it explicitly):