Skip to content

Commit

Permalink
feat: implemente xcm burned adapter for relay token
Browse files Browse the repository at this point in the history
  • Loading branch information
metricaez committed Feb 12, 2025
1 parent 30785be commit f3502c2
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

6 changes: 5 additions & 1 deletion runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ parity-scale-codec = { workspace = true, default-features = false, features = [
scale-info = { workspace = true, default-features = false, features = [
"derive",
] }
log = { workspace = true, default-features = false }

# Substrate
frame-support = { workspace = true, default-features = false }
Expand All @@ -26,6 +27,7 @@ sp-core = { workspace = true, default-features = false }
sp-std = { workspace = true, default-features = false }
pallet-balances = { workspace = true, default-features = false }
xcm = { workspace = true, default-features = false }
xcm-executor = { workspace = true, default-features = false }

parachains-common = { workspace = true, default-features = false }
polkadot-primitives = { workspace = true, default-features = false }
Expand All @@ -37,14 +39,16 @@ account = { workspace = true }
[features]
default = ["std"]
std = [
"account/std",
"frame-support/std",
"frame-system/std",
"log/std",
"sp-runtime/std",
"sp-core/std",
"sp-std/std",
"account/std",
"pallet-balances/std",
"xcm/std",
"xcm-executor/std",
]

runtime-benchmarks = [
Expand Down
95 changes: 95 additions & 0 deletions runtime/common/src/burner_adapter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
use crate::Balance;
use core::marker::PhantomData;
use xcm::latest::prelude::{Asset, Location, XcmContext, XcmError, XcmResult};
use xcm_executor::{
traits::{MatchesFungible, TransactAsset},
AssetsInHolding,
};
pub struct BurnerAdapter<Matcher>(PhantomData<Matcher>);
impl<Matcher: MatchesFungible<Balance>> TransactAsset for BurnerAdapter<Matcher> {
fn can_check_in(_origin: &Location, what: &Asset, _context: &XcmContext) -> XcmResult {
log::trace!(
target: "xcm::burner_adapter",
"can_check_in origin: {:?}, what: {:?}",
_origin, what
);
match Matcher::matches_fungible(what) {
Some(_) => Ok(()),
None => Err(XcmError::AssetNotFound),
}
}

fn check_in(_origin: &Location, _what: &Asset, _context: &XcmContext) {
// No-op
}

fn can_check_out(_dest: &Location, what: &Asset, _context: &XcmContext) -> XcmResult {
log::trace!(
target: "xcm::burner_adapter",
"can_check_out dest: {:?}, what: {:?}",
_dest, what
);
match Matcher::matches_fungible(what) {
Some(_) => Err(XcmError::Unimplemented),
None => Err(XcmError::AssetNotFound),
}
}

fn check_out(_dest: &Location, _what: &Asset, _context: &XcmContext) {
// No-op
}

fn deposit_asset(what: &Asset, _who: &Location, _context: Option<&XcmContext>) -> XcmResult {
// Only accept and do nothing with the matched asset
log::trace!(
target: "xcm::burner_adapter",
"deposit_asset what: {:?}, who: {:?}",
what, _who,
);
match Matcher::matches_fungible(what) {
Some(_) => Ok(()),
None => Err(XcmError::AssetNotFound),
}
}

fn withdraw_asset(
what: &Asset,
_who: &Location,
_maybe_context: Option<&XcmContext>,
) -> Result<AssetsInHolding, XcmError> {
log::trace!(
target: "xcm::burner_adapter",
"withdraw_asset called with asset: {:?}, who: {:?}, context: {:?}",
what, _who, _maybe_context
);
let matches = Matcher::matches_fungible(what);
match matches {
Some(_) => {
log::trace!(
target: "xcm::burner_adapter",
// Error propagrates as `AssetNotFound` in executor therefore the log.
"returning Unimplemented as we don't support withdrawals"
);
Err(XcmError::Unimplemented)
},
None => Err(XcmError::AssetNotFound),
}
}

fn internal_transfer_asset(
asset: &Asset,
_from: &Location,
_to: &Location,
_context: &XcmContext,
) -> Result<AssetsInHolding, XcmError> {
log::trace!(
target: "xcm::burner_adapter",
"internal_transfer_asset asset: {:?}, from: {:?}, to: {:?}",
asset, _from, _to,
);
match Matcher::matches_fungible(asset) {
Some(_) => Err(XcmError::Unimplemented),
None => Err(XcmError::AssetNotFound),
}
}
}
3 changes: 3 additions & 0 deletions runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#![cfg_attr(not(feature = "std"), no_std)]

pub mod burner_adapter;

use frame_support::weights::{constants::WEIGHT_REF_TIME_PER_SECOND, Weight};
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
Expand Down
6 changes: 5 additions & 1 deletion runtime/mainnet/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use hex_literal::hex;
use pallet_xcm::XcmPassthrough;
use parachains_common::xcm_config::ParentRelayOrSiblingParachains;
use polkadot_runtime_common::xcm_sender::ExponentialPrice;
use runtime_common::burner_adapter::BurnerAdapter;
use sp_std::vec::Vec;
use xcm::latest::prelude::*;
use xcm_builder::{
Expand Down Expand Up @@ -95,8 +96,11 @@ pub type BridgedLocalAssetTransactor = FungibleAdapter<
(),
>;

pub type DotBurnerTransactor = BurnerAdapter<IsConcrete<RelayLocation>>;

/// Means for transacting assets on this chain.
pub type AssetTransactors = (LocalAssetTransactor, BridgedLocalAssetTransactor);
pub type AssetTransactors =
(LocalAssetTransactor, BridgedLocalAssetTransactor, DotBurnerTransactor);

/// This is the type we use to convert an (incoming) XCM origin into a local `Origin` instance,
/// ready for dispatching a transaction with Xcm's `Transact`. There is an `OriginKind` which can
Expand Down
6 changes: 5 additions & 1 deletion runtime/testnet/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use hex_literal::hex;
use pallet_xcm::XcmPassthrough;
use parachains_common::xcm_config::ParentRelayOrSiblingParachains;
use polkadot_runtime_common::xcm_sender::ExponentialPrice;
use runtime_common::burner_adapter::BurnerAdapter;
use sp_std::vec::Vec;
use xcm::latest::prelude::*;
use xcm_builder::{
Expand Down Expand Up @@ -97,8 +98,11 @@ pub type BridgedLocalAssetTransactor = FungibleAdapter<
(),
>;

pub type DotBurnerTransactor = BurnerAdapter<IsConcrete<RelayLocation>>;

/// Means for transacting assets on this chain.
pub type AssetTransactors = (LocalAssetTransactor, BridgedLocalAssetTransactor);
pub type AssetTransactors =
(LocalAssetTransactor, BridgedLocalAssetTransactor, DotBurnerTransactor);

/// This is the type we use to convert an (incoming) XCM origin into a local `Origin` instance,
/// ready for dispatching a transaction with Xcm's `Transact`. There is an `OriginKind` which can
Expand Down

0 comments on commit f3502c2

Please sign in to comment.