Skip to content

Commit

Permalink
hadnle non-native assets differently in token gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
hqwangningbo committed Jan 3, 2025
1 parent f25e7ec commit c344037
Showing 1 changed file with 92 additions and 39 deletions.
131 changes: 92 additions & 39 deletions pallets/token-gateway/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use frame_support::{
pallet_prelude::Weight,
traits::{
fungibles::{self, Mutate},
tokens::{fungible::Mutate as FungibleMutate, Preservation},
tokens::{fungible::Mutate as FungibleMutate, Fortitude, Precision, Preservation},
Currency, ExistenceRequirement,
},
};
Expand Down Expand Up @@ -124,6 +124,11 @@ pub mod pallet {
pub type SupportedAssets<T: Config> =
StorageMap<_, Blake2_128Concat, AssetId<T>, H256, OptionQuery>;

/// Assets that originate from this chain
#[pallet::storage]
pub type NativeAssets<T: Config> =
StorageMap<_, Blake2_128Concat, AssetId<T>, bool, ValueQuery>;

/// Assets supported by this instance of token gateway
/// A map of the token gateway asset id to the local asset id
#[pallet::storage]
Expand Down Expand Up @@ -238,17 +243,30 @@ pub mod pallet {
&who,
&Self::pallet_account(),
params.amount,
ExistenceRequirement::KeepAlive,
ExistenceRequirement::AllowDeath,
)?;
T::Decimals::get()
} else {
<T as Config>::Assets::transfer(
params.asset_id.clone(),
&who,
&Self::pallet_account(),
params.amount.into(),
Preservation::Protect,
)?;
let is_native = NativeAssets::<T>::get(params.asset_id.clone());
if is_native {
<T as Config>::Assets::transfer(
params.asset_id.clone(),
&who,
&Self::pallet_account(),
params.amount.into(),
Preservation::Expendable,
)?;
} else {
// Assets that do not originate from this chain are burned
<T as Config>::Assets::burn_from(
params.asset_id.clone(),
&who,
params.amount.into(),
Preservation::Expendable,
Precision::Exact,
Fortitude::Polite,
)?;
}
params
.asset_id
.decimals()
Expand Down Expand Up @@ -326,11 +344,13 @@ pub mod pallet {
///
/// This works by dispatching a request to the TokenGateway module on each requested chain
/// to create the asset.
/// `native` should be true if this asset originates from this chain
#[pallet::call_index(2)]
#[pallet::weight(weight())]
pub fn create_erc6160_asset(
origin: OriginFor<T>,
asset: AssetRegistration<AssetId<T>>,
native: bool,
) -> DispatchResult {
T::ControlOrigin::ensure_origin(origin)?;
let who = T::AssetAdmin::get();
Expand All @@ -355,6 +375,7 @@ pub mod pallet {
// the mapping to its token gateway asset id

SupportedAssets::<T>::insert(asset.local_id.clone(), asset_id.clone());
NativeAssets::<T>::insert(asset.local_id.clone(), native);
LocalAssets::<T>::insert(asset_id, asset.local_id.clone());
// All ERC6160 assets use 18 decimals
Decimals::<T>::insert(asset.local_id, 18);
Expand Down Expand Up @@ -569,21 +590,35 @@ where
},
})?;
} else {
<T as Config>::Assets::transfer(
local_asset_id,
&Pallet::<T>::pallet_account(),
&beneficiary,
amount.into(),
Preservation::Protect,
)
.map_err(|_| ismp::error::Error::ModuleDispatchError {
msg: "Token Gateway: Failed to complete asset transfer".to_string(),
meta: Meta {
source,
dest,
nonce,
},
})?;
// Assets that do not originate from this chain are minted
let is_native = NativeAssets::<T>::get(local_asset_id.clone());
if is_native {
<T as Config>::Assets::transfer(
local_asset_id,
&Pallet::<T>::pallet_account(),
&beneficiary,
amount.into(),
Preservation::Expendable,
)
.map_err(|_| ismp::error::Error::ModuleDispatchError {
msg: "Token Gateway: Failed to complete asset transfer".to_string(),
meta: Meta {
source,
dest,
nonce,
},
})?;
} else {
<T as Config>::Assets::mint_into(local_asset_id, &beneficiary, amount.into())
.map_err(|_| ismp::error::Error::ModuleDispatchError {
msg: "Token Gateway: Failed to complete asset transfer".to_string(),
meta: Meta {
source,
dest,
nonce,
},
})?;
}
}

Self::deposit_event(Event::<T>::AssetReceived {
Expand Down Expand Up @@ -670,21 +705,39 @@ where
},
})?;
} else {
<T as Config>::Assets::transfer(
local_asset_id,
&Pallet::<T>::pallet_account(),
&beneficiary,
amount.into(),
Preservation::Protect,
)
.map_err(|_| ismp::error::Error::ModuleDispatchError {
msg: "Token Gateway: Failed to complete asset transfer".to_string(),
meta: Meta {
source,
dest,
nonce,
},
})?;
// Assets that do not originate from this chain are minted
let is_native = NativeAssets::<T>::get(local_asset_id.clone());
if is_native {
<T as Config>::Assets::transfer(
local_asset_id,
&Pallet::<T>::pallet_account(),
&beneficiary,
amount.into(),
Preservation::Expendable,
)
.map_err(|_| ismp::error::Error::ModuleDispatchError {
msg: "Token Gateway: Failed to complete asset transfer".to_string(),
meta: Meta {
source,
dest,
nonce,
},
})?;
} else {
<T as Config>::Assets::mint_into(
local_asset_id,
&beneficiary,
amount.into(),
)
.map_err(|_| ismp::error::Error::ModuleDispatchError {
msg: "Token Gateway: Failed to complete asset transfer".to_string(),
meta: Meta {
source,
dest,
nonce,
},
})?;
}
}

Pallet::<T>::deposit_event(Event::<T>::AssetRefunded {
Expand Down

0 comments on commit c344037

Please sign in to comment.