Skip to content

Commit

Permalink
[XCM] Fix conversion between MultiLocation to AssetId using `pall…
Browse files Browse the repository at this point in the history
…et-assets-registry` (#348)

* change(kreivo-runtime): add force-debug to try-runtime

* fix(kreivo-runtime): replace `TrustBackedAssetsConvertedConcreteId` with `MultiLocationConvertedConcreteId` as converter

* change(kreivo-runtime): use MultiLocation as AssetId for pallet_assets

* change(kreivo-runtime): remove pallet-asset-registry

* change(virto-common): define a MultiLocation-based AssetId

* change(kreivo-runtime): Use MutliLocationFungibleAsset as runtime's AssetId

* change(kreivo-runtime): configure XCM traders and transactors to use AsMultiLocationFungibleAsset converter

* chore(kreivo-runtime): implement tests to check call size

* fix(virto-common): use serde with no_std

* change(virto-common): apply requested changes in `FungibleAssetLocation` type

* fix(pallet-payments): on benchmarking, remove Zero bound and use Default instead for AssetIdOf<T>
  • Loading branch information
pandres95 authored Mar 29, 2024
1 parent fec51be commit 5f7e529
Show file tree
Hide file tree
Showing 14 changed files with 290 additions and 164 deletions.
26 changes: 4 additions & 22 deletions Cargo.lock

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

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ parity-scale-codec = { version = "3.6.4", default-features = false, features = [
futures = { version = "0.3.28" }
hex-literal = { version = "0.4.1" }
log = { version = "0.4.20" }
serde = { version = "1.0.188" }
serde = { version = "1.0.188", default-features = false, features = [
"derive"
] }
serde_json = "1.0.104"
scale-info = { version = "2.9.0", default-features = false, features = [
"derive",
Expand All @@ -53,7 +55,7 @@ pallet-asset-registry = { default-features = false, path = "pallets/asset-regist
pallet-burner = { default-features = false, path = "pallets/burner" }
pallet-payments = { default-features = false, path = "pallets/payments" }

virto-common = { path = "common" }
virto-common = { default-features = false, path = "common" }
runtime-common = { default-features = false, path = "runtime/common" }
kusama-runtime-constants = { default-features = false, path = "runtime/kusama-runtime-constants" }

Expand Down
19 changes: 15 additions & 4 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,33 @@ edition = "2021"
bs58 = { version = "0.5.0", default-features = false }
wasm-bindgen = { version = "0.2.87", optional = true }

pallet-payments = { workspace = true, optional = true }
cumulus-primitives-core = { workspace = true, optional = true }
frame-support = { workspace = true, optional = true }
pallet-payments = { workspace = true, optional = true }
parity-scale-codec = { workspace = true, optional = true }
scale-info = { workspace = true, optional = true }
serde = { workspace = true, optional = true, default-features = false }
sp-runtime = { workspace = true, optional = true }
xcm = { workspace = true, optional = true }

[dev-dependencies]
frame-support = { workspace = true, features = ["std"] }

[features]
default = ["runtime"]
default = ["std"]
std = [
"serde?/std",
]
alloc = []
js = ["alloc", "wasm-bindgen"]
nightly = []
runtime = [
"dep:cumulus-primitives-core",
"dep:frame-support",
"dep:parity-scale-codec",
"dep:scale-info",
"dep:pallet-payments",
"dep:parity-scale-codec",
"dep:serde",
"dep:scale-info",
"dep:sp-runtime",
"dep:xcm",
]
6 changes: 6 additions & 0 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ extern crate alloc;
use frame_support::pallet_prelude::{Decode, Encode, MaxEncodedLen, TypeInfo};

mod membership;
mod multilocation_asset_id;
mod payment_id;

pub use membership::{MembershipId, MembershipInfo};
pub use multilocation_asset_id::{FungibleAssetLocation, NetworkId};
pub use payment_id::PaymentId;

#[cfg(feature = "runtime")]
pub use multilocation_asset_id::runtime::AsFungibleAssetLocation;

#[cfg_attr(feature = "runtime", derive(Decode, Encode, MaxEncodedLen, TypeInfo))]
#[derive(Debug, Clone, Copy, Eq, Ord, PartialEq, PartialOrd)]
pub struct CommunityId(u16);
Expand Down
142 changes: 142 additions & 0 deletions common/src/multilocation_asset_id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
#[cfg(feature = "runtime")]
use {
frame_support::pallet_prelude::{Decode, Encode, MaxEncodedLen, TypeInfo},
serde::{Deserialize, Serialize},
};

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(
feature = "runtime",
derive(Encode, Decode, Serialize, Deserialize, MaxEncodedLen, TypeInfo)
)]
pub enum FungibleAssetLocation {
Here(u32),
Sibling {
id: u16,
pallet: u8,
index: u32,
},
External {
network: NetworkId,
id: u16,
pallet: u8,
index: u32,
},
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(
feature = "runtime",
derive(Encode, Decode, Serialize, Deserialize, MaxEncodedLen, TypeInfo)
)]
pub enum NetworkId {
Polkadot,
Kusama,
Ethereum { chain_id: u64 },
}

impl Default for FungibleAssetLocation {
fn default() -> Self {
Self::Here(0)
}
}

impl From<u32> for FungibleAssetLocation {
fn from(value: u32) -> Self {
FungibleAssetLocation::Here(value)
}
}

#[cfg(feature = "runtime")]
pub mod runtime {
use super::FungibleAssetLocation;
use cumulus_primitives_core::MultiLocation;
use sp_runtime::traits::MaybeEquivalence;
use xcm::v3::{
Junction::{GeneralIndex, GlobalConsensus, PalletInstance, Parachain},
Junctions, NetworkId,
};

impl TryFrom<NetworkId> for super::NetworkId {
type Error = &'static str;

fn try_from(value: NetworkId) -> Result<super::NetworkId, Self::Error> {
match value {
NetworkId::Polkadot => Ok(super::NetworkId::Polkadot),
NetworkId::Kusama => Ok(super::NetworkId::Kusama),
NetworkId::Ethereum { chain_id } => Ok(super::NetworkId::Ethereum { chain_id }),
_ => Err("This network is not supported"),
}
}
}

impl From<super::NetworkId> for NetworkId {
fn from(value: super::NetworkId) -> NetworkId {
match value {
super::NetworkId::Polkadot => NetworkId::Polkadot,
super::NetworkId::Kusama => NetworkId::Kusama,
super::NetworkId::Ethereum { chain_id } => NetworkId::Ethereum { chain_id },
}
}
}

pub struct AsFungibleAssetLocation;
impl MaybeEquivalence<MultiLocation, FungibleAssetLocation> for AsFungibleAssetLocation {
fn convert(value: &MultiLocation) -> Option<FungibleAssetLocation> {
match *value {
MultiLocation {
parents: 2,
interior:
Junctions::X4(GlobalConsensus(network), Parachain(id), PalletInstance(pallet), GeneralIndex(index)),
} => Some(FungibleAssetLocation::External {
network: network.try_into().ok()?,
id: id.try_into().ok()?,
pallet,
index: index.try_into().ok()?,
}),
MultiLocation {
parents: 1,
interior: Junctions::X3(Parachain(id), PalletInstance(pallet), GeneralIndex(index)),
} => Some(FungibleAssetLocation::Sibling {
id: id.try_into().ok()?,
pallet,
index: index.try_into().ok()?,
}),
MultiLocation {
parents: 0,
interior: Junctions::X2(PalletInstance(13), GeneralIndex(index)),
} => Some(FungibleAssetLocation::Here(
index.try_into().expect("as it is here, we the types will match; qed"),
)),
_ => None,
}
}

fn convert_back(value: &FungibleAssetLocation) -> Option<MultiLocation> {
match *value {
FungibleAssetLocation::Here(index) => Some(MultiLocation {
parents: 0,
interior: Junctions::X2(PalletInstance(13), GeneralIndex(index.into())),
}),
FungibleAssetLocation::Sibling { id, pallet, index } => Some(MultiLocation {
parents: 1,
interior: Junctions::X3(Parachain(id.into()), PalletInstance(pallet), GeneralIndex(index.into())),
}),
FungibleAssetLocation::External {
network,
id,
pallet,
index,
} => Some(MultiLocation {
parents: 2,
interior: Junctions::X4(
GlobalConsensus(network.into()),
Parachain(id.into()),
PalletInstance(pallet),
GeneralIndex(index.into()),
),
}),
}
}
}
}
20 changes: 10 additions & 10 deletions pallets/payments/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use frame_support::{

use frame_system::RawOrigin;
use log;
use sp_runtime::{traits::Zero, Percent};
use sp_runtime::Percent;
use sp_std::vec;

macro_rules! assert_has_event {
Expand Down Expand Up @@ -85,15 +85,15 @@ fn create_payment<T: Config>(

#[benchmarks(
where
<<T as Config>::Assets as Inspect<<T as frame_system::Config>::AccountId>>::AssetId: Zero,
<<T as Config>::Assets as Inspect<<T as frame_system::Config>::AccountId>>::AssetId: Default,
)]
mod benchmarks {
use super::*;
#[benchmark]
fn pay(q: Linear<1, { T::MaxRemarkLength::get() }>) -> Result<(), BenchmarkError> {
let (sender, beneficiary, _, beneficiary_lookup) = create_accounts::<T>();

let asset: AssetIdOf<T> = <AssetIdOf<T>>::zero();
let asset: AssetIdOf<T> = <AssetIdOf<T>>::default();
create_and_mint_asset::<T>(&sender, &beneficiary, &asset)?;
let amount = <BalanceOf<T>>::from(100000_u32);

Expand Down Expand Up @@ -122,7 +122,7 @@ mod benchmarks {
#[benchmark]
fn release() -> Result<(), BenchmarkError> {
let amount = <BalanceOf<T>>::from(100000_u32);
let asset = <AssetIdOf<T>>::zero();
let asset = <AssetIdOf<T>>::default();
let (payment_id, sender, _beneficiary, _, beneficiary_lookup) = create_payment::<T>(&amount, &asset, None)?;

log::info!("beneficiary_lookup: {:?}", beneficiary_lookup);
Expand All @@ -137,7 +137,7 @@ mod benchmarks {
#[benchmark]
fn cancel() -> Result<(), BenchmarkError> {
let amount = <BalanceOf<T>>::from(100000_u32);
let asset = <AssetIdOf<T>>::zero();
let asset = <AssetIdOf<T>>::default();
let (payment_id, _sender, beneficiary, _sender_lookup, _beneficiary_lookup) =
create_payment::<T>(&amount, &asset, None)?;

Expand All @@ -151,7 +151,7 @@ mod benchmarks {
#[benchmark]
fn request_refund() -> Result<(), BenchmarkError> {
let amount = <BalanceOf<T>>::from(100000_u32);
let asset = <AssetIdOf<T>>::zero();
let asset = <AssetIdOf<T>>::default();
let (payment_id, sender, _beneficiary, _sender_lookup, _beneficiary_lookup) =
create_payment::<T>(&amount, &asset, None)?;

Expand All @@ -169,7 +169,7 @@ mod benchmarks {
#[benchmark]
fn dispute_refund() -> Result<(), BenchmarkError> {
let amount = <BalanceOf<T>>::from(100000_u32);
let asset = <AssetIdOf<T>>::zero();
let asset = <AssetIdOf<T>>::default();
let (payment_id, sender, beneficiary, _sender_lookup, _beneficiary_lookup) =
create_payment::<T>(&amount, &asset, None)?;

Expand All @@ -188,7 +188,7 @@ mod benchmarks {
#[benchmark]
fn resolve_dispute() -> Result<(), BenchmarkError> {
let amount = <BalanceOf<T>>::from(100000_u32);
let asset = <AssetIdOf<T>>::zero();
let asset = <AssetIdOf<T>>::default();
let (payment_id, sender, beneficiary, _sender_lookup, _beneficiary_lookup) =
create_payment::<T>(&amount, &asset, None)?;

Expand Down Expand Up @@ -216,7 +216,7 @@ mod benchmarks {
#[benchmark]
fn request_payment() -> Result<(), BenchmarkError> {
let (sender, beneficiary, sender_lookup, _beneficiary_lookup) = create_accounts::<T>();
let asset: AssetIdOf<T> = <AssetIdOf<T>>::zero();
let asset: AssetIdOf<T> = <AssetIdOf<T>>::default();
create_and_mint_asset::<T>(&sender, &beneficiary, &asset)?;
let amount = <BalanceOf<T>>::from(100000_u32);

Expand All @@ -230,7 +230,7 @@ mod benchmarks {
#[benchmark]
fn accept_and_pay() -> Result<(), BenchmarkError> {
let (sender, beneficiary, _sender_lookup, _beneficiary_lookup) = create_accounts::<T>();
let asset: AssetIdOf<T> = <AssetIdOf<T>>::zero();
let asset: AssetIdOf<T> = <AssetIdOf<T>>::default();
create_and_mint_asset::<T>(&sender, &beneficiary, &asset)?;
let amount = <BalanceOf<T>>::from(100000_u32);

Expand Down
Loading

0 comments on commit 5f7e529

Please sign in to comment.