Skip to content

Commit

Permalink
Fix xcm execution fee destinations
Browse files Browse the repository at this point in the history
  • Loading branch information
JuaniRios committed Jun 13, 2024
1 parent 24f1285 commit dcd2502
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 10 deletions.
1 change: 1 addition & 0 deletions integration-tests/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ mod governance;
mod oracle;
mod reserve_backed_transfers;
mod vest;
mod xcm_config;
68 changes: 68 additions & 0 deletions integration-tests/src/tests/xcm_config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use crate::{PolimecAccountId, PolimecCall, PolimecForeignAssets, PolimecNet, PolimecRuntime};
use parity_scale_codec::Encode;
use polimec_runtime::{xcm_config::SupportedAssets, ForeignAssets, TreasuryAccount};
use sp_runtime::traits::MaybeEquivalence;
use xcm::prelude::*;
use xcm_emulator::TestExt;

pub fn fake_message_hash<T>(message: &Xcm<T>) -> XcmHash {
message.using_encoded(sp_io::hashing::blake2_256)
}
#[test]
fn execution_fees_go_to_treasury() {
let relay = MultiLocation::parent();
let asset_hub = MultiLocation::new(1, X1(Parachain(1000)));
let dot_amount = MultiAsset { id: Concrete(relay), fun: Fungible(100_0_000_000_000) };
let usdt_amount = MultiAsset {
id: Concrete(MultiLocation {
parents: 1,
interior: X3(Parachain(1000), PalletInstance(50), GeneralIndex(1984)),
}),
fun: Fungible(100_000_000),
};
let usdc_amount = MultiAsset {
id: Concrete(MultiLocation {
parents: 1,
interior: X3(Parachain(1000), PalletInstance(50), GeneralIndex(1337)),
}),
fun: Fungible(100_000_000),
};

let plmc_amount = MultiAsset { id: Concrete(relay), fun: Fungible(100_0_000_000_000) };
let beneficiary: PolimecAccountId = [0u8; 32].into();

let assert_fee_goes_to_treasury = |multi_asset: MultiAsset| {
let asset_multilocation =
if let Concrete(asset_multilocation) = multi_asset.id { asset_multilocation } else { unreachable!() };
let asset_id = SupportedAssets::convert(&asset_multilocation).unwrap();
let asset_amount = if let Fungible(amount) = multi_asset.fun { amount } else { unreachable!() };

let xcm = Xcm::<PolimecCall>(vec![
ReserveAssetDeposited(vec![multi_asset.clone()].into()),
ClearOrigin,
BuyExecution { fees: multi_asset, weight_limit: Unlimited },
DepositAsset {
assets: WildMultiAsset::All.into(),
beneficiary: MultiLocation::new(0, X1(AccountId32 { network: None, id: beneficiary.clone().into() })),
},
])
.into();
PolimecNet::execute_with(|| {
let outcome = <PolimecRuntime as pallet_xcm::Config>::XcmExecutor::execute_xcm(
asset_hub,
xcm.clone(),
fake_message_hash(&xcm),
Weight::MAX,
);
assert!(outcome.ensure_complete().is_ok());

let treasury_balance = PolimecForeignAssets::balance(asset_id, TreasuryAccount::get());
let beneficiary_balance = PolimecForeignAssets::balance(asset_id, beneficiary.clone());
assert_eq!(asset_amount, treasury_balance + beneficiary_balance);
});
};

assert_fee_goes_to_treasury(dot_amount);
assert_fee_goes_to_treasury(usdt_amount);
assert_fee_goes_to_treasury(usdc_amount);
}
12 changes: 10 additions & 2 deletions runtimes/polimec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ use frame_support::{
traits::{
fungible::{Credit, HoldConsideration, Inspect},
tokens::{self, PayFromAccount, UnityAssetBalanceConversion},
AsEnsureOriginWithArg, ConstU32, Contains, EitherOfDiverse, InstanceFilter, LinearStoragePrice, PrivilegeCmp,
TransformOrigin,
AsEnsureOriginWithArg, ConstU32, Contains, Currency, EitherOfDiverse, InstanceFilter, LinearStoragePrice,
PrivilegeCmp, TransformOrigin,
},
weights::{ConstantMultiplier, Weight},
};
use frame_system::{EnsureRoot, EnsureRootWithSuccess, EnsureSigned, EnsureSignedBy};
use pallet_balances::NegativeImbalance;
use pallet_democracy::GetElectorate;
use pallet_funding::DaysToBlocks;
use pallet_treasury::NegativeImbalanceOf;

use parachains_common::{
message_queue::{NarrowOriginToSibling, ParaIdToSibling},
Expand Down Expand Up @@ -551,6 +553,12 @@ impl tokens::imbalance::OnUnbalanced<CreditOf<Runtime>> for ToTreasury {
let _ = <Balances as tokens::fungible::Balanced<AccountId>>::resolve(&treasury_account, amount);
}
}
impl tokens::imbalance::OnUnbalanced<NegativeImbalance<Runtime>> for ToTreasury {
fn on_nonzero_unbalanced(amount: NegativeImbalance<Runtime>) {
let treasury_account = Treasury::account_id();
<Balances as Currency<AccountId>>::resolve_creating(&treasury_account, amount);
}
}

parameter_types! {
pub TreasuryAccount: AccountId = Treasury::account_id();
Expand Down
18 changes: 10 additions & 8 deletions runtimes/polimec/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@

use super::{
AccountId, AllPalletsWithSystem, AssetId as AssetIdPalletAssets, Balance, Balances, EnsureRoot, ForeignAssets,
ParachainInfo, ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, Vec, WeightToFee,
XcmpQueue,
ParachainInfo, ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, ToTreasury,
TreasuryAccount, Vec, WeightToFee, XcmpQueue,
};
use core::marker::PhantomData;
use core::{iter::Take, marker::PhantomData};
use frame_support::{
ensure, match_types, parameter_types,
traits::{ConstU32, Contains, ContainsPair, Everything, Nothing, ProcessMessageError},
weights::Weight,
};
use pallet_balances::NegativeImbalance;
use pallet_xcm::XcmPassthrough;
use polimec_xcm_executor::{
polimec_traits::{JustTry, Properties, ShouldExecute},
Expand Down Expand Up @@ -261,7 +262,8 @@ pub type Reserves = AssetHubAssetsAsReserve;
/// ForeignAssetsAdapter is a FungiblesAdapter that allows for transacting foreign assets.
/// Currently we only support DOT, USDT and USDC.
pub type AssetTransactors = (FungibleTransactor, ForeignAssetsAdapter);

pub type TakeRevenueToTreasury =
cumulus_primitives_utility::XcmFeesTo32ByteAccount<AssetTransactors, AccountId, TreasuryAccount>;
pub struct XcmConfig;
impl polimec_xcm_executor::Config for XcmConfig {
type Aliasers = ();
Expand Down Expand Up @@ -290,10 +292,10 @@ impl polimec_xcm_executor::Config for XcmConfig {
type SubscriptionService = PolkadotXcm;
type Trader = (
// TODO: weight to fee has to be carefully considered. For now use default
UsingComponents<WeightToFee, HereLocation, AccountId, Balances, ToAuthor<Runtime>>,
FixedRateOfFungible<UsdtTraderParams, ()>,
FixedRateOfFungible<DotTraderParams, ()>,
FixedRateOfFungible<UsdcTraderParams, ()>,
UsingComponents<WeightToFee, HereLocation, AccountId, Balances, ToTreasury>,
FixedRateOfFungible<UsdtTraderParams, TakeRevenueToTreasury>,
FixedRateOfFungible<DotTraderParams, TakeRevenueToTreasury>,
FixedRateOfFungible<UsdcTraderParams, TakeRevenueToTreasury>,
);
type UniversalAliases = Nothing;
type UniversalLocation = UniversalLocation;
Expand Down

0 comments on commit dcd2502

Please sign in to comment.