Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "393 use fee per second from asset metadata in xcm config" #431

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions Cargo.lock

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

3 changes: 1 addition & 2 deletions node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,9 +537,8 @@ fn foucoco_genesis(

let token_balances = balances
.iter()
.flat_map(|k| vec![(k.0.clone(), XCM(0), 10*u128::pow(10, 18)), (k.0.clone(), XCM(6), 10*u128::pow(10, 18)), (k.0.clone(), CurrencyId::StellarNative, 10*u128::pow(10, 18))])
.flat_map(|k| vec![(k.0.clone(), XCM(0), u128::pow(10, 18))])
.collect();


let stakers: Vec<_> = invulnerables
.iter()
Expand Down
1 change: 0 additions & 1 deletion node/src/constants/foucoco.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ pub const COLLATOR_ADDITIONAL: Balance = 10 * UNIT;

pub const OFF_CHAIN_WORKER_ADDRESS: &str = "6m69vWMouLarYCbJGJisVaDDpfNGETkD5hsDWf2T7osW4Cn1";


pub const TOKEN_DECIMALS: u32 = 12;

pub const INITIAL_SUDO_SIGNATORIES: [&str; 5] = [
Expand Down
6 changes: 0 additions & 6 deletions package-lock.json

This file was deleted.

83 changes: 69 additions & 14 deletions runtime/amplitude/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use core::marker::PhantomData;
use frame_support::{
log, match_types, parameter_types,
traits::{ContainsPair, Everything, Nothing},
weights::{Weight, WeightToFee as WeightToFeeTrait},
};
use orml_asset_registry::{AssetRegistryTrader, FixedRateAssetRegistryTrader};
use orml_traits::{
location::{RelativeReserveProvider, Reserve},
parameter_type_with_key,
Expand All @@ -16,15 +16,21 @@ use sp_runtime::traits::Convert;
use xcm::latest::{prelude::*, Weight as XCMWeight};
use xcm_builder::{
AccountId32Aliases, AllowKnownQueryResponses, AllowSubscriptionsFrom,
AllowTopLevelPaidExecutionFrom, AllowUnpaidExecutionFrom, EnsureXcmOrigin, FixedWeightBounds,
ParentIsPreset, RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia,
SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit,
AllowTopLevelPaidExecutionFrom, AllowUnpaidExecutionFrom, ConvertedConcreteId, EnsureXcmOrigin,
FixedWeightBounds, ParentIsPreset, RelayChainAsNative, SiblingParachainAsNative,
SiblingParachainConvertsVia, SignedAccountId32AsNative, SignedToAccountId32,
SovereignSignedViaLocation, TakeWeightCredit,
};
use xcm_executor::{
traits::{JustTry, ShouldExecute},
XcmExecutor,
};
use xcm_executor::{traits::ShouldExecute, XcmExecutor};

use runtime_common::{parachains::kusama::asset_hub, asset_registry::FixedConversionRateProvider};
use runtime_common::{parachains::kusama::asset_hub, RelativeValue};

use cumulus_primitives_utility::XcmFeesTo32ByteAccount;
use cumulus_primitives_utility::{
ChargeWeightInFungibles, TakeFirstAssetTrader, XcmFeesTo32ByteAccount,
};

use crate::{
assets::{
Expand All @@ -35,9 +41,9 @@ use crate::{
};

use super::{
AccountId, AmplitudeTreasuryAccount, AssetRegistry, Balance, Balances, Currencies, CurrencyId,
ParachainInfo, ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin,
XcmpQueue,
AccountId, AmplitudeTreasuryAccount, Balance, Balances, Currencies, CurrencyId, ParachainInfo,
ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, Tokens,
WeightToFee, XcmpQueue,
};

parameter_types! {
Expand Down Expand Up @@ -107,6 +113,23 @@ impl Convert<MultiAsset, Option<CurrencyId>> for CurrencyIdConvert {
}
}

type RelativeValueOf = RelativeValue<Balance>;

pub struct RelayRelativeValue;
impl RelayRelativeValue {
fn get_relative_value(id: CurrencyId) -> Option<RelativeValueOf> {
match id {
CurrencyId::XCM(index) => match index {
xcm_assets::RELAY_KSM => Some(RelativeValueOf { num: 100, denominator: 1 }),
xcm_assets::ASSETHUB_USDT => Some(RelativeValueOf { num: 20, denominator: 4 }),
_ => None,
},
CurrencyId::Native => Some(RelativeValueOf { num: 1, denominator: 1 }),
_ => Some(RelativeValueOf { num: 1, denominator: 1 }),
}
}
}

/// Convert an incoming `MultiLocation` into a `CurrencyId` if possible.
/// Here we need to know the canonical representation of all the tokens we handle in order to
/// correctly convert their `MultiLocation` representation into our internal `CurrencyId` type.
Expand Down Expand Up @@ -177,6 +200,13 @@ parameter_types! {
pub const MaxAssetsForTransfer: usize = 2;
}

match_types! {
pub type ParentOrParentsExecutivePlurality: impl Contains<MultiLocation> = {
MultiLocation { parents: 1, interior: Here } |
MultiLocation { parents: 1, interior: X1(Plurality { id: BodyId::Executive, .. }) }
};
}

//TODO: move DenyThenTry to polkadot's xcm module.
/// Deny executing the xcm message if it matches any of the Deny filter regardless of anything else.
/// If it passes the Deny, and matches one of the Allow cases then it is let through.
Expand Down Expand Up @@ -258,10 +288,35 @@ pub type Barrier = (
AllowSubscriptionsFrom<Everything>,
);

pub type Traders = AssetRegistryTrader<
FixedRateAssetRegistryTrader<FixedConversionRateProvider<AssetRegistry>>,
XcmFeesTo32ByteAccount<LocalAssetTransactor, AccountId, AmplitudeTreasuryAccount>,
>;
pub struct ChargeWeightInFungiblesImplementation;
impl ChargeWeightInFungibles<AccountId, Tokens> for ChargeWeightInFungiblesImplementation {
fn charge_weight_in_fungibles(
asset_id: CurrencyId,
weight: Weight,
) -> Result<Balance, XcmError> {
let amount = <WeightToFee as WeightToFeeTrait>::weight_to_fee(&weight);

if let Some(relative_value) = RelayRelativeValue::get_relative_value(asset_id) {
let adjusted_amount =
RelativeValue::<Balance>::divide_by_relative_value(amount, relative_value);
log::info!("amount to be charged: {:?} in asset: {:?}", adjusted_amount, asset_id);
return Ok(adjusted_amount)
} else {
log::info!("amount to be charged: {:?} in asset: {:?}", amount, asset_id);
return Ok(amount)
}
}
}

pub type Traders = (
TakeFirstAssetTrader<
AccountId,
ChargeWeightInFungiblesImplementation,
ConvertedConcreteId<CurrencyId, Balance, CurrencyIdConvert, JustTry>,
Tokens,
XcmFeesTo32ByteAccount<LocalAssetTransactor, AccountId, AmplitudeTreasuryAccount>,
>,
);

pub struct XcmConfig;
impl xcm_executor::Config for XcmConfig {
Expand Down
25 changes: 3 additions & 22 deletions runtime/common/src/asset_registry.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
use crate::*;
use frame_support::traits::AsEnsureOriginWithArg;
use frame_system::EnsureRoot;
use orml_traits::{FixedConversionRateProvider as FixedConversionRateProviderTrait,
asset_registry::{AssetMetadata, AssetProcessor, Inspect}};
use orml_traits::asset_registry::{AssetMetadata, AssetProcessor};
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_core::Get;
use sp_runtime::{BoundedVec, DispatchError, traits::PhantomData};
use sp_runtime::{BoundedVec, DispatchError};
use sp_std::fmt::Debug;
use spacewalk_primitives::CurrencyId;
use xcm::opaque::v3::MultiLocation;

#[derive(Clone, PartialOrd, Ord, PartialEq, Eq, Debug, Encode, Decode, TypeInfo, MaxEncodedLen)]
pub struct StringLimit;
pub struct StringLimit;
impl Get<u32> for StringLimit {
fn get() -> u32 {
50
Expand Down Expand Up @@ -56,20 +54,3 @@ impl AssetProcessor<CurrencyId, AssetMetadata<Balance, CustomMetadata>> for Cust
}

pub type AssetAuthority = AsEnsureOriginWithArg<EnsureRoot<AccountId>>;


pub struct FixedConversionRateProvider<OrmlAssetRegistry>(PhantomData<OrmlAssetRegistry>);

impl<
OrmlAssetRegistry: Inspect<
AssetId = CurrencyId,
Balance = Balance,
CustomMetadata = asset_registry::CustomMetadata,
>,
> FixedConversionRateProviderTrait for FixedConversionRateProvider<OrmlAssetRegistry>
{
fn get_fee_per_second(location: &MultiLocation) -> Option<u128> {
let metadata = OrmlAssetRegistry::metadata_by_location(location)?;
Some(metadata.additional.fee_per_second)
}
}
2 changes: 0 additions & 2 deletions runtime/common/src/benchmarking/orml_asset_registry.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use crate::asset_registry::{CustomMetadata, DiaKeys};
use frame_benchmarking::v2::benchmarks;
use frame_support::assert_ok;
use frame_system::RawOrigin;
use orml_asset_registry::AssetMetadata;
use sp_runtime::BoundedVec;
use sp_std::{vec, vec::Vec};
use crate::asset_registry::{CustomMetadata, DiaKeys};
use spacewalk_primitives::CurrencyId;
Expand Down
28 changes: 25 additions & 3 deletions runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![allow(non_snake_case)]

use sp_runtime::{
traits::{IdentifyAccount, Verify},
traits::{CheckedDiv, IdentifyAccount, Saturating, Verify},
DispatchError, MultiSignature,
};

Expand Down Expand Up @@ -70,6 +70,29 @@ pub mod opaque {
pub type BlockId = generic::BlockId<Block>;
}

pub struct RelativeValue<Amount> {
pub num: Amount,
pub denominator: Amount,
}

impl<Amount: CheckedDiv<Output = Amount> + Saturating + Clone> RelativeValue<Amount> {
pub fn divide_by_relative_value(
amount: Amount,
relative_value: RelativeValue<Amount>,
) -> Amount {
// Calculate the adjusted amount
if let Some(adjusted_amount) = amount
.clone()
.saturating_mul(relative_value.denominator)
.checked_div(&relative_value.num)
{
return adjusted_amount
}
// We should never specify a numerator of 0, but just to be safe
return amount
}
}

#[macro_use]
pub mod parachains {

Expand Down Expand Up @@ -167,8 +190,7 @@ pub mod parachains {

// The address of the BRZ token on Moonbeam `0x3225edCe8aD30Ae282e62fa32e7418E4b9cf197b` as byte array
pub const BRZ_ASSET_ACCOUNT_IN_BYTES: [u8; 20] = [
50, 37, 237, 206, 138, 211, 10, 226, 130, 230, 47, 163, 46, 116, 24, 228, 185, 207,
25, 123,
50, 37, 237, 206, 138, 211, 10, 226, 130, 230, 47, 163, 46, 116, 24, 228, 185, 207, 25, 123
];

parachain_asset_location!(
Expand Down
43 changes: 14 additions & 29 deletions runtime/foucoco/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use frame_support::{
log, match_types, parameter_types,
traits::{ConstU32, ContainsPair, Everything, Nothing},
};
use orml_asset_registry::{AssetRegistryTrader, FixedRateAssetRegistryTrader};
use orml_traits::{
location::{RelativeReserveProvider, Reserve},
parameter_type_with_key,
Expand All @@ -16,22 +15,21 @@ use polkadot_runtime_common::impls::ToAuthor;
use sp_runtime::traits::Convert;
use xcm::latest::{prelude::*, Weight as XCMWeight};
use xcm_builder::{
AccountId32Aliases, AllowUnpaidExecutionFrom, AllowSubscriptionsFrom,AllowTopLevelPaidExecutionFrom, AllowKnownQueryResponses, EnsureXcmOrigin, FixedWeightBounds,
AccountId32Aliases, AllowUnpaidExecutionFrom, EnsureXcmOrigin, FixedWeightBounds,
ParentIsPreset, RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia,
SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit,
SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, UsingComponents,
};
use xcm_executor::{traits::ShouldExecute, XcmExecutor};
use cumulus_primitives_utility::XcmFeesTo32ByteAccount;

use runtime_common::{parachains::moonbase_alpha_relay::moonbase_alpha, asset_registry::FixedConversionRateProvider};
use runtime_common::parachains::moonbase_alpha_relay::moonbase_alpha;

use crate::assets::{
native_locations::{native_location_external_pov, native_location_local_pov},
xcm_assets,
};

use super::{
AccountId, AssetRegistry, Balance, Balances, Currencies, CurrencyId, FoucocoTreasuryAccount, ParachainInfo,
AccountId, Balance, Balances, Currencies, CurrencyId, FoucocoTreasuryAccount, ParachainInfo,
ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, WeightToFee,
XcmpQueue,
};
Expand Down Expand Up @@ -172,6 +170,13 @@ parameter_types! {
pub const MaxAssetsForTransfer: usize = 2;
}

match_types! {
pub type ParentOrParentsExecutivePlurality: impl Contains<MultiLocation> = {
MultiLocation { parents: 1, interior: Here } |
MultiLocation { parents: 1, interior: X1(Plurality { id: BodyId::Executive, .. }) }
};
}

//TODO: move DenyThenTry to polkadot's xcm module.
/// Deny executing the xcm message if it matches any of the Deny filter regardless of anything else.
/// If it passes the Deny, and matches one of the Allow cases then it is let through.
Expand Down Expand Up @@ -235,28 +240,7 @@ impl ShouldExecute for DenyReserveTransferToRelayChain {
}
}

match_types! {
pub type ParentOrParentsPlurality: impl Contains<MultiLocation> = {
MultiLocation { parents: 1, interior: Here } |
MultiLocation { parents: 1, interior: X1(Plurality { .. }) }
};
}

pub type Barrier = (
TakeWeightCredit,
AllowTopLevelPaidExecutionFrom<Everything>,
// Parent and its plurality get free execution
AllowUnpaidExecutionFrom<ParentOrParentsPlurality>,
// Expected responses are OK.
AllowKnownQueryResponses<PolkadotXcm>,
// Subscriptions for version tracking are OK.
AllowSubscriptionsFrom<Everything>,
);

pub type Traders = AssetRegistryTrader<
FixedRateAssetRegistryTrader<FixedConversionRateProvider<AssetRegistry>>,
XcmFeesTo32ByteAccount<LocalAssetTransactor, AccountId, FoucocoTreasuryAccount>,
>;
pub type Barrier = AllowUnpaidExecutionFrom<Everything>;

pub struct XcmConfig;
impl xcm_executor::Config for XcmConfig {
Expand All @@ -271,7 +255,8 @@ impl xcm_executor::Config for XcmConfig {
type UniversalLocation = UniversalLocation;
type Barrier = Barrier;
type Weigher = FixedWeightBounds<UnitWeightCost, RuntimeCall, MaxInstructions>;
type Trader = Traders;
type Trader =
UsingComponents<WeightToFee, RelayLocation, AccountId, Balances, ToAuthor<Runtime>>;
type ResponseHandler = PolkadotXcm;
type AssetTrap = PolkadotXcm;
type AssetLocker = ();
Expand Down
1 change: 0 additions & 1 deletion runtime/integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ parachain-info = { git = "https://github.com/paritytech/cumulus", branch = "polk
statemint-runtime = { git = "https://github.com/paritytech/cumulus", branch = "polkadot-v0.9.40" }
statemine-runtime = { git = "https://github.com/paritytech/cumulus", branch = "polkadot-v0.9.40" }

orml-asset-registry = { git = "https://github.com/open-web3-stack/open-runtime-module-library.git", branch = "polkadot-v0.9.40" }
orml-xcm = { git = "https://github.com/open-web3-stack/open-runtime-module-library.git", branch = "polkadot-v0.9.40" }
orml-xcm-support = { git = "https://github.com/open-web3-stack/open-runtime-module-library.git", branch = "polkadot-v0.9.40" }
orml-traits = { git = "https://github.com/open-web3-stack/open-runtime-module-library.git", branch = "polkadot-v0.9.40" }
Expand Down
7 changes: 3 additions & 4 deletions runtime/integration-tests/src/amplitude_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ use statemine_runtime as kusama_asset_hub_runtime;
use xcm::latest::NetworkId;
use xcm_emulator::{decl_test_network, decl_test_parachain, decl_test_relay_chain, TestExt};

// Native fee expected for each token according to the `fee_per_second` values defined in the mock
const NATIVE_FEE_WHEN_TRANSFER_TO_PARACHAIN: polkadot_core_primitives::Balance = 4000000000;
const KSM_FEE_WHEN_TRANSFER_TO_PARACHAIN: polkadot_core_primitives::Balance = NATIVE_FEE_WHEN_TRANSFER_TO_PARACHAIN / 20;
const USDT_FEE_WHEN_TRANSFER_TO_PARACHAIN: polkadot_core_primitives::Balance = NATIVE_FEE_WHEN_TRANSFER_TO_PARACHAIN / 10;
const KSM_FEE_WHEN_TRANSFER_TO_PARACHAIN: polkadot_core_primitives::Balance = 32000000;
const USDT_FEE_WHEN_TRANSFER_TO_PARACHAIN: polkadot_core_primitives::Balance = 640000000;
const NATIVE_FEE_WHEN_TRANSFER_TO_PARACHAIN: polkadot_core_primitives::Balance = 3200000000;

decl_test_relay_chain! {
pub struct KusamaRelay {
Expand Down
Loading
Loading