FRAME: Unity Balance Conversion for Different IDs of Native Asset (#3659)

Introduce types to define 1:1 balance conversion for different relative
asset ids/locations of native asset.

Examples:
native asset on Asset Hub presented as `VersionedLocatableAsset` type in
the context of Relay Chain is
```
{
  `location`: (0, Parachain(1000)),
  `asset_id`: (1, Here),
}
```
and it's balance should be converted 1:1 by implementations of
`ConversionToAssetBalance` trait.

---------

Co-authored-by: Branislav Kontur <bkontur@gmail.com>
This commit is contained in:
Muharem
2024-04-16 18:11:14 +02:00
committed by GitHub
parent 753bf2d860
commit 6f3d890ed3
21 changed files with 790 additions and 26 deletions
+21 -1
View File
@@ -19,7 +19,7 @@
use frame_support::traits::{
fungible::{Balanced, Credit},
tokens::imbalance::ResolveTo,
Imbalance, OnUnbalanced,
Contains, ContainsPair, Imbalance, OnUnbalanced,
};
use pallet_treasury::TreasuryAccountId;
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
@@ -156,6 +156,26 @@ impl TryConvert<&VersionedLocation, xcm::latest::Location> for VersionedLocation
}
}
/// Adapter for [`Contains`] trait to match [`VersionedLocatableAsset`] type converted to the latest
/// version of itself where it's location matched by `L` and it's asset id by `A` parameter types.
pub struct ContainsParts<C>(core::marker::PhantomData<C>);
impl<C> Contains<VersionedLocatableAsset> for ContainsParts<C>
where
C: ContainsPair<xcm::latest::Location, xcm::latest::Location>,
{
fn contains(asset: &VersionedLocatableAsset) -> bool {
use VersionedLocatableAsset::*;
let (location, asset_id) = match asset.clone() {
V3 { location, asset_id } => match (location.try_into(), asset_id.try_into()) {
(Ok(l), Ok(a)) => (l, a),
_ => return false,
},
V4 { location, asset_id } => (location, asset_id),
};
C::contains(&location, &asset_id.0)
}
}
#[cfg(feature = "runtime-benchmarks")]
pub mod benchmarks {
use super::VersionedLocatableAsset;
+20 -8
View File
@@ -25,7 +25,10 @@ use beefy_primitives::{
ecdsa_crypto::{AuthorityId as BeefyId, Signature as BeefySignature},
mmr::{BeefyDataProvider, MmrLeafVersion},
};
use frame_support::dynamic_params::{dynamic_pallet_params, dynamic_params};
use frame_support::{
dynamic_params::{dynamic_pallet_params, dynamic_params},
traits::FromContains,
};
use pallet_nis::WithMaximumOf;
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use primitives::{
@@ -40,7 +43,8 @@ use rococo_runtime_constants::system_parachain::BROKER_ID;
use runtime_common::{
assigned_slots, auctions, claims, crowdloan, identity_migrator, impl_runtime_weights,
impls::{
LocatableAssetConverter, ToAuthor, VersionedLocatableAsset, VersionedLocationConverter,
ContainsParts, LocatableAssetConverter, ToAuthor, VersionedLocatableAsset,
VersionedLocationConverter,
},
paras_registrar, paras_sudo_wrapper, prod_or_fast, slots,
traits::{Leaser, OnSwap},
@@ -74,10 +78,10 @@ use frame_support::{
genesis_builder_helper::{build_state, get_preset},
parameter_types,
traits::{
fungible::HoldConsideration, Contains, EitherOf, EitherOfDiverse, EnsureOrigin,
EnsureOriginWithArg, EverythingBut, InstanceFilter, KeyOwnerProofSystem,
LinearStoragePrice, PrivilegeCmp, ProcessMessage, ProcessMessageError, StorageMapShim,
WithdrawReasons,
fungible::HoldConsideration, tokens::UnityOrOuterConversion, Contains, EitherOf,
EitherOfDiverse, EnsureOrigin, EnsureOriginWithArg, EverythingBut, InstanceFilter,
KeyOwnerProofSystem, LinearStoragePrice, PrivilegeCmp, ProcessMessage, ProcessMessageError,
StorageMapShim, WithdrawReasons,
},
weights::{ConstantMultiplier, WeightMeter, WeightToFee as _},
PalletId,
@@ -87,7 +91,7 @@ use pallet_grandpa::{fg_primitives, AuthorityId as GrandpaId};
use pallet_identity::legacy::IdentityInfo;
use pallet_session::historical as session_historical;
use pallet_transaction_payment::{FeeDetails, FungibleAdapter, RuntimeDispatchInfo};
use sp_core::{ConstU128, OpaqueMetadata, H256};
use sp_core::{ConstU128, ConstU8, OpaqueMetadata, H256};
use sp_runtime::{
create_runtime_str, generic, impl_opaque_keys,
traits::{
@@ -523,7 +527,15 @@ impl pallet_treasury::Config for Runtime {
LocatableAssetConverter,
VersionedLocationConverter,
>;
type BalanceConverter = AssetRate;
type BalanceConverter = UnityOrOuterConversion<
ContainsParts<
FromContains<
xcm_builder::IsChildSystemParachain<ParaId>,
xcm_builder::IsParentsOnly<ConstU8<1>>,
>,
>,
AssetRate,
>;
type PayoutPeriod = PayoutSpendPeriod;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = runtime_common::impls::benchmarks::TreasuryArguments;
+15 -6
View File
@@ -31,9 +31,9 @@ use frame_support::{
genesis_builder_helper::{build_state, get_preset},
parameter_types,
traits::{
fungible::HoldConsideration, ConstU32, Contains, EitherOf, EitherOfDiverse, EverythingBut,
InstanceFilter, KeyOwnerProofSystem, LinearStoragePrice, ProcessMessage,
ProcessMessageError, WithdrawReasons,
fungible::HoldConsideration, tokens::UnityOrOuterConversion, ConstU32, Contains, EitherOf,
EitherOfDiverse, EverythingBut, FromContains, InstanceFilter, KeyOwnerProofSystem,
LinearStoragePrice, ProcessMessage, ProcessMessageError, WithdrawReasons,
},
weights::{ConstantMultiplier, WeightMeter, WeightToFee as _},
PalletId,
@@ -57,7 +57,8 @@ use runtime_common::{
elections::OnChainAccuracy,
identity_migrator, impl_runtime_weights,
impls::{
LocatableAssetConverter, ToAuthor, VersionedLocatableAsset, VersionedLocationConverter,
ContainsParts, LocatableAssetConverter, ToAuthor, VersionedLocatableAsset,
VersionedLocationConverter,
},
paras_registrar, paras_sudo_wrapper, prod_or_fast, slots,
traits::{Leaser, OnSwap},
@@ -80,7 +81,7 @@ use runtime_parachains::{
shared as parachains_shared,
};
use scale_info::TypeInfo;
use sp_core::{OpaqueMetadata, RuntimeDebug, H256};
use sp_core::{ConstU8, OpaqueMetadata, RuntimeDebug, H256};
use sp_runtime::{
create_runtime_str,
curve::PiecewiseLinear,
@@ -712,7 +713,15 @@ impl pallet_treasury::Config for Runtime {
LocatableAssetConverter,
VersionedLocationConverter,
>;
type BalanceConverter = AssetRate;
type BalanceConverter = UnityOrOuterConversion<
ContainsParts<
FromContains<
xcm_builder::IsChildSystemParachain<ParaId>,
xcm_builder::IsParentsOnly<ConstU8<1>>,
>,
>,
AssetRate,
>;
type PayoutPeriod = PayoutSpendPeriod;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = runtime_common::impls::benchmarks::TreasuryArguments;
+23
View File
@@ -322,6 +322,29 @@ impl<ParaId: IsSystem + From<u32>> Contains<Location> for IsChildSystemParachain
}
}
/// Matches if the given location is a system-level sibling parachain.
pub struct IsSiblingSystemParachain<ParaId, SelfParaId>(PhantomData<(ParaId, SelfParaId)>);
impl<ParaId: IsSystem + From<u32> + Eq, SelfParaId: Get<ParaId>> Contains<Location>
for IsSiblingSystemParachain<ParaId, SelfParaId>
{
fn contains(l: &Location) -> bool {
matches!(
l.unpack(),
(1, [Junction::Parachain(id)])
if SelfParaId::get() != ParaId::from(*id) && ParaId::from(*id).is_system(),
)
}
}
/// Matches if the given location contains only the specified amount of parents and no interior
/// junctions.
pub struct IsParentsOnly<Count>(PhantomData<Count>);
impl<Count: Get<u8>> Contains<Location> for IsParentsOnly<Count> {
fn contains(t: &Location) -> bool {
t.contains_parents_only(Count::get())
}
}
/// Allows only messages if the generic `ResponseHandler` expects them via `expecting_response`.
pub struct AllowKnownQueryResponses<ResponseHandler>(PhantomData<ResponseHandler>);
impl<ResponseHandler: OnResponse> ShouldExecute for AllowKnownQueryResponses<ResponseHandler> {
+2 -2
View File
@@ -37,8 +37,8 @@ mod barriers;
pub use barriers::{
AllowExplicitUnpaidExecutionFrom, AllowKnownQueryResponses, AllowSubscriptionsFrom,
AllowTopLevelPaidExecutionFrom, AllowUnpaidExecutionFrom, DenyReserveTransferToRelayChain,
DenyThenTry, IsChildSystemParachain, RespectSuspension, TakeWeightCredit, TrailingSetTopicAsId,
WithComputedOrigin,
DenyThenTry, IsChildSystemParachain, IsParentsOnly, IsSiblingSystemParachain,
RespectSuspension, TakeWeightCredit, TrailingSetTopicAsId, WithComputedOrigin,
};
mod controller;