Skip to content

Commit 9ef2bcc

Browse files
committed
fix(xcm): charge delivery fees (#457)
* resolve rebase conflicts * fix(xcm): reserve_transfer_native_asset_from_para_to_system_para accounts for delivery fees * fix(integration-tests): handle delivery_fees only with mainnet feature on fmt after rebase fmt changes after rebase fmt
1 parent 109493f commit 9ef2bcc

File tree

5 files changed

+73
-25
lines changed

5 files changed

+73
-25
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

integration-tests/src/lib.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ use emulated_integration_tests_common::{
2222
};
2323
use frame_support::{pallet_prelude::Weight, sp_runtime::DispatchResult};
2424
use pop_runtime_common::Balance;
25+
#[cfg(not(feature = "mainnet"))]
2526
use pop_runtime_devnet::config::xcm::XcmConfig as PopNetworkXcmConfig;
27+
#[cfg(feature = "mainnet")]
28+
use pop_runtime_mainnet::config::xcm::XcmConfig as PopNetworkXcmConfig;
2629
use xcm::prelude::*;
2730

2831
mod chains;
@@ -313,7 +316,19 @@ fn reserve_transfer_native_asset_from_para_to_system_para() {
313316
let destination = PopNetworkPara::sibling_location_of(AssetHubPara::para_id());
314317
let beneficiary_id = AssetHubParaReceiver::get(); // bob on asset hub
315318
let amount_to_send = PopNetworkPara::account_data_of(PopNetworkParaReceiver::get()).free; // bob on pop balance
316-
let assets = (Parent, amount_to_send).into();
319+
let assets: Assets = (Parent, amount_to_send).into();
320+
321+
let delivery_fees = PopNetworkPara::execute_with(|| {
322+
xcm_helpers::teleport_assets_delivery_fees::<
323+
<PopNetworkXcmConfig as xcm_executor::Config>::XcmSender,
324+
>(
325+
assets.clone(), 0, Unlimited, Location::from(beneficiary_id.clone()), destination.clone()
326+
)
327+
});
328+
#[cfg(feature = "mainnet")]
329+
let amount_to_send = amount_to_send - (ASSET_HUB_ED + delivery_fees);
330+
#[cfg(feature = "mainnet")]
331+
let assets: Assets = (Parent, amount_to_send).into();
317332

318333
let test_args = TestContext {
319334
sender: PopNetworkParaReceiver::get(), // bob on pop
@@ -341,16 +356,11 @@ fn reserve_transfer_native_asset_from_para_to_system_para() {
341356
let sender_balance_after = test.sender.balance;
342357
let receiver_balance_after = test.receiver.balance;
343358

344-
let delivery_fees = PopNetworkPara::execute_with(|| {
345-
xcm_helpers::teleport_assets_delivery_fees::<
346-
<PopNetworkXcmConfig as xcm_executor::Config>::XcmSender,
347-
>(
348-
test.args.assets.clone(), 0, test.args.weight_limit, test.args.beneficiary, test.args.dest
349-
)
350-
});
351-
352359
// Sender's balance is reduced
353360
assert_eq!(sender_balance_before - amount_to_send - delivery_fees, sender_balance_after);
361+
// Sender's balance is the remaining ED.
362+
#[cfg(feature = "mainnet")]
363+
assert_eq!(ASSET_HUB_ED, sender_balance_after);
354364
// Receiver's balance is increased
355365
assert!(receiver_balance_after > receiver_balance_before);
356366
// Receiver's balance increased by `amount_to_send - delivery_fees - bought_execution`;

runtime/mainnet/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ parachains-common.workspace = true
8787
enumflags2 = "0.7.9"
8888
env_logger = "0.11.2"
8989
hex = "0.4.3"
90+
polkadot-runtime-parachains.workspace = true
9091

9192
[features]
9293
default = [ "std" ]

runtime/mainnet/src/config/xcm.rs

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use parachains_common::{
1616
xcm_config::ParentRelayOrSiblingParachains,
1717
};
1818
use polkadot_parachain_primitives::primitives::Sibling;
19-
use polkadot_runtime_common::xcm_sender::NoPriceForMessageDelivery;
19+
use polkadot_runtime_common::xcm_sender::ExponentialPrice;
2020
use sp_runtime::{traits::AccountIdConversion, Vec};
2121
use xcm::latest::prelude::*;
2222
use xcm_builder::{
@@ -31,10 +31,16 @@ use xcm_builder::{
3131
use xcm_executor::XcmExecutor;
3232

3333
use crate::{
34-
config::{governance::SudoAddress, monetary::fee::WeightToFee, system::RuntimeBlockWeights},
35-
AccountId, AllPalletsWithSystem, Balances, ParachainInfo, ParachainSystem,
36-
PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin,
37-
XcmpQueue, Perbill,
34+
config::{
35+
monetary::{
36+
fee::{WeightToFee, CENTS},
37+
TransactionByteFee,
38+
},
39+
system::RuntimeBlockWeights,
40+
},
41+
AccountId, AllPalletsWithSystem, Balances, MessageQueue, PalletId, ParachainInfo,
42+
ParachainSystem, Perbill, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin,
43+
XcmpQueue,
3844
};
3945

4046
parameter_types! {
@@ -46,6 +52,7 @@ parameter_types! {
4652
pub MessageQueueIdleServiceWeight: Weight = Perbill::from_percent(20) * RuntimeBlockWeights::get().max_block;
4753
pub MessageQueueServiceWeight: Weight = Perbill::from_percent(35) * RuntimeBlockWeights::get().max_block;
4854
pub TreasuryAccount: AccountId = PalletId(*b"treasury").into_account_truncating();
55+
pub const BaseDeliveryFee: u128 = CENTS.saturating_mul(3);
4956
}
5057

5158
impl pallet_message_queue::Config for Runtime {
@@ -261,7 +268,8 @@ impl cumulus_pallet_xcmp_queue::Config for Runtime {
261268
// Limit the number of HRMP channels.
262269
// note: https://github.com/polkadot-fellows/runtimes/blob/76d1fa680d00c3e447e40199e7b2250862ad4bfa/system-parachains/asset-hubs/asset-hub-polkadot/src/lib.rs#L692C2-L693C90
263270
type MaxPageSize = ConstU32<{ 103 * 1024 }>;
264-
type PriceForSiblingDelivery = NoPriceForMessageDelivery<ParaId>;
271+
type PriceForSiblingDelivery =
272+
ExponentialPrice<RelayLocation, BaseDeliveryFee, TransactionByteFee, XcmpQueue>;
265273
type RuntimeEvent = RuntimeEvent;
266274
type VersionWrapper = PolkadotXcm;
267275
type WeightInfo = cumulus_pallet_xcmp_queue::weights::SubstrateWeight<Runtime>;
@@ -273,9 +281,19 @@ impl cumulus_pallet_xcmp_queue::Config for Runtime {
273281
mod tests {
274282
use std::any::TypeId;
275283

284+
use polkadot_runtime_common::xcm_sender::*;
285+
use polkadot_runtime_parachains::FeeTracker;
286+
use sp_runtime::FixedPointNumber;
276287
use xcm_executor::traits::{FeeManager, FeeReason};
277288

278289
use super::*;
290+
use crate::System;
291+
292+
fn new_test_ext() -> sp_io::TestExternalities {
293+
let mut ext = sp_io::TestExternalities::new_empty();
294+
ext.execute_with(|| System::set_block_number(1));
295+
ext
296+
}
279297

280298
mod reserves_config {
281299
use super::*;
@@ -338,6 +356,7 @@ mod tests {
338356
));
339357
}
340358
}
359+
341360
mod message_queue {
342361
use super::*;
343362

@@ -1038,13 +1057,35 @@ mod tests {
10381057
}
10391058

10401059
#[test]
1041-
#[ignore]
10421060
fn price_for_sibling_delivery() {
10431061
assert_eq!(
10441062
TypeId::of::<<Runtime as cumulus_pallet_xcmp_queue::Config>::PriceForSiblingDelivery>(
10451063
),
1046-
TypeId::of::<NoPriceForMessageDelivery<ParaId>>()
1064+
TypeId::of::<
1065+
ExponentialPrice<RelayLocation, BaseDeliveryFee, TransactionByteFee, XcmpQueue>,
1066+
>()
10471067
);
1068+
1069+
new_test_ext().execute_with(|| {
1070+
type ExponentialDeliveryPrice =
1071+
ExponentialPrice<RelayLocation, BaseDeliveryFee, TransactionByteFee, XcmpQueue>;
1072+
let id: ParaId = 420.into();
1073+
let b: u128 = BaseDeliveryFee::get();
1074+
let m: u128 = TransactionByteFee::get();
1075+
1076+
// F * (B + msg_length * M)
1077+
// A: RelayLocation
1078+
// B: BaseDeliveryFee
1079+
// M: TransactionByteFee
1080+
// F: XcmpQueue
1081+
//
1082+
// message_length = 1
1083+
let result: u128 = XcmpQueue::get_fee_factor(id).saturating_mul_int(b + m);
1084+
assert_eq!(
1085+
ExponentialDeliveryPrice::price_for_delivery(id, &Xcm(vec![])),
1086+
(RelayLocation::get(), result).into()
1087+
);
1088+
})
10481089
}
10491090

10501091
#[test]

runtime/mainnet/src/lib.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,16 @@ extern crate alloc;
1515
use alloc::{borrow::Cow, vec::Vec};
1616

1717
pub use apis::{RuntimeApi, RUNTIME_API_VERSIONS};
18-
use config::{
19-
monetary::deposit,
20-
system::{ConsensusHook, RuntimeBlockWeights},
21-
xcm::XcmOriginToTransactDispatchOrigin,
22-
};
23-
use cumulus_primitives_core::{AggregateMessageOrigin, ParaId};
18+
use config::{monetary::deposit, system::ConsensusHook};
19+
use cumulus_primitives_core::AggregateMessageOrigin;
2420
use cumulus_primitives_storage_weight_reclaim::StorageWeightReclaim;
2521
use frame_metadata_hash_extension::CheckMetadataHash;
2622
use frame_support::{
2723
dispatch::DispatchClass,
2824
parameter_types,
2925
traits::{
3026
fungible::HoldConsideration, tokens::imbalance::ResolveTo, ConstBool, ConstU32, ConstU64,
31-
ConstU8, Contains, EitherOfDiverse, EverythingBut, LinearStoragePrice, VariantCountOf,
32-
EqualPrivilegeOnly, TransformOrigin,
27+
ConstU8, EqualPrivilegeOnly, LinearStoragePrice, VariantCountOf,
3328
},
3429
weights::{ConstantMultiplier, Weight},
3530
PalletId,

0 commit comments

Comments
 (0)