From f9233b933eed86005b6bc4de5d804689c4fbc74b Mon Sep 17 00:00:00 2001 From: zjb0807 Date: Tue, 21 Feb 2023 06:43:36 +0800 Subject: [PATCH 1/4] Add more methods on XcmTransfer trait --- traits/src/xcm_transfer.rs | 36 ++++++++++++++++++++++++++++++++---- xtokens/src/lib.rs | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 5 deletions(-) diff --git a/traits/src/xcm_transfer.rs b/traits/src/xcm_transfer.rs index c4dacb116..9bff880a6 100644 --- a/traits/src/xcm_transfer.rs +++ b/traits/src/xcm_transfer.rs @@ -3,7 +3,7 @@ use xcm::latest::prelude::*; /// Abstraction over cross-chain token transfers. pub trait XcmTransfer { - /// Transfer native currencies. + /// Transfer local assets with given `CurrencyId` and `Amount`. fn transfer( who: AccountId, currency_id: CurrencyId, @@ -12,15 +12,25 @@ pub trait XcmTransfer { dest_weight_limit: WeightLimit, ) -> DispatchResult; - /// Transfer `MultiAsset` - fn transfer_multi_asset( + /// Transfer `MultiAsset` assets. + fn transfer_multiasset( who: AccountId, asset: MultiAsset, dest: MultiLocation, dest_weight_limit: WeightLimit, ) -> DispatchResult; - /// Transfer `MultiAssetWithFee` + /// Transfer native currencies specifying the fee and amount as separate. + fn transfer_with_fee( + who: AccountId, + currency_id: CurrencyId, + amount: Balance, + fee: Balance, + dest: MultiLocation, + dest_weight_limit: WeightLimit, + ) -> DispatchResult; + + /// Transfer `MultiAsset` specifying the fee and amount as separate. fn transfer_multiasset_with_fee( who: AccountId, asset: MultiAsset, @@ -28,4 +38,22 @@ pub trait XcmTransfer { dest: MultiLocation, dest_weight_limit: WeightLimit, ) -> DispatchResult; + + /// Transfer several currencies specifying the item to be used as fee. + fn transfer_multicurrencies( + who: AccountId, + currencies: Vec<(CurrencyId, Balance)>, + fee_item: u32, + dest: MultiLocation, + dest_weight_limit: WeightLimit, + ) -> DispatchResult; + + /// Transfer several `MultiAsset` specifying the item to be used as fee. + fn transfer_multiassets( + who: AccountId, + assets: MultiAssets, + fee: MultiAsset, + dest: MultiLocation, + dest_weight_limit: WeightLimit, + ) -> DispatchResult; } diff --git a/xtokens/src/lib.rs b/xtokens/src/lib.rs index 0af581872..d70cf5024 100644 --- a/xtokens/src/lib.rs +++ b/xtokens/src/lib.rs @@ -952,7 +952,7 @@ pub mod module { } #[require_transactional] - fn transfer_multi_asset( + fn transfer_multiasset( who: T::AccountId, asset: MultiAsset, dest: MultiLocation, @@ -961,6 +961,18 @@ pub mod module { Self::do_transfer_multiasset(who, asset, dest, dest_weight_limit) } + #[require_transactional] + fn transfer_with_fee( + who: T::AccountId, + currency_id: T::CurrencyId, + amount: T::Balance, + fee: T::Balance, + dest: MultiLocation, + dest_weight_limit: WeightLimit, + ) -> DispatchResult { + Self::do_transfer_with_fee(who, currency_id, amount, fee, dest, dest_weight_limit) + } + #[require_transactional] fn transfer_multiasset_with_fee( who: T::AccountId, @@ -971,6 +983,28 @@ pub mod module { ) -> DispatchResult { Self::do_transfer_multiasset_with_fee(who, asset, fee, dest, dest_weight_limit) } + + #[require_transactional] + fn transfer_multicurrencies( + who: T::AccountId, + currencies: Vec<(T::CurrencyId, T::Balance)>, + fee_item: u32, + dest: MultiLocation, + dest_weight_limit: WeightLimit, + ) -> DispatchResult { + Self::do_transfer_multicurrencies(who, currencies, fee_item, dest, dest_weight_limit) + } + + #[require_transactional] + fn transfer_multiassets( + who: T::AccountId, + assets: MultiAssets, + fee: MultiAsset, + dest: MultiLocation, + dest_weight_limit: WeightLimit, + ) -> DispatchResult { + Self::do_transfer_multiassets(who, assets, fee, dest, dest_weight_limit) + } } } From e51b558133bd69fac05c15462df8dd6d3fc35687 Mon Sep 17 00:00:00 2001 From: zjb0807 Date: Thu, 2 Mar 2023 07:05:02 +0800 Subject: [PATCH 2/4] Add Transferred for XcmTransfer --- traits/src/xcm_transfer.rs | 21 ++++++++++----- xtokens/src/lib.rs | 52 ++++++++++++++++++++------------------ 2 files changed, 42 insertions(+), 31 deletions(-) diff --git a/traits/src/xcm_transfer.rs b/traits/src/xcm_transfer.rs index 9bff880a6..baef8bcee 100644 --- a/traits/src/xcm_transfer.rs +++ b/traits/src/xcm_transfer.rs @@ -1,6 +1,13 @@ -use frame_support::dispatch::DispatchResult; +use frame_support::dispatch::DispatchError; use xcm::latest::prelude::*; +pub struct Transferred { + pub sender: AccountId, + pub assets: MultiAssets, + pub fee: MultiAsset, + pub dest: MultiLocation, +} + /// Abstraction over cross-chain token transfers. pub trait XcmTransfer { /// Transfer local assets with given `CurrencyId` and `Amount`. @@ -10,7 +17,7 @@ pub trait XcmTransfer { amount: Balance, dest: MultiLocation, dest_weight_limit: WeightLimit, - ) -> DispatchResult; + ) -> Result, DispatchError>; /// Transfer `MultiAsset` assets. fn transfer_multiasset( @@ -18,7 +25,7 @@ pub trait XcmTransfer { asset: MultiAsset, dest: MultiLocation, dest_weight_limit: WeightLimit, - ) -> DispatchResult; + ) -> Result, DispatchError>; /// Transfer native currencies specifying the fee and amount as separate. fn transfer_with_fee( @@ -28,7 +35,7 @@ pub trait XcmTransfer { fee: Balance, dest: MultiLocation, dest_weight_limit: WeightLimit, - ) -> DispatchResult; + ) -> Result, DispatchError>; /// Transfer `MultiAsset` specifying the fee and amount as separate. fn transfer_multiasset_with_fee( @@ -37,7 +44,7 @@ pub trait XcmTransfer { fee: MultiAsset, dest: MultiLocation, dest_weight_limit: WeightLimit, - ) -> DispatchResult; + ) -> Result, DispatchError>; /// Transfer several currencies specifying the item to be used as fee. fn transfer_multicurrencies( @@ -46,7 +53,7 @@ pub trait XcmTransfer { fee_item: u32, dest: MultiLocation, dest_weight_limit: WeightLimit, - ) -> DispatchResult; + ) -> Result, DispatchError>; /// Transfer several `MultiAsset` specifying the item to be used as fee. fn transfer_multiassets( @@ -55,5 +62,5 @@ pub trait XcmTransfer { fee: MultiAsset, dest: MultiLocation, dest_weight_limit: WeightLimit, - ) -> DispatchResult; + ) -> Result, DispatchError>; } diff --git a/xtokens/src/lib.rs b/xtokens/src/lib.rs index d70cf5024..4f10abe30 100644 --- a/xtokens/src/lib.rs +++ b/xtokens/src/lib.rs @@ -52,6 +52,7 @@ use xcm_executor::traits::{InvertLocation, WeightBounds}; pub use module::*; use orml_traits::{ location::{Parse, Reserve}, + xcm_transfer::Transferred, GetByKey, XcmTransfer, }; @@ -217,7 +218,7 @@ pub mod module { ) -> DispatchResult { let who = ensure_signed(origin)?; let dest: MultiLocation = (*dest).try_into().map_err(|()| Error::::BadVersion)?; - Self::do_transfer(who, currency_id, amount, dest, dest_weight_limit) + Self::do_transfer(who, currency_id, amount, dest, dest_weight_limit).map(|_| ()) } /// Transfer `MultiAsset`. @@ -243,7 +244,7 @@ pub mod module { let who = ensure_signed(origin)?; let asset: MultiAsset = (*asset).try_into().map_err(|()| Error::::BadVersion)?; let dest: MultiLocation = (*dest).try_into().map_err(|()| Error::::BadVersion)?; - Self::do_transfer_multiasset(who, asset, dest, dest_weight_limit) + Self::do_transfer_multiasset(who, asset, dest, dest_weight_limit).map(|_| ()) } /// Transfer native currencies specifying the fee and amount as @@ -280,7 +281,7 @@ pub mod module { let who = ensure_signed(origin)?; let dest: MultiLocation = (*dest).try_into().map_err(|()| Error::::BadVersion)?; - Self::do_transfer_with_fee(who, currency_id, amount, fee, dest, dest_weight_limit) + Self::do_transfer_with_fee(who, currency_id, amount, fee, dest, dest_weight_limit).map(|_| ()) } /// Transfer `MultiAsset` specifying the fee and amount as separate. @@ -318,7 +319,7 @@ pub mod module { let fee: MultiAsset = (*fee).try_into().map_err(|()| Error::::BadVersion)?; let dest: MultiLocation = (*dest).try_into().map_err(|()| Error::::BadVersion)?; - Self::do_transfer_multiasset_with_fee(who, asset, fee, dest, dest_weight_limit) + Self::do_transfer_multiasset_with_fee(who, asset, fee, dest, dest_weight_limit).map(|_| ()) } /// Transfer several currencies specifying the item to be used as fee @@ -348,7 +349,7 @@ pub mod module { let who = ensure_signed(origin)?; let dest: MultiLocation = (*dest).try_into().map_err(|()| Error::::BadVersion)?; - Self::do_transfer_multicurrencies(who, currencies, fee_item, dest, dest_weight_limit) + Self::do_transfer_multicurrencies(who, currencies, fee_item, dest, dest_weight_limit).map(|_| ()) } /// Transfer several `MultiAsset` specifying the item to be used as fee @@ -382,7 +383,7 @@ pub mod module { // We first grab the fee let fee: &MultiAsset = assets.get(fee_item as usize).ok_or(Error::::AssetIndexNonExistent)?; - Self::do_transfer_multiassets(who, assets.clone(), fee.clone(), dest, dest_weight_limit) + Self::do_transfer_multiassets(who, assets.clone(), fee.clone(), dest, dest_weight_limit).map(|_| ()) } } @@ -393,7 +394,7 @@ pub mod module { amount: T::Balance, dest: MultiLocation, dest_weight_limit: WeightLimit, - ) -> DispatchResult { + ) -> Result, DispatchError> { let location: MultiLocation = T::CurrencyIdConvert::convert(currency_id).ok_or(Error::::NotCrossChainTransferableCurrency)?; @@ -414,7 +415,7 @@ pub mod module { fee: T::Balance, dest: MultiLocation, dest_weight_limit: WeightLimit, - ) -> DispatchResult { + ) -> Result, DispatchError> { let location: MultiLocation = T::CurrencyIdConvert::convert(currency_id).ok_or(Error::::NotCrossChainTransferableCurrency)?; @@ -441,7 +442,7 @@ pub mod module { asset: MultiAsset, dest: MultiLocation, dest_weight_limit: WeightLimit, - ) -> DispatchResult { + ) -> Result, DispatchError> { Self::do_transfer_multiassets(who, vec![asset.clone()].into(), asset, dest, dest_weight_limit) } @@ -451,15 +452,13 @@ pub mod module { fee: MultiAsset, dest: MultiLocation, dest_weight_limit: WeightLimit, - ) -> DispatchResult { + ) -> Result, DispatchError> { // Push contains saturated addition, so we should be able to use it safely let mut assets = MultiAssets::new(); assets.push(asset); assets.push(fee.clone()); - Self::do_transfer_multiassets(who, assets, fee, dest, dest_weight_limit)?; - - Ok(()) + Self::do_transfer_multiassets(who, assets, fee, dest, dest_weight_limit) } fn do_transfer_multicurrencies( @@ -468,7 +467,7 @@ pub mod module { fee_item: u32, dest: MultiLocation, dest_weight_limit: WeightLimit, - ) -> DispatchResult { + ) -> Result, DispatchError> { ensure!( currencies.len() <= T::MaxAssetsForTransfer::get(), Error::::TooManyAssetsBeingSent @@ -510,7 +509,7 @@ pub mod module { fee: MultiAsset, dest: MultiLocation, dest_weight_limit: WeightLimit, - ) -> DispatchResult { + ) -> Result, DispatchError> { ensure!( assets.len() <= T::MaxAssetsForTransfer::get(), Error::::TooManyAssetsBeingSent @@ -618,13 +617,18 @@ pub mod module { } Self::deposit_event(Event::::TransferredMultiAssets { + sender: who.clone(), + assets: assets.clone(), + fee: fee.clone(), + dest: dest.clone(), + }); + + Ok(Transferred { sender: who, assets, fee, dest, - }); - - Ok(()) + }) } /// Execute and send xcm with given assets and fee to dest chain or @@ -947,7 +951,7 @@ pub mod module { amount: T::Balance, dest: MultiLocation, dest_weight_limit: WeightLimit, - ) -> DispatchResult { + ) -> Result, DispatchError> { Self::do_transfer(who, currency_id, amount, dest, dest_weight_limit) } @@ -957,7 +961,7 @@ pub mod module { asset: MultiAsset, dest: MultiLocation, dest_weight_limit: WeightLimit, - ) -> DispatchResult { + ) -> Result, DispatchError> { Self::do_transfer_multiasset(who, asset, dest, dest_weight_limit) } @@ -969,7 +973,7 @@ pub mod module { fee: T::Balance, dest: MultiLocation, dest_weight_limit: WeightLimit, - ) -> DispatchResult { + ) -> Result, DispatchError> { Self::do_transfer_with_fee(who, currency_id, amount, fee, dest, dest_weight_limit) } @@ -980,7 +984,7 @@ pub mod module { fee: MultiAsset, dest: MultiLocation, dest_weight_limit: WeightLimit, - ) -> DispatchResult { + ) -> Result, DispatchError> { Self::do_transfer_multiasset_with_fee(who, asset, fee, dest, dest_weight_limit) } @@ -991,7 +995,7 @@ pub mod module { fee_item: u32, dest: MultiLocation, dest_weight_limit: WeightLimit, - ) -> DispatchResult { + ) -> Result, DispatchError> { Self::do_transfer_multicurrencies(who, currencies, fee_item, dest, dest_weight_limit) } @@ -1002,7 +1006,7 @@ pub mod module { fee: MultiAsset, dest: MultiLocation, dest_weight_limit: WeightLimit, - ) -> DispatchResult { + ) -> Result, DispatchError> { Self::do_transfer_multiassets(who, assets, fee, dest, dest_weight_limit) } } From c5dd46d0d52cf0997a904f6cb24115ca9b12e8a4 Mon Sep 17 00:00:00 2001 From: zjb0807 Date: Wed, 8 Mar 2023 03:10:16 +0800 Subject: [PATCH 3/4] fix tests --- traits/src/xcm_transfer.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/traits/src/xcm_transfer.rs b/traits/src/xcm_transfer.rs index baef8bcee..2fc4fe9d3 100644 --- a/traits/src/xcm_transfer.rs +++ b/traits/src/xcm_transfer.rs @@ -1,4 +1,5 @@ use frame_support::dispatch::DispatchError; +use sp_std::vec::Vec; use xcm::latest::prelude::*; pub struct Transferred { From 3c081f8a2cbe0d88977cb05369f3ef0ed61259aa Mon Sep 17 00:00:00 2001 From: zjb0807 Date: Mon, 13 Mar 2023 09:49:06 +0800 Subject: [PATCH 4/4] fix clippy --- xtokens/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xtokens/src/lib.rs b/xtokens/src/lib.rs index 67339576a..acac5520a 100644 --- a/xtokens/src/lib.rs +++ b/xtokens/src/lib.rs @@ -623,7 +623,7 @@ pub mod module { sender: who.clone(), assets: assets.clone(), fee: fee.clone(), - dest: dest.clone(), + dest, }); Ok(Transferred {