difftreelog
impl_fungibles refactored
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};2829// type BalanceSelf<T> =30// <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;31type BalanceRelay<T> =32 <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;3334impl<T: Config> fungibles::Inspect<<T as SystemConfig>::AccountId> for Pallet<T>35where36 T: orml_tokens::Config<CurrencyId = AssetIds>,37 BalanceRelay<T>: From<BalanceOf<T>>,38 BalanceOf<T>: From<BalanceRelay<T>>,39 BalanceRelay<T>: From<<T as pallet_balances::Config>::Balance>,40 BalanceOf<T>: From<<T as orml_tokens::Config>::Balance>,41 <T as pallet_balances::Config>::Balance: From<BalanceRelay<T>>,42 <T as orml_tokens::Config>::Balance: From<BalanceRelay<T>>,43{44 type AssetId = AssetIds;45 type Balance = BalanceOf<T>;4647 fn total_issuance(asset: Self::AssetId) -> Self::Balance {48 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible total_issuance");4950 match asset {51 AssetIds::NativeAssetId(NativeCurrency::Here) => {52 <pallet_balances::Pallet<T> as fungible::Inspect<T::AccountId>>::total_issuance()53 .into()54 }55 AssetIds::NativeAssetId(NativeCurrency::Parent) => {56 <orml_tokens::Pallet<T> as fungibles::Inspect<T::AccountId>>::total_issuance(57 AssetIds::NativeAssetId(NativeCurrency::Parent),58 )59 .into()60 }61 AssetIds::ForeignAssetId(fid) => {62 let target_collection_id = match <AssetBinding<T>>::get(fid) {63 Some(v) => v,64 None => return Zero::zero(),65 };66 let collection_handle = match <CollectionHandle<T>>::try_get(target_collection_id) {67 Ok(v) => v,68 Err(_) => return Zero::zero(),69 };70 let collection = FungibleHandle::cast(collection_handle);71 Self::Balance::try_from(collection.total_supply()).unwrap_or(Zero::zero())72 }73 }74 }7576 fn minimum_balance(asset: Self::AssetId) -> Self::Balance {77 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible minimum_balance");78 match asset {79 AssetIds::NativeAssetId(NativeCurrency::Here) => {80 <pallet_balances::Pallet<T> as fungible::Inspect<T::AccountId>>::minimum_balance()81 .into()82 }83 AssetIds::NativeAssetId(NativeCurrency::Parent) => {84 <orml_tokens::Pallet<T> as fungibles::Inspect<T::AccountId>>::minimum_balance(85 AssetIds::NativeAssetId(NativeCurrency::Parent),86 )87 .into()88 }89 AssetIds::ForeignAssetId(fid) => {90 AssetMetadatas::<T>::get(AssetIds::ForeignAssetId(fid))91 .map(|x| x.minimal_balance)92 .unwrap_or_else(Zero::zero)93 }94 }95 }9697 fn balance(asset: Self::AssetId, who: &<T as SystemConfig>::AccountId) -> Self::Balance {98 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible balance");99 match asset {100 AssetIds::NativeAssetId(NativeCurrency::Here) => {101 <pallet_balances::Pallet<T> as fungible::Inspect<T::AccountId>>::balance(who).into()102 }103 AssetIds::NativeAssetId(NativeCurrency::Parent) => {104 <orml_tokens::Pallet<T> as fungibles::Inspect<T::AccountId>>::balance(105 AssetIds::NativeAssetId(NativeCurrency::Parent),106 who,107 )108 .into()109 }110 AssetIds::ForeignAssetId(fid) => {111 let target_collection_id = match <AssetBinding<T>>::get(fid) {112 Some(v) => v,113 None => return Zero::zero(),114 };115 let collection_handle = match <CollectionHandle<T>>::try_get(target_collection_id) {116 Ok(v) => v,117 Err(_) => return Zero::zero(),118 };119 let collection = FungibleHandle::cast(collection_handle);120 Self::Balance::try_from(121 collection.balance(T::CrossAccountId::from_sub(who.clone()), TokenId(0)),122 )123 .unwrap_or(Zero::zero())124 }125 }126 }127128 fn reducible_balance(129 asset: Self::AssetId,130 who: &<T as SystemConfig>::AccountId,131 keep_alive: bool,132 ) -> Self::Balance {133 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible reducible_balance");134135 match asset {136 AssetIds::NativeAssetId(NativeCurrency::Here) => {137 <pallet_balances::Pallet<T> as fungible::Inspect<T::AccountId>>::reducible_balance(138 who, keep_alive,139 )140 .into()141 }142 AssetIds::NativeAssetId(NativeCurrency::Parent) => {143 <orml_tokens::Pallet<T> as fungibles::Inspect<T::AccountId>>::reducible_balance(144 AssetIds::NativeAssetId(NativeCurrency::Parent),145 who,146 keep_alive,147 )148 .into()149 }150 _ => Self::balance(asset, who),151 }152 }153154 fn can_deposit(155 asset: Self::AssetId,156 who: &<T as SystemConfig>::AccountId,157 amount: Self::Balance,158 mint: bool,159 ) -> DepositConsequence {160 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible can_deposit");161162 match asset {163 AssetIds::NativeAssetId(NativeCurrency::Here) => {164 <pallet_balances::Pallet<T> as fungible::Inspect<T::AccountId>>::can_deposit(165 who,166 amount.into(),167 mint,168 )169 }170 AssetIds::NativeAssetId(NativeCurrency::Parent) => {171 <orml_tokens::Pallet<T> as fungibles::Inspect<T::AccountId>>::can_deposit(172 AssetIds::NativeAssetId(NativeCurrency::Parent),173 who,174 amount.into(),175 mint,176 )177 }178 _ => {179 if amount.is_zero() {180 return DepositConsequence::Success;181 }182183 let extential_deposit_value = T::ExistentialDeposit::get();184 let ed_value: u128 = match extential_deposit_value.try_into() {185 Ok(val) => val,186 Err(_) => return DepositConsequence::CannotCreate,187 };188 let extential_deposit: Self::Balance = match ed_value.try_into() {189 Ok(val) => val,190 Err(_) => return DepositConsequence::CannotCreate,191 };192193 let new_total_balance = match Self::balance(asset, who).checked_add(&amount) {194 Some(x) => x,195 None => return DepositConsequence::Overflow,196 };197198 if new_total_balance < extential_deposit {199 return DepositConsequence::BelowMinimum;200 }201202 DepositConsequence::Success203 }204 }205 }206207 fn can_withdraw(208 asset: Self::AssetId,209 who: &<T as SystemConfig>::AccountId,210 amount: Self::Balance,211 ) -> WithdrawConsequence<Self::Balance> {212 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible can_withdraw");213 let value: u128 = match amount.try_into() {214 Ok(val) => val,215 Err(_) => return WithdrawConsequence::UnknownAsset,216 };217218 match asset {219 AssetIds::NativeAssetId(NativeCurrency::Here) => {220 let this_amount: <T as pallet_balances::Config>::Balance = match value.try_into() {221 Ok(val) => val,222 Err(_) => {223 return WithdrawConsequence::UnknownAsset;224 }225 };226 match <pallet_balances::Pallet<T> as fungible::Inspect<T::AccountId>>::can_withdraw(227 who,228 this_amount,229 ) {230 WithdrawConsequence::NoFunds => WithdrawConsequence::NoFunds,231 WithdrawConsequence::WouldDie => WithdrawConsequence::WouldDie,232 WithdrawConsequence::UnknownAsset => WithdrawConsequence::UnknownAsset,233 WithdrawConsequence::Underflow => WithdrawConsequence::Underflow,234 WithdrawConsequence::Overflow => WithdrawConsequence::Overflow,235 WithdrawConsequence::Frozen => WithdrawConsequence::Frozen,236 WithdrawConsequence::Success => WithdrawConsequence::Success,237 _ => WithdrawConsequence::NoFunds,238 }239 }240 AssetIds::NativeAssetId(NativeCurrency::Parent) => {241 let parent_amount: <T as orml_tokens::Config>::Balance = match value.try_into() {242 Ok(val) => val,243 Err(_) => {244 return WithdrawConsequence::UnknownAsset;245 }246 };247 match <orml_tokens::Pallet<T> as fungibles::Inspect<T::AccountId>>::can_withdraw(248 AssetIds::NativeAssetId(NativeCurrency::Parent),249 who,250 parent_amount,251 ) {252 WithdrawConsequence::NoFunds => WithdrawConsequence::NoFunds,253 WithdrawConsequence::WouldDie => WithdrawConsequence::WouldDie,254 WithdrawConsequence::UnknownAsset => WithdrawConsequence::UnknownAsset,255 WithdrawConsequence::Underflow => WithdrawConsequence::Underflow,256 WithdrawConsequence::Overflow => WithdrawConsequence::Overflow,257 WithdrawConsequence::Frozen => WithdrawConsequence::Frozen,258 WithdrawConsequence::Success => WithdrawConsequence::Success,259 _ => WithdrawConsequence::NoFunds,260 }261 }262 _ => match Self::balance(asset, who).checked_sub(&amount) {263 Some(_) => WithdrawConsequence::Success,264 None => WithdrawConsequence::NoFunds,265 },266 }267 }268}269270impl<T: Config> fungibles::Mutate<<T as SystemConfig>::AccountId> for Pallet<T>271where272 T: orml_tokens::Config<CurrencyId = AssetIds>,273 BalanceRelay<T>: From<BalanceOf<T>>,274 BalanceOf<T>: From<BalanceRelay<T>>,275 BalanceRelay<T>: From<<T as pallet_balances::Config>::Balance>,276 BalanceOf<T>: From<<T as orml_tokens::Config>::Balance>,277 <T as pallet_balances::Config>::Balance: From<BalanceRelay<T>>,278 <T as orml_tokens::Config>::Balance: From<BalanceRelay<T>>,279 u128: From<BalanceRelay<T>>,280{281 fn mint_into(282 asset: Self::AssetId,283 who: &<T as SystemConfig>::AccountId,284 amount: Self::Balance,285 ) -> DispatchResult {286 //Self::do_mint(asset, who, amount, None)287 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible mint_into {:?}", asset);288289 match asset {290 AssetIds::NativeAssetId(NativeCurrency::Here) => {291 <pallet_balances::Pallet<T> as fungible::Mutate<T::AccountId>>::mint_into(292 who,293 amount.into(),294 )295 .into()296 }297 AssetIds::NativeAssetId(NativeCurrency::Parent) => {298 <orml_tokens::Pallet<T> as fungibles::Mutate<T::AccountId>>::mint_into(299 AssetIds::NativeAssetId(NativeCurrency::Parent),300 who,301 amount.into(),302 )303 .into()304 }305 AssetIds::ForeignAssetId(fid) => {306 let target_collection_id = match <AssetBinding<T>>::get(fid) {307 Some(v) => v,308 None => {309 return Err(DispatchError::Other(310 "Associated collection not found for asset",311 ))312 }313 };314 let collection =315 FungibleHandle::cast(<CollectionHandle<T>>::try_get(target_collection_id)?);316 let account = T::CrossAccountId::from_sub(who.clone());317318 let amount_data: pallet_fungible::CreateItemData<T> =319 (account.clone(), amount.into());320321 pallet_fungible::Pallet::<T>::create_item_foreign(322 &collection,323 &account,324 amount_data,325 &Unlimited,326 )?;327328 Ok(())329 }330 }331 }332333 fn burn_from(334 asset: Self::AssetId,335 who: &<T as SystemConfig>::AccountId,336 amount: Self::Balance,337 ) -> Result<Self::Balance, DispatchError> {338 // let f = DebitFlags { keep_alive: false, best_effort: false };339 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible burn_from");340341 match asset {342 AssetIds::NativeAssetId(NativeCurrency::Here) => {343 match <pallet_balances::Pallet<T> as fungible::Mutate<T::AccountId>>::burn_from(344 who,345 amount.into(),346 ) {347 Ok(v) => Ok(v.into()),348 Err(e) => Err(e),349 }350 }351 AssetIds::NativeAssetId(NativeCurrency::Parent) => {352 match <orml_tokens::Pallet<T> as fungibles::Mutate<T::AccountId>>::burn_from(353 AssetIds::NativeAssetId(NativeCurrency::Parent),354 who,355 amount.into(),356 ) {357 Ok(v) => Ok(v.into()),358 Err(e) => Err(e),359 }360 }361 AssetIds::ForeignAssetId(fid) => {362 let target_collection_id = match <AssetBinding<T>>::get(fid) {363 Some(v) => v,364 None => {365 return Err(DispatchError::Other(366 "Associated collection not found for asset",367 ))368 }369 };370 let collection =371 FungibleHandle::cast(<CollectionHandle<T>>::try_get(target_collection_id)?);372 pallet_fungible::Pallet::<T>::burn_foreign(373 &collection,374 &T::CrossAccountId::from_sub(who.clone()),375 amount.into(),376 )?;377378 Ok(amount)379 }380 }381 }382383 fn slash(384 asset: Self::AssetId,385 who: &<T as SystemConfig>::AccountId,386 amount: Self::Balance,387 ) -> Result<Self::Balance, DispatchError> {388 // let f = DebitFlags { keep_alive: false, best_effort: true };389 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible slash");390 Ok(Self::burn_from(asset, who, amount)?)391 }392}393394impl<T: Config> fungibles::Transfer<T::AccountId> for Pallet<T>395where396 T: orml_tokens::Config<CurrencyId = AssetIds>,397 BalanceRelay<T>: From<BalanceOf<T>>,398 BalanceOf<T>: From<BalanceRelay<T>>,399 BalanceRelay<T>: From<<T as pallet_balances::Config>::Balance>,400 BalanceOf<T>: From<<T as orml_tokens::Config>::Balance>,401 <T as pallet_balances::Config>::Balance: From<BalanceRelay<T>>,402 <T as orml_tokens::Config>::Balance: From<BalanceRelay<T>>,403 u128: From<BalanceRelay<T>>,404{405 fn transfer(406 asset: Self::AssetId,407 source: &<T as SystemConfig>::AccountId,408 dest: &<T as SystemConfig>::AccountId,409 amount: Self::Balance,410 keep_alive: bool,411 ) -> Result<Self::Balance, DispatchError> {412 // let f = TransferFlags { keep_alive, best_effort: false, burn_dust: false };413 log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible transfer");414415 match asset {416 AssetIds::NativeAssetId(NativeCurrency::Here) => {417 match <pallet_balances::Pallet<T> as fungible::Transfer<T::AccountId>>::transfer(418 source,419 dest,420 amount.into(),421 keep_alive,422 ) {423 Ok(_) => Ok(amount),424 Err(_) => Err(DispatchError::Other(425 "Bad amount to relay chain value conversion",426 )),427 }428 }429 AssetIds::NativeAssetId(NativeCurrency::Parent) => {430 match <orml_tokens::Pallet<T> as fungibles::Transfer<T::AccountId>>::transfer(431 AssetIds::NativeAssetId(NativeCurrency::Parent),432 source,433 dest,434 amount.into(),435 keep_alive,436 ) {437 Ok(_) => Ok(amount),438 Err(e) => Err(e),439 }440 }441 AssetIds::ForeignAssetId(fid) => {442 let target_collection_id = match <AssetBinding<T>>::get(fid) {443 Some(v) => v,444 None => {445 return Err(DispatchError::Other(446 "Associated collection not found for asset",447 ))448 }449 };450 let collection =451 FungibleHandle::cast(<CollectionHandle<T>>::try_get(target_collection_id)?);452453 pallet_fungible::Pallet::<T>::transfer(454 &collection,455 &T::CrossAccountId::from_sub(source.clone()),456 &T::CrossAccountId::from_sub(dest.clone()),457 amount.into(),458 &Unlimited,459 )?;460461 Ok(amount)462 }463 }464 }465}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 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 //Self::do_mint(asset, who, amount, None)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 &Unlimited,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 // let f = DebitFlags { keep_alive: false, best_effort: false };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 // let f = DebitFlags { keep_alive: false, best_effort: true };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 // let f = TransferFlags { keep_alive, best_effort: false, burn_dust: false };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 &Unlimited,448 )?;449450 Ok(amount)451 }452 }453 }454}