Skip to content

Commit

Permalink
chore: Move registried assets from runtime to pallets (#1888)
Browse files Browse the repository at this point in the history
* update CurrencyIdConvert and FirstAssetTrader

* fix LCDOT_KEY

* fix errors
  • Loading branch information
mclyk authored Oct 7, 2022
1 parent b7f8a90 commit 3dfb1a8
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 74 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ LAUNCH_CONFIG_YAML := config.yml
LAUNCH_CONFIG_JSON := config.json
DOCKER_OVERRIDE_YAML := docker-compose.override.yml
DOCKER_TAG := latest_evm
RELAY_DOCKER_TAG := v0.9.24
CUMULUS_DOCKER_TAG := v0.9.24
RELAY_DOCKER_TAG := v0.9.28
CUMULUS_DOCKER_TAG := v0.9.28

.PHONY: init
init: submodules
Expand Down
6 changes: 3 additions & 3 deletions config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
relaychain:
image: parallelfinance/polkadot:v0.9.24
image: parallelfinance/polkadot:v0.9.28
chain: polkadot-local
runtimeGenesisConfig:
hrmp:
Expand Down Expand Up @@ -50,9 +50,9 @@ parachains:
nodes:
- flags:
- --alice
- image: parallelfinance/cumulus:v0.9.24
- image: parallelfinance/cumulus:v0.9.28
chain:
base: statemint-dev
base: statemint-local
collators:
- alice
sudo: dave
Expand Down
37 changes: 15 additions & 22 deletions pallets/traits/src/xcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ impl<
// For cases (specially tests) where the asset is very cheap with respect
// to the weight needed
if amount.is_zero() {
log::trace!(
target: "xcm::buy_weight::payment",
"asset_type: {:?}",
id,
);
return Ok(payment);
}

Expand All @@ -165,6 +170,11 @@ impl<

// In short, we only refund on the asset the trader first successfully was able
// to pay for an execution
log::trace!(
target: "xcm::buy_weight::unused",
"asset_type: {:?}",
id,
);
let new_asset = match self.1.clone() {
Some((prev_id, prev_amount, units_per_second)) => {
if prev_id == id {
Expand All @@ -181,7 +191,6 @@ impl<
self.0 = self.0.saturating_add(weight);
self.1 = Some(new_asset);
};

Ok(unused)
}
_ => Err(XcmError::TooExpensive),
Expand All @@ -198,6 +207,11 @@ impl<
prev_amount.saturating_sub(amount),
units_per_second,
));
log::trace!(
target: "xcm::refund_weight",
"id: {:?}",
id,
);
Some(MultiAsset {
fun: Fungibility::Fungible(amount),
id: xcmAssetId::Concrete(id),
Expand Down Expand Up @@ -319,27 +333,6 @@ impl From<AssetType> for CurrencyId {
}
}

// How to convert from CurrencyId to MultiLocation
pub struct CurrencyIdtoMultiLocation<LegacyAssetConverter, ForeignAssetConverter>(
sp_std::marker::PhantomData<(LegacyAssetConverter, ForeignAssetConverter)>,
);
impl<LegacyAssetConverter, ForeignAssetConverter>
sp_runtime::traits::Convert<CurrencyId, Option<MultiLocation>>
for CurrencyIdtoMultiLocation<LegacyAssetConverter, ForeignAssetConverter>
where
LegacyAssetConverter: Convert<CurrencyId, Option<MultiLocation>>,
ForeignAssetConverter: xcm_executor::traits::Convert<MultiLocation, CurrencyId>,
{
fn convert(currency_id: CurrencyId) -> Option<MultiLocation> {
let mut multi_location = LegacyAssetConverter::convert(currency_id);
multi_location = match multi_location {
Some(_) => multi_location,
None => ForeignAssetConverter::reverse_ref(&currency_id).ok(),
};
multi_location
}
}

pub struct MultiCurrencyAdapter<
MultiCurrency,
Match,
Expand Down
41 changes: 31 additions & 10 deletions runtime/heiko/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ use xcm_builder::{
SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, TakeRevenue,
TakeWeightCredit,
};
use xcm_executor::{traits::JustTry, Config, XcmExecutor};
use xcm_executor::{
traits::{Convert as XcmConvert, JustTry},
Config, XcmExecutor,
};

// A few exports that help ease life for downstream crates.
// re-exports
Expand All @@ -99,8 +102,7 @@ pub use pallet_streaming;

use pallet_traits::{
xcm::{
AccountIdToMultiLocation, AsAssetType, AssetType, CurrencyIdtoMultiLocation,
FirstAssetTrader, MultiCurrencyAdapter,
AccountIdToMultiLocation, AsAssetType, AssetType, FirstAssetTrader, MultiCurrencyAdapter,
},
DecimalProvider, EmergencyCallFilter, ValidationDataProvider,
};
Expand Down Expand Up @@ -365,6 +367,17 @@ parameter_types! {
pub struct CurrencyIdConvert;
impl Convert<CurrencyId, Option<MultiLocation>> for CurrencyIdConvert {
fn convert(id: CurrencyId) -> Option<MultiLocation> {
let multi_location =
AsAssetType::<CurrencyId, AssetType, AssetRegistry>::reverse_ref(&id).ok();
log::trace!(
target: "xcm::convert",
"currency_id: {:?}, multi_location: {:?}",
id,
multi_location,
);
if multi_location.is_some() {
return multi_location;
}
match id {
KSM => Some(MultiLocation::parent()),
SKSM => Some(MultiLocation::new(
Expand Down Expand Up @@ -462,6 +475,17 @@ impl Convert<CurrencyId, Option<MultiLocation>> for CurrencyIdConvert {

impl Convert<MultiLocation, Option<CurrencyId>> for CurrencyIdConvert {
fn convert(location: MultiLocation) -> Option<CurrencyId> {
let currency_id =
AsAssetType::<CurrencyId, AssetType, AssetRegistry>::convert_ref(&location).ok();
log::trace!(
target: "xcm::convert",
"multi_location: {:?}. currency_id: {:?}",
location,
currency_id,
);
if currency_id.is_some() {
return currency_id;
}
match location {
MultiLocation {
parents: 1,
Expand Down Expand Up @@ -580,10 +604,7 @@ impl orml_xtokens::Config for Runtime {
type Event = Event;
type Balance = Balance;
type CurrencyId = CurrencyId;
type CurrencyIdConvert = CurrencyIdtoMultiLocation<
CurrencyIdConvert,
AsAssetType<CurrencyId, AssetType, AssetRegistry>,
>;
type CurrencyIdConvert = CurrencyIdConvert;
type AccountIdToMultiLocation = AccountIdToMultiLocation<AccountId>;
type SelfLocation = SelfLocation;
type XcmExecutor = XcmExecutor<XcmConfig>;
Expand Down Expand Up @@ -1367,6 +1388,9 @@ impl TakeRevenue for ToTreasury {
}

pub type Trader = (
// we can try use asset-registry for statemine asset first,just comment following
// FixedRateOfFungible<UsdtPerSecond, ToTreasury>,
FirstAssetTrader<AssetType, AssetRegistry, XcmFeesToAccount>,
FixedRateOfFungible<KsmPerSecond, ToTreasury>,
FixedRateOfFungible<SKSMPerSecond, ToTreasury>,
FixedRateOfFungible<SKSMPerSecondOfCanonicalLocation, ToTreasury>,
Expand All @@ -1389,9 +1413,6 @@ pub type Trader = (
FixedRateOfFungible<TurPerSecond, ToTreasury>,
// Calamari
FixedRateOfFungible<KmaPerSecond, ToTreasury>,
// we can try use asset-registry for statemine asset first,just comment following
// FixedRateOfFungible<UsdtPerSecond, ToTreasury>,
FirstAssetTrader<AssetType, AssetRegistry, XcmFeesToAccount>,
);

parameter_types! {
Expand Down
2 changes: 1 addition & 1 deletion runtime/kerria/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ pub mod paras {
pub const ACA_KEY: &[u8] = &[0, 0];
pub const AUSD_KEY: &[u8] = &[0, 1];
pub const LDOT_KEY: &[u8] = &[0, 3];
pub const LCDOT_KEY: &[u8] = &[2, 13];
pub const LCDOT_KEY: &[u8] = &[4, 13];
}

pub mod moonbeam {
Expand Down
41 changes: 31 additions & 10 deletions runtime/kerria/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ use xcm_builder::{
SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, TakeRevenue,
TakeWeightCredit,
};
use xcm_executor::{traits::JustTry, Config, XcmExecutor};
use xcm_executor::{
traits::{Convert as XcmConvert, JustTry},
Config, XcmExecutor,
};

// A few exports that help ease life for downstream crates.
// re-exports
Expand All @@ -103,8 +106,7 @@ pub use pallet_streaming;

use pallet_traits::{
xcm::{
AccountIdToMultiLocation, AsAssetType, AssetType, CurrencyIdtoMultiLocation,
FirstAssetTrader, MultiCurrencyAdapter,
AccountIdToMultiLocation, AsAssetType, AssetType, FirstAssetTrader, MultiCurrencyAdapter,
},
DecimalProvider, EmergencyCallFilter, ValidationDataProvider,
};
Expand Down Expand Up @@ -404,6 +406,17 @@ parameter_types! {
pub struct CurrencyIdConvert;
impl Convert<CurrencyId, Option<MultiLocation>> for CurrencyIdConvert {
fn convert(id: CurrencyId) -> Option<MultiLocation> {
let multi_location =
AsAssetType::<CurrencyId, AssetType, AssetRegistry>::reverse_ref(&id).ok();
log::trace!(
target: "xcm::convert",
"currency_id: {:?}, multi_location: {:?}",
id,
multi_location,
);
if multi_location.is_some() {
return multi_location;
}
match id {
DOT => Some(MultiLocation::parent()),
SDOT => Some(MultiLocation::new(
Expand Down Expand Up @@ -507,6 +520,17 @@ impl Convert<CurrencyId, Option<MultiLocation>> for CurrencyIdConvert {

impl Convert<MultiLocation, Option<CurrencyId>> for CurrencyIdConvert {
fn convert(location: MultiLocation) -> Option<CurrencyId> {
let currency_id =
AsAssetType::<CurrencyId, AssetType, AssetRegistry>::convert_ref(&location).ok();
log::trace!(
target: "xcm::convert",
"multi_location: {:?}. currency_id: {:?}",
location,
currency_id,
);
if currency_id.is_some() {
return currency_id;
}
match location {
MultiLocation {
parents: 1,
Expand Down Expand Up @@ -607,10 +631,7 @@ impl orml_xtokens::Config for Runtime {
type Event = Event;
type Balance = Balance;
type CurrencyId = CurrencyId;
type CurrencyIdConvert = CurrencyIdtoMultiLocation<
CurrencyIdConvert,
AsAssetType<CurrencyId, AssetType, AssetRegistry>,
>;
type CurrencyIdConvert = CurrencyIdConvert;
type AccountIdToMultiLocation = AccountIdToMultiLocation<AccountId>;
type SelfLocation = SelfLocation;
type XcmExecutor = XcmExecutor<XcmConfig>;
Expand Down Expand Up @@ -1521,6 +1542,9 @@ impl TakeRevenue for ToTreasury {
}

pub type Trader = (
// Foreign Assets registered in AssetRegistry
// TODO: replace all above except local reserved asset later
FirstAssetTrader<AssetType, AssetRegistry, XcmFeesToAccount>,
FixedRateOfFungible<DotPerSecond, ToTreasury>,
FixedRateOfFungible<SDOTPerSecond, ToTreasury>,
FixedRateOfFungible<SDOTPerSecondOfCanonicalLocation, ToTreasury>,
Expand All @@ -1540,9 +1564,6 @@ pub type Trader = (
FixedRateOfFungible<IbtcPerSecond, ToTreasury>,
// Equilibrium
FixedRateOfFungible<EqPerSecond, ToTreasury>,
// Foreign Assets registered in AssetRegistry
// TODO: replace all above except local reserved asset later
FirstAssetTrader<AssetType, AssetRegistry, XcmFeesToAccount>,
);

// Min fee required when transferring non-reserve asset back to sibling chain
Expand Down
2 changes: 1 addition & 1 deletion runtime/parallel/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ pub mod paras {
pub const ACA_KEY: &[u8] = &[0, 0];
pub const AUSD_KEY: &[u8] = &[0, 1];
pub const LDOT_KEY: &[u8] = &[0, 3];
pub const LCDOT_KEY: &[u8] = &[2, 13];
pub const LCDOT_KEY: &[u8] = &[4, 13];
}

pub mod moonbeam {
Expand Down
42 changes: 32 additions & 10 deletions runtime/parallel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ use xcm_builder::{
SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, TakeRevenue,
TakeWeightCredit,
};
use xcm_executor::{traits::JustTry, Config, XcmExecutor};
use xcm_executor::{
traits::{Convert as XcmConvert, JustTry},
Config, XcmExecutor,
};

// A few exports that help ease life for downstream crates.
// re-exports
Expand All @@ -100,8 +103,7 @@ pub use pallet_streaming;

use pallet_traits::{
xcm::{
AccountIdToMultiLocation, AsAssetType, AssetType, CurrencyIdtoMultiLocation,
FirstAssetTrader, MultiCurrencyAdapter,
AccountIdToMultiLocation, AsAssetType, AssetType, FirstAssetTrader, MultiCurrencyAdapter,
},
DecimalProvider, EmergencyCallFilter, ValidationDataProvider,
};
Expand Down Expand Up @@ -370,6 +372,17 @@ parameter_types! {
pub struct CurrencyIdConvert;
impl Convert<CurrencyId, Option<MultiLocation>> for CurrencyIdConvert {
fn convert(id: CurrencyId) -> Option<MultiLocation> {
let multi_location =
AsAssetType::<CurrencyId, AssetType, AssetRegistry>::reverse_ref(&id).ok();
log::trace!(
target: "xcm::convert",
"currency_id: {:?}, multi_location: {:?}",
id,
multi_location,
);
if multi_location.is_some() {
return multi_location;
}
match id {
DOT => Some(MultiLocation::parent()),
SDOT => Some(MultiLocation::new(
Expand Down Expand Up @@ -472,6 +485,18 @@ impl Convert<CurrencyId, Option<MultiLocation>> for CurrencyIdConvert {

impl Convert<MultiLocation, Option<CurrencyId>> for CurrencyIdConvert {
fn convert(location: MultiLocation) -> Option<CurrencyId> {
let currency_id =
AsAssetType::<CurrencyId, AssetType, AssetRegistry>::convert_ref(&location).ok();
log::trace!(
target: "xcm::convert",
"multi_location: {:?}. currency_id: {:?}",
location,
currency_id,
);
if currency_id.is_some() {
return currency_id;
}

match location {
MultiLocation {
parents: 1,
Expand Down Expand Up @@ -600,10 +625,7 @@ impl orml_xtokens::Config for Runtime {
type Event = Event;
type Balance = Balance;
type CurrencyId = CurrencyId;
type CurrencyIdConvert = CurrencyIdtoMultiLocation<
CurrencyIdConvert,
AsAssetType<CurrencyId, AssetType, AssetRegistry>,
>;
type CurrencyIdConvert = CurrencyIdConvert;
type AccountIdToMultiLocation = AccountIdToMultiLocation<AccountId>;
type SelfLocation = SelfLocation;
type XcmExecutor = XcmExecutor<XcmConfig>;
Expand Down Expand Up @@ -1358,6 +1380,9 @@ impl TakeRevenue for ToTreasury {
}

pub type Trader = (
// Foreign Assets registered in AssetRegistry
// TODO: replace all above except local reserved asset later
FirstAssetTrader<AssetType, AssetRegistry, XcmFeesToAccount>,
FixedRateOfFungible<DotPerSecond, ToTreasury>,
FixedRateOfFungible<SDOTPerSecond, ToTreasury>,
FixedRateOfFungible<SDOTPerSecondOfCanonicalLocation, ToTreasury>,
Expand All @@ -1377,9 +1402,6 @@ pub type Trader = (
FixedRateOfFungible<IbtcPerSecond, ToTreasury>,
// Equilibrium
FixedRateOfFungible<EqPerSecond, ToTreasury>,
// Foreign Assets registered in AssetRegistry
// TODO: replace all above except local reserved asset later
FirstAssetTrader<AssetType, AssetRegistry, XcmFeesToAccount>,
);

parameter_types! {
Expand Down
Loading

0 comments on commit 3dfb1a8

Please sign in to comment.