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

feat(xcm): xcm location for ibc assets #4114

Merged
merged 4 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions code/Cargo.lock

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

36 changes: 21 additions & 15 deletions code/parachain/runtime/common/src/xcmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{fees::NativeBalance, prelude::*, AccountId, Balance};
use frame_support::{
dispatch::Weight,
log, match_types, parameter_types,
traits::{tokens::BalanceConversion, Contains, Get},
traits::{tokens::BalanceConversion, Contains, Get, PalletInfoAccess},
weights::{WeightToFee, WeightToFeePolynomial},
};
use num_traits::{One, Zero};
Expand Down Expand Up @@ -178,45 +178,49 @@ impl Convert<AccountId, MultiLocation> for AccountIdToMultiLocation {
}
}

pub struct CurrencyIdConvert<AssetRegistry, WellKnownCurrency, ThisParaId>(
PhantomData<(AssetRegistry, WellKnownCurrency, ThisParaId)>,
pub struct CurrencyIdConvert<ForeignAssetsToXcm, WellKnownCurrency, AssetsRegistry, ThisParaId>(
PhantomData<(ForeignAssetsToXcm, WellKnownCurrency, AssetsRegistry, ThisParaId)>,
);

impl<
AssetRegistry: Convert<CurrencyId, Option<MultiLocation>>,
ForeignAssetsToXcm: Convert<CurrencyId, Option<MultiLocation>>,
WellKnown: WellKnownCurrency,
AssetsRegistry: PalletInfoAccess,
ThisParaId: Get<Id>,
> sp_runtime::traits::Convert<CurrencyId, Option<MultiLocation>>
for CurrencyIdConvert<AssetRegistry, WellKnown, ThisParaId>
for CurrencyIdConvert<ForeignAssetsToXcm, WellKnown, AssetsRegistry, ThisParaId>
{
fn convert(id: CurrencyId) -> Option<MultiLocation> {
WellKnown::local_to_remote(id).or_else(|| AssetRegistry::convert(id))
WellKnown::local_to_remote(id).or_else(|| ForeignAssetsToXcm::convert(id))
}
}

impl<
AssetsRegistry: Convert<MultiLocation, Option<CurrencyId>>,
ForeignAssetsToXcm: Convert<MultiLocation, Option<CurrencyId>>,
WellKnown: WellKnownCurrency,
AssetsRegistry: PalletInfoAccess,
ThisParaId: Get<Id>,
> Convert<MultiLocation, Option<CurrencyId>>
for CurrencyIdConvert<AssetsRegistry, WellKnown, ThisParaId>
for CurrencyIdConvert<ForeignAssetsToXcm, WellKnown, AssetsRegistry, ThisParaId>
{
fn convert(location: MultiLocation) -> Option<CurrencyId> {
log::trace!(target: "xcmp::convert", "converting {:?} on {:?}", &location, ThisParaId::get());
match location {
topology::relay::LOCATION => Some(WellKnown::RELAY_NATIVE),
topology::this::LOCAL => Some(WellKnown::NATIVE),
MultiLocation { parents, interior: X2(Parachain(id), GeneralIndex(index)) }
if parents == 1 && Id::from(id) == ThisParaId::get() =>
Some(CurrencyId(index)),
MultiLocation { parents: 0, interior: X1(GeneralIndex(index)) } =>
MultiLocation {
parents,
interior: X3(Parachain(id), PalletInstance(pallet_index), GeneralIndex(index)),
} if parents == 1 &&
kkast marked this conversation as resolved.
Show resolved Hide resolved
Id::from(id) == ThisParaId::get() &&
pallet_index == AssetsRegistry::index() as u8 =>
Some(CurrencyId(index)),
_ =>
if let Some(currency_id) = WellKnown::remote_to_local(location) {
Some(currency_id)
} else {
log::trace!(target: "xcmp", "using assets registry for {:?}", location);
let result = AssetsRegistry::convert(location).map(Into::into);
let result = ForeignAssetsToXcm::convert(location).map(Into::into);
if let Some(result) = result {
log::trace!(target: "xcmp", "mapped remote to {:?} local", result);
} else {
Expand All @@ -230,10 +234,12 @@ impl<
}

impl<
T: Convert<MultiLocation, Option<CurrencyId>>,
ForeignAssetsToXcm: Convert<MultiLocation, Option<CurrencyId>>,
WellKnown: WellKnownCurrency,
AssetsRegistry: PalletInfoAccess,
ThisParaId: Get<Id>,
> Convert<MultiAsset, Option<CurrencyId>> for CurrencyIdConvert<T, WellKnown, ThisParaId>
> Convert<MultiAsset, Option<CurrencyId>>
for CurrencyIdConvert<ForeignAssetsToXcm, WellKnown, AssetsRegistry, ThisParaId>
{
fn convert(asset: MultiAsset) -> Option<CurrencyId> {
log::trace!(target: "xcmp", "converting {:?}", &asset);
Expand Down
4 changes: 4 additions & 0 deletions code/parachain/runtime/composable/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ orml-xtokens = { workspace = true, default-features = false }
ibc = { workspace = true, default-features = false }
ibc-primitives = { workspace = true, default-features = false }
ibc-runtime-api = { workspace = true, default-features = false }
ibc-rs-scale = { workspace = true, default-features = false, features = [
"parity-scale-codec",
"serde",
] }
pallet-ibc = { workspace = true, default-features = false }

pallet-proxy = { default-features = false, workspace = true }
Expand Down
136 changes: 131 additions & 5 deletions code/parachain/runtime/composable/src/xcmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,25 +333,151 @@ impl<
}
}

pub struct ForeignXcm;
pub struct ForeignAssetsToXcm;

impl Convert<CurrencyId, Option<MultiLocation>> for ForeignXcm {
impl Convert<CurrencyId, Option<MultiLocation>> for ForeignAssetsToXcm {
fn convert(a: CurrencyId) -> Option<MultiLocation> {
match AssetsRegistry::asset_to_remote(a) {
// XCMed assets
Some(ForeignAssetId::Xcm(VersionedMultiLocation::V3(xcm))) => Some(xcm),
// IBCed assets
kkast marked this conversation as resolved.
Show resolved Hide resolved
Some(ForeignAssetId::IbcIcs20(_)) => Some(MultiLocation {
parents: 0,
interior: X3(
Parachain(ParachainInfo::parachain_id().into()),
kkast marked this conversation as resolved.
Show resolved Hide resolved
PalletInstance(<AssetsRegistry as PalletInfoAccess>::index() as u8),
GeneralIndex(a.into()),
),
}),
_ => None,
}
}
}

impl Convert<MultiLocation, Option<CurrencyId>> for ForeignXcm {
impl Convert<MultiLocation, Option<CurrencyId>> for ForeignAssetsToXcm {
fn convert(a: MultiLocation) -> Option<CurrencyId> {
AssetsRegistry::location_to_asset(ForeignAssetId::Xcm(VersionedMultiLocation::V3(a)))
}
}

type AssetsIdConverter =
CurrencyIdConvert<ForeignXcm, primitives::topology::Composable, ParachainInfo>;
type AssetsIdConverter = CurrencyIdConvert<
ForeignAssetsToXcm,
primitives::topology::Composable,
AssetsRegistry,
ParachainInfo,
>;

#[cfg(test)]
mod test {
kkast marked this conversation as resolved.
Show resolved Hide resolved
use composable_traits::{
assets::{AssetInfo, BiBoundedAssetName, BiBoundedAssetSymbol},
rational,
xcm::assets::RemoteAssetRegistryMutate,
};
use frame_support::sp_io;
#[cfg(feature = "std")]
use ibc_rs_scale::{
applications::transfer::{PrefixedDenom as InnerPrefixedDenom, TracePrefix},
core::ics24_host::identifier::{ChannelId, PortId},
};
use prelude::*;
#[cfg(feature = "std")]
use primitives::currency::{CurrencyId, ForeignAssetId, PrefixedDenom};
use primitives::topology::statemine;

use super::*;

pub fn new_test_ext() -> sp_io::TestExternalities {
let storage = frame_system::GenesisConfig::default()
.build_storage::<Runtime>()
.expect("in memory test");
let mut externalities = sp_io::TestExternalities::new(storage);
externalities.execute_with(|| System::set_block_number(1));
externalities
}

#[test]
fn test_xcm_conversion_composable() {
new_test_ext().execute_with(|| {
let usdt = (
CurrencyId::USDT,
Some(ForeignAssetId::Xcm(VersionedMultiLocation::V3(MultiLocation::new(
1,
X3(
Parachain(statemine::ID),
PalletInstance(statemine::ASSETS),
GeneralIndex(statemine::USDT),
),
)))),
AssetInfo {
name: Some(
BiBoundedAssetName::from_vec(b"Statemine USDT".to_vec())
.expect("String is within bounds"),
),
symbol: Some(
BiBoundedAssetSymbol::from_vec(b"USDT".to_vec())
.expect("String is within bounds"),
),
decimals: Some(6),
existential_deposit: 1500,
ratio: Some(rational!(2 / 10000000)),
},
);

let mut ksm_denom =
InnerPrefixedDenom::from_str(CurrencyId::KSM.to_string().as_str())
.expect("genesis");
ksm_denom
.add_trace_prefix(TracePrefix::new(PortId::transfer(), ChannelId::new(1)));

let ksm = (
CurrencyId::COMPOSABLE_KSM,
Some(ForeignAssetId::IbcIcs20(PrefixedDenom(ksm_denom))),
AssetInfo {
name: Some(
BiBoundedAssetName::from_vec(b"Kusama".to_vec())
.expect("String is within bounds"),
),
symbol: Some(
BiBoundedAssetSymbol::from_vec(b"KSM".to_vec())
.expect("String is within bounds"),
),
decimals: Some(10),
existential_deposit: 21430000,
ratio: Some(rational!(3 / 10000)),
},
);
<AssetsRegistry as RemoteAssetRegistryMutate>::register_asset(usdt.0, usdt.1, usdt.2)
.expect("asset wasnt registered");
<AssetsRegistry as RemoteAssetRegistryMutate>::register_asset(ksm.0, ksm.1, ksm.2)
.expect("asset wasnt registered");

// foreign xcm asset
assert_eq!(
AssetsIdConverter::convert(usdt.0),
Some(MultiLocation::new(
1,
X3(
Parachain(statemine::ID),
PalletInstance(statemine::ASSETS),
GeneralIndex(statemine::USDT),
),
))
);
// foreign ibc asset
assert_eq!(
AssetsIdConverter::convert(ksm.0),
Some(MultiLocation {
parents: 0,
interior: X3(Parachain(100), PalletInstance(59), GeneralIndex(CurrencyId::COMPOSABLE_KSM.0 as u128))
})
);
// well known assets
assert_eq!(AssetsIdConverter::convert(CurrencyId::COMPOSABLE_LAYR), Some(MultiLocation::here()));
assert_eq!(AssetsIdConverter::convert(CurrencyId::COMPOSABLE_DOT), Some(MultiLocation::parent()));
});
}
}

pub type Trader = TransactionFeePoolTrader<
AssetsIdConverter,
Expand Down
4 changes: 4 additions & 0 deletions code/parachain/runtime/picasso/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ orml-xtokens = { workspace = true, default-features = false }
ibc = { workspace = true, default-features = false }
ibc-primitives = { workspace = true, default-features = false }
ibc-runtime-api = { workspace = true, default-features = false }
ibc-rs-scale = { workspace = true, default-features = false, features = [
"parity-scale-codec",
"serde",
] }
pallet-ibc = { workspace = true, default-features = false }

invarch-xcm-builder = { workspace = true, default-features = false }
Expand Down
Loading
Loading