Skip to content

Commit a075154

Browse files
committed
Expose Xtokens pallet weight (#898)
1 parent 30a2d8b commit a075154

File tree

3 files changed

+45
-27
lines changed

3 files changed

+45
-27
lines changed

traits/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub use rewards::RewardHandler;
2424
use scale_info::TypeInfo;
2525
#[cfg(feature = "std")]
2626
use serde::{Deserialize, Serialize};
27-
pub use xcm_transfer::XcmTransfer;
27+
pub use xcm_transfer::{XcmTransfer, XtokensWeightInfo};
2828

2929
pub mod arithmetic;
3030
pub mod asset_registry;

traits/src/xcm_transfer.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use frame_support::dispatch::DispatchError;
22
use sp_std::vec::Vec;
3-
use xcm::v3::prelude::*;
4-
3+
use xcm::{
4+
v3::{prelude::*, Weight},
5+
VersionedMultiAsset, VersionedMultiAssets, VersionedMultiLocation,
6+
};
57
pub struct Transferred<AccountId> {
68
pub sender: AccountId,
79
pub assets: MultiAssets,
@@ -65,3 +67,18 @@ pub trait XcmTransfer<AccountId, Balance, CurrencyId> {
6567
dest_weight_limit: WeightLimit,
6668
) -> Result<Transferred<AccountId>, DispatchError>;
6769
}
70+
71+
pub trait XtokensWeightInfo<AccountId, Balance, CurrencyId> {
72+
fn weight_of_transfer_multiasset(asset: &VersionedMultiAsset, dest: &VersionedMultiLocation) -> Weight;
73+
fn weight_of_transfer(currency_id: CurrencyId, amount: Balance, dest: &VersionedMultiLocation) -> Weight;
74+
fn weight_of_transfer_multicurrencies(
75+
currencies: &[(CurrencyId, Balance)],
76+
fee_item: &u32,
77+
dest: &VersionedMultiLocation,
78+
) -> Weight;
79+
fn weight_of_transfer_multiassets(
80+
assets: &VersionedMultiAssets,
81+
fee_item: &u32,
82+
dest: &VersionedMultiLocation,
83+
) -> Weight;
84+
}

xtokens/src/lib.rs

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ use xcm_executor::traits::WeightBounds;
5555
pub use module::*;
5656
use orml_traits::{
5757
location::{Parse, Reserve},
58-
xcm_transfer::Transferred,
58+
xcm_transfer::{Transferred, XtokensWeightInfo},
5959
GetByKey, XcmTransfer,
6060
};
6161

@@ -211,7 +211,7 @@ pub mod module {
211211
/// by the network, and if the receiving chain would handle
212212
/// messages correctly.
213213
#[pallet::call_index(0)]
214-
#[pallet::weight(Pallet::<T>::weight_of_transfer(currency_id.clone(), *amount, dest))]
214+
#[pallet::weight(XtokensWeight::<T>::weight_of_transfer(currency_id.clone(), *amount, dest))]
215215
pub fn transfer(
216216
origin: OriginFor<T>,
217217
currency_id: T::CurrencyId,
@@ -237,7 +237,7 @@ pub mod module {
237237
/// by the network, and if the receiving chain would handle
238238
/// messages correctly.
239239
#[pallet::call_index(1)]
240-
#[pallet::weight(Pallet::<T>::weight_of_transfer_multiasset(asset, dest))]
240+
#[pallet::weight(XtokensWeight::<T>::weight_of_transfer_multiasset(asset, dest))]
241241
pub fn transfer_multiasset(
242242
origin: OriginFor<T>,
243243
asset: Box<VersionedMultiAsset>,
@@ -272,7 +272,7 @@ pub mod module {
272272
/// by the network, and if the receiving chain would handle
273273
/// messages correctly.
274274
#[pallet::call_index(2)]
275-
#[pallet::weight(Pallet::<T>::weight_of_transfer(currency_id.clone(), *amount, dest))]
275+
#[pallet::weight(XtokensWeight::<T>::weight_of_transfer(currency_id.clone(), *amount, dest))]
276276
pub fn transfer_with_fee(
277277
origin: OriginFor<T>,
278278
currency_id: T::CurrencyId,
@@ -309,7 +309,7 @@ pub mod module {
309309
/// by the network, and if the receiving chain would handle
310310
/// messages correctly.
311311
#[pallet::call_index(3)]
312-
#[pallet::weight(Pallet::<T>::weight_of_transfer_multiasset(asset, dest))]
312+
#[pallet::weight(XtokensWeight::<T>::weight_of_transfer_multiasset(asset, dest))]
313313
pub fn transfer_multiasset_with_fee(
314314
origin: OriginFor<T>,
315315
asset: Box<VersionedMultiAsset>,
@@ -341,7 +341,7 @@ pub mod module {
341341
/// by the network, and if the receiving chain would handle
342342
/// messages correctly.
343343
#[pallet::call_index(4)]
344-
#[pallet::weight(Pallet::<T>::weight_of_transfer_multicurrencies(currencies, fee_item, dest))]
344+
#[pallet::weight(XtokensWeight::<T>::weight_of_transfer_multicurrencies(currencies, fee_item, dest))]
345345
pub fn transfer_multicurrencies(
346346
origin: OriginFor<T>,
347347
currencies: Vec<(T::CurrencyId, T::Balance)>,
@@ -371,7 +371,7 @@ pub mod module {
371371
/// by the network, and if the receiving chain would handle
372372
/// messages correctly.
373373
#[pallet::call_index(5)]
374-
#[pallet::weight(Pallet::<T>::weight_of_transfer_multiassets(assets, fee_item, dest))]
374+
#[pallet::weight(XtokensWeight::<T>::weight_of_transfer_multiassets(assets, fee_item, dest))]
375375
pub fn transfer_multiassets(
376376
origin: OriginFor<T>,
377377
assets: Box<VersionedMultiAssets>,
@@ -831,17 +831,31 @@ pub mod module {
831831
};
832832
Ok((transfer_kind, dest, reserve, recipient))
833833
}
834+
835+
/// Get reserve location by `assets` and `fee_item`. the `assets`
836+
/// includes fee asset and non fee asset. make sure assets have ge one
837+
/// asset. all non fee asset should share same reserve location.
838+
fn get_reserve_location(assets: &MultiAssets, fee_item: &u32) -> Option<MultiLocation> {
839+
let reserve_idx = if assets.len() == 1 {
840+
0
841+
} else {
842+
(*fee_item == 0) as usize
843+
};
844+
let asset = assets.get(reserve_idx);
845+
asset.and_then(T::ReserveProvider::reserve)
846+
}
834847
}
835848

849+
pub struct XtokensWeight<T>(PhantomData<T>);
836850
// weights
837-
impl<T: Config> Pallet<T> {
851+
impl<T: Config> XtokensWeightInfo<T::AccountId, T::Balance, T::CurrencyId> for XtokensWeight<T> {
838852
/// Returns weight of `transfer_multiasset` call.
839853
fn weight_of_transfer_multiasset(asset: &VersionedMultiAsset, dest: &VersionedMultiLocation) -> Weight {
840854
let asset: Result<MultiAsset, _> = asset.clone().try_into();
841855
let dest = dest.clone().try_into();
842856
if let (Ok(asset), Ok(dest)) = (asset, dest) {
843857
if let Ok((transfer_kind, dest, _, reserve)) =
844-
Self::transfer_kind(T::ReserveProvider::reserve(&asset), &dest)
858+
Pallet::<T>::transfer_kind(T::ReserveProvider::reserve(&asset), &dest)
845859
{
846860
let mut msg = match transfer_kind {
847861
SelfReserveAsset => Xcm(vec![TransferReserveAsset {
@@ -904,8 +918,8 @@ pub mod module {
904918
let assets: Result<MultiAssets, ()> = assets.clone().try_into();
905919
let dest = dest.clone().try_into();
906920
if let (Ok(assets), Ok(dest)) = (assets, dest) {
907-
let reserve_location = Self::get_reserve_location(&assets, fee_item);
908-
if let Ok((transfer_kind, dest, _, reserve)) = Self::transfer_kind(reserve_location, &dest) {
921+
let reserve_location = Pallet::<T>::get_reserve_location(&assets, fee_item);
922+
if let Ok((transfer_kind, dest, _, reserve)) = Pallet::<T>::transfer_kind(reserve_location, &dest) {
909923
let mut msg = match transfer_kind {
910924
SelfReserveAsset => Xcm(vec![TransferReserveAsset {
911925
assets,
@@ -928,19 +942,6 @@ pub mod module {
928942
}
929943
Weight::zero()
930944
}
931-
932-
/// Get reserve location by `assets` and `fee_item`. the `assets`
933-
/// includes fee asset and non fee asset. make sure assets have ge one
934-
/// asset. all non fee asset should share same reserve location.
935-
fn get_reserve_location(assets: &MultiAssets, fee_item: &u32) -> Option<MultiLocation> {
936-
let reserve_idx = if assets.len() == 1 {
937-
0
938-
} else {
939-
(*fee_item == 0) as usize
940-
};
941-
let asset = assets.get(reserve_idx);
942-
asset.and_then(T::ReserveProvider::reserve)
943-
}
944945
}
945946

946947
impl<T: Config> XcmTransfer<T::AccountId, T::Balance, T::CurrencyId> for Pallet<T> {

0 commit comments

Comments
 (0)