Skip to content

Commit

Permalink
296 Enable XCM assets on Pendulum (#339)
Browse files Browse the repository at this point in the history
* first iteration

* add moonbeam;
also move macro to `common`

* added PDEX
#296 (comment)

* segregate assets/tokens

added EURC

* #339 (comment),
#339 (comment),
#339 (comment)

* https://github.com/pendulum-chain/pendulum/actions/runs/6926668943/job/18839202556?pr=339#step:8:2029

* #339 (comment)

* cargo fmt
  • Loading branch information
b-yap authored Nov 21, 2023
1 parent b586dc7 commit e7024c8
Show file tree
Hide file tree
Showing 17 changed files with 388 additions and 111 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions runtime/amplitude/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ substrate-wasm-builder = { git = "https://github.com/paritytech/substrate", bran
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }
hex-literal = { version = "0.3.4", optional = true }
log = { version = "0.4.17", default-features = false }
paste = "1.0.14"
scale-info = { version = "2.1.1", default-features = false, features = ["derive"] }
serde = { version = "1.0.144", optional = true, features = ["derive"] }
smallvec = "1.9.0"
Expand Down
39 changes: 39 additions & 0 deletions runtime/amplitude/src/assets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#![allow(non_snake_case)]

pub mod xcm_assets {
use runtime_common::create_xcm_id;

create_xcm_id!(RELAY_KSM, 0);
create_xcm_id!(ASSETHUB_USDT, 1);
}

/// Locations for native currency and all natively issued tokens
pub mod native_locations {
use crate::ParachainInfo;
use frame_support::traits::PalletInfoAccess;
use xcm::latest::{
Junction::{PalletInstance, Parachain},
Junctions::{X1, X2},
MultiLocation,
};

fn balances_pallet_id() -> u8 {
crate::Balances::index() as u8
}

/// location of the native currency from the point of view of Pendulum parachain
pub fn native_location_local_pov() -> MultiLocation {
MultiLocation { parents: 0, interior: X1(PalletInstance(balances_pallet_id())) }
}

/// location of the native currency from the point of view of other parachains(external)
pub fn native_location_external_pov() -> MultiLocation {
MultiLocation {
parents: 1,
interior: X2(
Parachain(ParachainInfo::parachain_id().into()),
PalletInstance(balances_pallet_id()),
),
}
}
}
3 changes: 2 additions & 1 deletion runtime/amplitude/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#[cfg(feature = "std")]
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));

mod assets;
mod weights;
pub mod xcm_config;
pub mod zenlink;
Expand Down Expand Up @@ -1889,4 +1890,4 @@ cumulus_pallet_parachain_system::register_validate_block! {
Runtime = Runtime,
BlockExecutor = cumulus_pallet_aura_ext::BlockExecutor::<Runtime, Executive>,
CheckInherents = CheckInherents,
}
}
47 changes: 14 additions & 33 deletions runtime/amplitude/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ use super::{
AccountId, Balance, Balances, CurrencyId, ParachainInfo, ParachainSystem, PolkadotXcm, Runtime,
RuntimeCall, RuntimeEvent, RuntimeOrigin, Tokens, WeightToFee, XcmpQueue,
};
use crate::ConstU32;
use crate::{
assets::{
native_locations::{native_location_external_pov, native_location_local_pov},
xcm_assets,
},
ConstU32,
};
use core::marker::PhantomData;
use frame_support::{
log, match_types, parameter_types,
Expand All @@ -29,9 +35,6 @@ use xcm_executor::{
XcmExecutor,
};

const XCM_ASSET_RELAY_KSM: u8 = 0;
const XCM_ASSET_ASSETHUB_USDT: u8 = 1;

parameter_types! {
pub const RelayLocation: MultiLocation = MultiLocation::parent();
pub const RelayNetwork: NetworkId = NetworkId::Kusama;
Expand Down Expand Up @@ -64,21 +67,11 @@ impl Convert<CurrencyId, Option<MultiLocation>> for CurrencyIdConvert {
fn convert(id: CurrencyId) -> Option<MultiLocation> {
match id {
CurrencyId::XCM(index) => match index {
XCM_ASSET_RELAY_KSM => Some(MultiLocation::parent()),
XCM_ASSET_ASSETHUB_USDT => Some(MultiLocation::new(
1,
X3(
Parachain(asset_hub::PARA_ID),
PalletInstance(asset_hub::ASSET_PALLET_ID),
GeneralIndex(asset_hub::USDT_ASSET_ID),
),
)),
xcm_assets::RELAY_KSM => Some(MultiLocation::parent()),
xcm_assets::ASSETHUB_USDT => Some(asset_hub::USDT_location()),
_ => None,
},
CurrencyId::Native => Some(MultiLocation::new(
1,
X2(Parachain(ParachainInfo::parachain_id().into()), PalletInstance(10)),
)),
CurrencyId::Native => Some(native_location_external_pov()),
_ => None,
}
}
Expand All @@ -87,25 +80,13 @@ impl Convert<CurrencyId, Option<MultiLocation>> for CurrencyIdConvert {
impl Convert<MultiLocation, Option<CurrencyId>> for CurrencyIdConvert {
fn convert(location: MultiLocation) -> Option<CurrencyId> {
match location {
MultiLocation { parents: 1, interior: Here } =>
Some(CurrencyId::XCM(XCM_ASSET_RELAY_KSM)),
MultiLocation {
parents: 1,
interior:
X3(
Parachain(asset_hub::PARA_ID),
PalletInstance(asset_hub::ASSET_PALLET_ID),
GeneralIndex(asset_hub::USDT_ASSET_ID),
),
} => Some(CurrencyId::XCM(XCM_ASSET_ASSETHUB_USDT)),
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
MultiLocation { parents: 1, interior: X2(Parachain(id), PalletInstance(10)) }
if id == u32::from(ParachainInfo::parachain_id()) =>
Some(CurrencyId::Native),
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
MultiLocation { parents: 0, interior: X1(PalletInstance(10)) } =>
Some(CurrencyId::Native),
loc if loc == native_location_local_pov() => Some(CurrencyId::Native),
_ => None,
}
}
Expand Down
1 change: 1 addition & 0 deletions runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ edition = "2021"
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
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"] }

Expand Down
161 changes: 153 additions & 8 deletions runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(non_snake_case)]

use sp_runtime::{
traits::{IdentifyAccount, Verify},
Expand Down Expand Up @@ -68,24 +69,168 @@ pub mod opaque {
pub type BlockId = generic::BlockId<Block>;
}

#[macro_use]
pub mod parachains {

/// Creates a function and a const u8 representation of the value.
/// # Examples
/// `create_xcm_id!(PARACHAIN_ASSET,100);`
///
/// will look like:
/// ```
/// use spacewalk_primitives::CurrencyId;
///
/// pub const PARACHAIN_ASSET : u8 = 100;
/// pub fn PARACHAIN_ASSET_id() -> CurrencyId {
/// CurrencyId::XCM(PARACHAIN_ASSET)
/// }
/// ```
#[macro_export]
macro_rules! create_xcm_id {
($asset_name:ident, $u8_repr:literal) => {
paste::item! {
pub const $asset_name :u8 = $u8_repr;

pub fn [< $asset_name _id >]() -> crate::CurrencyId {
spacewalk_primitives::CurrencyId::XCM($asset_name)
}
}
};
}

/// Creates a location for the given asset in this format: `fn <asset_name>_location() -> MultiLocation`
macro_rules! parachain_asset_location {
// Also declares a constant variable <asset_name>_ASSET_ID with <asset_value>.
// This assumes that the following constant variables exist:
// * `PARA_ID` - the parachain id
// * `ASSET_PALLET_INDEX` - the index of the Assets Pallet
($asset_name:ident, $asset_index: literal) => {
paste::item! {
pub const [< $asset_name _ASSET_ID >] : u128 = $asset_index;

pub fn [< $asset_name _location >] () -> xcm::latest::MultiLocation {
xcm::latest::MultiLocation {
parents: 1,
interior: xcm::latest::Junctions::X3(
xcm::latest::Junction::Parachain(PARA_ID),
xcm::latest::Junction::PalletInstance(ASSET_PALLET_INDEX),
xcm::latest::Junction::GeneralIndex($asset_index)
),
}
}
}
};

// Accepts the asset name AND the interior of the location
// mostly for locations that do not use a `GeneralIndex`
($asset_name:ident, $interiors: expr) => {
paste::item! {
pub fn [< $asset_name _location >] () -> xcm::latest::MultiLocation {
xcm::latest::MultiLocation {
parents: 1,
interior: $interiors
}
}

}
};
}

pub mod polkadot {
pub mod asset_hub {
pub const PARA_ID: u32 = 1000;
pub const ASSET_PALLET_ID: u8 = 50;
pub const ASSET_PALLET_INDEX: u8 = 50;

parachain_asset_location!(USDC, 1337);
parachain_asset_location!(USDT, 1984);
}

pub mod equilibrium {
pub const PARA_ID: u32 = 2011;
pub const ASSET_PALLET_INDEX: u8 = 11;

parachain_asset_location!(EQ, 25_969);
parachain_asset_location!(EQD, 6_648_164);
}

pub mod moonbeam {
use xcm::latest::{
Junction::{AccountKey20, PalletInstance, Parachain},
Junctions::X3,
};

pub const PARA_ID: u32 = 2004;
pub const ASSET_PALLET_INDEX: u8 = 110;
// 0xD65A1872f2E2E26092A443CB86bb5d8572027E6E
// extracted using `H160::from_str("...")` then `as_bytes()`
pub const BRZ_ASSET_ACCOUNT_IN_BYTES: [u8; 20] = [
214, 90, 24, 114, 242, 226, 226, 96, 146, 164, 67, 203, 134, 187, 93, 133, 114, 2,
126, 110,
];

parachain_asset_location!(
BRZ,
X3(
Parachain(PARA_ID),
PalletInstance(ASSET_PALLET_INDEX),
AccountKey20 { network: None, key: BRZ_ASSET_ACCOUNT_IN_BYTES }
)
);
}

pub const USDC_ASSET_ID: u128 = 1337;
pub const USDT_ASSET_ID: u128 = 1984;
pub mod polkadex {
use xcm::latest::{Junction::Parachain, Junctions::X1};

pub const PARA_ID: u32 = 2040;

parachain_asset_location!(PDEX, X1(Parachain(PARA_ID)));
}
}

pub mod kusama {
/// values of kusama asset_hub is similar to polkadot's asset_hub
pub mod asset_hub {
pub const PARA_ID: u32 = 1000;
pub const ASSET_PALLET_ID: u8 = 50;

pub const USDC_ASSET_ID: u128 = 1337;
pub const USDT_ASSET_ID: u128 = 1984;
pub use super::super::polkadot::asset_hub::*;
}
}
}

#[cfg(test)]
mod tests {
use super::parachains::polkadot::*;
use xcm::{
latest::prelude::{AccountKey20, PalletInstance, Parachain},
prelude::GeneralIndex,
};

#[test]
fn test_BRZ() {
let brz_loc = moonbeam::BRZ_location();
let mut junctions = brz_loc.interior().clone().into_iter();

assert_eq!(junctions.next(), Some(Parachain(moonbeam::PARA_ID)));
assert_eq!(junctions.next(), Some(PalletInstance(moonbeam::ASSET_PALLET_INDEX)));
assert_eq!(
junctions.next(),
Some(AccountKey20 { network: None, key: moonbeam::BRZ_ASSET_ACCOUNT_IN_BYTES })
);
assert_eq!(junctions.next(), None);
}

#[test]
fn test_constants() {
let expected_EQ_value = 25_969;
assert_eq!(equilibrium::EQ_ASSET_ID, expected_EQ_value);

let eq_interior = equilibrium::EQ_location().interior;
let mut junctions = eq_interior.clone().into_iter();

assert_eq!(junctions.next(), Some(Parachain(equilibrium::PARA_ID)));
assert_eq!(junctions.next(), Some(PalletInstance(equilibrium::ASSET_PALLET_INDEX)));
assert_eq!(junctions.next(), Some(GeneralIndex(equilibrium::EQ_ASSET_ID)));
assert_eq!(junctions.next(), None);

let expected_USDT_value = 1984;
assert_eq!(asset_hub::USDT_ASSET_ID, expected_USDT_value);
}
}
6 changes: 3 additions & 3 deletions runtime/common/src/zenlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn discriminant(currency: &CurrencyId) -> u8 {
CurrencyId::XCM(_) => 1,
CurrencyId::Stellar(_) => 2,
CurrencyId::ZenlinkLPToken(_, _, _, _) => 3,
_=>0
_ => 0,
}
}

Expand Down Expand Up @@ -98,7 +98,7 @@ pub fn currency_id_to_zenlink_id(
((token2_type as u64) << 40);
Ok(ZenlinkAssetId { chain_id: parachain_id, asset_type: LOCAL, asset_index: index })
},
CurrencyId::Token(_)=> Err(()),
CurrencyId::Token(_) => Err(()),
}
}

Expand Down Expand Up @@ -235,7 +235,7 @@ mod zenlink_tests {
let currency: Result<CurrencyId, _> =
zenlink_id_to_currency_id(fake_zenlink_asset, 1000u32);
let expected_currency: CurrencyId = CurrencyId::ZenlinkLPToken(0, 2, 1, 2);
assert_eq!(currency, Ok(expected_currency));
assert_eq!(currency, Ok(expected_currency));
}

#[test]
Expand Down
Loading

0 comments on commit e7024c8

Please sign in to comment.