Skip to content

Commit

Permalink
modify foreign assets origin to use ForeignAssetOwnerOrigin
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzamontiel committed Jan 13, 2025
1 parent 251497f commit 5e62ad1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 38 deletions.
25 changes: 16 additions & 9 deletions pallets/moonbeam-foreign-assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ pub enum AssetStatus {
#[pallet]
pub mod pallet {
use super::*;
use frame_support::traits::{Currency, ReservableCurrency};
use frame_support::traits::{Currency, EnsureOriginWithArg, ReservableCurrency};
use pallet_evm::{GasWeightMapping, Runner};
use sp_runtime::traits::{AccountIdConversion, AtLeast32BitUnsigned, Convert};
use xcm_executor::traits::ConvertLocation;
Expand All @@ -133,17 +133,17 @@ pub mod pallet {
type EvmRunner: Runner<Self>;

/// Origin that is allowed to create a new foreign assets
type ForeignAssetCreatorOrigin: EnsureOrigin<Self::RuntimeOrigin>;
type ForeignAssetCreatorOrigin: EnsureOriginWithArg<Self::RuntimeOrigin, Location>;

/// Origin that is allowed to freeze all tokens of a foreign asset
type ForeignAssetFreezerOrigin: EnsureOrigin<Self::RuntimeOrigin>;
type ForeignAssetFreezerOrigin: EnsureOriginWithArg<Self::RuntimeOrigin, Location>;

/// Origin that is allowed to modify asset information for foreign assets
type ForeignAssetModifierOrigin: EnsureOrigin<Self::RuntimeOrigin>;
type ForeignAssetModifierOrigin: EnsureOriginWithArg<Self::RuntimeOrigin, Location>;

/// Origin that is allowed to unfreeze all tokens of a foreign asset that was previously
/// frozen
type ForeignAssetUnfreezerOrigin: EnsureOrigin<Self::RuntimeOrigin>;
type ForeignAssetUnfreezerOrigin: EnsureOriginWithArg<Self::RuntimeOrigin, Location>;

/// Hook to be called when new foreign asset is registered.
type OnForeignAssetCreated: ForeignAssetCreatedHook<Location>;
Expand Down Expand Up @@ -410,7 +410,7 @@ pub mod pallet {
symbol: BoundedVec<u8, ConstU32<256>>,
name: BoundedVec<u8, ConstU32<256>>,
) -> DispatchResult {
T::ForeignAssetCreatorOrigin::ensure_origin(origin.clone())?;
T::ForeignAssetCreatorOrigin::ensure_origin(origin.clone(), &xcm_location)?;

// Ensure such an assetId does not exist
ensure!(
Expand Down Expand Up @@ -478,7 +478,8 @@ pub mod pallet {
asset_id: AssetId,
new_xcm_location: Location,
) -> DispatchResult {
T::ForeignAssetModifierOrigin::ensure_origin(origin)?;
// Ensures that the origin is an XCM location that contains the asset
T::ForeignAssetModifierOrigin::ensure_origin(origin, &new_xcm_location)?;

let previous_location =
AssetsById::<T>::get(&asset_id).ok_or(Error::<T>::AssetDoesNotExist)?;
Expand Down Expand Up @@ -511,11 +512,15 @@ pub mod pallet {
asset_id: AssetId,
allow_xcm_deposit: bool,
) -> DispatchResult {
T::ForeignAssetFreezerOrigin::ensure_origin(origin)?;
ensure_signed(origin.clone())?;

let xcm_location =
AssetsById::<T>::get(&asset_id).ok_or(Error::<T>::AssetDoesNotExist)?;

// Ensures that the origin is an XCM location that owns the asset
// represented by the assets xcm location
T::ForeignAssetFreezerOrigin::ensure_origin(origin, &xcm_location)?;

let (_asset_id, asset_status) = AssetsByLocation::<T>::get(&xcm_location)
.ok_or(Error::<T>::CorruptedStorageOrphanLocation)?;

Expand Down Expand Up @@ -545,10 +550,12 @@ pub mod pallet {
#[pallet::call_index(3)]
#[pallet::weight(<T as Config>::WeightInfo::unfreeze_foreign_asset())]
pub fn unfreeze_foreign_asset(origin: OriginFor<T>, asset_id: AssetId) -> DispatchResult {
T::ForeignAssetUnfreezerOrigin::ensure_origin(origin)?;
ensure_signed(origin.clone())?;

let xcm_location =
AssetsById::<T>::get(&asset_id).ok_or(Error::<T>::AssetDoesNotExist)?;
// Ensures that the origin is an XCM location that contains the asset
T::ForeignAssetUnfreezerOrigin::ensure_origin(origin, &xcm_location)?;

let (_asset_id, asset_status) = AssetsByLocation::<T>::get(&xcm_location)
.ok_or(Error::<T>::CorruptedStorageOrphanLocation)?;
Expand Down
22 changes: 9 additions & 13 deletions runtime/moonbase/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use frame_support::{
traits::{EitherOfDiverse, Everything, Nothing, PalletInfoAccess, TransformOrigin},
};

use frame_system::{EnsureRoot, EnsureSigned, RawOrigin};
use frame_system::{EnsureRoot, RawOrigin};
use sp_core::{ConstU32, H160, H256};
use sp_weights::Weight;
use xcm_builder::{
Expand Down Expand Up @@ -67,14 +67,14 @@ use xcm_primitives::{
use parity_scale_codec::{Decode, Encode};
use scale_info::TypeInfo;

use crate::foreign_origin::ForeignAssetOwnerOrigin;
use crate::governance::referenda::{FastGeneralAdminOrRoot, GeneralAdminOrRoot};
use sp_core::Get;
use sp_std::{
convert::{From, Into, TryFrom},
prelude::*,
};

use crate::governance::referenda::{FastGeneralAdminOrRoot, GeneralAdminOrRoot};

parameter_types! {
// The network Id of the relay
pub const RelayNetwork: NetworkId = NetworkId::Westend;
Expand All @@ -98,8 +98,9 @@ parameter_types! {
}

/// Type for specifying how a `Location` can be converted into an `AccountId`. This is used
/// when determining ownership of accounts for asset transacting and when attempting to use XCM
/// `Transact` in order to determine the dispatch Origin.
/// when determining ownership of accounts for asset transacting, when attempting to use XCM
/// `Transact` in order to determine the dispatch Origin, and when validating foreign assets
/// creation and ownership through the moonbeam_foreign_assets pallet.
pub type LocationToAccountId = (
// The parent (Relay-chain) origin converts to the default `AccountId`.
ParentIsPreset<AccountId>,
Expand Down Expand Up @@ -702,19 +703,14 @@ parameter_types! {
runtime_params::dynamic_params::xcm_config::ForeignAssetCreationDeposit::get();
}

pub type ForeignAssetManagerOrigin = EitherOfDiverse<
EnsureRoot<AccountId>,
EitherOfDiverse<
pallet_collective::EnsureProportionMoreThan<AccountId, OpenTechCommitteeInstance, 5, 9>,
governance::custom_origins::FastGeneralAdmin,
>,
>;
pub type ForeignAssetManagerOrigin =
EitherOfDiverse<EnsureRoot<AccountId>, ForeignAssetOwnerOrigin>;

impl pallet_moonbeam_foreign_assets::Config for Runtime {
type AccountIdToH160 = AccountIdToH160;
type AssetIdFilter = EvmForeignAssetIdFilter;
type EvmRunner = EvmRunnerPrecompileOrEthXcm<MoonbeamCall, Self>;
type ForeignAssetCreatorOrigin = EnsureSigned<AccountId>;
type ForeignAssetCreatorOrigin = ForeignAssetManagerOrigin;
type ForeignAssetFreezerOrigin = ForeignAssetManagerOrigin;
type ForeignAssetModifierOrigin = ForeignAssetManagerOrigin;
type ForeignAssetUnfreezerOrigin = ForeignAssetManagerOrigin;
Expand Down
10 changes: 3 additions & 7 deletions runtime/moonbeam/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ use xcm_primitives::{
use parity_scale_codec::{Decode, Encode};
use scale_info::TypeInfo;

use crate::foreign_origin::ForeignAssetOwnerOrigin;
use crate::governance::referenda::{FastGeneralAdminOrRoot, GeneralAdminOrRoot};
use sp_core::Get;
use sp_std::{
Expand Down Expand Up @@ -680,13 +681,8 @@ impl frame_support::traits::Contains<AssetId> for EvmForeignAssetIdFilter {
}
}

pub type ForeignAssetManagerOrigin = EitherOfDiverse<
EnsureRoot<AccountId>,
EitherOfDiverse<
pallet_collective::EnsureProportionMoreThan<AccountId, OpenTechCommitteeInstance, 5, 9>,
governance::custom_origins::FastGeneralAdmin,
>,
>;
pub type ForeignAssetManagerOrigin =
EitherOfDiverse<EnsureRoot<AccountId>, ForeignAssetOwnerOrigin>;

parameter_types! {
/// Balance in the native currency that will be reserved from the user
Expand Down
14 changes: 5 additions & 9 deletions runtime/moonriver/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use sp_runtime::{
};
use sp_weights::Weight;

use frame_system::{EnsureRoot, EnsureSigned, RawOrigin};
use frame_system::{EnsureRoot, RawOrigin};
use sp_core::{ConstU32, H160, H256};

use xcm_builder::{
Expand Down Expand Up @@ -66,6 +66,7 @@ use xcm_primitives::{
use parity_scale_codec::{Decode, Encode};
use scale_info::TypeInfo;

use crate::foreign_origin::ForeignAssetOwnerOrigin;
use crate::governance::referenda::{FastGeneralAdminOrRoot, GeneralAdminOrRoot};
use sp_core::Get;
use sp_std::{
Expand Down Expand Up @@ -700,19 +701,14 @@ parameter_types! {
runtime_params::dynamic_params::xcm_config::ForeignAssetCreationDeposit::get();
}

pub type ForeignAssetManagerOrigin = EitherOfDiverse<
EnsureRoot<AccountId>,
EitherOfDiverse<
pallet_collective::EnsureProportionMoreThan<AccountId, OpenTechCommitteeInstance, 5, 9>,
governance::custom_origins::FastGeneralAdmin,
>,
>;
pub type ForeignAssetManagerOrigin =
EitherOfDiverse<EnsureRoot<AccountId>, ForeignAssetOwnerOrigin>;

impl pallet_moonbeam_foreign_assets::Config for Runtime {
type AccountIdToH160 = AccountIdToH160;
type AssetIdFilter = EvmForeignAssetIdFilter;
type EvmRunner = EvmRunnerPrecompileOrEthXcm<MoonbeamCall, Self>;
type ForeignAssetCreatorOrigin = EnsureSigned<AccountId>;
type ForeignAssetCreatorOrigin = ForeignAssetManagerOrigin;
type ForeignAssetFreezerOrigin = ForeignAssetManagerOrigin;
type ForeignAssetModifierOrigin = ForeignAssetManagerOrigin;
type ForeignAssetUnfreezerOrigin = ForeignAssetManagerOrigin;
Expand Down

0 comments on commit 5e62ad1

Please sign in to comment.