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

393 use fee per second from asset metadata in xcm config #432

Merged
merged 84 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 75 commits
Commits
Show all changes
84 commits
Select commit Hold shift + click to select a range
b1ad3f1
WIP
bogdanS98 Jan 11, 2024
dd83e1b
Fmt
bogdanS98 Jan 11, 2024
3ff7277
Add comment
bogdanS98 Jan 11, 2024
dcc6a4b
Still wip
bogdanS98 Jan 12, 2024
d998215
Fmt
bogdanS98 Jan 12, 2024
0afee4a
Add doc comments
bogdanS98 Jan 12, 2024
3e7b9bb
Address change requests
bogdanS98 Jan 12, 2024
edc4d94
Fmt
bogdanS98 Jan 12, 2024
1359c94
Add comment
bogdanS98 Jan 12, 2024
dc68751
Add fee for basic asset
bogdanS98 Jan 15, 2024
069d17e
Fmt
bogdanS98 Jan 15, 2024
67f3513
Return NoPrice error when price fetching fails
bogdanS98 Jan 15, 2024
beb234b
Add signed extension
bogdanS98 Jan 15, 2024
2479198
Add first iteration of mock
bogdanS98 Jan 26, 2024
449296a
Allow only XCM assets for buyout
bogdanS98 Jan 26, 2024
69a81c7
Update mock to only allow specific XCM assets
bogdanS98 Jan 26, 2024
b8cda83
Add tests and benchmarks - still wip
bogdanS98 Jan 30, 2024
95035a4
Fmt
bogdanS98 Jan 31, 2024
eebf511
Handle currency id in benchmarks
bogdanS98 Jan 31, 2024
a75ad1a
Update comments
bogdanS98 Jan 31, 2024
560268b
Remove benchmark comment
bogdanS98 Jan 31, 2024
be4f99a
Fix benchmarks and tests, add pallet to Foucoco, switch from UnixTime…
bogdanS98 Feb 1, 2024
19932f9
Merge remote-tracking branch 'origin' into 395-implement-pallet-that-…
bogdanS98 Feb 1, 2024
4aaee33
Fmt
bogdanS98 Feb 1, 2024
f2d1eda
Refactor dot currency id in tests
bogdanS98 Feb 2, 2024
8bae6e8
Small refactor for test attempt_buyout_after_buyout_limit_exceeded_fails
bogdanS98 Feb 2, 2024
11d9609
Update comments
bogdanS98 Feb 2, 2024
d224292
Small refactor
bogdanS98 Feb 2, 2024
e07318c
Add comment in Foucoco runtime
bogdanS98 Feb 2, 2024
fa68d4d
Add comment in Foucoco runtime
bogdanS98 Feb 2, 2024
de93547
Update comment
bogdanS98 Feb 2, 2024
9a19333
Update comment
bogdanS98 Feb 2, 2024
b2e2f17
Add signed extension to runtime
bogdanS98 Feb 2, 2024
f02fbc8
Add comment
bogdanS98 Feb 2, 2024
4fa8b91
Fix indentation in tests
bogdanS98 Feb 2, 2024
fe04294
Fix indentation in tests
bogdanS98 Feb 2, 2024
7c46f0b
Fix indentation in tests
bogdanS98 Feb 2, 2024
382b0d3
Fix indentation in tests
bogdanS98 Feb 2, 2024
66a86da
Update comment
bogdanS98 Feb 2, 2024
96af52c
Fix building project with runtime benchmarks flag
bogdanS98 Feb 2, 2024
d8c6738
Address change requests
bogdanS98 Feb 5, 2024
d31c2b0
Merge branch '395-implement-pallet-that-extends-our-current-treasury-…
bogdanS98 Feb 5, 2024
d4fcb93
Small refactor for asset registry benchmarks
bogdanS98 Feb 5, 2024
510b4ff
Fix compiler warning
bogdanS98 Feb 5, 2024
9e8f129
Address change requests
bogdanS98 Feb 5, 2024
f3ebd72
Update comment
bogdanS98 Feb 5, 2024
d020484
Optimise fetching oracle price for benchmarks and regenerate weights
bogdanS98 Feb 5, 2024
8352c1c
Merge branch '395-implement-pallet-that-extends-our-current-treasury-…
bogdanS98 Feb 5, 2024
470b0e7
Fmt
bogdanS98 Feb 5, 2024
5cc4cca
using fee per second value for relative fee adjusting
gianfra-t Feb 5, 2024
433206e
WIP
gianfra-t Feb 6, 2024
eb0de40
fixing xcm integration tests
gianfra-t Feb 6, 2024
420815f
fix pendulum integration tests, reuse v3 location
gianfra-t Feb 6, 2024
53bfb97
cleanup imports, reuse type for fee distribution
gianfra-t Feb 6, 2024
118b3d5
cleanup and format
gianfra-t Feb 6, 2024
e9f76e7
more cleanup
gianfra-t Feb 6, 2024
39ea1dc
Merge branch '395-implement-pallet-that-extends-our-current-treasury-…
gianfra-t Feb 6, 2024
d8674cf
more import cleaning and fixes after merging with future CustomMetada…
gianfra-t Feb 6, 2024
7ba5243
Merge branch 'main' into 393-use-fee_per_second-from-asset-metadata-i…
bogdanS98 Feb 7, 2024
87db579
moving implementation
gianfra-t Feb 16, 2024
dc679f1
Merge branch '393-use-fee_per_second-from-asset-metadata-in-xcm-confi…
gianfra-t Feb 16, 2024
fb74127
merged main
gianfra-t Feb 19, 2024
40628ce
modify expected fees in tests
gianfra-t Feb 19, 2024
d12ac51
added current xcm config to foucoco
gianfra-t Feb 19, 2024
b381318
removing unused constants
gianfra-t Feb 26, 2024
049c215
fully remove alice test account
gianfra-t Feb 27, 2024
b084a87
use multipliers for fee expected constants in tests
gianfra-t Feb 28, 2024
67abe63
merged current main
gianfra-t Mar 1, 2024
1391b74
merged with main
gianfra-t Mar 20, 2024
760f332
wip. addition of convertion functions from asset registry and partia…
gianfra-t Mar 20, 2024
07d6212
add coversion to fee per second
gianfra-t Mar 21, 2024
8458303
fix failing test
gianfra-t Mar 21, 2024
d220df1
fix failing test
gianfra-t Mar 21, 2024
9438c6c
Merge branch '393-use-fee_per_second-from-asset-metadata-in-xcm-confi…
gianfra-t Mar 21, 2024
8891238
cleanup unused imports, logs
gianfra-t Mar 21, 2024
e08b060
add convert from asset registry to Foucoco
gianfra-t Mar 21, 2024
ddabc24
migrate CurrencyIdConvert to common definitions
gianfra-t Mar 25, 2024
254754a
use Inspect trait from asset registry to simplify code
gianfra-t Apr 4, 2024
ff45e0c
use native asset, local perspective
gianfra-t Apr 4, 2024
5026906
fix incorrect native xcm representation
gianfra-t Apr 4, 2024
abae571
remove genesis change used for testing
gianfra-t Apr 4, 2024
80b5dc5
simplify implementation, remove reanchoring before convertion, remove…
gianfra-t Apr 5, 2024
6fc29e8
merged main
gianfra-t Apr 5, 2024
27c0191
modify foucoco SelfLocation
gianfra-t Apr 5, 2024
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
2 changes: 2 additions & 0 deletions Cargo.lock

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

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

let token_balances = balances
.iter()
.flat_map(|k| vec![(k.0.clone(), XCM(0), u128::pow(10, 18))])
.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))])
TorstenStueber marked this conversation as resolved.
Show resolved Hide resolved
.collect();


let stakers: Vec<_> = invulnerables
.iter()
Expand Down
1 change: 1 addition & 0 deletions node/src/constants/foucoco.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ 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: 6 additions & 0 deletions package-lock.json

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

221 changes: 40 additions & 181 deletions runtime/amplitude/src/xcm_config.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use core::marker::PhantomData;

use frame_support::{
log, match_types,
pallet_prelude::DispatchError,
parameter_types,
traits::{tokens::fungibles, ContainsPair, Everything, Nothing, ProcessMessageError},
weights::{Weight, WeightToFee as WeightToFeeTrait},
log, match_types, parameter_types,
traits::{ContainsPair, Everything, Nothing, ProcessMessageError},
};
use orml_asset_registry::{AssetRegistryTrader, FixedRateAssetRegistryTrader};
use orml_traits::{
location::{RelativeReserveProvider, Reserve},
parameter_type_with_key,
Expand All @@ -18,35 +16,22 @@ use sp_runtime::traits::Convert;
use xcm::latest::{prelude::*, Weight as XCMWeight};
use xcm_builder::{
AccountId32Aliases, AllowKnownQueryResponses, AllowSubscriptionsFrom,
AllowTopLevelPaidExecutionFrom, AllowUnpaidExecutionFrom, ConvertedConcreteId, EnsureXcmOrigin,
FixedWeightBounds, ParentIsPreset, RelayChainAsNative, SiblingParachainAsNative,
SiblingParachainConvertsVia, SignedAccountId32AsNative, SignedToAccountId32,
SovereignSignedViaLocation, TakeWeightCredit,
};
use xcm_executor::{
traits::{JustTry, ShouldExecute},
XcmExecutor,
AllowTopLevelPaidExecutionFrom, AllowUnpaidExecutionFrom, EnsureXcmOrigin, FixedWeightBounds,
ParentIsPreset, RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia,
SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit,
};
use xcm_executor::{traits::ShouldExecute, XcmExecutor};

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

use cumulus_primitives_utility::{
ChargeWeightInFungibles, TakeFirstAssetTrader, XcmFeesTo32ByteAccount,
};
use sp_runtime::traits::Zero;

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

use crate::ConstU32;

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

Expand Down Expand Up @@ -80,30 +65,32 @@ pub struct CurrencyIdConvert;

impl Convert<CurrencyId, Option<MultiLocation>> for CurrencyIdConvert {
fn convert(id: CurrencyId) -> Option<MultiLocation> {
match id {
CurrencyId::XCM(index) => match index {
xcm_assets::RELAY_KSM => Some(MultiLocation::parent()),
xcm_assets::ASSETHUB_USDT => Some(asset_hub::USDT_location()),
_ => None,
},
CurrencyId::Native => Some(native_location_external_pov()),
_ => None,
}
AssetRegistry::metadata(&id)
.filter(|m| m.location.is_some())
gianfra-t marked this conversation as resolved.
Show resolved Hide resolved
.and_then(|m| m.location)
.and_then(|l| l.try_into().ok())
}
}

impl Convert<MultiLocation, Option<CurrencyId>> for CurrencyIdConvert {
fn convert(location: MultiLocation) -> Option<CurrencyId> {
match location {
loc if loc == MultiLocation::parent() => Some(xcm_assets::RELAY_KSM_id()),
loc if loc == asset_hub::USDT_location() => Some(xcm_assets::ASSETHUB_USDT_id()),
// Our native currency location without re-anchoring
loc if loc == native_location_external_pov() => Some(CurrencyId::Native),
// Our native currency location with re-anchoring
// The XCM pallet will try to re-anchor the location before it reaches here
loc if loc == native_location_local_pov() => Some(CurrencyId::Native),
_ => None,
}
fn convert(location: MultiLocation) -> Option<CurrencyId> {
let para_id = ParachainInfo::parachain_id();
let unanchored_location = match location {
MultiLocation { parents: 0, interior } => {

match interior.pushed_front_with(Parachain(u32::from(para_id))) {
Ok(new_interior) => MultiLocation {
parents: 1,
interior: new_interior,
},
Err(_) => return None,
}
},

x => x,
};

AssetRegistry::location_to_asset_id(unanchored_location)
}
}

Expand All @@ -117,23 +104,6 @@ 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 @@ -204,13 +174,6 @@ 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 @@ -292,114 +255,10 @@ pub type Barrier = (
AllowSubscriptionsFrom<Everything>,
);

pub struct ChargeWeightInFungiblesImplementation;
impl ChargeWeightInFungibles<AccountId, ConcreteAssets> 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)
}
}
}

// Workarround for TakeFirstAssetTrader
use frame_support::traits::tokens::{
DepositConsequence, Fortitude, Preservation, Provenance, WithdrawConsequence,
};

pub struct ConcreteAssets;
impl fungibles::Mutate<AccountId> for ConcreteAssets {}
impl fungibles::Balanced<AccountId> for ConcreteAssets {
type OnDropCredit = fungibles::DecreaseIssuance<AccountId, Self>;
type OnDropDebt = fungibles::IncreaseIssuance<AccountId, Self>;
}
// We only use minimum_balance of these implementations
impl fungibles::Inspect<AccountId> for ConcreteAssets {
type AssetId = <Tokens as fungibles::Inspect<AccountId>>::AssetId;
type Balance = <Tokens as fungibles::Inspect<AccountId>>::Balance;

fn minimum_balance(id: Self::AssetId) -> Self::Balance {
Tokens::minimum_balance(id)
}

fn total_issuance(asset_id: Self::AssetId) -> Self::Balance {
Tokens::total_issuance(asset_id)
}

fn balance(asset_id: Self::AssetId, account_id: &AccountId) -> Self::Balance {
Tokens::balance(asset_id, account_id)
}

fn total_balance(asset_id: Self::AssetId, account_id: &AccountId) -> Self::Balance {
Tokens::balance(asset_id, account_id)
}

fn reducible_balance(
_: Self::AssetId,
_: &AccountId,
_: Preservation,
_: Fortitude,
) -> Self::Balance {
Self::Balance::zero()
}

fn can_deposit(
_: Self::AssetId,
_: &AccountId,
_: Self::Balance,
_: Provenance,
) -> DepositConsequence {
DepositConsequence::UnknownAsset
}

fn can_withdraw(
_: Self::AssetId,
_: &AccountId,
_: Self::Balance,
) -> WithdrawConsequence<Self::Balance> {
WithdrawConsequence::UnknownAsset
}

fn asset_exists(_: Self::AssetId) -> bool {
false
}
}

// Not used
impl fungibles::Unbalanced<AccountId> for ConcreteAssets {
fn handle_dust(_: fungibles::Dust<AccountId, Self>) {
}
fn write_balance(
_: Self::AssetId,
_: &AccountId,
_: Self::Balance,
) -> Result<Option<Self::Balance>, DispatchError> {
core::prelude::v1::Err(DispatchError::CannotLookup)
}

fn set_total_issuance(_: Self::AssetId, _: Self::Balance) {
}
}

pub type Traders = (
TakeFirstAssetTrader<
AccountId,
ChargeWeightInFungiblesImplementation,
ConvertedConcreteId<CurrencyId, Balance, CurrencyIdConvert, JustTry>,
ConcreteAssets,
XcmFeesTo32ByteAccount<LocalAssetTransactor, AccountId, AmplitudeTreasuryAccount>,
>,
);
pub type Traders = AssetRegistryTrader<
FixedRateAssetRegistryTrader<FixedConversionRateProvider<AssetRegistry, CurrencyIdConvert>>,
XcmFeesTo32ByteAccount<LocalAssetTransactor, AccountId, AmplitudeTreasuryAccount>,
>;

pub struct XcmConfig;
impl xcm_executor::Config for XcmConfig {
Expand Down
2 changes: 2 additions & 0 deletions runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ targets = ["x86_64-unknown-linux-gnu"]
paste = "1.0.14"
parity-scale-codec = { version = "3.1.5", default-features = false, features = ["derive"] }
scale-info = { version = "2.1.1", default-features = false, features = ["derive"] }
log = { version = "0.4.17", default-features = false }

frame-support = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.42" }
frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false, optional = true }
Expand Down Expand Up @@ -41,6 +42,7 @@ default = [
]

std = [
"log/std",
"parity-scale-codec/std",
"scale-info/std",
"frame-benchmarking?/std",
Expand Down
29 changes: 26 additions & 3 deletions runtime/common/src/asset_registry.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
use crate::*;
use frame_support::traits::AsEnsureOriginWithArg;
use frame_system::EnsureRoot;
use orml_traits::asset_registry::{AssetMetadata, AssetProcessor};
use orml_traits::{FixedConversionRateProvider as FixedConversionRateProviderTrait,
asset_registry::{AssetMetadata, AssetProcessor, Inspect}};
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_core::Get;
use sp_runtime::{BoundedVec, DispatchError};
use sp_runtime::{BoundedVec, DispatchError, traits::{PhantomData, Convert}};
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 @@ -54,3 +56,24 @@ impl AssetProcessor<CurrencyId, AssetMetadata<Balance, CustomMetadata>> for Cust
}

pub type AssetAuthority = AsEnsureOriginWithArg<EnsureRoot<AccountId>>;
pub struct FixedConversionRateProvider<OrmlAssetRegistry, CurrencyIdConvert>(PhantomData<(OrmlAssetRegistry,CurrencyIdConvert)>);

impl<
OrmlAssetRegistry: Inspect<
AssetId = CurrencyId,
Balance = Balance,
CustomMetadata = asset_registry::CustomMetadata,
>,
CurrencyIdConvert: Convert<MultiLocation, Option<CurrencyId>>,
> FixedConversionRateProviderTrait for FixedConversionRateProvider<OrmlAssetRegistry, CurrencyIdConvert>
{
fn get_fee_per_second(location: &MultiLocation) -> Option<u128> {
let asset_id_maybe = CurrencyIdConvert::convert(location.clone());
gianfra-t marked this conversation as resolved.
Show resolved Hide resolved
let asset_id = match asset_id_maybe {
Some(id) => id,
None => return None,
};
let metadata = OrmlAssetRegistry::metadata(&asset_id)?;
Some(metadata.additional.fee_per_second)
}
}
2 changes: 2 additions & 0 deletions runtime/common/src/benchmarking/orml_asset_registry.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
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 spacewalk_primitives::CurrencyId;
use xcm::{
Expand Down
Loading
Loading