difftreelog
fix(foreign-assets) use map_err
in: master
1 file changed
pallets/foreign-assets/src/impl_fungibles.rsdiffbeforeafterboth1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617//! Implementations for fungibles trait.1819use 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::Unlimited;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{33 type AssetId = AssetIds;34 type Balance = BalanceOf<T>;3536 fn total_issuance(asset: Self::AssetId) -> Self::Balance {37 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible total_issuance");3839 match asset {40 AssetIds::NativeAssetId(NativeCurrency::Here) => {41 let parent_amount = <pallet_balances::Pallet<T> as fungible::Inspect<42 T::AccountId,43 >>::total_issuance();4445 let value: u128 = match parent_amount.try_into() {46 Ok(val) => val,47 Err(_) => return Zero::zero(),48 };4950 let ti: Self::Balance = match value.try_into() {51 Ok(val) => val,52 Err(_) => return Zero::zero(),53 };5455 ti56 }57 AssetIds::NativeAssetId(NativeCurrency::Parent) => {58 let amount =59 <orml_tokens::Pallet<T> as fungibles::Inspect<T::AccountId>>::total_issuance(60 AssetIds::NativeAssetId(NativeCurrency::Parent),61 );6263 let value: u128 = match amount.try_into() {64 Ok(val) => val,65 Err(_) => return Zero::zero(),66 };6768 let ti: Self::Balance = match value.try_into() {69 Ok(val) => val,70 Err(_) => return Zero::zero(),71 };7273 ti74 }75 AssetIds::ForeignAssetId(fid) => {76 let target_collection_id = match <AssetBinding<T>>::get(fid) {77 Some(v) => v,78 None => return Zero::zero(),79 };80 let collection_handle = match <CollectionHandle<T>>::try_get(target_collection_id) {81 Ok(v) => v,82 Err(_) => return Zero::zero(),83 };84 let collection = FungibleHandle::cast(collection_handle);85 Self::Balance::try_from(collection.total_supply()).unwrap_or(Zero::zero())86 }87 }88 }8990 fn minimum_balance(asset: Self::AssetId) -> Self::Balance {91 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible minimum_balance");92 match asset {93 AssetIds::NativeAssetId(NativeCurrency::Here) => {94 let parent_amount = <pallet_balances::Pallet<T> as fungible::Inspect<95 T::AccountId,96 >>::minimum_balance();9798 let value: u128 = match parent_amount.try_into() {99 Ok(val) => val,100 Err(_) => return Zero::zero(),101 };102103 let ti: Self::Balance = match value.try_into() {104 Ok(val) => val,105 Err(_) => return Zero::zero(),106 };107108 ti109 }110 AssetIds::NativeAssetId(NativeCurrency::Parent) => {111 let amount =112 <orml_tokens::Pallet<T> as fungibles::Inspect<T::AccountId>>::minimum_balance(113 AssetIds::NativeAssetId(NativeCurrency::Parent),114 );115116 let value: u128 = match amount.try_into() {117 Ok(val) => val,118 Err(_) => return Zero::zero(),119 };120121 let ti: Self::Balance = match value.try_into() {122 Ok(val) => val,123 Err(_) => return Zero::zero(),124 };125126 ti127 }128 AssetIds::ForeignAssetId(fid) => {129 AssetMetadatas::<T>::get(AssetIds::ForeignAssetId(fid))130 .map(|x| x.minimal_balance)131 .unwrap_or_else(Zero::zero)132 }133 }134 }135136 fn balance(asset: Self::AssetId, who: &<T as SystemConfig>::AccountId) -> Self::Balance {137 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible balance");138 match asset {139 AssetIds::NativeAssetId(NativeCurrency::Here) => {140 let parent_amount =141 <pallet_balances::Pallet<T> as fungible::Inspect<T::AccountId>>::balance(who);142143 let value: u128 = match parent_amount.try_into() {144 Ok(val) => val,145 Err(_) => return Zero::zero(),146 };147148 let ti: Self::Balance = match value.try_into() {149 Ok(val) => val,150 Err(_) => return Zero::zero(),151 };152153 ti154 }155 AssetIds::NativeAssetId(NativeCurrency::Parent) => {156 let amount = <orml_tokens::Pallet<T> as fungibles::Inspect<T::AccountId>>::balance(157 AssetIds::NativeAssetId(NativeCurrency::Parent),158 who,159 );160161 let value: u128 = match amount.try_into() {162 Ok(val) => val,163 Err(_) => return Zero::zero(),164 };165166 let ti: Self::Balance = match value.try_into() {167 Ok(val) => val,168 Err(_) => return Zero::zero(),169 };170171 ti172 }173 AssetIds::ForeignAssetId(fid) => {174 let target_collection_id = match <AssetBinding<T>>::get(fid) {175 Some(v) => v,176 None => return Zero::zero(),177 };178 let collection_handle = match <CollectionHandle<T>>::try_get(target_collection_id) {179 Ok(v) => v,180 Err(_) => return Zero::zero(),181 };182 let collection = FungibleHandle::cast(collection_handle);183 Self::Balance::try_from(184 collection.balance(T::CrossAccountId::from_sub(who.clone()), TokenId(0)),185 )186 .unwrap_or(Zero::zero())187 }188 }189 }190191 fn reducible_balance(192 asset: Self::AssetId,193 who: &<T as SystemConfig>::AccountId,194 keep_alive: bool,195 ) -> Self::Balance {196 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible reducible_balance");197198 match asset {199 AssetIds::NativeAssetId(NativeCurrency::Here) => {200 let parent_amount = <pallet_balances::Pallet<T> as fungible::Inspect<201 T::AccountId,202 >>::reducible_balance(who, keep_alive);203204 let value: u128 = match parent_amount.try_into() {205 Ok(val) => val,206 Err(_) => return Zero::zero(),207 };208209 let ti: Self::Balance = match value.try_into() {210 Ok(val) => val,211 Err(_) => return Zero::zero(),212 };213214 ti215 }216 AssetIds::NativeAssetId(NativeCurrency::Parent) => {217 let amount =218 <orml_tokens::Pallet<T> as fungibles::Inspect<T::AccountId>>::reducible_balance(219 AssetIds::NativeAssetId(NativeCurrency::Parent),220 who,221 keep_alive,222 );223224 let value: u128 = match amount.try_into() {225 Ok(val) => val,226 Err(_) => return Zero::zero(),227 };228229 let ti: Self::Balance = match value.try_into() {230 Ok(val) => val,231 Err(_) => return Zero::zero(),232 };233234 ti235 }236 _ => Self::balance(asset, who),237 }238 }239240 fn can_deposit(241 asset: Self::AssetId,242 who: &<T as SystemConfig>::AccountId,243 amount: Self::Balance,244 mint: bool,245 ) -> DepositConsequence {246 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible can_deposit");247248 let value: u128 = match amount.try_into() {249 Ok(val) => val,250 Err(_) => return DepositConsequence::CannotCreate,251 };252253 match asset {254 AssetIds::NativeAssetId(NativeCurrency::Here) => {255 let this_amount: <T as pallet_balances::Config>::Balance = match value.try_into() {256 Ok(val) => val,257 Err(_) => {258 return DepositConsequence::CannotCreate;259 }260 };261 <pallet_balances::Pallet<T> as fungible::Inspect<T::AccountId>>::can_deposit(262 who,263 this_amount,264 mint,265 )266 }267 AssetIds::NativeAssetId(NativeCurrency::Parent) => {268 let parent_amount: <T as orml_tokens::Config>::Balance = match value.try_into() {269 Ok(val) => val,270 Err(_) => {271 return DepositConsequence::CannotCreate;272 }273 };274 <orml_tokens::Pallet<T> as fungibles::Inspect<T::AccountId>>::can_deposit(275 AssetIds::NativeAssetId(NativeCurrency::Parent),276 who,277 parent_amount,278 mint,279 )280 }281 _ => {282 if amount.is_zero() {283 return DepositConsequence::Success;284 }285286 let extential_deposit_value = T::ExistentialDeposit::get();287 let ed_value: u128 = match extential_deposit_value.try_into() {288 Ok(val) => val,289 Err(_) => return DepositConsequence::CannotCreate,290 };291 let extential_deposit: Self::Balance = match ed_value.try_into() {292 Ok(val) => val,293 Err(_) => return DepositConsequence::CannotCreate,294 };295296 let new_total_balance = match Self::balance(asset, who).checked_add(&amount) {297 Some(x) => x,298 None => return DepositConsequence::Overflow,299 };300301 if new_total_balance < extential_deposit {302 return DepositConsequence::BelowMinimum;303 }304305 DepositConsequence::Success306 }307 }308 }309310 fn can_withdraw(311 asset: Self::AssetId,312 who: &<T as SystemConfig>::AccountId,313 amount: Self::Balance,314 ) -> WithdrawConsequence<Self::Balance> {315 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible can_withdraw");316 let value: u128 = match amount.try_into() {317 Ok(val) => val,318 Err(_) => return WithdrawConsequence::UnknownAsset,319 };320321 match asset {322 AssetIds::NativeAssetId(NativeCurrency::Here) => {323 let this_amount: <T as pallet_balances::Config>::Balance = match value.try_into() {324 Ok(val) => val,325 Err(_) => {326 return WithdrawConsequence::UnknownAsset;327 }328 };329 match <pallet_balances::Pallet<T> as fungible::Inspect<T::AccountId>>::can_withdraw(330 who,331 this_amount,332 ) {333 WithdrawConsequence::NoFunds => WithdrawConsequence::NoFunds,334 WithdrawConsequence::WouldDie => WithdrawConsequence::WouldDie,335 WithdrawConsequence::UnknownAsset => WithdrawConsequence::UnknownAsset,336 WithdrawConsequence::Underflow => WithdrawConsequence::Underflow,337 WithdrawConsequence::Overflow => WithdrawConsequence::Overflow,338 WithdrawConsequence::Frozen => WithdrawConsequence::Frozen,339 WithdrawConsequence::Success => WithdrawConsequence::Success,340 _ => WithdrawConsequence::NoFunds,341 }342 }343 AssetIds::NativeAssetId(NativeCurrency::Parent) => {344 let parent_amount: <T as orml_tokens::Config>::Balance = match value.try_into() {345 Ok(val) => val,346 Err(_) => {347 return WithdrawConsequence::UnknownAsset;348 }349 };350 match <orml_tokens::Pallet<T> as fungibles::Inspect<T::AccountId>>::can_withdraw(351 AssetIds::NativeAssetId(NativeCurrency::Parent),352 who,353 parent_amount,354 ) {355 WithdrawConsequence::NoFunds => WithdrawConsequence::NoFunds,356 WithdrawConsequence::WouldDie => WithdrawConsequence::WouldDie,357 WithdrawConsequence::UnknownAsset => WithdrawConsequence::UnknownAsset,358 WithdrawConsequence::Underflow => WithdrawConsequence::Underflow,359 WithdrawConsequence::Overflow => WithdrawConsequence::Overflow,360 WithdrawConsequence::Frozen => WithdrawConsequence::Frozen,361 WithdrawConsequence::Success => WithdrawConsequence::Success,362 _ => WithdrawConsequence::NoFunds,363 }364 }365 _ => match Self::balance(asset, who).checked_sub(&amount) {366 Some(_) => WithdrawConsequence::Success,367 None => WithdrawConsequence::NoFunds,368 },369 }370 }371}372373impl<T: Config> fungibles::Mutate<<T as SystemConfig>::AccountId> for Pallet<T>374where375 T: orml_tokens::Config<CurrencyId = AssetIds>,376{377 fn mint_into(378 asset: Self::AssetId,379 who: &<T as SystemConfig>::AccountId,380 amount: Self::Balance,381 ) -> DispatchResult {382 //Self::do_mint(asset, who, amount, None)383 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible mint_into {:?}", asset);384385 let value: u128 = match amount.try_into() {386 Ok(val) => val,387 Err(_) => return Err(DispatchError::Other("Bad amount to value conversion")),388 };389390 match asset {391 AssetIds::NativeAssetId(NativeCurrency::Here) => {392 let this_amount: <T as pallet_balances::Config>::Balance = match value.try_into() {393 Ok(val) => val,394 Err(_) => {395 return Err(DispatchError::Other(396 "Bad amount to this parachain value conversion",397 ))398 }399 };400401 <pallet_balances::Pallet<T> as fungible::Mutate<T::AccountId>>::mint_into(402 who,403 this_amount,404 )405 }406 AssetIds::NativeAssetId(NativeCurrency::Parent) => {407 let parent_amount: <T as orml_tokens::Config>::Balance = match value.try_into() {408 Ok(val) => val,409 Err(_) => {410 return Err(DispatchError::Other(411 "Bad amount to relay chain value conversion",412 ))413 }414 };415416 <orml_tokens::Pallet<T> as fungibles::Mutate<T::AccountId>>::mint_into(417 AssetIds::NativeAssetId(NativeCurrency::Parent),418 who,419 parent_amount,420 )421 }422 AssetIds::ForeignAssetId(fid) => {423 let target_collection_id = match <AssetBinding<T>>::get(fid) {424 Some(v) => v,425 None => {426 return Err(DispatchError::Other(427 "Associated collection not found for asset",428 ))429 }430 };431 let collection =432 FungibleHandle::cast(<CollectionHandle<T>>::try_get(target_collection_id)?);433 let account = T::CrossAccountId::from_sub(who.clone());434435 let amount_data: pallet_fungible::CreateItemData<T> = (account.clone(), value);436437 pallet_fungible::Pallet::<T>::create_item_foreign(438 &collection,439 &account,440 amount_data,441 &Unlimited,442 )?;443444 Ok(())445 }446 }447 }448449 fn burn_from(450 asset: Self::AssetId,451 who: &<T as SystemConfig>::AccountId,452 amount: Self::Balance,453 ) -> Result<Self::Balance, DispatchError> {454 // let f = DebitFlags { keep_alive: false, best_effort: false };455 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible burn_from");456457 let value: u128 = match amount.try_into() {458 Ok(val) => val,459 Err(_) => return Err(DispatchError::Other("Bad amount to value conversion")),460 };461462 match asset {463 AssetIds::NativeAssetId(NativeCurrency::Here) => {464 let this_amount: <T as pallet_balances::Config>::Balance = match value.try_into() {465 Ok(val) => val,466 Err(_) => {467 return Err(DispatchError::Other(468 "Bad amount to this parachain value conversion",469 ))470 }471 };472473 match <pallet_balances::Pallet<T> as fungible::Mutate<T::AccountId>>::burn_from(474 who,475 this_amount,476 ) {477 Ok(_) => Ok(amount),478 Err(e) => Err(e),479 }480 }481 AssetIds::NativeAssetId(NativeCurrency::Parent) => {482 let parent_amount: <T as orml_tokens::Config>::Balance = match value.try_into() {483 Ok(val) => val,484 Err(_) => {485 return Err(DispatchError::Other(486 "Bad amount to relay chain value conversion",487 ))488 }489 };490491 match <orml_tokens::Pallet<T> as fungibles::Mutate<T::AccountId>>::burn_from(492 AssetIds::NativeAssetId(NativeCurrency::Parent),493 who,494 parent_amount,495 ) {496 Ok(_) => Ok(amount),497 Err(e) => Err(e),498 }499 }500 AssetIds::ForeignAssetId(fid) => {501 let target_collection_id = match <AssetBinding<T>>::get(fid) {502 Some(v) => v,503 None => {504 return Err(DispatchError::Other(505 "Associated collection not found for asset",506 ))507 }508 };509 let collection =510 FungibleHandle::cast(<CollectionHandle<T>>::try_get(target_collection_id)?);511 pallet_fungible::Pallet::<T>::burn_foreign(512 &collection,513 &T::CrossAccountId::from_sub(who.clone()),514 value,515 )?;516517 Ok(amount)518 }519 }520 }521522 fn slash(523 asset: Self::AssetId,524 who: &<T as SystemConfig>::AccountId,525 amount: Self::Balance,526 ) -> Result<Self::Balance, DispatchError> {527 // let f = DebitFlags { keep_alive: false, best_effort: true };528 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible slash");529 Self::burn_from(asset, who, amount)?;530 Ok(amount)531 }532}533534impl<T: Config> fungibles::Transfer<T::AccountId> for Pallet<T>535where536 T: orml_tokens::Config<CurrencyId = AssetIds>,537{538 fn transfer(539 asset: Self::AssetId,540 source: &<T as SystemConfig>::AccountId,541 dest: &<T as SystemConfig>::AccountId,542 amount: Self::Balance,543 keep_alive: bool,544 ) -> Result<Self::Balance, DispatchError> {545 // let f = TransferFlags { keep_alive, best_effort: false, burn_dust: false };546 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible transfer");547548 let value: u128 = match amount.try_into() {549 Ok(val) => val,550 Err(_) => return Err(DispatchError::Other("Bad amount to value conversion")),551 };552553 match asset {554 AssetIds::NativeAssetId(NativeCurrency::Here) => {555 let this_amount: <T as pallet_balances::Config>::Balance = match value.try_into() {556 Ok(val) => val,557 Err(_) => {558 return Err(DispatchError::Other(559 "Bad amount to this parachain value conversion",560 ))561 }562 };563564 match <pallet_balances::Pallet<T> as fungible::Transfer<T::AccountId>>::transfer(565 source,566 dest,567 this_amount,568 keep_alive,569 ) {570 Ok(_) => Ok(amount),571 Err(_) => Err(DispatchError::Other(572 "Bad amount to relay chain value conversion",573 )),574 }575 }576 AssetIds::NativeAssetId(NativeCurrency::Parent) => {577 let parent_amount: <T as orml_tokens::Config>::Balance = match value.try_into() {578 Ok(val) => val,579 Err(_) => {580 return Err(DispatchError::Other(581 "Bad amount to relay chain value conversion",582 ))583 }584 };585586 match <orml_tokens::Pallet<T> as fungibles::Transfer<T::AccountId>>::transfer(587 AssetIds::NativeAssetId(NativeCurrency::Parent),588 source,589 dest,590 parent_amount,591 keep_alive,592 ) {593 Ok(_) => Ok(amount),594 Err(e) => Err(e),595 }596 }597 AssetIds::ForeignAssetId(fid) => {598 let target_collection_id = match <AssetBinding<T>>::get(fid) {599 Some(v) => v,600 None => {601 return Err(DispatchError::Other(602 "Associated collection not found for asset",603 ))604 }605 };606 let collection =607 FungibleHandle::cast(<CollectionHandle<T>>::try_get(target_collection_id)?);608609 pallet_fungible::Pallet::<T>::transfer(610 &collection,611 &T::CrossAccountId::from_sub(source.clone()),612 &T::CrossAccountId::from_sub(dest.clone()),613 value,614 &Unlimited,615 )?;616617 Ok(amount)618 }619 }620 }621}1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617//! Implementations for fungibles trait.1819use 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::Unlimited;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{33 type AssetId = AssetIds;34 type Balance = BalanceOf<T>;3536 fn total_issuance(asset: Self::AssetId) -> Self::Balance {37 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible total_issuance");3839 match asset {40 AssetIds::NativeAssetId(NativeCurrency::Here) => {41 let parent_amount = <pallet_balances::Pallet<T> as fungible::Inspect<42 T::AccountId,43 >>::total_issuance();4445 let value: u128 = match parent_amount.try_into() {46 Ok(val) => val,47 Err(_) => return Zero::zero(),48 };4950 let ti: Self::Balance = match value.try_into() {51 Ok(val) => val,52 Err(_) => return Zero::zero(),53 };5455 ti56 }57 AssetIds::NativeAssetId(NativeCurrency::Parent) => {58 let amount =59 <orml_tokens::Pallet<T> as fungibles::Inspect<T::AccountId>>::total_issuance(60 AssetIds::NativeAssetId(NativeCurrency::Parent),61 );6263 let value: u128 = match amount.try_into() {64 Ok(val) => val,65 Err(_) => return Zero::zero(),66 };6768 let ti: Self::Balance = match value.try_into() {69 Ok(val) => val,70 Err(_) => return Zero::zero(),71 };7273 ti74 }75 AssetIds::ForeignAssetId(fid) => {76 let target_collection_id = match <AssetBinding<T>>::get(fid) {77 Some(v) => v,78 None => return Zero::zero(),79 };80 let collection_handle = match <CollectionHandle<T>>::try_get(target_collection_id) {81 Ok(v) => v,82 Err(_) => return Zero::zero(),83 };84 let collection = FungibleHandle::cast(collection_handle);85 Self::Balance::try_from(collection.total_supply()).unwrap_or(Zero::zero())86 }87 }88 }8990 fn minimum_balance(asset: Self::AssetId) -> Self::Balance {91 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible minimum_balance");92 match asset {93 AssetIds::NativeAssetId(NativeCurrency::Here) => {94 let parent_amount = <pallet_balances::Pallet<T> as fungible::Inspect<95 T::AccountId,96 >>::minimum_balance();9798 let value: u128 = match parent_amount.try_into() {99 Ok(val) => val,100 Err(_) => return Zero::zero(),101 };102103 let ti: Self::Balance = match value.try_into() {104 Ok(val) => val,105 Err(_) => return Zero::zero(),106 };107108 ti109 }110 AssetIds::NativeAssetId(NativeCurrency::Parent) => {111 let amount =112 <orml_tokens::Pallet<T> as fungibles::Inspect<T::AccountId>>::minimum_balance(113 AssetIds::NativeAssetId(NativeCurrency::Parent),114 );115116 let value: u128 = match amount.try_into() {117 Ok(val) => val,118 Err(_) => return Zero::zero(),119 };120121 let ti: Self::Balance = match value.try_into() {122 Ok(val) => val,123 Err(_) => return Zero::zero(),124 };125126 ti127 }128 AssetIds::ForeignAssetId(fid) => {129 AssetMetadatas::<T>::get(AssetIds::ForeignAssetId(fid))130 .map(|x| x.minimal_balance)131 .unwrap_or_else(Zero::zero)132 }133 }134 }135136 fn balance(asset: Self::AssetId, who: &<T as SystemConfig>::AccountId) -> Self::Balance {137 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible balance");138 match asset {139 AssetIds::NativeAssetId(NativeCurrency::Here) => {140 let parent_amount =141 <pallet_balances::Pallet<T> as fungible::Inspect<T::AccountId>>::balance(who);142143 let value: u128 = match parent_amount.try_into() {144 Ok(val) => val,145 Err(_) => return Zero::zero(),146 };147148 let ti: Self::Balance = match value.try_into() {149 Ok(val) => val,150 Err(_) => return Zero::zero(),151 };152153 ti154 }155 AssetIds::NativeAssetId(NativeCurrency::Parent) => {156 let amount = <orml_tokens::Pallet<T> as fungibles::Inspect<T::AccountId>>::balance(157 AssetIds::NativeAssetId(NativeCurrency::Parent),158 who,159 );160161 let value: u128 = match amount.try_into() {162 Ok(val) => val,163 Err(_) => return Zero::zero(),164 };165166 let ti: Self::Balance = match value.try_into() {167 Ok(val) => val,168 Err(_) => return Zero::zero(),169 };170171 ti172 }173 AssetIds::ForeignAssetId(fid) => {174 let target_collection_id = match <AssetBinding<T>>::get(fid) {175 Some(v) => v,176 None => return Zero::zero(),177 };178 let collection_handle = match <CollectionHandle<T>>::try_get(target_collection_id) {179 Ok(v) => v,180 Err(_) => return Zero::zero(),181 };182 let collection = FungibleHandle::cast(collection_handle);183 Self::Balance::try_from(184 collection.balance(T::CrossAccountId::from_sub(who.clone()), TokenId(0)),185 )186 .unwrap_or(Zero::zero())187 }188 }189 }190191 fn reducible_balance(192 asset: Self::AssetId,193 who: &<T as SystemConfig>::AccountId,194 keep_alive: bool,195 ) -> Self::Balance {196 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible reducible_balance");197198 match asset {199 AssetIds::NativeAssetId(NativeCurrency::Here) => {200 let parent_amount = <pallet_balances::Pallet<T> as fungible::Inspect<201 T::AccountId,202 >>::reducible_balance(who, keep_alive);203204 let value: u128 = match parent_amount.try_into() {205 Ok(val) => val,206 Err(_) => return Zero::zero(),207 };208209 let ti: Self::Balance = match value.try_into() {210 Ok(val) => val,211 Err(_) => return Zero::zero(),212 };213214 ti215 }216 AssetIds::NativeAssetId(NativeCurrency::Parent) => {217 let amount =218 <orml_tokens::Pallet<T> as fungibles::Inspect<T::AccountId>>::reducible_balance(219 AssetIds::NativeAssetId(NativeCurrency::Parent),220 who,221 keep_alive,222 );223224 let value: u128 = match amount.try_into() {225 Ok(val) => val,226 Err(_) => return Zero::zero(),227 };228229 let ti: Self::Balance = match value.try_into() {230 Ok(val) => val,231 Err(_) => return Zero::zero(),232 };233234 ti235 }236 _ => Self::balance(asset, who),237 }238 }239240 fn can_deposit(241 asset: Self::AssetId,242 who: &<T as SystemConfig>::AccountId,243 amount: Self::Balance,244 mint: bool,245 ) -> DepositConsequence {246 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible can_deposit");247248 let value: u128 = match amount.try_into() {249 Ok(val) => val,250 Err(_) => return DepositConsequence::CannotCreate,251 };252253 match asset {254 AssetIds::NativeAssetId(NativeCurrency::Here) => {255 let this_amount: <T as pallet_balances::Config>::Balance = match value.try_into() {256 Ok(val) => val,257 Err(_) => {258 return DepositConsequence::CannotCreate;259 }260 };261 <pallet_balances::Pallet<T> as fungible::Inspect<T::AccountId>>::can_deposit(262 who,263 this_amount,264 mint,265 )266 }267 AssetIds::NativeAssetId(NativeCurrency::Parent) => {268 let parent_amount: <T as orml_tokens::Config>::Balance = match value.try_into() {269 Ok(val) => val,270 Err(_) => {271 return DepositConsequence::CannotCreate;272 }273 };274 <orml_tokens::Pallet<T> as fungibles::Inspect<T::AccountId>>::can_deposit(275 AssetIds::NativeAssetId(NativeCurrency::Parent),276 who,277 parent_amount,278 mint,279 )280 }281 _ => {282 if amount.is_zero() {283 return DepositConsequence::Success;284 }285286 let extential_deposit_value = T::ExistentialDeposit::get();287 let ed_value: u128 = match extential_deposit_value.try_into() {288 Ok(val) => val,289 Err(_) => return DepositConsequence::CannotCreate,290 };291 let extential_deposit: Self::Balance = match ed_value.try_into() {292 Ok(val) => val,293 Err(_) => return DepositConsequence::CannotCreate,294 };295296 let new_total_balance = match Self::balance(asset, who).checked_add(&amount) {297 Some(x) => x,298 None => return DepositConsequence::Overflow,299 };300301 if new_total_balance < extential_deposit {302 return DepositConsequence::BelowMinimum;303 }304305 DepositConsequence::Success306 }307 }308 }309310 fn can_withdraw(311 asset: Self::AssetId,312 who: &<T as SystemConfig>::AccountId,313 amount: Self::Balance,314 ) -> WithdrawConsequence<Self::Balance> {315 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible can_withdraw");316 let value: u128 = match amount.try_into() {317 Ok(val) => val,318 Err(_) => return WithdrawConsequence::UnknownAsset,319 };320321 match asset {322 AssetIds::NativeAssetId(NativeCurrency::Here) => {323 let this_amount: <T as pallet_balances::Config>::Balance = match value.try_into() {324 Ok(val) => val,325 Err(_) => {326 return WithdrawConsequence::UnknownAsset;327 }328 };329 match <pallet_balances::Pallet<T> as fungible::Inspect<T::AccountId>>::can_withdraw(330 who,331 this_amount,332 ) {333 WithdrawConsequence::NoFunds => WithdrawConsequence::NoFunds,334 WithdrawConsequence::WouldDie => WithdrawConsequence::WouldDie,335 WithdrawConsequence::UnknownAsset => WithdrawConsequence::UnknownAsset,336 WithdrawConsequence::Underflow => WithdrawConsequence::Underflow,337 WithdrawConsequence::Overflow => WithdrawConsequence::Overflow,338 WithdrawConsequence::Frozen => WithdrawConsequence::Frozen,339 WithdrawConsequence::Success => WithdrawConsequence::Success,340 _ => WithdrawConsequence::NoFunds,341 }342 }343 AssetIds::NativeAssetId(NativeCurrency::Parent) => {344 let parent_amount: <T as orml_tokens::Config>::Balance = match value.try_into() {345 Ok(val) => val,346 Err(_) => {347 return WithdrawConsequence::UnknownAsset;348 }349 };350 match <orml_tokens::Pallet<T> as fungibles::Inspect<T::AccountId>>::can_withdraw(351 AssetIds::NativeAssetId(NativeCurrency::Parent),352 who,353 parent_amount,354 ) {355 WithdrawConsequence::NoFunds => WithdrawConsequence::NoFunds,356 WithdrawConsequence::WouldDie => WithdrawConsequence::WouldDie,357 WithdrawConsequence::UnknownAsset => WithdrawConsequence::UnknownAsset,358 WithdrawConsequence::Underflow => WithdrawConsequence::Underflow,359 WithdrawConsequence::Overflow => WithdrawConsequence::Overflow,360 WithdrawConsequence::Frozen => WithdrawConsequence::Frozen,361 WithdrawConsequence::Success => WithdrawConsequence::Success,362 _ => WithdrawConsequence::NoFunds,363 }364 }365 _ => match Self::balance(asset, who).checked_sub(&amount) {366 Some(_) => WithdrawConsequence::Success,367 None => WithdrawConsequence::NoFunds,368 },369 }370 }371}372373impl<T: Config> fungibles::Mutate<<T as SystemConfig>::AccountId> for Pallet<T>374where375 T: orml_tokens::Config<CurrencyId = AssetIds>,376{377 fn mint_into(378 asset: Self::AssetId,379 who: &<T as SystemConfig>::AccountId,380 amount: Self::Balance,381 ) -> DispatchResult {382 //Self::do_mint(asset, who, amount, None)383 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible mint_into {:?}", asset);384385 let value: u128 = match amount.try_into() {386 Ok(val) => val,387 Err(_) => return Err(DispatchError::Other("Bad amount to value conversion")),388 };389390 match asset {391 AssetIds::NativeAssetId(NativeCurrency::Here) => {392 let this_amount: <T as pallet_balances::Config>::Balance = match value.try_into() {393 Ok(val) => val,394 Err(_) => {395 return Err(DispatchError::Other(396 "Bad amount to this parachain value conversion",397 ))398 }399 };400401 <pallet_balances::Pallet<T> as fungible::Mutate<T::AccountId>>::mint_into(402 who,403 this_amount,404 )405 }406 AssetIds::NativeAssetId(NativeCurrency::Parent) => {407 let parent_amount: <T as orml_tokens::Config>::Balance = match value.try_into() {408 Ok(val) => val,409 Err(_) => {410 return Err(DispatchError::Other(411 "Bad amount to relay chain value conversion",412 ))413 }414 };415416 <orml_tokens::Pallet<T> as fungibles::Mutate<T::AccountId>>::mint_into(417 AssetIds::NativeAssetId(NativeCurrency::Parent),418 who,419 parent_amount,420 )421 }422 AssetIds::ForeignAssetId(fid) => {423 let target_collection_id = match <AssetBinding<T>>::get(fid) {424 Some(v) => v,425 None => {426 return Err(DispatchError::Other(427 "Associated collection not found for asset",428 ))429 }430 };431 let collection =432 FungibleHandle::cast(<CollectionHandle<T>>::try_get(target_collection_id)?);433 let account = T::CrossAccountId::from_sub(who.clone());434435 let amount_data: pallet_fungible::CreateItemData<T> = (account.clone(), value);436437 pallet_fungible::Pallet::<T>::create_item_foreign(438 &collection,439 &account,440 amount_data,441 &Unlimited,442 )?;443444 Ok(())445 }446 }447 }448449 fn burn_from(450 asset: Self::AssetId,451 who: &<T as SystemConfig>::AccountId,452 amount: Self::Balance,453 ) -> Result<Self::Balance, DispatchError> {454 // let f = DebitFlags { keep_alive: false, best_effort: false };455 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible burn_from");456457 let value: u128 = match amount.try_into() {458 Ok(val) => val,459 Err(_) => return Err(DispatchError::Other("Bad amount to value conversion")),460 };461462 match asset {463 AssetIds::NativeAssetId(NativeCurrency::Here) => {464 let this_amount: <T as pallet_balances::Config>::Balance =465 value.try_into().map_err(|_| {466 DispatchError::Other("Bad amount to this parachain value conversion")467 })?;468469 match <pallet_balances::Pallet<T> as fungible::Mutate<T::AccountId>>::burn_from(470 who,471 this_amount,472 ) {473 Ok(_) => Ok(amount),474 Err(e) => Err(e),475 }476 }477 AssetIds::NativeAssetId(NativeCurrency::Parent) => {478 let parent_amount: <T as orml_tokens::Config>::Balance = match value.try_into() {479 Ok(val) => val,480 Err(_) => {481 return Err(DispatchError::Other(482 "Bad amount to relay chain value conversion",483 ))484 }485 };486487 match <orml_tokens::Pallet<T> as fungibles::Mutate<T::AccountId>>::burn_from(488 AssetIds::NativeAssetId(NativeCurrency::Parent),489 who,490 parent_amount,491 ) {492 Ok(_) => Ok(amount),493 Err(e) => Err(e),494 }495 }496 AssetIds::ForeignAssetId(fid) => {497 let target_collection_id = match <AssetBinding<T>>::get(fid) {498 Some(v) => v,499 None => {500 return Err(DispatchError::Other(501 "Associated collection not found for asset",502 ))503 }504 };505 let collection =506 FungibleHandle::cast(<CollectionHandle<T>>::try_get(target_collection_id)?);507 pallet_fungible::Pallet::<T>::burn_foreign(508 &collection,509 &T::CrossAccountId::from_sub(who.clone()),510 value,511 )?;512513 Ok(amount)514 }515 }516 }517518 fn slash(519 asset: Self::AssetId,520 who: &<T as SystemConfig>::AccountId,521 amount: Self::Balance,522 ) -> Result<Self::Balance, DispatchError> {523 // let f = DebitFlags { keep_alive: false, best_effort: true };524 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible slash");525 Self::burn_from(asset, who, amount)?;526 Ok(amount)527 }528}529530impl<T: Config> fungibles::Transfer<T::AccountId> for Pallet<T>531where532 T: orml_tokens::Config<CurrencyId = AssetIds>,533{534 fn transfer(535 asset: Self::AssetId,536 source: &<T as SystemConfig>::AccountId,537 dest: &<T as SystemConfig>::AccountId,538 amount: Self::Balance,539 keep_alive: bool,540 ) -> Result<Self::Balance, DispatchError> {541 // let f = TransferFlags { keep_alive, best_effort: false, burn_dust: false };542 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible transfer");543544 let value: u128 = match amount.try_into() {545 Ok(val) => val,546 Err(_) => return Err(DispatchError::Other("Bad amount to value conversion")),547 };548549 match asset {550 AssetIds::NativeAssetId(NativeCurrency::Here) => {551 let this_amount: <T as pallet_balances::Config>::Balance = match value.try_into() {552 Ok(val) => val,553 Err(_) => {554 return Err(DispatchError::Other(555 "Bad amount to this parachain value conversion",556 ))557 }558 };559560 match <pallet_balances::Pallet<T> as fungible::Transfer<T::AccountId>>::transfer(561 source,562 dest,563 this_amount,564 keep_alive,565 ) {566 Ok(_) => Ok(amount),567 Err(_) => Err(DispatchError::Other(568 "Bad amount to relay chain value conversion",569 )),570 }571 }572 AssetIds::NativeAssetId(NativeCurrency::Parent) => {573 let parent_amount: <T as orml_tokens::Config>::Balance = match value.try_into() {574 Ok(val) => val,575 Err(_) => {576 return Err(DispatchError::Other(577 "Bad amount to relay chain value conversion",578 ))579 }580 };581582 match <orml_tokens::Pallet<T> as fungibles::Transfer<T::AccountId>>::transfer(583 AssetIds::NativeAssetId(NativeCurrency::Parent),584 source,585 dest,586 parent_amount,587 keep_alive,588 ) {589 Ok(_) => Ok(amount),590 Err(e) => Err(e),591 }592 }593 AssetIds::ForeignAssetId(fid) => {594 let target_collection_id = match <AssetBinding<T>>::get(fid) {595 Some(v) => v,596 None => {597 return Err(DispatchError::Other(598 "Associated collection not found for asset",599 ))600 }601 };602 let collection =603 FungibleHandle::cast(<CollectionHandle<T>>::try_get(target_collection_id)?);604605 pallet_fungible::Pallet::<T>::transfer(606 &collection,607 &T::CrossAccountId::from_sub(source.clone()),608 &T::CrossAccountId::from_sub(dest.clone()),609 value,610 &Unlimited,611 )?;612613 Ok(amount)614 }615 }616 }617}