Skip to content

Commit

Permalink
Check AssetId
Browse files Browse the repository at this point in the history
  • Loading branch information
icodezjb committed Apr 7, 2021
1 parent 13aa8c9 commit cc4b361
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
17 changes: 15 additions & 2 deletions zenlink-protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -228,9 +230,10 @@ decl_module! {
force_transfer: bool
) -> DispatchResult {
let who = ensure_signed(origin)?;
ensure!(asset_id.is_valid(), Error::<T>::InvalidAssetId);
ensure!(para_id != T::ParaId::get(), Error::<T>::DeniedTransferToSelf);
ensure!(force_transfer || Self::is_reachable(para_id), Error::<T>::DeniedReachTargetChain);
ensure!(force_transfer || Self::must_more_than_minimum(para_id, amount), Error::<T>::ExistentialDeposit);
ensure!(force_transfer || Self::must_more_than_minimum(asset_id, para_id, amount), Error::<T>::ExistentialDeposit);
ensure!(Self::multi_asset_balance_of(&asset_id, &who) >= amount, Error::<T>::InsufficientAssetBalance);

let xcm = Self::make_xcm_transfer_to_parachain(&asset_id, para_id, &account, amount)
Expand Down Expand Up @@ -426,7 +429,17 @@ impl<T: Config> Module<T> {
.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()))
Expand Down
12 changes: 12 additions & 0 deletions zenlink-protocol/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand All @@ -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.
Expand Down

0 comments on commit cc4b361

Please sign in to comment.