From cc4b3619b777116738a01f7866952948d5fe9f0e Mon Sep 17 00:00:00 2001 From: icodezjb Date: Wed, 7 Apr 2021 19:44:12 +0800 Subject: [PATCH] Check AssetId --- zenlink-protocol/src/lib.rs | 17 +++++++++++++++-- zenlink-protocol/src/primitives.rs | 12 ++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/zenlink-protocol/src/lib.rs b/zenlink-protocol/src/lib.rs index 8bfa8db..20c781d 100644 --- a/zenlink-protocol/src/lib.rs +++ b/zenlink-protocol/src/lib.rs @@ -152,6 +152,8 @@ decl_error! { DeniedTransferToSelf, /// Value too low to create account due to existential deposit ExistentialDeposit, + /// Invalid Asset Id. + InvalidAssetId, /// Trading pair can't be created. DeniedCreatePair, @@ -228,9 +230,10 @@ decl_module! { force_transfer: bool ) -> DispatchResult { let who = ensure_signed(origin)?; + ensure!(asset_id.is_valid(), Error::::InvalidAssetId); ensure!(para_id != T::ParaId::get(), Error::::DeniedTransferToSelf); ensure!(force_transfer || Self::is_reachable(para_id), Error::::DeniedReachTargetChain); - ensure!(force_transfer || Self::must_more_than_minimum(para_id, amount), Error::::ExistentialDeposit); + ensure!(force_transfer || Self::must_more_than_minimum(asset_id, para_id, amount), Error::::ExistentialDeposit); ensure!(Self::multi_asset_balance_of(&asset_id, &who) >= amount, Error::::InsufficientAssetBalance); let xcm = Self::make_xcm_transfer_to_parachain(&asset_id, para_id, &account, amount) @@ -426,7 +429,17 @@ impl Module { .any(|l| *l == make_x2_location(para_id.into())) } - pub(crate) fn must_more_than_minimum(para_id: ParaId, amount: TokenBalance) -> bool { + // Check the native currency must be more than ExistentialDeposit, + // other assets always return true + pub(crate) fn must_more_than_minimum( + asset_id: AssetId, + para_id: ParaId, + amount: TokenBalance, + ) -> bool { + if asset_id.module_index != NATIVE_CURRENCY { + return true; + } + T::TargetChains::get() .iter() .find(|(l, _)| *l == make_x2_location(para_id.into())) diff --git a/zenlink-protocol/src/primitives.rs b/zenlink-protocol/src/primitives.rs index f532ded..b9cfcdd 100644 --- a/zenlink-protocol/src/primitives.rs +++ b/zenlink-protocol/src/primitives.rs @@ -16,6 +16,7 @@ pub type TokenBalance = u128; /// The pair id of the zenlink dex. pub type PairId = u32; +// TODO: refactor AssetId /// AssetId use to locate assets in framed base chain. #[derive(Encode, Decode, Eq, PartialEq, Copy, Clone, RuntimeDebug, PartialOrd, Ord)] #[cfg_attr(feature = "std", derive(Serialize, Deserialize, Default))] @@ -25,6 +26,17 @@ pub struct AssetId { pub asset_index: u32, } +impl AssetId { + pub fn is_valid(&self) -> bool { + match self.module_index { + NATIVE_CURRENCY => matches!(self.asset_index, 0u32), + INNER_ASSET => true, + OTHER_ASSET => true, + _ => false, + } + } +} + /// Zenlink module has tow kinds of assets /// Foreign: The assets are generated by the other module /// Lp: The assets are generated by pair.