difftreelog
fix forbid non-1 tokenid in native fungibles (#1015)
in: master
* fix: native fungibles collection * refactor: move FungibleItemsHaveNoId to pallet-common
6 files changed
pallets/balances-adapter/src/common.rsdiffbeforeafterboth--- 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 @@
_data: up_data_structs::CreateItemData,
_nesting_budget: &dyn up_data_structs::budget::Budget,
) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
- fail!(<pallet_common::Error<T>>::UnsupportedOperation);
+ fail!(<CommonError<T>>::UnsupportedOperation);
}
fn create_multiple_items(
@@ -87,7 +87,7 @@
_data: Vec<up_data_structs::CreateItemData>,
_nesting_budget: &dyn up_data_structs::budget::Budget,
) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
- fail!(<pallet_common::Error<T>>::UnsupportedOperation);
+ fail!(<CommonError<T>>::UnsupportedOperation);
}
fn create_multiple_items_ex(
@@ -96,7 +96,7 @@
_data: up_data_structs::CreateItemExData<<T>::CrossAccountId>,
_nesting_budget: &dyn up_data_structs::budget::Budget,
) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
- fail!(<pallet_common::Error<T>>::UnsupportedOperation);
+ fail!(<CommonError<T>>::UnsupportedOperation);
}
fn burn_item(
@@ -105,7 +105,7 @@
_token: TokenId,
_amount: u128,
) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
- fail!(<pallet_common::Error<T>>::UnsupportedOperation);
+ fail!(<CommonError<T>>::UnsupportedOperation);
}
fn set_collection_properties(
@@ -113,7 +113,7 @@
_sender: <T>::CrossAccountId,
_properties: Vec<up_data_structs::Property>,
) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
- fail!(<pallet_common::Error<T>>::UnsupportedOperation);
+ fail!(<CommonError<T>>::UnsupportedOperation);
}
fn delete_collection_properties(
@@ -121,7 +121,7 @@
_sender: &<T>::CrossAccountId,
_property_keys: Vec<up_data_structs::PropertyKey>,
) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
- fail!(<pallet_common::Error<T>>::UnsupportedOperation);
+ fail!(<CommonError<T>>::UnsupportedOperation);
}
fn set_token_properties(
@@ -131,7 +131,7 @@
_properties: Vec<up_data_structs::Property>,
_budget: &dyn up_data_structs::budget::Budget,
) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
- fail!(<pallet_common::Error<T>>::UnsupportedOperation);
+ fail!(<CommonError<T>>::UnsupportedOperation);
}
fn delete_token_properties(
@@ -141,7 +141,7 @@
_property_keys: Vec<up_data_structs::PropertyKey>,
_budget: &dyn up_data_structs::budget::Budget,
) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
- fail!(<pallet_common::Error<T>>::UnsupportedOperation);
+ fail!(<CommonError<T>>::UnsupportedOperation);
}
fn get_token_properties_raw(
@@ -161,18 +161,23 @@
_sender: &<T>::CrossAccountId,
_property_permissions: Vec<up_data_structs::PropertyKeyPermission>,
) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
- fail!(<pallet_common::Error<T>>::UnsupportedOperation);
+ fail!(<CommonError<T>>::UnsupportedOperation);
}
fn transfer(
&self,
sender: <T>::CrossAccountId,
to: <T>::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 {
- <Pallet<T>>::transfer(self, &sender, &to, amount, budget)
+ ensure!(
+ token == TokenId::default(),
+ <CommonError<T>>::FungibleItemsHaveNoId
+ );
+
+ <Pallet<T>>::transfer(&sender, &to, amount)
}
fn approve(
@@ -182,7 +187,7 @@
_token: TokenId,
_amount: u128,
) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
- fail!(<pallet_common::Error<T>>::UnsupportedOperation);
+ fail!(<CommonError<T>>::UnsupportedOperation);
}
fn approve_from(
@@ -193,7 +198,7 @@
_token: TokenId,
_amount: u128,
) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
- fail!(<pallet_common::Error<T>>::UnsupportedOperation);
+ fail!(<CommonError<T>>::UnsupportedOperation);
}
fn transfer_from(
@@ -201,11 +206,16 @@
sender: <T>::CrossAccountId,
from: <T>::CrossAccountId,
to: <T>::CrossAccountId,
- _token: TokenId,
+ token: TokenId,
amount: u128,
budget: &dyn up_data_structs::budget::Budget,
) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
- <Pallet<T>>::transfer_from(self, &sender, &from, &to, amount, budget)
+ ensure!(
+ token == TokenId::default(),
+ <CommonError<T>>::FungibleItemsHaveNoId
+ );
+
+ <Pallet<T>>::transfer_from(&sender, &from, &to, amount, budget)
}
fn burn_from(
@@ -216,7 +226,7 @@
_amount: u128,
_budget: &dyn up_data_structs::budget::Budget,
) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
- fail!(<pallet_common::Error<T>>::UnsupportedOperation);
+ fail!(<CommonError<T>>::UnsupportedOperation);
}
fn check_nesting(
@@ -226,7 +236,7 @@
_under: TokenId,
_budget: &dyn up_data_structs::budget::Budget,
) -> frame_support::sp_runtime::DispatchResult {
- fail!(<pallet_common::Error<T>>::UnsupportedOperation);
+ fail!(<CommonError<T>>::UnsupportedOperation);
}
fn nest(&self, _under: TokenId, _to_nest: (up_data_structs::CollectionId, TokenId)) {}
@@ -332,7 +342,7 @@
_operator: <T>::CrossAccountId,
_approve: bool,
) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
- fail!(<pallet_common::Error<T>>::UnsupportedOperation);
+ fail!(<CommonError<T>>::UnsupportedOperation);
}
fn allowance_for_all(
@@ -347,6 +357,6 @@
&self,
_token: TokenId,
) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
- fail!(<pallet_common::Error<T>>::UnsupportedOperation);
+ fail!(<CommonError<T>>::UnsupportedOperation);
}
}
pallets/balances-adapter/src/erc.rsdiffbeforeafterboth--- a/pallets/balances-adapter/src/erc.rs
+++ b/pallets/balances-adapter/src/erc.rs
@@ -58,12 +58,8 @@
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(<StructureWeight<T>>::find_parent());
- <Pallet<T>>::transfer(self, &caller, &to, amount, &budget)
- .map_err(|e| dispatch_to_evm::<T>(e.error))?;
+ <Pallet<T>>::transfer(&caller, &to, amount).map_err(|e| dispatch_to_evm::<T>(e.error))?;
Ok(true)
}
@@ -83,7 +79,7 @@
.recorder()
.weight_calls_budget(<StructureWeight<T>>::find_parent());
- <Pallet<T>>::transfer_from(self, &caller, &from, &to, amount, &budget)
+ <Pallet<T>>::transfer_from(&caller, &from, &to, amount, &budget)
.map_err(|e| dispatch_to_evm::<T>(e.error))?;
Ok(true)
}
@@ -106,12 +102,8 @@
let caller = T::CrossAccountId::from_eth(caller);
let to = to.into_sub_cross_account::<T>()?;
let amount = amount.try_into().map_err(|_| "amount overflow")?;
- let budget = self
- .recorder()
- .weight_calls_budget(<StructureWeight<T>>::find_parent());
- <Pallet<T>>::transfer(self, &caller, &to, amount, &budget)
- .map_err(|e| dispatch_to_evm::<T>(e.error))?;
+ <Pallet<T>>::transfer(&caller, &to, amount).map_err(|e| dispatch_to_evm::<T>(e.error))?;
Ok(true)
}
@@ -137,7 +129,7 @@
.recorder()
.weight_calls_budget(<StructureWeight<T>>::find_parent());
- <Pallet<T>>::transfer_from(self, &caller, &from, &to, amount, &budget)
+ <Pallet<T>>::transfer_from(&caller, &from, &to, amount, &budget)
.map_err(|e| dispatch_to_evm::<T>(e.error))?;
Ok(true)
pallets/balances-adapter/src/lib.rsdiffbeforeafterboth--- a/pallets/balances-adapter/src/lib.rs
+++ b/pallets/balances-adapter/src/lib.rs
@@ -155,20 +155,15 @@
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<T>,
from: &T::CrossAccountId,
to: &T::CrossAccountId,
amount: u128,
- _nesting_budget: &dyn Budget,
) -> DispatchResultWithPostInfo {
<PalletCommon<T>>::ensure_correct_receiver(to)?;
@@ -185,19 +180,18 @@
})
}
- /// 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<T>,
spender: &T::CrossAccountId,
from: &T::CrossAccountId,
to: &T::CrossAccountId,
@@ -208,7 +202,7 @@
if allowance < amount {
return Err(<CommonError<T>>::ApprovedValueTooLow.into());
}
- Self::transfer(collection, from, to, amount, nesting_budget)
+ Self::transfer(from, to, amount)
}
}
}
pallets/common/src/lib.rsdiffbeforeafterboth--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -783,6 +783,9 @@
/// 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.
pallets/fungible/src/common.rsdiffbeforeafterboth19use frame_support::{dispatch::DispatchResultWithPostInfo, ensure, fail, weights::Weight};19use frame_support::{dispatch::DispatchResultWithPostInfo, ensure, fail, weights::Weight};20use pallet_common::{20use pallet_common::{21 weights::WeightInfo as _, with_weight, CommonCollectionOperations, CommonWeightInfo,21 weights::WeightInfo as _, with_weight, CommonCollectionOperations, CommonWeightInfo,22 RefungibleExtensions, SelfWeightOf as PalletCommonWeightOf,22 Error as CommonError, RefungibleExtensions, SelfWeightOf as PalletCommonWeightOf,23};23};24use sp_runtime::{ArithmeticError, DispatchError};24use sp_runtime::{ArithmeticError, DispatchError};25use sp_std::{vec, vec::Vec};25use sp_std::{vec, vec::Vec};169 ) -> DispatchResultWithPostInfo {169 ) -> DispatchResultWithPostInfo {170 ensure!(170 ensure!(171 token == TokenId::default(),171 token == TokenId::default(),172 <Error<T>>::FungibleItemsHaveNoId172 <CommonError<T>>::FungibleItemsHaveNoId173 );173 );174174175 with_weight(175 with_weight(188 ) -> DispatchResultWithPostInfo {188 ) -> DispatchResultWithPostInfo {189 ensure!(189 ensure!(190 token == TokenId::default(),190 token == TokenId::default(),191 <Error<T>>::FungibleItemsHaveNoId191 <CommonError<T>>::FungibleItemsHaveNoId192 );192 );193193194 <Pallet<T>>::transfer(self, &from, &to, amount, nesting_budget)194 <Pallet<T>>::transfer(self, &from, &to, amount, nesting_budget)203 ) -> DispatchResultWithPostInfo {203 ) -> DispatchResultWithPostInfo {204 ensure!(204 ensure!(205 token == TokenId::default(),205 token == TokenId::default(),206 <Error<T>>::FungibleItemsHaveNoId206 <CommonError<T>>::FungibleItemsHaveNoId207 );207 );208208209 with_weight(209 with_weight(222 ) -> DispatchResultWithPostInfo {222 ) -> DispatchResultWithPostInfo {223 ensure!(223 ensure!(224 token == TokenId::default(),224 token == TokenId::default(),225 <Error<T>>::FungibleItemsHaveNoId225 <CommonError<T>>::FungibleItemsHaveNoId226 );226 );227227228 with_weight(228 with_weight(242 ) -> DispatchResultWithPostInfo {242 ) -> DispatchResultWithPostInfo {243 ensure!(243 ensure!(244 token == TokenId::default(),244 token == TokenId::default(),245 <Error<T>>::FungibleItemsHaveNoId245 <CommonError<T>>::FungibleItemsHaveNoId246 );246 );247247248 <Pallet<T>>::transfer_from(self, &sender, &from, &to, amount, nesting_budget)248 <Pallet<T>>::transfer_from(self, &sender, &from, &to, amount, nesting_budget)258 ) -> DispatchResultWithPostInfo {258 ) -> DispatchResultWithPostInfo {259 ensure!(259 ensure!(260 token == TokenId::default(),260 token == TokenId::default(),261 <Error<T>>::FungibleItemsHaveNoId261 <CommonError<T>>::FungibleItemsHaveNoId262 );262 );263263264 with_weight(264 with_weight(pallets/fungible/src/lib.rsdiffbeforeafterboth--- a/pallets/fungible/src/lib.rs
+++ b/pallets/fungible/src/lib.rs
@@ -123,8 +123,6 @@
pub enum Error<T> {
/// 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.