Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Instantiate All Assets Pallets #1908

Merged
merged 15 commits into from
Jan 18, 2023
11 changes: 6 additions & 5 deletions parachains/common/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,17 @@ where

/// A `HandleCredit` implementation that naively transfers the fees to the block author.
/// Will drop and burn the assets in case the transfer fails.
pub struct AssetsToBlockAuthor<R>(PhantomData<R>);
impl<R> HandleCredit<AccountIdOf<R>, pallet_assets::Pallet<R>> for AssetsToBlockAuthor<R>
pub struct AssetsToBlockAuthor<R, I>(PhantomData<(R, I)>);
impl<R, I> HandleCredit<AccountIdOf<R>, pallet_assets::Pallet<R, I>> for AssetsToBlockAuthor<R, I>
where
R: pallet_authorship::Config + pallet_assets::Config,
I: 'static,
R: pallet_authorship::Config + pallet_assets::Config<I>,
AccountIdOf<R>: From<polkadot_primitives::AccountId> + Into<polkadot_primitives::AccountId>,
{
fn handle_credit(credit: CreditOf<AccountIdOf<R>, pallet_assets::Pallet<R>>) {
fn handle_credit(credit: CreditOf<AccountIdOf<R>, pallet_assets::Pallet<R, I>>) {
if let Some(author) = pallet_authorship::Pallet::<R>::author() {
// In case of error: Will drop the result triggering the `OnDrop` of the imbalance.
let _ = pallet_assets::Pallet::<R>::resolve(&author, credit);
let _ = pallet_assets::Pallet::<R, I>::resolve(&author, credit);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion parachains/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ mod types {
pub type StatemintAuraId = sp_consensus_aura::ed25519::AuthorityId;

// Id used for identifying assets.
pub type AssetId = u32;
pub type AssetIdForTrustBackedAssets = u32;
}

/// Common constants of parachains.
Expand Down
31 changes: 19 additions & 12 deletions parachains/common/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,30 +76,37 @@ impl ShouldExecute for DenyReserveTransferToRelayChain {
/// A `ChargeFeeInFungibles` implementation that converts the output of
/// a given WeightToFee implementation an amount charged in
/// a particular assetId from pallet-assets
pub struct AssetFeeAsExistentialDepositMultiplier<Runtime, WeightToFee, BalanceConverter>(
PhantomData<(Runtime, WeightToFee, BalanceConverter)>,
);
impl<CurrencyBalance, Runtime, WeightToFee, BalanceConverter>
pub struct AssetFeeAsExistentialDepositMultiplier<
Runtime,
WeightToFee,
BalanceConverter,
AssetInstance: 'static,
>(PhantomData<(Runtime, WeightToFee, BalanceConverter, AssetInstance)>);
impl<CurrencyBalance, Runtime, WeightToFee, BalanceConverter, AssetInstance>
cumulus_primitives_utility::ChargeWeightInFungibles<
AccountIdOf<Runtime>,
pallet_assets::Pallet<Runtime>,
> for AssetFeeAsExistentialDepositMultiplier<Runtime, WeightToFee, BalanceConverter>
pallet_assets::Pallet<Runtime, AssetInstance>,
> for AssetFeeAsExistentialDepositMultiplier<Runtime, WeightToFee, BalanceConverter, AssetInstance>
where
Runtime: pallet_assets::Config,
Runtime: pallet_assets::Config<AssetInstance>,
WeightToFee: WeightToFeePolynomial<Balance = CurrencyBalance>,
BalanceConverter: BalanceConversion<
CurrencyBalance,
<Runtime as pallet_assets::Config>::AssetId,
<Runtime as pallet_assets::Config>::Balance,
<Runtime as pallet_assets::Config<AssetInstance>>::AssetId,
<Runtime as pallet_assets::Config<AssetInstance>>::Balance,
>,
AccountIdOf<Runtime>:
From<polkadot_primitives::AccountId> + Into<polkadot_primitives::AccountId>,
{
fn charge_weight_in_fungibles(
asset_id: <pallet_assets::Pallet<Runtime> as Inspect<AccountIdOf<Runtime>>>::AssetId,
asset_id: <pallet_assets::Pallet<Runtime, AssetInstance> as Inspect<
AccountIdOf<Runtime>,
>>::AssetId,
weight: Weight,
) -> Result<<pallet_assets::Pallet<Runtime> as Inspect<AccountIdOf<Runtime>>>::Balance, XcmError>
{
) -> Result<
<pallet_assets::Pallet<Runtime, AssetInstance> as Inspect<AccountIdOf<Runtime>>>::Balance,
XcmError,
> {
let amount = WeightToFee::weight_to_fee(&weight);
// If the amount gotten is not at least the ED, then make it be the ED of the asset
// This is to avoid burning assets and decreasing the supply
Expand Down
77 changes: 49 additions & 28 deletions parachains/runtimes/assets/statemine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ use frame_system::{
pub use parachains_common as common;
use parachains_common::{
impls::{AssetsToBlockAuthor, DealWithFees},
opaque, AccountId, AssetId, AuraId, Balance, BlockNumber, Hash, Header, Index, Signature,
AVERAGE_ON_INITIALIZE_RATIO, HOURS, MAXIMUM_BLOCK_WEIGHT, NORMAL_DISPATCH_RATIO, SLOT_DURATION,
opaque, AccountId, AssetIdForTrustBackedAssets, AuraId, Balance, BlockNumber, Hash, Header,
Index, Signature, AVERAGE_ON_INITIALIZE_RATIO, HOURS, MAXIMUM_BLOCK_WEIGHT,
NORMAL_DISPATCH_RATIO, SLOT_DURATION,
};
use xcm_config::{KsmLocation, XcmConfig};

Expand Down Expand Up @@ -231,11 +232,13 @@ parameter_types! {
pub type AssetsForceOrigin =
EitherOfDiverse<EnsureRoot<AccountId>, EnsureXcm<IsMajorityOfBody<KsmLocation, ExecutiveBody>>>;

impl pallet_assets::Config for Runtime {
pub type TrustBackedAssetsInstance = pallet_assets::Instance1;
type TrustBackedAssetsCall = pallet_assets::Call<Runtime, TrustBackedAssetsInstance>;
impl pallet_assets::Config<TrustBackedAssetsInstance> for Runtime {
type RuntimeEvent = RuntimeEvent;
type Balance = Balance;
type AssetId = AssetId;
type AssetIdParameter = codec::Compact<AssetId>;
type AssetId = AssetIdForTrustBackedAssets;
type AssetIdParameter = codec::Compact<AssetIdForTrustBackedAssets>;
type Currency = Balances;
type CreateOrigin = AsEnsureOriginWithArg<EnsureSigned<AccountId>>;
type ForceOrigin = AssetsForceOrigin;
Expand Down Expand Up @@ -333,7 +336,7 @@ impl InstanceFilter<RuntimeCall> for ProxyType {
ProxyType::NonTransfer => !matches!(
c,
RuntimeCall::Balances { .. } |
RuntimeCall::Assets { .. } |
RuntimeCall::TrustBackedAssets { .. } |
RuntimeCall::Uniques { .. }
),
ProxyType::CancelProxy => matches!(
Expand All @@ -345,23 +348,25 @@ impl InstanceFilter<RuntimeCall> for ProxyType {
ProxyType::Assets => {
matches!(
c,
RuntimeCall::Assets { .. } |
RuntimeCall::TrustBackedAssets { .. } |
RuntimeCall::Utility { .. } |
RuntimeCall::Multisig { .. } |
RuntimeCall::Uniques { .. }
)
},
ProxyType::AssetOwner => matches!(
c,
RuntimeCall::Assets(pallet_assets::Call::create { .. }) |
RuntimeCall::Assets(pallet_assets::Call::start_destroy { .. }) |
RuntimeCall::Assets(pallet_assets::Call::destroy_accounts { .. }) |
RuntimeCall::Assets(pallet_assets::Call::destroy_approvals { .. }) |
RuntimeCall::Assets(pallet_assets::Call::finish_destroy { .. }) |
RuntimeCall::Assets(pallet_assets::Call::transfer_ownership { .. }) |
RuntimeCall::Assets(pallet_assets::Call::set_team { .. }) |
RuntimeCall::Assets(pallet_assets::Call::set_metadata { .. }) |
RuntimeCall::Assets(pallet_assets::Call::clear_metadata { .. }) |
RuntimeCall::TrustBackedAssets(TrustBackedAssetsCall::create { .. }) |
RuntimeCall::TrustBackedAssets(TrustBackedAssetsCall::start_destroy { .. }) |
RuntimeCall::TrustBackedAssets(
TrustBackedAssetsCall::destroy_accounts { .. }
) | RuntimeCall::TrustBackedAssets(TrustBackedAssetsCall::destroy_approvals { .. }) |
RuntimeCall::TrustBackedAssets(TrustBackedAssetsCall::finish_destroy { .. }) |
RuntimeCall::TrustBackedAssets(
TrustBackedAssetsCall::transfer_ownership { .. }
) | RuntimeCall::TrustBackedAssets(TrustBackedAssetsCall::set_team { .. }) |
RuntimeCall::TrustBackedAssets(TrustBackedAssetsCall::set_metadata { .. }) |
RuntimeCall::TrustBackedAssets(TrustBackedAssetsCall::clear_metadata { .. }) |
RuntimeCall::Uniques(pallet_uniques::Call::create { .. }) |
RuntimeCall::Uniques(pallet_uniques::Call::destroy { .. }) |
RuntimeCall::Uniques(pallet_uniques::Call::transfer_ownership { .. }) |
Expand All @@ -378,12 +383,12 @@ impl InstanceFilter<RuntimeCall> for ProxyType {
),
ProxyType::AssetManager => matches!(
c,
RuntimeCall::Assets(pallet_assets::Call::mint { .. }) |
RuntimeCall::Assets(pallet_assets::Call::burn { .. }) |
RuntimeCall::Assets(pallet_assets::Call::freeze { .. }) |
RuntimeCall::Assets(pallet_assets::Call::thaw { .. }) |
RuntimeCall::Assets(pallet_assets::Call::freeze_asset { .. }) |
RuntimeCall::Assets(pallet_assets::Call::thaw_asset { .. }) |
RuntimeCall::TrustBackedAssets(TrustBackedAssetsCall::mint { .. }) |
RuntimeCall::TrustBackedAssets(TrustBackedAssetsCall::burn { .. }) |
RuntimeCall::TrustBackedAssets(TrustBackedAssetsCall::freeze { .. }) |
RuntimeCall::TrustBackedAssets(TrustBackedAssetsCall::thaw { .. }) |
RuntimeCall::TrustBackedAssets(TrustBackedAssetsCall::freeze_asset { .. }) |
RuntimeCall::TrustBackedAssets(TrustBackedAssetsCall::thaw_asset { .. }) |
RuntimeCall::Uniques(pallet_uniques::Call::mint { .. }) |
RuntimeCall::Uniques(pallet_uniques::Call::burn { .. }) |
RuntimeCall::Uniques(pallet_uniques::Call::freeze { .. }) |
Expand Down Expand Up @@ -541,10 +546,15 @@ impl pallet_collator_selection::Config for Runtime {

impl pallet_asset_tx_payment::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Fungibles = Assets;
type Fungibles = TrustBackedAssets;
type OnChargeAssetTransaction = pallet_asset_tx_payment::FungiblesAdapter<
pallet_assets::BalanceToAssetBalance<Balances, Runtime, ConvertInto>,
AssetsToBlockAuthor<Runtime>,
pallet_assets::BalanceToAssetBalance<
Balances,
Runtime,
ConvertInto,
TrustBackedAssetsInstance,
>,
AssetsToBlockAuthor<Runtime, TrustBackedAssetsInstance>,
>;
}

Expand Down Expand Up @@ -620,7 +630,7 @@ construct_runtime!(
Proxy: pallet_proxy::{Pallet, Call, Storage, Event<T>} = 42,

// The main stage.
Assets: pallet_assets::{Pallet, Call, Storage, Event<T>} = 50,
TrustBackedAssets: pallet_assets::<Instance1>::{Pallet, Call, Storage, Event<T>} = 50,
Uniques: pallet_uniques::{Pallet, Call, Storage, Event<T>} = 51,

#[cfg(feature = "state-trie-version-1")]
Expand Down Expand Up @@ -660,9 +670,20 @@ pub type Executive = frame_executive::Executive<
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
pallet_balances::migration::MigrateToTrackInactive<Runtime, xcm_config::CheckingAccount>,
// Note: Removed v1 storage migration. Assume this has been applied in some upgrade.
// Do not release until verified!
MigrateAssetsPallet,
>;

pub struct MigrateAssetsPallet;
impl frame_support::traits::OnRuntimeUpgrade for MigrateAssetsPallet {
fn on_runtime_upgrade() -> Weight {
use frame_support::storage::migration;
migration::move_pallet(b"Assets", b"TrustBackedAssets");
<Runtime as frame_system::Config>::DbWeight::get().writes(1)
}
}

#[cfg(feature = "runtime-benchmarks")]
#[macro_use]
extern crate frame_benchmarking;
Expand All @@ -671,7 +692,7 @@ extern crate frame_benchmarking;
mod benches {
define_benchmarks!(
[frame_system, SystemBench::<Runtime>]
[pallet_assets, Assets]
[pallet_assets, TrustBackedAssets]
[pallet_balances, Balances]
[pallet_multisig, Multisig]
[pallet_proxy, Proxy]
Expand Down
34 changes: 22 additions & 12 deletions parachains/runtimes/assets/statemine/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
// limitations under the License.

use super::{
AccountId, AssetId, Assets, Authorship, Balance, Balances, ParachainInfo, ParachainSystem,
PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, WeightToFee, XcmpQueue,
AccountId, AssetIdForTrustBackedAssets, Authorship, Balance, Balances, ParachainInfo,
ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin,
TrustBackedAssets, TrustBackedAssetsInstance, WeightToFee, XcmpQueue,
};
use frame_support::{
match_types, parameter_types,
Expand Down Expand Up @@ -48,8 +49,8 @@ parameter_types! {
pub RelayChainOrigin: RuntimeOrigin = cumulus_pallet_xcm::Origin::Relay.into();
pub Ancestry: MultiLocation = Parachain(ParachainInfo::parachain_id().into()).into();
pub const Local: MultiLocation = Here.into();
pub AssetsPalletLocation: MultiLocation =
PalletInstance(<Assets as PalletInfoAccess>::index() as u8).into();
pub TrustBackedAssetsPalletLocation: MultiLocation =
PalletInstance(<TrustBackedAssets as PalletInfoAccess>::index() as u8).into();
pub CheckingAccount: AccountId = PolkadotXcm::check_account();
}

Expand Down Expand Up @@ -82,12 +83,16 @@ pub type CurrencyTransactor = CurrencyAdapter<
/// Means for transacting assets besides the native currency on this chain.
pub type FungiblesTransactor = FungiblesAdapter<
// Use this fungibles implementation:
Assets,
TrustBackedAssets,
// Use this currency when it is a fungible asset matching the given location or name:
ConvertedConcreteAssetId<
AssetId,
AssetIdForTrustBackedAssets,
Balance,
AsPrefixedGeneralIndex<AssetsPalletLocation, AssetId, JustTry>,
AsPrefixedGeneralIndex<
TrustBackedAssetsPalletLocation,
AssetIdForTrustBackedAssets,
JustTry,
>,
JustTry,
>,
// Convert an XCM MultiLocation into a local account id:
Expand All @@ -96,7 +101,7 @@ pub type FungiblesTransactor = FungiblesAdapter<
AccountId,
// We only want to allow teleports of known assets. We use non-zero issuance as an indication
// that this asset is known.
parachains_common::impls::NonZeroIssuance<AccountId, Assets>,
parachains_common::impls::NonZeroIssuance<AccountId, TrustBackedAssets>,
// The account to use for tracking teleports.
CheckingAccount,
>;
Expand Down Expand Up @@ -160,7 +165,8 @@ pub type Barrier = DenyThenTry<
pub type AssetFeeAsExistentialDepositMultiplierFeeCharger = AssetFeeAsExistentialDepositMultiplier<
Runtime,
WeightToFee,
pallet_assets::BalanceToAssetBalance<Balances, Runtime, ConvertInto>,
pallet_assets::BalanceToAssetBalance<Balances, Runtime, ConvertInto, TrustBackedAssetsInstance>,
TrustBackedAssetsInstance,
>;

pub struct XcmConfig;
Expand All @@ -187,12 +193,16 @@ impl xcm_executor::Config for XcmConfig {
AccountId,
AssetFeeAsExistentialDepositMultiplierFeeCharger,
ConvertedConcreteAssetId<
AssetId,
AssetIdForTrustBackedAssets,
Balance,
AsPrefixedGeneralIndex<AssetsPalletLocation, AssetId, JustTry>,
AsPrefixedGeneralIndex<
TrustBackedAssetsPalletLocation,
AssetIdForTrustBackedAssets,
JustTry,
>,
JustTry,
>,
Assets,
TrustBackedAssets,
cumulus_primitives_utility::XcmFeesTo32ByteAccount<
FungiblesTransactor,
AccountId,
Expand Down
Loading