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

Commit

Permalink
Statemint runtimes to accept sufficient assets as xcm fee payment (#1278
Browse files Browse the repository at this point in the history
)

* point to my branch

* girazoki-add-TakeFirstAssetTrader-to-utility

* Commit lock

* point at custom branch

* add new trader to statemine runtimes

* compiles

* Back to master

* Update last tomls

* Imports up

* remove non-needing imports

* FMT

* log messages properly

* Use TakeRevenue instead of HandleCredit

* Introduce xcm fee handler

* check total supply in tests

* FMT

* fix test

* Start decoupling balance calculation into different traits

* Make traits a bit more generic

* PR suggestions

* add import

* import well

* Place xcmfeesassethandler into parachains common

* fix tests

* config parameters

* Min amount to fee receiver

* Make minimum amount for block author to be at least the ED

* Doc in AssetFeeAsExistentialDepositMultiplier

* saturating sub

* make sure we dont enter twice

* FMT

* fmt again

* adapt tests

* Add doc and struct for weight refund

* Doc

* More doc

* PR suggestions

* store all info related to asset payment as multiasset

* return AssetNotFound instead of TooExpensive

* Use asset transactor to deposit fee

* uninstall from statemint

* R for RUntime and CON for BalanceConverter

* Rework logic to avoid unnecesary match and error

* Rework ED check, also in case of refund

* rework typo

* In case refund makes drop below ED, just refund the difference

* fix test westmint

* clone id

* move test imports to preamble

* move test imports to preamble

* test-utils with builderS

* lock file updated

* remove unused imports

Co-authored-by: Stephen Shelton <steve@brewcraft.org>
Co-authored-by: joe petrowski <25483142+joepetrowski@users.noreply.github.com>
Co-authored-by: joepetrowski <joe@parity.io>
  • Loading branch information
4 people authored Aug 3, 2022
1 parent acc409c commit 4d04eeb
Show file tree
Hide file tree
Showing 16 changed files with 1,209 additions and 11 deletions.
27 changes: 27 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions parachains/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ polkadot-primitives = { git = "https://github.com/paritytech/polkadot", default-
polkadot-runtime-common = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "master" }
xcm = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "master" }
xcm-executor = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "master" }
xcm-builder = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "master" }

# 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 = "master" }
Expand All @@ -60,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 = "master" }
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 4d04eeb

Please sign in to comment.