Skip to content
10 changes: 10 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ dp-consensus = { path = "primitives/consensus", default-features = false }
dp-container-chain-genesis-data = { path = "primitives/container-chain-genesis-data", default-features = false }
dp-core = { path = "primitives/core", default-features = false }
dp-impl-tanssi-pallets-config = { path = "primitives/core", default-features = false }
dp-xcm-reserve = { path = "primitives/xcm-reserve", default-features = false }
test-relay-sproof-builder = { path = "test-sproof-builder", default-features = false }

# Moonkit (wasm)
Expand Down
4 changes: 4 additions & 0 deletions pallets/xcm-executor-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@ sp-io = { workspace = true }
sp-runtime = { workspace = true }
staging-xcm = { workspace = true }

# Dancekit
dp-xcm-reserve = { workspace = true }

# Moonkit
pallet-migrations = { workspace = true }


[features]
default = [ "std" ]
std = [
"dp-xcm-reserve/std",
"frame-benchmarking/std",
"frame-support/std",
"frame-system/std",
Expand Down
59 changes: 2 additions & 57 deletions pallets/xcm-executor-utils/src/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@

use {
crate::{Config, DefaultTrustPolicy, TrustPolicy},
dp_xcm_reserve::NativeAssetReserve,
frame_support::{pallet_prelude::*, traits::ContainsPair},
staging_xcm::latest::{Asset, Junction::Parachain, Junctions::Here, Location},
staging_xcm::latest::{Asset, Location},
};

/// Returns true if the policy allows this asset
Expand Down Expand Up @@ -74,62 +75,6 @@ where
}
}

// TODO: this should probably move to somewhere in the polkadot-sdk repo
pub struct NativeAssetReserve;
impl ContainsPair<Asset, Location> for NativeAssetReserve {
fn contains(asset: &Asset, origin: &Location) -> bool {
let reserve = if asset.id.0.parents == 0
&& !matches!(asset.id.0.first_interior(), Some(Parachain(_)))
{
Some(Location::here())
} else {
asset.id.0.chain_part()
};

if let Some(ref reserve) = reserve {
if reserve == origin {
return true;
}
}
false
}
}

pub trait Parse {
/// Returns the "chain" location part. It could be parent, sibling
/// parachain, or child parachain.
fn chain_part(&self) -> Option<Location>;
/// Returns "non-chain" location part.
fn non_chain_part(&self) -> Option<Location>;
}

impl Parse for Location {
fn chain_part(&self) -> Option<Location> {
match (self.parents, self.first_interior()) {
// sibling parachain
(1, Some(Parachain(id))) => Some(Location::new(1, [Parachain(*id)])),
// parent
(1, _) => Some(Location::parent()),
// children parachain
(0, Some(Parachain(id))) => Some(Location::new(0, [Parachain(*id)])),
_ => None,
}
}

fn non_chain_part(&self) -> Option<Location> {
let junctions = self.interior();
while matches!(junctions.first(), Some(Parachain(_))) {
let _ = junctions.clone().take_first();
}

if junctions.clone() != Here {
Some(Location::new(0, junctions.clone()))
} else {
None
}
}
}

#[cfg(test)]
mod test {
use {
Expand Down
28 changes: 28 additions & 0 deletions primitives/xcm-reserve/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[package]
name = "dp-xcm-reserve"
authors = { workspace = true }
description = "Dancekit xcm reserve primitives"
edition = "2021"
license = "GPL-3.0-only"
version = "0.1.0"

[package.metadata.docs.rs]
targets = [ "x86_64-unknown-linux-gnu" ]

[lints]
workspace = true

[dependencies]
log = { workspace = true }

# Substrate
frame-support = { workspace = true }
staging-xcm = { workspace = true }

[features]
default = [ "std" ]
std = [
"frame-support/std",
"log/std",
"staging-xcm/std",
]
58 changes: 58 additions & 0 deletions primitives/xcm-reserve/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (C) Moondance Labs Ltd.
// This file is part of Tanssi.

// Tanssi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Tanssi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Tanssi. If not, see <http://www.gnu.org/licenses/>

#![cfg_attr(not(feature = "std"), no_std)]

use {
frame_support::traits::ContainsPair,
staging_xcm::latest::{Asset, Junction::Parachain, Location},
};

// TODO: this should probably move to somewhere in the polkadot-sdk repo
pub struct NativeAssetReserve;
impl ContainsPair<Asset, Location> for NativeAssetReserve {
fn contains(asset: &Asset, origin: &Location) -> bool {
log::trace!(target: "xcm::contains", "NativeAssetReserve asset: {:?}, origin: {:?}", asset, origin);
let reserve = if asset.id.0.parents == 0
&& !matches!(asset.id.0.first_interior(), Some(Parachain(_)))
{
Some(Location::here())
} else {
chain_part(&asset.id.0)
};

if let Some(ref reserve) = reserve {
if reserve == origin {
return true;
}
}
false
}
}

/// Returns the "chain" location part. It could be parent, sibling
/// parachain, or child parachain.
pub fn chain_part(this: &Location) -> Option<Location> {
match (this.parents, this.first_interior()) {
// sibling parachain
(1, Some(Parachain(id))) => Some(Location::new(1, [Parachain(*id)])),
// parent
(1, _) => Some(Location::parent()),
// children parachain
(0, Some(Parachain(id))) => Some(Location::new(0, [Parachain(*id)])),
_ => None,
}
}
Loading