From f094d6b081d9fbcbde5c87f0e2c3118ad321100e Mon Sep 17 00:00:00 2001 From: Freddy Li Date: Tue, 5 Dec 2023 10:49:42 -0500 Subject: [PATCH] finish the transfer kind logic --- traits/src/lib.rs | 4 ++++ xcm-bridge/src/lib.rs | 23 ++++++++++++++++------- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/traits/src/lib.rs b/traits/src/lib.rs index 1063be8..23a1eec 100644 --- a/traits/src/lib.rs +++ b/traits/src/lib.rs @@ -82,3 +82,7 @@ pub trait Bridge { dest: MultiLocation, ) -> DispatchResult; } + +pub trait AssetReserveLocationParser { + fn reserved_location(asset: &MultiAsset) -> Option; +} \ No newline at end of file diff --git a/xcm-bridge/src/lib.rs b/xcm-bridge/src/lib.rs index 597ab51..7e1d6bd 100644 --- a/xcm-bridge/src/lib.rs +++ b/xcm-bridge/src/lib.rs @@ -19,6 +19,11 @@ pub mod pallet { type Weigher: WeightBounds; type XcmExecutor: ExecuteXcm; + + #[pallet::constant] + type SelfLocation: Get; + + type ReserveLocationParser: AssetReserveLocationParser; } enum TransferKind { @@ -51,18 +56,22 @@ pub mod pallet { } pub trait XcmHandler { - fn transfer_kind(&self) -> Result, DispatchError>; + fn transfer_kind(&self) -> Result; fn create_instructions(&self) -> Result, DispatchError>; fn execute_instructions(&self, xcm_message: Xcm) -> DispatchResult; } impl XcmHandler for Xcm { /// Get the transfer kind. - fn transfer_kind(&self) -> Result, DispatchError> { - // todo: impl the xcm kind logic, return the following: - // SelfReserveAsset, - // ToReserve, - // ToNonReserve, + fn transfer_kind(&self) -> Result { + let asset_location = T::ReserveLocationParser::reserved_location(&self.asset).ok_or()?; + if asset_location == T::SelfLocation { + TransferKind::SelfReserveAsset + } else if asset_location == self.dest { + TransferKind::ToReserve + } else { + TransferKind::ToNonReserve + } } fn create_instructions(&self) { let kind = Self::transfer_kind(&self)?; @@ -120,7 +129,7 @@ pub mod pallet { weight: XCMWeight::from_parts(6_000_000_000u64, 2_000_000u64), }; let mut msg = xcm.create_instructions()?; - // TODO: execute xcm message + xcm.execute_instructions(msg)?; Ok(())