From f3502c21fa731db82d0b601714f9dcbc2624ed8d Mon Sep 17 00:00:00 2001 From: metricaez Date: Wed, 12 Feb 2025 15:06:27 -0300 Subject: [PATCH] feat: implemente xcm burned adapter for relay token --- Cargo.lock | 2 + runtime/common/Cargo.toml | 6 +- runtime/common/src/burner_adapter.rs | 95 ++++++++++++++++++++++++++++ runtime/common/src/lib.rs | 3 + runtime/mainnet/src/xcm_config.rs | 6 +- runtime/testnet/src/xcm_config.rs | 6 +- 6 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 runtime/common/src/burner_adapter.rs diff --git a/Cargo.lock b/Cargo.lock index 3a5e5494..fee5a776 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10630,6 +10630,7 @@ dependencies = [ "account", "frame-support", "frame-system", + "log", "pallet-balances", "parachains-common", "parity-scale-codec", @@ -10639,6 +10640,7 @@ dependencies = [ "sp-runtime", "sp-std", "staging-xcm", + "staging-xcm-executor", ] [[package]] diff --git a/runtime/common/Cargo.toml b/runtime/common/Cargo.toml index 253ebc0c..06ff5bad 100644 --- a/runtime/common/Cargo.toml +++ b/runtime/common/Cargo.toml @@ -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 } @@ -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 } @@ -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 = [ diff --git a/runtime/common/src/burner_adapter.rs b/runtime/common/src/burner_adapter.rs new file mode 100644 index 00000000..a6962f65 --- /dev/null +++ b/runtime/common/src/burner_adapter.rs @@ -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(PhantomData); +impl> TransactAsset for BurnerAdapter { + 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 { + 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 { + 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), + } + } +} diff --git a/runtime/common/src/lib.rs b/runtime/common/src/lib.rs index 8d66f4fe..4759bdb3 100644 --- a/runtime/common/src/lib.rs +++ b/runtime/common/src/lib.rs @@ -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; diff --git a/runtime/mainnet/src/xcm_config.rs b/runtime/mainnet/src/xcm_config.rs index 1a043306..b25c946a 100644 --- a/runtime/mainnet/src/xcm_config.rs +++ b/runtime/mainnet/src/xcm_config.rs @@ -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::{ @@ -95,8 +96,11 @@ pub type BridgedLocalAssetTransactor = FungibleAdapter< (), >; +pub type DotBurnerTransactor = BurnerAdapter>; + /// 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 diff --git a/runtime/testnet/src/xcm_config.rs b/runtime/testnet/src/xcm_config.rs index 355bdbdd..599f5bdc 100644 --- a/runtime/testnet/src/xcm_config.rs +++ b/runtime/testnet/src/xcm_config.rs @@ -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::{ @@ -97,8 +98,11 @@ pub type BridgedLocalAssetTransactor = FungibleAdapter< (), >; +pub type DotBurnerTransactor = BurnerAdapter>; + /// 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