difftreelog
Foreign assets pallet fixes
in: master
4 files 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 =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}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};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}pallets/foreign-assets/src/lib.rsdiffbeforeafterboth--- a/pallets/foreign-assets/src/lib.rs
+++ b/pallets/foreign-assets/src/lib.rs
@@ -122,7 +122,7 @@
pub type CurrencyId = AssetIds;
mod impl_fungibles;
-mod weights;
+pub mod weights;
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
pallets/foreign-assets/src/weights.rsdiffbeforeafterboth--- a/pallets/foreign-assets/src/weights.rs
+++ b/pallets/foreign-assets/src/weights.rs
@@ -13,9 +13,9 @@
fn update_foreign_asset() -> Weight;
}
-/// Weights for module_asset_registry using the Acala node and recommended hardware.
-pub struct AcalaWeight<T>(PhantomData<T>);
-impl<T: frame_system::Config> WeightInfo for AcalaWeight<T> {
+/// Weights for pallet_fungible using the Substrate node and recommended hardware.
+pub struct SubstrateWeight<T>(PhantomData<T>);
+impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
fn register_foreign_asset() -> Weight {
(29_819_000 as Weight)
.saturating_add(T::DbWeight::get().reads(2 as Weight))
runtime/common/config/pallets/foreign_asset.rsdiffbeforeafterboth--- a/runtime/common/config/pallets/foreign_asset.rs
+++ b/runtime/common/config/pallets/foreign_asset.rs
@@ -5,5 +5,5 @@
type Event = Event;
type Currency = Balances;
type RegisterOrigin = frame_system::EnsureRoot<AccountId>;
- type WeightInfo = ();
+ type WeightInfo = pallet_foreign_assets::weights::SubstrateWeight<Self>;
}