Skip to content

Commit

Permalink
weight_to_fee implemeted in pallet-xc-asset-config
Browse files Browse the repository at this point in the history
  • Loading branch information
ipapandinas committed Sep 11, 2024
1 parent 0187025 commit 0546708
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 41 deletions.
44 changes: 22 additions & 22 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion pallets/xc-asset-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
//! `ExecutionPaymentRate` interface for fetching `units per second` if asset is supported payment asset
//! - `get_units_per_second`
//!
//! - `weight_to_fee` method is used to convert weight to fee based on units per second and weight.

#![cfg_attr(not(feature = "std"), no_std)]

Expand All @@ -72,7 +73,9 @@ pub use weights::WeightInfo;
pub mod pallet {

use crate::weights::WeightInfo;
use frame_support::{pallet_prelude::*, traits::EnsureOrigin};
use frame_support::{
pallet_prelude::*, traits::EnsureOrigin, weights::constants::WEIGHT_REF_TIME_PER_SECOND,
};
use frame_system::pallet_prelude::*;
use parity_scale_codec::HasCompact;
use sp_std::boxed::Box;
Expand Down Expand Up @@ -116,6 +119,14 @@ pub mod pallet {
}
}

impl<T: Config> Pallet<T> {
/// Convert weight to fee based on units per second and weight.
pub fn weight_to_fee(weight: Weight, units_per_second: u128) -> u128 {
units_per_second.saturating_mul(weight.ref_time() as u128)
/ (WEIGHT_REF_TIME_PER_SECOND as u128)
}
}

#[pallet::config]
pub trait Config: frame_system::Config {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
Expand Down
10 changes: 0 additions & 10 deletions primitives/src/xcm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
//! - `FixedRateOfForeignAsset` - weight trader for execution payment in foreign asset
//! - `ReserveAssetFilter` - used to check whether asset/origin are a valid reserve location
//! - `XcmFungibleFeeHandler` - used to handle XCM fee execution fees
//! - `WeightToForeignAssetFee` - used to convert weight to fee based on units per second and weight.
//!
//! Please refer to implementation below for more info.
//!
Expand Down Expand Up @@ -356,12 +355,3 @@ impl<T: Contains<Location>> ShouldExecute for AllowTopLevelPaidExecutionFrom<T>
Ok(())
}
}

pub struct WeightToForeignAssetFee;
impl WeightToForeignAssetFee {
/// Convert weight to fee based on units per second and weight.
pub fn weight_to_fee(weight: Weight, units_per_second: u128) -> u128 {
units_per_second.saturating_mul(weight.ref_time() as u128)
/ (WEIGHT_REF_TIME_PER_SECOND as u128)
}
}
4 changes: 2 additions & 2 deletions runtime/astar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ use astar_primitives::{
},
evm::EvmRevertCodeHandler,
oracle::{CurrencyAmount, CurrencyId, DummyCombineData, Price},
xcm::{AssetLocationIdConverter, WeightToForeignAssetFee},
xcm::AssetLocationIdConverter,
Address, AssetId, BlockNumber, Hash, Header, Nonce, UnfreezeChainOnFailedMigration,
};
pub use astar_primitives::{governance::OracleMembershipInst, AccountId, Balance, Signature};
Expand Down Expand Up @@ -1946,7 +1946,7 @@ impl_runtime_apis! {

match pallet_xc_asset_config::AssetLocationUnitsPerSecond::<Runtime>::get(versioned_location) {
Some(units_per_sec) => {
Ok(WeightToForeignAssetFee::weight_to_fee(weight, units_per_sec))
Ok(pallet_xc_asset_config::Pallet::<Runtime>::weight_to_fee(weight, units_per_sec))
}
None => Err(XcmPaymentApiError::AssetNotFound),
}
Expand Down
4 changes: 2 additions & 2 deletions runtime/shibuya/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ use astar_primitives::{
TechnicalCommitteeMembershipInst,
},
oracle::{CurrencyAmount, CurrencyId, DummyCombineData, Price},
xcm::{AssetLocationIdConverter, WeightToForeignAssetFee},
xcm::AssetLocationIdConverter,
Address, AssetId, BlockNumber, Hash, Header, Nonce, UnfreezeChainOnFailedMigration,
};
pub use astar_primitives::{AccountId, Balance, Signature};
Expand Down Expand Up @@ -2277,7 +2277,7 @@ impl_runtime_apis! {

match pallet_xc_asset_config::AssetLocationUnitsPerSecond::<Runtime>::get(versioned_location) {
Some(units_per_sec) => {
Ok(WeightToForeignAssetFee::weight_to_fee(weight, units_per_sec))
Ok(pallet_xc_asset_config::Pallet::<Runtime>::weight_to_fee(weight, units_per_sec))
}
None => Err(XcmPaymentApiError::AssetNotFound),
}
Expand Down
4 changes: 2 additions & 2 deletions runtime/shiden/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ use astar_primitives::{
evm::EvmRevertCodeHandler,
governance::OracleMembershipInst,
oracle::{CurrencyAmount, CurrencyId, DummyCombineData, Price},
xcm::{AssetLocationIdConverter, WeightToForeignAssetFee},
xcm::AssetLocationIdConverter,
Address, AssetId, BlockNumber, Hash, Header, Nonce, UnfreezeChainOnFailedMigration,
};
pub use astar_primitives::{AccountId, Balance, Signature};
Expand Down Expand Up @@ -1947,7 +1947,7 @@ impl_runtime_apis! {

match pallet_xc_asset_config::AssetLocationUnitsPerSecond::<Runtime>::get(versioned_location) {
Some(units_per_sec) => {
Ok(WeightToForeignAssetFee::weight_to_fee(weight, units_per_sec))
Ok(pallet_xc_asset_config::Pallet::<Runtime>::weight_to_fee(weight, units_per_sec))
}
None => Err(XcmPaymentApiError::AssetNotFound),
}
Expand Down
3 changes: 1 addition & 2 deletions tests/integration/src/xcm_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

use crate::setup::*;

pub use astar_primitives::xcm::WeightToForeignAssetFee;
use sp_runtime::traits::Zero;
use xcm::{
v4::{
Expand Down Expand Up @@ -140,7 +139,7 @@ fn query_weight_to_asset_fee_is_ok() {
);

let weight = Weight::from_parts(1_000_000_000, 1_000_000);
let expected_fee = WeightToForeignAssetFee::weight_to_fee(weight, units_per_second);
let expected_fee = XcAssetConfig::weight_to_fee(weight, units_per_second);
let fee =
Runtime::query_weight_to_asset_fee(weight, XcmAssetId(payable_location).into())
.expect("Must return fee for payable asset.");
Expand Down

0 comments on commit 0546708

Please sign in to comment.