Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
Query account balances using accounts_common (#202)
Browse files Browse the repository at this point in the history
* query_account_balances added

* fmt

* Added assets-common dependency from Cumulus

---------

Co-authored-by: Steve Degosserie <steve@parity.io>
  • Loading branch information
valentinfernandez1 and Steve Degosserie authored Jun 6, 2023
1 parent 2e3c910 commit 855f102
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 12 deletions.
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion primitives/xcm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions runtime/trappist/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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",
Expand Down
33 changes: 31 additions & 2 deletions runtime/trappist/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand Down Expand Up @@ -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};
Expand Down Expand Up @@ -907,6 +909,33 @@ impl_runtime_apis! {
}
}

impl assets_common::runtime_api::FungiblesApi<
Block,
AccountId,
> for Runtime
{
fn query_account_balances(account: AccountId) -> Result<Vec<MultiAsset>, 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::<RelayLocation, 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<Block, AccountId, Balance, BlockNumber, Hash> for Runtime {
fn call(
origin: AccountId,
Expand Down
17 changes: 8 additions & 9 deletions runtime/trappist/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -90,6 +90,10 @@ pub type LocationToAccountId = (
AccountId32Aliases<RelayNetwork, AccountId>,
);

/// `AssetId/Balancer` converter for `TrustBackedAssets`
pub type TrustBackedAssetsConvertedConcreteId =
assets_common::TrustBackedAssetsConvertedConcreteId<AssetsPalletLocation, Balance>;

/// Means for transacting the native currency on this chain.
pub type LocalAssetTransactor = CurrencyAdapter<
// Use this currency:
Expand All @@ -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<AssetsPalletLocation, AssetIdForTrustBackedAssets, JustTry>,
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):
Expand Down

0 comments on commit 855f102

Please sign in to comment.