--- a/pallets/foreign-assets/src/impl_fungibles.rs +++ b/pallets/foreign-assets/src/impl_fungibles.rs @@ -26,9 +26,20 @@ use up_data_structs::budget::Unlimited; use sp_runtime::traits::{CheckedAdd, CheckedSub}; +// type BalanceSelf = +// <::Currency as Currency<::AccountId>>::Balance; +type BalanceRelay = + <::Currency as Currency<::AccountId>>::Balance; + impl fungibles::Inspect<::AccountId> for Pallet where T: orml_tokens::Config, + BalanceRelay: From>, + BalanceOf: From>, + BalanceRelay: From<::Balance>, + BalanceOf: From<::Balance>, + ::Balance: From>, + ::Balance: From>, { type AssetId = AssetIds; type Balance = BalanceOf; @@ -38,39 +49,14 @@ match asset { AssetIds::NativeAssetId(NativeCurrency::Here) => { - let parent_amount = as fungible::Inspect< - T::AccountId, - >>::total_issuance(); - - let value: u128 = match parent_amount.try_into() { - Ok(val) => val, - Err(_) => return Zero::zero(), - }; - - let ti: Self::Balance = match value.try_into() { - Ok(val) => val, - Err(_) => return Zero::zero(), - }; - - ti + as fungible::Inspect>::total_issuance() + .into() } AssetIds::NativeAssetId(NativeCurrency::Parent) => { - let amount = - as fungibles::Inspect>::total_issuance( - AssetIds::NativeAssetId(NativeCurrency::Parent), - ); - - let value: u128 = match amount.try_into() { - Ok(val) => val, - Err(_) => return Zero::zero(), - }; - - let ti: Self::Balance = match value.try_into() { - Ok(val) => val, - Err(_) => return Zero::zero(), - }; - - ti + as fungibles::Inspect>::total_issuance( + AssetIds::NativeAssetId(NativeCurrency::Parent), + ) + .into() } AssetIds::ForeignAssetId(fid) => { let target_collection_id = match >::get(fid) { @@ -91,39 +77,14 @@ log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible minimum_balance"); match asset { AssetIds::NativeAssetId(NativeCurrency::Here) => { - let parent_amount = as fungible::Inspect< - T::AccountId, - >>::minimum_balance(); - - let value: u128 = match parent_amount.try_into() { - Ok(val) => val, - Err(_) => return Zero::zero(), - }; - - let ti: Self::Balance = match value.try_into() { - Ok(val) => val, - Err(_) => return Zero::zero(), - }; - - ti + as fungible::Inspect>::minimum_balance() + .into() } AssetIds::NativeAssetId(NativeCurrency::Parent) => { - let amount = - as fungibles::Inspect>::minimum_balance( - AssetIds::NativeAssetId(NativeCurrency::Parent), - ); - - let value: u128 = match amount.try_into() { - Ok(val) => val, - Err(_) => return Zero::zero(), - }; - - let ti: Self::Balance = match value.try_into() { - Ok(val) => val, - Err(_) => return Zero::zero(), - }; - - ti + as fungibles::Inspect>::minimum_balance( + AssetIds::NativeAssetId(NativeCurrency::Parent), + ) + .into() } AssetIds::ForeignAssetId(fid) => { AssetMetadatas::::get(AssetIds::ForeignAssetId(fid)) @@ -137,38 +98,14 @@ log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible balance"); match asset { AssetIds::NativeAssetId(NativeCurrency::Here) => { - let parent_amount = - as fungible::Inspect>::balance(who); - - let value: u128 = match parent_amount.try_into() { - Ok(val) => val, - Err(_) => return Zero::zero(), - }; - - let ti: Self::Balance = match value.try_into() { - Ok(val) => val, - Err(_) => return Zero::zero(), - }; - - ti + as fungible::Inspect>::balance(who).into() } AssetIds::NativeAssetId(NativeCurrency::Parent) => { - let amount = as fungibles::Inspect>::balance( + as fungibles::Inspect>::balance( AssetIds::NativeAssetId(NativeCurrency::Parent), who, - ); - - let value: u128 = match amount.try_into() { - Ok(val) => val, - Err(_) => return Zero::zero(), - }; - - let ti: Self::Balance = match value.try_into() { - Ok(val) => val, - Err(_) => return Zero::zero(), - }; - - ti + ) + .into() } AssetIds::ForeignAssetId(fid) => { let target_collection_id = match >::get(fid) { @@ -197,41 +134,18 @@ match asset { AssetIds::NativeAssetId(NativeCurrency::Here) => { - let parent_amount = as fungible::Inspect< - T::AccountId, - >>::reducible_balance(who, keep_alive); - - let value: u128 = match parent_amount.try_into() { - Ok(val) => val, - Err(_) => return Zero::zero(), - }; - - let ti: Self::Balance = match value.try_into() { - Ok(val) => val, - Err(_) => return Zero::zero(), - }; - - ti + as fungible::Inspect>::reducible_balance( + who, keep_alive, + ) + .into() } AssetIds::NativeAssetId(NativeCurrency::Parent) => { - let amount = - as fungibles::Inspect>::reducible_balance( - AssetIds::NativeAssetId(NativeCurrency::Parent), - who, - keep_alive, - ); - - let value: u128 = match amount.try_into() { - Ok(val) => val, - Err(_) => return Zero::zero(), - }; - - let ti: Self::Balance = match value.try_into() { - Ok(val) => val, - Err(_) => return Zero::zero(), - }; - - ti + as fungibles::Inspect>::reducible_balance( + AssetIds::NativeAssetId(NativeCurrency::Parent), + who, + keep_alive, + ) + .into() } _ => Self::balance(asset, who), } @@ -244,37 +158,20 @@ mint: bool, ) -> DepositConsequence { log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible can_deposit"); - - let value: u128 = match amount.try_into() { - Ok(val) => val, - Err(_) => return DepositConsequence::CannotCreate, - }; match asset { AssetIds::NativeAssetId(NativeCurrency::Here) => { - let this_amount: ::Balance = match value.try_into() { - Ok(val) => val, - Err(_) => { - return DepositConsequence::CannotCreate; - } - }; as fungible::Inspect>::can_deposit( who, - this_amount, + amount.into(), mint, ) } AssetIds::NativeAssetId(NativeCurrency::Parent) => { - let parent_amount: ::Balance = match value.try_into() { - Ok(val) => val, - Err(_) => { - return DepositConsequence::CannotCreate; - } - }; as fungibles::Inspect>::can_deposit( AssetIds::NativeAssetId(NativeCurrency::Parent), who, - parent_amount, + amount.into(), mint, ) } @@ -373,6 +270,13 @@ impl fungibles::Mutate<::AccountId> for Pallet where T: orml_tokens::Config, + BalanceRelay: From>, + BalanceOf: From>, + BalanceRelay: From<::Balance>, + BalanceOf: From<::Balance>, + ::Balance: From>, + ::Balance: From>, + u128: From>, { fn mint_into( asset: Self::AssetId, @@ -381,43 +285,22 @@ ) -> DispatchResult { //Self::do_mint(asset, who, amount, None) log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible mint_into {:?}", asset); - - let value: u128 = match amount.try_into() { - Ok(val) => val, - Err(_) => return Err(DispatchError::Other("Bad amount to value conversion")), - }; match asset { AssetIds::NativeAssetId(NativeCurrency::Here) => { - let this_amount: ::Balance = match value.try_into() { - Ok(val) => val, - Err(_) => { - return Err(DispatchError::Other( - "Bad amount to this parachain value conversion", - )) - } - }; - as fungible::Mutate>::mint_into( who, - this_amount, + amount.into(), ) + .into() } AssetIds::NativeAssetId(NativeCurrency::Parent) => { - let parent_amount: ::Balance = match value.try_into() { - Ok(val) => val, - Err(_) => { - return Err(DispatchError::Other( - "Bad amount to relay chain value conversion", - )) - } - }; - as fungibles::Mutate>::mint_into( AssetIds::NativeAssetId(NativeCurrency::Parent), who, - parent_amount, + amount.into(), ) + .into() } AssetIds::ForeignAssetId(fid) => { let target_collection_id = match >::get(fid) { @@ -432,7 +315,8 @@ FungibleHandle::cast(>::try_get(target_collection_id)?); let account = T::CrossAccountId::from_sub(who.clone()); - let amount_data: pallet_fungible::CreateItemData = (account.clone(), value); + let amount_data: pallet_fungible::CreateItemData = + (account.clone(), amount.into()); pallet_fungible::Pallet::::create_item_foreign( &collection, @@ -453,43 +337,24 @@ ) -> Result { // let f = DebitFlags { keep_alive: false, best_effort: false }; log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible burn_from"); - - let value: u128 = match amount.try_into() { - Ok(val) => val, - Err(_) => return Err(DispatchError::Other("Bad amount to value conversion")), - }; match asset { AssetIds::NativeAssetId(NativeCurrency::Here) => { - let this_amount: ::Balance = - value.try_into().map_err(|_| { - DispatchError::Other("Bad amount to this parachain value conversion") - })?; - match as fungible::Mutate>::burn_from( who, - this_amount, + amount.into(), ) { - Ok(_) => Ok(amount), + Ok(v) => Ok(v.into()), Err(e) => Err(e), } } AssetIds::NativeAssetId(NativeCurrency::Parent) => { - let parent_amount: ::Balance = match value.try_into() { - Ok(val) => val, - Err(_) => { - return Err(DispatchError::Other( - "Bad amount to relay chain value conversion", - )) - } - }; - match as fungibles::Mutate>::burn_from( AssetIds::NativeAssetId(NativeCurrency::Parent), who, - parent_amount, + amount.into(), ) { - Ok(_) => Ok(amount), + Ok(v) => Ok(v.into()), Err(e) => Err(e), } } @@ -507,7 +372,7 @@ pallet_fungible::Pallet::::burn_foreign( &collection, &T::CrossAccountId::from_sub(who.clone()), - value, + amount.into(), )?; Ok(amount) @@ -522,14 +387,20 @@ ) -> Result { // let f = DebitFlags { keep_alive: false, best_effort: true }; log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible slash"); - Self::burn_from(asset, who, amount)?; - Ok(amount) + Ok(Self::burn_from(asset, who, amount)?) } } impl fungibles::Transfer for Pallet where T: orml_tokens::Config, + BalanceRelay: From>, + BalanceOf: From>, + BalanceRelay: From<::Balance>, + BalanceOf: From<::Balance>, + ::Balance: From>, + ::Balance: From>, + u128: From>, { fn transfer( asset: Self::AssetId, @@ -540,27 +411,13 @@ ) -> Result { // let f = TransferFlags { keep_alive, best_effort: false, burn_dust: false }; log::trace!(target: "fassets::impl_foreign_assets", "impl_fungible transfer"); - - let value: u128 = match amount.try_into() { - Ok(val) => val, - Err(_) => return Err(DispatchError::Other("Bad amount to value conversion")), - }; match asset { AssetIds::NativeAssetId(NativeCurrency::Here) => { - let this_amount: ::Balance = match value.try_into() { - Ok(val) => val, - Err(_) => { - return Err(DispatchError::Other( - "Bad amount to this parachain value conversion", - )) - } - }; - match as fungible::Transfer>::transfer( source, dest, - this_amount, + amount.into(), keep_alive, ) { Ok(_) => Ok(amount), @@ -570,20 +427,11 @@ } } AssetIds::NativeAssetId(NativeCurrency::Parent) => { - let parent_amount: ::Balance = match value.try_into() { - Ok(val) => val, - Err(_) => { - return Err(DispatchError::Other( - "Bad amount to relay chain value conversion", - )) - } - }; - match as fungibles::Transfer>::transfer( AssetIds::NativeAssetId(NativeCurrency::Parent), source, dest, - parent_amount, + amount.into(), keep_alive, ) { Ok(_) => Ok(amount), @@ -606,7 +454,7 @@ &collection, &T::CrossAccountId::from_sub(source.clone()), &T::CrossAccountId::from_sub(dest.clone()), - value, + amount.into(), &Unlimited, )?; --- 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; --- 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(PhantomData); -impl WeightInfo for AcalaWeight { +/// Weights for pallet_fungible using the Substrate node and recommended hardware. +pub struct SubstrateWeight(PhantomData); +impl WeightInfo for SubstrateWeight { fn register_foreign_asset() -> Weight { (29_819_000 as Weight) .saturating_add(T::DbWeight::get().reads(2 as Weight)) --- 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; - type WeightInfo = (); + type WeightInfo = pallet_foreign_assets::weights::SubstrateWeight; }