Skip to content

Commit

Permalink
add a call to freeze asset and create smart contract
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmadkaouk committed Oct 21, 2024
1 parent daec7fe commit d6bcca7
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

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

7 changes: 7 additions & 0 deletions pallets/moonbeam-lazy-migrations/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ frame-support = { workspace = true }
frame-system = { workspace = true }
pallet-scheduler = { workspace = true }
pallet-assets = { workspace = true }
pallet-asset-manager = { workspace = true }
pallet-balances = { workspace = true }
pallet-moonbeam-foreign-assets = { workspace = true }
parity-scale-codec = { workspace = true }
scale-info = { workspace = true, features = ["derive"] }
sp-core = { workspace = true }
sp-io = { workspace = true }
sp-runtime = { workspace = true }
sp-std = { workspace = true }
xcm-primitives = { workspace = true }

# Frontier
pallet-evm = { workspace = true, features = ["forbid-evm-reentrancy"] }
Expand Down Expand Up @@ -52,7 +55,11 @@ std = [
"pallet-evm/std",
"pallet-timestamp/std",
"pallet-assets/std",
"pallet-moonbeam-foreign-assets/std",
"pallet-asset-manager/std",
"cumulus-primitives-storage-weight-reclaim/std",
"rlp/std",
"xcm/std",
"xcm-primitives/std",
]
try-runtime = ["frame-support/try-runtime"]
56 changes: 54 additions & 2 deletions pallets/moonbeam-lazy-migrations/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ pub mod pallet {
use super::*;
use cumulus_primitives_storage_weight_reclaim::get_proof_size;
use frame_support::pallet_prelude::*;
use frame_support::traits::fungibles::metadata::Inspect;
use frame_system::pallet_prelude::*;
use sp_core::H160;
use xcm::latest::Location;
use xcm_primitives::AssetTypeGetter;

pub const ARRAY_LIMIT: u32 = 1000;
pub type GetArrayLimit = ConstU32<ARRAY_LIMIT>;
Expand Down Expand Up @@ -76,7 +79,16 @@ pub mod pallet {

/// Configuration trait of this pallet.
#[pallet::config]
pub trait Config: frame_system::Config + pallet_evm::Config + pallet_balances::Config {
pub trait Config:
frame_system::Config
+ pallet_evm::Config
+ pallet_balances::Config
+ pallet_assets::Config<AssetId = u128>
+ pallet_asset_manager::Config<AssetId = u128>
+ pallet_moonbeam_foreign_assets::Config
{
// Origin that is allowed to freeze foreign assets for migration
type ForeignAssetFreezerOrigin: EnsureOrigin<Self::RuntimeOrigin>;
type WeightInfo: WeightInfo;
}

Expand All @@ -94,6 +106,12 @@ pub mod pallet {
ContractNotExist,
/// The key lengths exceeds the maximum allowed
KeyTooLong,
/// The symbol length exceeds the maximum allowed
SymbolTooLong,
/// The name length exceeds the maximum allowed
NameTooLong,
/// The asset type was not found
AssetTypeNotFound,
}

pub(crate) const MAX_ITEM_PROOF_SIZE: u64 = 30 * 1024; // 30 KB
Expand Down Expand Up @@ -322,7 +340,8 @@ pub mod pallet {
}

#[pallet::call]
impl<T: Config> Pallet<T> {
impl<T: Config> Pallet<T>
where <T as pallet_asset_manager::Config>::ForeignAssetType: Into<Option<Location>>{
// TODO(rodrigo): This extrinsic should be removed once the storage of destroyed contracts
// has been removed
#[pallet::call_index(1)]
Expand Down Expand Up @@ -412,6 +431,39 @@ pub mod pallet {
)
.into())
}

// TODO update weights
#[pallet::call_index(3)]
#[pallet::weight(0)]
pub fn freeze_foreign_asset(
origin: OriginFor<T>,
asset_id: u128,
) -> DispatchResultWithPostInfo {
<T as pallet_moonbeam_foreign_assets::Config>::ForeignAssetFreezerOrigin::ensure_origin(origin.clone())?;

// Freeze the asset
pallet_assets::Pallet::<T>::freeze_asset(origin.clone(), asset_id.into())?;

let decimals = pallet_assets::Pallet::<T>::decimals(asset_id);
let symbol = pallet_assets::Pallet::<T>::symbol(asset_id)
.try_into()
.map_err(|_| Error::<T>::SymbolTooLong)?;
let name = <pallet_assets::Pallet<T> as Inspect<_>>::name(asset_id)
.try_into()
.map_err(|_| Error::<T>::NameTooLong)?;

let location: Option<Location> =
pallet_asset_manager::Pallet::<T>::get_asset_type(asset_id)
.ok_or(Error::<T>::AssetTypeNotFound)?
.into();

// Create the SC for the asset with moonbeam foreign assets pallet
pallet_moonbeam_foreign_assets::Pallet::<T>::create_foreign_asset(
origin, asset_id, location.unwrap(), decimals, symbol, name,
)?;

Ok(Pays::No.into())
}
}

impl<T: Config> Pallet<T> {
Expand Down
2 changes: 2 additions & 0 deletions runtime/moonbase/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ use sp_std::{
use sp_version::NativeVersion;
use sp_version::RuntimeVersion;
use xcm::{VersionedAssetId, VersionedAssets, VersionedLocation, VersionedXcm};
use xcm_config::ForeignAssetManagerOrigin;
use xcm_runtime_apis::{
dry_run::{CallDryRunEffects, Error as XcmDryRunApiError, XcmDryRunEffects},
fees::Error as XcmPaymentApiError,
Expand Down Expand Up @@ -1168,6 +1169,7 @@ impl pallet_migrations::Config for Runtime {
}

impl pallet_moonbeam_lazy_migrations::Config for Runtime {
type ForeignAssetFreezerOrigin = ForeignAssetManagerOrigin;
type WeightInfo = moonbase_weights::pallet_moonbeam_lazy_migrations::WeightInfo<Runtime>;
}

Expand Down
8 changes: 8 additions & 0 deletions runtime/moonbeam/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 +1158,15 @@ impl pallet_migrations::Config for Runtime {
type XcmExecutionManager = XcmExecutionManager;
}

pub type ForeignAssetFreezerOrigin = EitherOfDiverse<
EnsureRoot<AccountId>,
EitherOfDiverse<
pallet_collective::EnsureProportionMoreThan<AccountId, OpenTechCommitteeInstance, 5, 9>,
governance::custom_origins::GeneralAdmin,
>,
>;
impl pallet_moonbeam_lazy_migrations::Config for Runtime {
type ForeignAssetFreezerOrigin = ForeignAssetFreezerOrigin;
type WeightInfo = moonbeam_weights::pallet_moonbeam_lazy_migrations::WeightInfo<Runtime>;
}

Expand Down
9 changes: 9 additions & 0 deletions runtime/moonriver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,16 @@ impl pallet_migrations::Config for Runtime {
type XcmExecutionManager = XcmExecutionManager;
}

pub type ForeignAssetFreezerOrigin = EitherOfDiverse<
EnsureRoot<AccountId>,
EitherOfDiverse<
pallet_collective::EnsureProportionMoreThan<AccountId, OpenTechCommitteeInstance, 5, 9>,
governance::custom_origins::GeneralAdmin,
>,
>;

impl pallet_moonbeam_lazy_migrations::Config for Runtime {
type ForeignAssetFreezerOrigin = ForeignAssetFreezerOrigin;
type WeightInfo = moonriver_weights::pallet_moonbeam_lazy_migrations::WeightInfo<Runtime>;
}

Expand Down

0 comments on commit d6bcca7

Please sign in to comment.