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}