diff --git a/pallets/balances-adapter/src/common.rs b/pallets/balances-adapter/src/common.rs index 7e9bb38708..f6dbac1be3 100644 --- a/pallets/balances-adapter/src/common.rs +++ b/pallets/balances-adapter/src/common.rs @@ -1,9 +1,9 @@ use alloc::{vec, vec::Vec}; use core::marker::PhantomData; -use frame_support::{fail, weights::Weight}; +use frame_support::{ensure, fail, weights::Weight}; use pallet_balances::{weights::SubstrateWeight as BalancesWeight, WeightInfo}; -use pallet_common::{CommonCollectionOperations, CommonWeightInfo}; +use pallet_common::{CommonCollectionOperations, CommonWeightInfo, Error as CommonError}; use up_data_structs::TokenId; use crate::{Config, NativeFungibleHandle, Pallet}; @@ -77,7 +77,7 @@ impl CommonCollectionOperations for NativeFungibleHandle { _data: up_data_structs::CreateItemData, _nesting_budget: &dyn up_data_structs::budget::Budget, ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo { - fail!(>::UnsupportedOperation); + fail!(>::UnsupportedOperation); } fn create_multiple_items( @@ -87,7 +87,7 @@ impl CommonCollectionOperations for NativeFungibleHandle { _data: Vec, _nesting_budget: &dyn up_data_structs::budget::Budget, ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo { - fail!(>::UnsupportedOperation); + fail!(>::UnsupportedOperation); } fn create_multiple_items_ex( @@ -96,7 +96,7 @@ impl CommonCollectionOperations for NativeFungibleHandle { _data: up_data_structs::CreateItemExData<::CrossAccountId>, _nesting_budget: &dyn up_data_structs::budget::Budget, ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo { - fail!(>::UnsupportedOperation); + fail!(>::UnsupportedOperation); } fn burn_item( @@ -105,7 +105,7 @@ impl CommonCollectionOperations for NativeFungibleHandle { _token: TokenId, _amount: u128, ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo { - fail!(>::UnsupportedOperation); + fail!(>::UnsupportedOperation); } fn set_collection_properties( @@ -113,7 +113,7 @@ impl CommonCollectionOperations for NativeFungibleHandle { _sender: ::CrossAccountId, _properties: Vec, ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo { - fail!(>::UnsupportedOperation); + fail!(>::UnsupportedOperation); } fn delete_collection_properties( @@ -121,7 +121,7 @@ impl CommonCollectionOperations for NativeFungibleHandle { _sender: &::CrossAccountId, _property_keys: Vec, ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo { - fail!(>::UnsupportedOperation); + fail!(>::UnsupportedOperation); } fn set_token_properties( @@ -131,7 +131,7 @@ impl CommonCollectionOperations for NativeFungibleHandle { _properties: Vec, _budget: &dyn up_data_structs::budget::Budget, ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo { - fail!(>::UnsupportedOperation); + fail!(>::UnsupportedOperation); } fn delete_token_properties( @@ -141,7 +141,7 @@ impl CommonCollectionOperations for NativeFungibleHandle { _property_keys: Vec, _budget: &dyn up_data_structs::budget::Budget, ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo { - fail!(>::UnsupportedOperation); + fail!(>::UnsupportedOperation); } fn get_token_properties_raw( @@ -161,18 +161,23 @@ impl CommonCollectionOperations for NativeFungibleHandle { _sender: &::CrossAccountId, _property_permissions: Vec, ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo { - fail!(>::UnsupportedOperation); + fail!(>::UnsupportedOperation); } fn transfer( &self, sender: ::CrossAccountId, to: ::CrossAccountId, - _token: TokenId, + token: TokenId, amount: u128, - budget: &dyn up_data_structs::budget::Budget, + _budget: &dyn up_data_structs::budget::Budget, ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo { - >::transfer(self, &sender, &to, amount, budget) + ensure!( + token == TokenId::default(), + >::FungibleItemsHaveNoId + ); + + >::transfer(&sender, &to, amount) } fn approve( @@ -182,7 +187,7 @@ impl CommonCollectionOperations for NativeFungibleHandle { _token: TokenId, _amount: u128, ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo { - fail!(>::UnsupportedOperation); + fail!(>::UnsupportedOperation); } fn approve_from( @@ -193,7 +198,7 @@ impl CommonCollectionOperations for NativeFungibleHandle { _token: TokenId, _amount: u128, ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo { - fail!(>::UnsupportedOperation); + fail!(>::UnsupportedOperation); } fn transfer_from( @@ -201,11 +206,16 @@ impl CommonCollectionOperations for NativeFungibleHandle { sender: ::CrossAccountId, from: ::CrossAccountId, to: ::CrossAccountId, - _token: TokenId, + token: TokenId, amount: u128, budget: &dyn up_data_structs::budget::Budget, ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo { - >::transfer_from(self, &sender, &from, &to, amount, budget) + ensure!( + token == TokenId::default(), + >::FungibleItemsHaveNoId + ); + + >::transfer_from(&sender, &from, &to, amount, budget) } fn burn_from( @@ -216,7 +226,7 @@ impl CommonCollectionOperations for NativeFungibleHandle { _amount: u128, _budget: &dyn up_data_structs::budget::Budget, ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo { - fail!(>::UnsupportedOperation); + fail!(>::UnsupportedOperation); } fn check_nesting( @@ -226,7 +236,7 @@ impl CommonCollectionOperations for NativeFungibleHandle { _under: TokenId, _budget: &dyn up_data_structs::budget::Budget, ) -> frame_support::sp_runtime::DispatchResult { - fail!(>::UnsupportedOperation); + fail!(>::UnsupportedOperation); } fn nest(&self, _under: TokenId, _to_nest: (up_data_structs::CollectionId, TokenId)) {} @@ -332,7 +342,7 @@ impl CommonCollectionOperations for NativeFungibleHandle { _operator: ::CrossAccountId, _approve: bool, ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo { - fail!(>::UnsupportedOperation); + fail!(>::UnsupportedOperation); } fn allowance_for_all( @@ -347,6 +357,6 @@ impl CommonCollectionOperations for NativeFungibleHandle { &self, _token: TokenId, ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo { - fail!(>::UnsupportedOperation); + fail!(>::UnsupportedOperation); } } diff --git a/pallets/balances-adapter/src/erc.rs b/pallets/balances-adapter/src/erc.rs index 90b12327f9..857b49037e 100644 --- a/pallets/balances-adapter/src/erc.rs +++ b/pallets/balances-adapter/src/erc.rs @@ -58,12 +58,8 @@ impl NativeFungibleHandle { let caller = T::CrossAccountId::from_eth(caller); let to = T::CrossAccountId::from_eth(to); let amount = amount.try_into().map_err(|_| "amount overflow")?; - let budget = self - .recorder() - .weight_calls_budget(>::find_parent()); - >::transfer(self, &caller, &to, amount, &budget) - .map_err(|e| dispatch_to_evm::(e.error))?; + >::transfer(&caller, &to, amount).map_err(|e| dispatch_to_evm::(e.error))?; Ok(true) } @@ -83,7 +79,7 @@ impl NativeFungibleHandle { .recorder() .weight_calls_budget(>::find_parent()); - >::transfer_from(self, &caller, &from, &to, amount, &budget) + >::transfer_from(&caller, &from, &to, amount, &budget) .map_err(|e| dispatch_to_evm::(e.error))?; Ok(true) } @@ -106,12 +102,8 @@ where let caller = T::CrossAccountId::from_eth(caller); let to = to.into_sub_cross_account::()?; let amount = amount.try_into().map_err(|_| "amount overflow")?; - let budget = self - .recorder() - .weight_calls_budget(>::find_parent()); - >::transfer(self, &caller, &to, amount, &budget) - .map_err(|e| dispatch_to_evm::(e.error))?; + >::transfer(&caller, &to, amount).map_err(|e| dispatch_to_evm::(e.error))?; Ok(true) } @@ -137,7 +129,7 @@ where .recorder() .weight_calls_budget(>::find_parent()); - >::transfer_from(self, &caller, &from, &to, amount, &budget) + >::transfer_from(&caller, &from, &to, amount, &budget) .map_err(|e| dispatch_to_evm::(e.error))?; Ok(true) diff --git a/pallets/balances-adapter/src/lib.rs b/pallets/balances-adapter/src/lib.rs index 91997c0b79..aeb792fe92 100644 --- a/pallets/balances-adapter/src/lib.rs +++ b/pallets/balances-adapter/src/lib.rs @@ -155,20 +155,15 @@ pub mod pallet { Ok(Self::balance_of(from)) } - /// Transfers the specified amount of tokens. Will check that - /// the transfer is allowed for the token. + /// Transfers the specified amount of tokens. /// - /// - `collection`: Collection that contains the token. /// - `from`: Owner of tokens to transfer. /// - `to`: Recepient of transfered tokens. /// - `amount`: Amount of tokens to transfer. - /// - `nesting_budget`: Limit for searching parents in-depth to check ownership. pub fn transfer( - _collection: &NativeFungibleHandle, from: &T::CrossAccountId, to: &T::CrossAccountId, amount: u128, - _nesting_budget: &dyn Budget, ) -> DispatchResultWithPostInfo { >::ensure_correct_receiver(to)?; @@ -185,19 +180,18 @@ pub mod pallet { }) } - /// Transfer NFT token from one account to another. + /// Transfer tokens from one account to another. /// - /// Same as the [`Self::transfer`] but spender doesn't needs to be the owner of the token. - /// The owner should set allowance for the spender to transfer token. + /// Same as the [`Self::transfer`] but the spender doesn't needs to be the direct owner of the token. + /// The spender must be allowed to transfer token. + /// If the tokens are nested in an NFT and the spender owns the NFT, the allowance is considered to be set. /// - /// - `collection`: Collection that contains the token. /// - `spender`: Account that spend the money. /// - `from`: Owner of tokens to transfer. /// - `to`: Recepient of transfered tokens. /// - `amount`: Amount of tokens to transfer. /// - `nesting_budget`: Limit for searching parents in-depth to check ownership. pub fn transfer_from( - collection: &NativeFungibleHandle, spender: &T::CrossAccountId, from: &T::CrossAccountId, to: &T::CrossAccountId, @@ -208,7 +202,7 @@ pub mod pallet { if allowance < amount { return Err(>::ApprovedValueTooLow.into()); } - Self::transfer(collection, from, to, amount, nesting_budget) + Self::transfer(from, to, amount) } } } diff --git a/pallets/common/src/lib.rs b/pallets/common/src/lib.rs index d2f31a58c6..50c4420332 100644 --- a/pallets/common/src/lib.rs +++ b/pallets/common/src/lib.rs @@ -783,6 +783,9 @@ pub mod pallet { /// The user is not an administrator. UserIsNotCollectionAdmin, + + /// Fungible tokens hold no ID, and the default value of TokenId for a fungible collection is 0. + FungibleItemsHaveNoId, } /// Storage of the count of created collections. Essentially contains the last collection ID. diff --git a/pallets/fungible/src/common.rs b/pallets/fungible/src/common.rs index 0512bc886b..dd8a76f35e 100644 --- a/pallets/fungible/src/common.rs +++ b/pallets/fungible/src/common.rs @@ -19,7 +19,7 @@ use core::marker::PhantomData; use frame_support::{dispatch::DispatchResultWithPostInfo, ensure, fail, weights::Weight}; use pallet_common::{ weights::WeightInfo as _, with_weight, CommonCollectionOperations, CommonWeightInfo, - RefungibleExtensions, SelfWeightOf as PalletCommonWeightOf, + Error as CommonError, RefungibleExtensions, SelfWeightOf as PalletCommonWeightOf, }; use sp_runtime::{ArithmeticError, DispatchError}; use sp_std::{vec, vec::Vec}; @@ -169,7 +169,7 @@ impl CommonCollectionOperations for FungibleHandle { ) -> DispatchResultWithPostInfo { ensure!( token == TokenId::default(), - >::FungibleItemsHaveNoId + >::FungibleItemsHaveNoId ); with_weight( @@ -188,7 +188,7 @@ impl CommonCollectionOperations for FungibleHandle { ) -> DispatchResultWithPostInfo { ensure!( token == TokenId::default(), - >::FungibleItemsHaveNoId + >::FungibleItemsHaveNoId ); >::transfer(self, &from, &to, amount, nesting_budget) @@ -203,7 +203,7 @@ impl CommonCollectionOperations for FungibleHandle { ) -> DispatchResultWithPostInfo { ensure!( token == TokenId::default(), - >::FungibleItemsHaveNoId + >::FungibleItemsHaveNoId ); with_weight( @@ -222,7 +222,7 @@ impl CommonCollectionOperations for FungibleHandle { ) -> DispatchResultWithPostInfo { ensure!( token == TokenId::default(), - >::FungibleItemsHaveNoId + >::FungibleItemsHaveNoId ); with_weight( @@ -242,7 +242,7 @@ impl CommonCollectionOperations for FungibleHandle { ) -> DispatchResultWithPostInfo { ensure!( token == TokenId::default(), - >::FungibleItemsHaveNoId + >::FungibleItemsHaveNoId ); >::transfer_from(self, &sender, &from, &to, amount, nesting_budget) @@ -258,7 +258,7 @@ impl CommonCollectionOperations for FungibleHandle { ) -> DispatchResultWithPostInfo { ensure!( token == TokenId::default(), - >::FungibleItemsHaveNoId + >::FungibleItemsHaveNoId ); with_weight( diff --git a/pallets/fungible/src/lib.rs b/pallets/fungible/src/lib.rs index b2ab9bbcc0..1aadc416fc 100644 --- a/pallets/fungible/src/lib.rs +++ b/pallets/fungible/src/lib.rs @@ -123,8 +123,6 @@ pub mod pallet { pub enum Error { /// Not Fungible item data used to mint in Fungible collection. NotFungibleDataUsedToMintFungibleCollectionToken, - /// Fungible tokens hold no ID, and the default value of TokenId for Fungible collection is 0. - FungibleItemsHaveNoId, /// Tried to set data for fungible item. FungibleItemsDontHaveData, /// Fungible token does not support nesting.