12345678910111213141516171819use super::*;20use frame_system::Config as SystemConfig;2122use frame_support::traits::tokens::{DepositConsequence, WithdrawConsequence};23use pallet_common::CollectionHandle;24use pallet_fungible::FungibleHandle;25use pallet_common::CommonCollectionOperations;26use up_data_structs::budget::Value;27use sp_runtime::traits::{CheckedAdd, CheckedSub};2829impl<T: Config> fungibles::Inspect<<T as SystemConfig>::AccountId> for Pallet<T>30where31 T: orml_tokens::Config<CurrencyId = AssetIds>,32 BalanceOf<T>: From<<T as pallet_balances::Config>::Balance>,33 BalanceOf<T>: From<<T as orml_tokens::Config>::Balance>,34 <T as pallet_balances::Config>::Balance: From<BalanceOf<T>>,35 <T as orml_tokens::Config>::Balance: From<BalanceOf<T>>,36{37 type AssetId = AssetIds;38 type Balance = BalanceOf<T>;3940 fn total_issuance(asset: Self::AssetId) -> Self::Balance {41 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible total_issuance");4243 match asset {44 AssetIds::NativeAssetId(NativeCurrency::Here) => {45 <pallet_balances::Pallet<T> as fungible::Inspect<T::AccountId>>::total_issuance()46 .into()47 }48 AssetIds::NativeAssetId(NativeCurrency::Parent) => {49 <orml_tokens::Pallet<T> as fungibles::Inspect<T::AccountId>>::total_issuance(50 AssetIds::NativeAssetId(NativeCurrency::Parent),51 )52 .into()53 }54 AssetIds::ForeignAssetId(fid) => {55 let target_collection_id = match <AssetBinding<T>>::get(fid) {56 Some(v) => v,57 None => return Zero::zero(),58 };59 let collection_handle = match <CollectionHandle<T>>::try_get(target_collection_id) {60 Ok(v) => v,61 Err(_) => return Zero::zero(),62 };63 let collection = FungibleHandle::cast(collection_handle);64 Self::Balance::try_from(collection.total_supply()).unwrap_or(Zero::zero())65 }66 }67 }6869 fn minimum_balance(asset: Self::AssetId) -> Self::Balance {70 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible minimum_balance");71 match asset {72 AssetIds::NativeAssetId(NativeCurrency::Here) => {73 <pallet_balances::Pallet<T> as fungible::Inspect<T::AccountId>>::minimum_balance()74 .into()75 }76 AssetIds::NativeAssetId(NativeCurrency::Parent) => {77 <orml_tokens::Pallet<T> as fungibles::Inspect<T::AccountId>>::minimum_balance(78 AssetIds::NativeAssetId(NativeCurrency::Parent),79 )80 .into()81 }82 AssetIds::ForeignAssetId(fid) => {83 AssetMetadatas::<T>::get(AssetIds::ForeignAssetId(fid))84 .map(|x| x.minimal_balance)85 .unwrap_or_else(Zero::zero)86 }87 }88 }8990 fn balance(asset: Self::AssetId, who: &<T as SystemConfig>::AccountId) -> Self::Balance {91 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible balance");92 match asset {93 AssetIds::NativeAssetId(NativeCurrency::Here) => {94 <pallet_balances::Pallet<T> as fungible::Inspect<T::AccountId>>::balance(who).into()95 }96 AssetIds::NativeAssetId(NativeCurrency::Parent) => {97 <orml_tokens::Pallet<T> as fungibles::Inspect<T::AccountId>>::balance(98 AssetIds::NativeAssetId(NativeCurrency::Parent),99 who,100 )101 .into()102 }103 AssetIds::ForeignAssetId(fid) => {104 let target_collection_id = match <AssetBinding<T>>::get(fid) {105 Some(v) => v,106 None => return Zero::zero(),107 };108 let collection_handle = match <CollectionHandle<T>>::try_get(target_collection_id) {109 Ok(v) => v,110 Err(_) => return Zero::zero(),111 };112 let collection = FungibleHandle::cast(collection_handle);113 Self::Balance::try_from(114 collection.balance(T::CrossAccountId::from_sub(who.clone()), TokenId(0)),115 )116 .unwrap_or(Zero::zero())117 }118 }119 }120121 fn reducible_balance(122 asset: Self::AssetId,123 who: &<T as SystemConfig>::AccountId,124 keep_alive: bool,125 ) -> Self::Balance {126 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible reducible_balance");127128 match asset {129 AssetIds::NativeAssetId(NativeCurrency::Here) => {130 <pallet_balances::Pallet<T> as fungible::Inspect<T::AccountId>>::reducible_balance(131 who, keep_alive,132 )133 .into()134 }135 AssetIds::NativeAssetId(NativeCurrency::Parent) => {136 <orml_tokens::Pallet<T> as fungibles::Inspect<T::AccountId>>::reducible_balance(137 AssetIds::NativeAssetId(NativeCurrency::Parent),138 who,139 keep_alive,140 )141 .into()142 }143 _ => Self::balance(asset, who),144 }145 }146147 fn can_deposit(148 asset: Self::AssetId,149 who: &<T as SystemConfig>::AccountId,150 amount: Self::Balance,151 mint: bool,152 ) -> DepositConsequence {153 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible can_deposit");154155 match asset {156 AssetIds::NativeAssetId(NativeCurrency::Here) => {157 <pallet_balances::Pallet<T> as fungible::Inspect<T::AccountId>>::can_deposit(158 who,159 amount.into(),160 mint,161 )162 }163 AssetIds::NativeAssetId(NativeCurrency::Parent) => {164 <orml_tokens::Pallet<T> as fungibles::Inspect<T::AccountId>>::can_deposit(165 AssetIds::NativeAssetId(NativeCurrency::Parent),166 who,167 amount.into(),168 mint,169 )170 }171 _ => {172 if amount.is_zero() {173 return DepositConsequence::Success;174 }175176 let extential_deposit_value = T::ExistentialDeposit::get();177 let ed_value: u128 = match extential_deposit_value.try_into() {178 Ok(val) => val,179 Err(_) => return DepositConsequence::CannotCreate,180 };181 let extential_deposit: Self::Balance = match ed_value.try_into() {182 Ok(val) => val,183 Err(_) => return DepositConsequence::CannotCreate,184 };185186 let new_total_balance = match Self::balance(asset, who).checked_add(&amount) {187 Some(x) => x,188 None => return DepositConsequence::Overflow,189 };190191 if new_total_balance < extential_deposit {192 return DepositConsequence::BelowMinimum;193 }194195 DepositConsequence::Success196 }197 }198 }199200 fn can_withdraw(201 asset: Self::AssetId,202 who: &<T as SystemConfig>::AccountId,203 amount: Self::Balance,204 ) -> WithdrawConsequence<Self::Balance> {205 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible can_withdraw");206 let value: u128 = match amount.try_into() {207 Ok(val) => val,208 Err(_) => return WithdrawConsequence::UnknownAsset,209 };210211 match asset {212 AssetIds::NativeAssetId(NativeCurrency::Here) => {213 let this_amount: <T as pallet_balances::Config>::Balance = match value.try_into() {214 Ok(val) => val,215 Err(_) => {216 return WithdrawConsequence::UnknownAsset;217 }218 };219 match <pallet_balances::Pallet<T> as fungible::Inspect<T::AccountId>>::can_withdraw(220 who,221 this_amount,222 ) {223 WithdrawConsequence::NoFunds => WithdrawConsequence::NoFunds,224 WithdrawConsequence::WouldDie => WithdrawConsequence::WouldDie,225 WithdrawConsequence::UnknownAsset => WithdrawConsequence::UnknownAsset,226 WithdrawConsequence::Underflow => WithdrawConsequence::Underflow,227 WithdrawConsequence::Overflow => WithdrawConsequence::Overflow,228 WithdrawConsequence::Frozen => WithdrawConsequence::Frozen,229 WithdrawConsequence::Success => WithdrawConsequence::Success,230 _ => WithdrawConsequence::NoFunds,231 }232 }233 AssetIds::NativeAssetId(NativeCurrency::Parent) => {234 let parent_amount: <T as orml_tokens::Config>::Balance = match value.try_into() {235 Ok(val) => val,236 Err(_) => {237 return WithdrawConsequence::UnknownAsset;238 }239 };240 match <orml_tokens::Pallet<T> as fungibles::Inspect<T::AccountId>>::can_withdraw(241 AssetIds::NativeAssetId(NativeCurrency::Parent),242 who,243 parent_amount,244 ) {245 WithdrawConsequence::NoFunds => WithdrawConsequence::NoFunds,246 WithdrawConsequence::WouldDie => WithdrawConsequence::WouldDie,247 WithdrawConsequence::UnknownAsset => WithdrawConsequence::UnknownAsset,248 WithdrawConsequence::Underflow => WithdrawConsequence::Underflow,249 WithdrawConsequence::Overflow => WithdrawConsequence::Overflow,250 WithdrawConsequence::Frozen => WithdrawConsequence::Frozen,251 WithdrawConsequence::Success => WithdrawConsequence::Success,252 _ => WithdrawConsequence::NoFunds,253 }254 }255 _ => match Self::balance(asset, who).checked_sub(&amount) {256 Some(_) => WithdrawConsequence::Success,257 None => WithdrawConsequence::NoFunds,258 },259 }260 }261}262263impl<T: Config> fungibles::Mutate<<T as SystemConfig>::AccountId> for Pallet<T>264where265 T: orml_tokens::Config<CurrencyId = AssetIds>,266 BalanceOf<T>: From<<T as pallet_balances::Config>::Balance>,267 BalanceOf<T>: From<<T as orml_tokens::Config>::Balance>,268 <T as pallet_balances::Config>::Balance: From<BalanceOf<T>>,269 <T as orml_tokens::Config>::Balance: From<BalanceOf<T>>,270 u128: From<BalanceOf<T>>,271{272 fn mint_into(273 asset: Self::AssetId,274 who: &<T as SystemConfig>::AccountId,275 amount: Self::Balance,276 ) -> DispatchResult {277 278 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible mint_into {:?}", asset);279280 match asset {281 AssetIds::NativeAssetId(NativeCurrency::Here) => {282 <pallet_balances::Pallet<T> as fungible::Mutate<T::AccountId>>::mint_into(283 who,284 amount.into(),285 )286 .into()287 }288 AssetIds::NativeAssetId(NativeCurrency::Parent) => {289 <orml_tokens::Pallet<T> as fungibles::Mutate<T::AccountId>>::mint_into(290 AssetIds::NativeAssetId(NativeCurrency::Parent),291 who,292 amount.into(),293 )294 .into()295 }296 AssetIds::ForeignAssetId(fid) => {297 let target_collection_id = match <AssetBinding<T>>::get(fid) {298 Some(v) => v,299 None => {300 return Err(DispatchError::Other(301 "Associated collection not found for asset",302 ))303 }304 };305 let collection =306 FungibleHandle::cast(<CollectionHandle<T>>::try_get(target_collection_id)?);307 let account = T::CrossAccountId::from_sub(who.clone());308309 let amount_data: pallet_fungible::CreateItemData<T> =310 (account.clone(), amount.into());311312 pallet_fungible::Pallet::<T>::create_item_foreign(313 &collection,314 &account,315 amount_data,316 &Value::new(0),317 )?;318319 Ok(())320 }321 }322 }323324 fn burn_from(325 asset: Self::AssetId,326 who: &<T as SystemConfig>::AccountId,327 amount: Self::Balance,328 ) -> Result<Self::Balance, DispatchError> {329 330 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible burn_from");331332 match asset {333 AssetIds::NativeAssetId(NativeCurrency::Here) => {334 match <pallet_balances::Pallet<T> as fungible::Mutate<T::AccountId>>::burn_from(335 who,336 amount.into(),337 ) {338 Ok(v) => Ok(v.into()),339 Err(e) => Err(e),340 }341 }342 AssetIds::NativeAssetId(NativeCurrency::Parent) => {343 match <orml_tokens::Pallet<T> as fungibles::Mutate<T::AccountId>>::burn_from(344 AssetIds::NativeAssetId(NativeCurrency::Parent),345 who,346 amount.into(),347 ) {348 Ok(v) => Ok(v.into()),349 Err(e) => Err(e),350 }351 }352 AssetIds::ForeignAssetId(fid) => {353 let target_collection_id = match <AssetBinding<T>>::get(fid) {354 Some(v) => v,355 None => {356 return Err(DispatchError::Other(357 "Associated collection not found for asset",358 ))359 }360 };361 let collection =362 FungibleHandle::cast(<CollectionHandle<T>>::try_get(target_collection_id)?);363 pallet_fungible::Pallet::<T>::burn_foreign(364 &collection,365 &T::CrossAccountId::from_sub(who.clone()),366 amount.into(),367 )?;368369 Ok(amount)370 }371 }372 }373374 fn slash(375 asset: Self::AssetId,376 who: &<T as SystemConfig>::AccountId,377 amount: Self::Balance,378 ) -> Result<Self::Balance, DispatchError> {379 380 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible slash");381 Ok(Self::burn_from(asset, who, amount)?)382 }383}384385impl<T: Config> fungibles::Transfer<T::AccountId> for Pallet<T>386where387 T: orml_tokens::Config<CurrencyId = AssetIds>,388 BalanceOf<T>: From<<T as pallet_balances::Config>::Balance>,389 BalanceOf<T>: From<<T as orml_tokens::Config>::Balance>,390 <T as pallet_balances::Config>::Balance: From<BalanceOf<T>>,391 <T as orml_tokens::Config>::Balance: From<BalanceOf<T>>,392 u128: From<BalanceOf<T>>,393{394 fn transfer(395 asset: Self::AssetId,396 source: &<T as SystemConfig>::AccountId,397 dest: &<T as SystemConfig>::AccountId,398 amount: Self::Balance,399 keep_alive: bool,400 ) -> Result<Self::Balance, DispatchError> {401 402 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible transfer");403404 match asset {405 AssetIds::NativeAssetId(NativeCurrency::Here) => {406 match <pallet_balances::Pallet<T> as fungible::Transfer<T::AccountId>>::transfer(407 source,408 dest,409 amount.into(),410 keep_alive,411 ) {412 Ok(_) => Ok(amount),413 Err(_) => Err(DispatchError::Other(414 "Bad amount to relay chain value conversion",415 )),416 }417 }418 AssetIds::NativeAssetId(NativeCurrency::Parent) => {419 match <orml_tokens::Pallet<T> as fungibles::Transfer<T::AccountId>>::transfer(420 AssetIds::NativeAssetId(NativeCurrency::Parent),421 source,422 dest,423 amount.into(),424 keep_alive,425 ) {426 Ok(_) => Ok(amount),427 Err(e) => Err(e),428 }429 }430 AssetIds::ForeignAssetId(fid) => {431 let target_collection_id = match <AssetBinding<T>>::get(fid) {432 Some(v) => v,433 None => {434 return Err(DispatchError::Other(435 "Associated collection not found for asset",436 ))437 }438 };439 let collection =440 FungibleHandle::cast(<CollectionHandle<T>>::try_get(target_collection_id)?);441442 pallet_fungible::Pallet::<T>::transfer(443 &collection,444 &T::CrossAccountId::from_sub(source.clone()),445 &T::CrossAccountId::from_sub(dest.clone()),446 amount.into(),447 &Value::new(0),448 )?;449450 Ok(amount)451 }452 }453 }454}