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

Commit

Permalink
backport #1278
Browse files Browse the repository at this point in the history
  • Loading branch information
NachoPal authored and bkontur committed Aug 9, 2022
1 parent 38920db commit a90303a
Show file tree
Hide file tree
Showing 15 changed files with 1,157 additions and 11 deletions.
24 changes: 24 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions parachains/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ xcm-builder = { git = "https://github.com/paritytech/polkadot", default-features

# Cumulus
pallet-collator-selection = { path = "../../pallets/collator-selection", default-features = false }
cumulus-primitives-utility = { path = "../../primitives/utility", default-features = false }

[dev-dependencies]
pallet-authorship = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.27" }
Expand All @@ -61,4 +62,6 @@ std = [
"sp-io/std",
"sp-std/std",
"pallet-collator-selection/std",
"cumulus-primitives-utility/std",
"xcm-builder/std"
]
43 changes: 42 additions & 1 deletion parachains/common/src/xcm_config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
use crate::impls::AccountIdOf;
use core::marker::PhantomData;
use frame_support::{log, weights::Weight};
use frame_support::{
log,
traits::{fungibles::Inspect, tokens::BalanceConversion},
weights::{Weight, WeightToFee, WeightToFeePolynomial},
};
use xcm::latest::prelude::*;
use xcm_executor::traits::ShouldExecute;

Expand Down Expand Up @@ -66,3 +71,39 @@ impl ShouldExecute for DenyReserveTransferToRelayChain {
Ok(())
}
}

/// 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>
cumulus_primitives_utility::ChargeWeightInFungibles<
AccountIdOf<Runtime>,
pallet_assets::Pallet<Runtime>,
> for AssetFeeAsExistentialDepositMultiplier<Runtime, WeightToFee, BalanceConverter>
where
Runtime: pallet_assets::Config,
WeightToFee: WeightToFeePolynomial<Balance = CurrencyBalance>,
BalanceConverter: BalanceConversion<
CurrencyBalance,
<Runtime as pallet_assets::Config>::AssetId,
<Runtime as pallet_assets::Config>::Balance,
>,
AccountIdOf<Runtime>:
From<polkadot_primitives::v2::AccountId> + Into<polkadot_primitives::v2::AccountId>,
{
fn charge_weight_in_fungibles(
asset_id: <pallet_assets::Pallet<Runtime> as Inspect<AccountIdOf<Runtime>>>::AssetId,
weight: Weight,
) -> Result<<pallet_assets::Pallet<Runtime> 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
let asset_amount = BalanceConverter::to_asset_balance(amount, asset_id)
.map_err(|_| XcmError::TooExpensive)?;
Ok(asset_amount)
}
}
1 change: 1 addition & 0 deletions parachains/runtimes/assets/statemine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ parachains-common = { path = "../../../common", default-features = false }

[dev-dependencies]
hex-literal = "0.3.4"
asset-test-utils = { path = "../test-utils"}

[build-dependencies]
substrate-wasm-builder = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.27" }
Expand Down
33 changes: 29 additions & 4 deletions parachains/runtimes/assets/statemine/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// limitations under the License.

use super::{
AccountId, AssetId, Assets, Balance, Balances, Call, Event, Origin, ParachainInfo,
AccountId, AssetId, Assets, Authorship, Balance, Balances, Call, Event, Origin, ParachainInfo,
ParachainSystem, PolkadotXcm, Runtime, WeightToFee, XcmpQueue,
};
use frame_support::{
Expand All @@ -25,9 +25,12 @@ use frame_support::{
use pallet_xcm::XcmPassthrough;
use parachains_common::{
impls::ToStakingPot,
xcm_config::{DenyReserveTransferToRelayChain, DenyThenTry},
xcm_config::{
AssetFeeAsExistentialDepositMultiplier, DenyReserveTransferToRelayChain, DenyThenTry,
},
};
use polkadot_parachain::primitives::Sibling;
use sp_runtime::traits::ConvertInto;
use xcm::latest::prelude::*;
use xcm_builder::{
AccountId32Aliases, AllowKnownQueryResponses, AllowSubscriptionsFrom,
Expand Down Expand Up @@ -129,6 +132,7 @@ parameter_types! {
// One XCM operation is 1_000_000_000 weight - almost certainly a conservative estimate.
pub UnitWeightCost: Weight = 1_000_000_000;
pub const MaxInstructions: u32 = 100;
pub XcmAssetFeesReceiver: Option<AccountId> = Authorship::author();
}

match_types! {
Expand Down Expand Up @@ -170,8 +174,29 @@ impl xcm_executor::Config for XcmConfig {
type LocationInverter = LocationInverter<Ancestry>;
type Barrier = Barrier;
type Weigher = FixedWeightBounds<UnitWeightCost, Call, MaxInstructions>;
type Trader =
UsingComponents<WeightToFee, KsmLocation, AccountId, Balances, ToStakingPot<Runtime>>;
type Trader = (
UsingComponents<WeightToFee, KsmLocation, AccountId, Balances, ToStakingPot<Runtime>>,
cumulus_primitives_utility::TakeFirstAssetTrader<
AccountId,
AssetFeeAsExistentialDepositMultiplier<
Runtime,
WeightToFee,
pallet_assets::BalanceToAssetBalance<Balances, Runtime, ConvertInto>,
>,
ConvertedConcreteAssetId<
AssetId,
Balance,
AsPrefixedGeneralIndex<AssetsPalletLocation, AssetId, JustTry>,
JustTry,
>,
Assets,
cumulus_primitives_utility::XcmFeesTo32ByteAccount<
FungiblesTransactor,
AccountId,
XcmAssetFeesReceiver,
>,
>,
);
type ResponseHandler = PolkadotXcm;
type AssetTrap = PolkadotXcm;
type AssetClaims = PolkadotXcm;
Expand Down
Loading

0 comments on commit a90303a

Please sign in to comment.