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 14, 2024
1 parent 24f1285 commit 683a1f6
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 13 deletions.
10 changes: 7 additions & 3 deletions integration-tests/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ pub mod polimec {
use super::*;
use crate::{PolimecNet, PolimecOrigin, PolimecRuntime};
use pallet_funding::AcceptedFundingAsset;
use polimec_runtime::PayMaster;
use polimec_runtime::{PayMaster, TreasuryAccount};
use xcm::v3::Parent;
use xcm_emulator::TestExt;

Expand Down Expand Up @@ -622,7 +622,7 @@ pub mod polimec {

funded_accounts.extend(accounts::init_balances().iter().cloned().map(|k| (k, INITIAL_DEPOSIT)));
funded_accounts.extend(collators::initial_authorities().iter().cloned().map(|(acc, _)| (acc, 20_005 * PLMC)));
funded_accounts.push((get_account_id_from_seed::<sr25519::Public>("TREASURY_STASH"), 20_005 * PLMC));
funded_accounts.push((TreasuryAccount::get(), 20_005 * PLMC));
funded_accounts.push((PayMaster::get(), 20_005 * PLMC));

let genesis_config = polimec_runtime::RuntimeGenesisConfig {
Expand All @@ -640,7 +640,11 @@ pub mod polimec {
(usdt_asset_id, "Local USDT".as_bytes().to_vec(), "USDT".as_bytes().to_vec(), 6),
(usdc_asset_id, "Local USDC".as_bytes().to_vec(), "USDC".as_bytes().to_vec(), 6),
],
accounts: vec![],
accounts: vec![
(dot_asset_id, TreasuryAccount::get(), 0_0_010_000_000u128),
(usdt_asset_id, TreasuryAccount::get(), 0_0_010_000_000u128),
(usdc_asset_id, TreasuryAccount::get(), 0_0_010_000_000u128),
],
},
parachain_info: polimec_runtime::ParachainInfoConfig { parachain_id: PARA_ID.into(), ..Default::default() },
session: polimec_runtime::SessionConfig {
Expand Down
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;
113 changes: 113 additions & 0 deletions integration-tests/src/tests/xcm_config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
use crate::{PolimecAccountId, PolimecBalances, PolimecCall, PolimecForeignAssets, PolimecNet, PolimecRuntime, ALICE};
use parity_scale_codec::Encode;
use polimec_runtime::{xcm_config::SupportedAssets, ExistentialDeposit, ForeignAssets, TreasuryAccount};
use sp_runtime::traits::MaybeEquivalence;
use xcm::prelude::*;
use xcm_emulator::TestExt;
use xcm_emulator::Chain;
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 dot_amount = MultiAsset { id: Concrete(MultiLocation::parent()), 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 beneficiary: PolimecAccountId = [0u8; 32].into();

let assert_reserve_asset_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 prev_treasury_balance = PolimecForeignAssets::balance(asset_id, TreasuryAccount::get());
let prev_beneficiary_balance = PolimecForeignAssets::balance(asset_id, beneficiary.clone());

let outcome = <PolimecRuntime as pallet_xcm::Config>::XcmExecutor::execute_xcm(
MultiLocation::new(1, X1(Parachain(1000))),
xcm.clone(),
fake_message_hash(&xcm),
Weight::MAX,
);
assert!(outcome.ensure_complete().is_ok());

let post_treasury_balance = PolimecForeignAssets::balance(asset_id, TreasuryAccount::get());
let post_beneficiary_balance = PolimecForeignAssets::balance(asset_id, beneficiary.clone());

let net_treasury_balance = post_treasury_balance - prev_treasury_balance;
let net_beneficiary_balance = post_beneficiary_balance - prev_beneficiary_balance;

let net_total = net_treasury_balance + net_beneficiary_balance;

assert_eq!(net_total, asset_amount);
});
};

let assert_plmc_fee_goes_to_treasury = || {
let asset_amount = 100_0_000_000_000;
let multi_asset =
MultiAsset { id: Concrete(MultiLocation::here()), fun: Fungible(asset_amount) };

let xcm = Xcm::<PolimecCall>(vec![
WithdrawAsset(vec![multi_asset.clone()].into()),
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 prev_treasury_balance = PolimecBalances::free_balance(TreasuryAccount::get());
let prev_beneficiary_balance = PolimecBalances::free_balance(beneficiary.clone());

let outcome = <PolimecRuntime as pallet_xcm::Config>::XcmExecutor::execute_xcm(
MultiLocation::new(0, X1(AccountId32 { network: None, id: PolimecNet::account_id_of(ALICE).into() })),
xcm.clone(),
fake_message_hash(&xcm),
Weight::MAX,
);
assert!(outcome.ensure_complete().is_ok());

let post_treasury_balance = PolimecBalances::free_balance(TreasuryAccount::get());
let post_beneficiary_balance = PolimecBalances::free_balance(beneficiary.clone());

let net_treasury_balance = post_treasury_balance - prev_treasury_balance;
let net_beneficiary_balance = post_beneficiary_balance - prev_beneficiary_balance;

let net_total = net_treasury_balance + net_beneficiary_balance;

assert_eq!(net_total, asset_amount);
});
};

assert_reserve_asset_fee_goes_to_treasury(dot_amount);
assert_reserve_asset_fee_goes_to_treasury(usdt_amount);
assert_reserve_asset_fee_goes_to_treasury(usdc_amount);
assert_plmc_fee_goes_to_treasury();
}
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 683a1f6

Please sign in to comment.