From c5552f91d0bf55d1f1fe111195be6c8df6719099 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Wed, 22 Jan 2025 23:34:45 -0300 Subject: [PATCH 1/2] fix correct xcm sending --- runtime/src/xcm_config.rs | 108 ++++++++++++++++++++++++++++++-------- 1 file changed, 87 insertions(+), 21 deletions(-) diff --git a/runtime/src/xcm_config.rs b/runtime/src/xcm_config.rs index af87808..9455759 100644 --- a/runtime/src/xcm_config.rs +++ b/runtime/src/xcm_config.rs @@ -35,7 +35,7 @@ use xcm_builder::{ HashedDescription, IsConcrete, MintLocation, SignedAccountKey20AsNative, WithUniqueTopic, }; use xcm_executor::{ - traits::{TransactAsset, WeightTrader}, + traits::{ConvertLocation, TransactAsset, WeightTrader}, AssetsInHolding, XcmExecutor, }; @@ -113,32 +113,98 @@ impl Contains for LocalPlurality { } } -pub struct DoNothingRouter; -impl SendXcm for DoNothingRouter { - type Ticket = (); - fn validate(_dest: &mut Option, _msg: &mut Option>) -> SendResult<()> { - Ok(((), Assets::new())) - } - fn deliver(_: ()) -> Result { - Ok([0; 32]) - } -} - /// The barriers one of which must be passed for an XCM message to be executed. pub type Barrier = AllowUnpaidExecutionFrom; -pub struct DummyAssetTransactor; -impl TransactAsset for DummyAssetTransactor { - fn deposit_asset(_what: &Asset, _who: &Location, _context: Option<&XcmContext>) -> XcmResult { +pub struct AtletaAssetTransactor; +impl TransactAsset for AtletaAssetTransactor { + fn deposit_asset( + what: &Asset, + who: &Location, + _maybe_context: Option<&XcmContext>, + ) -> XcmResult { + log::trace!( + target: "xcm:AtletaAssetTransactor", + "deposit_asset: what: {:?}, who: {:?}", + what.clone(), who.clone(), + ); + + if what.id.0 != Location::here() { + log::error!( + target: "xcm:AtletaAssetTransactor", + "deposit_asset: Asset is not supported what: {:?}, who: {:?}", + what.clone(), who.clone(), + ); + return Err(XcmError::FailedToTransactAsset("Only ATLA token is supported")); + } + + let Fungibility::Fungible(amount) = what.fun else { + return Err(XcmError::FailedToTransactAsset( + "Not Funglible Assets transfers are not supported", + )); + }; + + let beneficiary = LocationConverter::convert_location(who) + .ok_or(XcmError::FailedToTransactAsset("Cannot convert parachain id to account id"))?; + + let new_amount = Balances::free_balance(beneficiary.clone()) + .checked_add(amount) + .ok_or(XcmError::Overflow)?; + let Ok(_) = Balances::force_set_balance(RuntimeOrigin::root(), beneficiary, new_amount) + else { + log::error!( + target: "xcm:AtletaAssetTransactor", + "deposit_asset: Balance Add Failed: amount: {:?}", + new_amount, + ); + return Err(XcmError::FailedToTransactAsset("Balance Add Failed")); + }; Ok(()) } fn withdraw_asset( - _what: &Asset, - _who: &Location, + what: &Asset, + who: &Location, _maybe_context: Option<&XcmContext>, ) -> Result { - let asset: Assets = (Parent, 100_000).into(); + log::trace!( + target: "xcm:AtletaAssetTransactor", + "withdraw_asset: what: {:?}, who: {:?}", + what.clone(), who.clone(), + ); + + if what.id.0 != Location::here() { + log::error!( + target: "xcm:AtletaAssetTransactor", + "withdraw_asset: Asset is not supported what: {:?}, who: {:?}", + what.clone(), who.clone(), + ); + return Err(XcmError::FailedToTransactAsset("Only ATLA token is supported")); + } + + let Fungibility::Fungible(amount) = what.fun else { + return Err(XcmError::FailedToTransactAsset( + "Not Funglible Assets transfers are not supported", + )); + }; + + let beneficiary = LocationConverter::convert_location(who) + .ok_or(XcmError::FailedToTransactAsset("Cannot convert parachain id to account id"))?; + + let new_amount = Balances::free_balance(beneficiary.clone()) + .checked_sub(amount) + .ok_or(XcmError::Overflow)?; + let Ok(_) = Balances::force_set_balance(RuntimeOrigin::root(), beneficiary, new_amount) + else { + log::error!( + target: "xcm:AtletaAssetTransactor", + "withdraw_asset: Balance Sub Failed: amount: {:?}", + new_amount, + ); + return Err(XcmError::FailedToTransactAsset("Balance Sub Failed")); + }; + + let asset: Assets = (Parent, new_amount).into(); Ok(asset.into()) } } @@ -167,8 +233,8 @@ type OriginConverter = ( pub struct XcmConfig; impl xcm_executor::Config for XcmConfig { type RuntimeCall = RuntimeCall; - type XcmSender = DoNothingRouter; - type AssetTransactor = DummyAssetTransactor; + type XcmSender = XcmRouter; + type AssetTransactor = AtletaAssetTransactor; type OriginConverter = OriginConverter; type IsReserve = (); type IsTeleporter = (); @@ -224,7 +290,7 @@ impl pallet_xcm::Config for Runtime { // Note that this configuration of `SendXcmOrigin` is different from the one present in // production. type SendXcmOrigin = xcm_builder::EnsureXcmOrigin; - type XcmRouter = DoNothingRouter; + type XcmRouter = XcmRouter; // Anyone can execute XCM messages locally. type ExecuteXcmOrigin = xcm_builder::EnsureXcmOrigin; type XcmExecuteFilter = Everything; From de38b812517c775508520aedc9a3163adcb4eaa7 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Thu, 23 Jan 2025 05:59:18 -0300 Subject: [PATCH 2/2] fix clippy --- runtime/src/xcm_config.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/runtime/src/xcm_config.rs b/runtime/src/xcm_config.rs index 9455759..30ebf93 100644 --- a/runtime/src/xcm_config.rs +++ b/runtime/src/xcm_config.rs @@ -147,7 +147,7 @@ impl TransactAsset for AtletaAssetTransactor { let beneficiary = LocationConverter::convert_location(who) .ok_or(XcmError::FailedToTransactAsset("Cannot convert parachain id to account id"))?; - let new_amount = Balances::free_balance(beneficiary.clone()) + let new_amount = Balances::free_balance(beneficiary) .checked_add(amount) .ok_or(XcmError::Overflow)?; let Ok(_) = Balances::force_set_balance(RuntimeOrigin::root(), beneficiary, new_amount) @@ -191,7 +191,7 @@ impl TransactAsset for AtletaAssetTransactor { let beneficiary = LocationConverter::convert_location(who) .ok_or(XcmError::FailedToTransactAsset("Cannot convert parachain id to account id"))?; - let new_amount = Balances::free_balance(beneficiary.clone()) + let new_amount = Balances::free_balance(beneficiary) .checked_sub(amount) .ok_or(XcmError::Overflow)?; let Ok(_) = Balances::force_set_balance(RuntimeOrigin::root(), beneficiary, new_amount)