Skip to content

Commit

Permalink
fix: forbid non-1 tokenid in native fungibles (#1015)
Browse files Browse the repository at this point in the history
* fix: native fungibles collection

* refactor: move FungibleItemsHaveNoId to pallet-common
  • Loading branch information
mrshiposha authored Oct 16, 2023
1 parent 0cf3df0 commit 0a49946
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 55 deletions.
54 changes: 32 additions & 22 deletions pallets/balances-adapter/src/common.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -77,7 +77,7 @@ impl<T: Config> CommonCollectionOperations<T> for NativeFungibleHandle<T> {
_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(
Expand All @@ -87,7 +87,7 @@ impl<T: Config> CommonCollectionOperations<T> for NativeFungibleHandle<T> {
_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(
Expand All @@ -96,7 +96,7 @@ impl<T: Config> CommonCollectionOperations<T> for NativeFungibleHandle<T> {
_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(
Expand All @@ -105,23 +105,23 @@ impl<T: Config> CommonCollectionOperations<T> for NativeFungibleHandle<T> {
_token: TokenId,
_amount: u128,
) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
fail!(<pallet_common::Error<T>>::UnsupportedOperation);
fail!(<CommonError<T>>::UnsupportedOperation);
}

fn set_collection_properties(
&self,
_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(
&self,
_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(
Expand All @@ -131,7 +131,7 @@ impl<T: Config> CommonCollectionOperations<T> for NativeFungibleHandle<T> {
_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(
Expand All @@ -141,7 +141,7 @@ impl<T: Config> CommonCollectionOperations<T> for NativeFungibleHandle<T> {
_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(
Expand All @@ -161,18 +161,23 @@ impl<T: Config> CommonCollectionOperations<T> for NativeFungibleHandle<T> {
_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(
Expand All @@ -182,7 +187,7 @@ impl<T: Config> CommonCollectionOperations<T> for NativeFungibleHandle<T> {
_token: TokenId,
_amount: u128,
) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
fail!(<pallet_common::Error<T>>::UnsupportedOperation);
fail!(<CommonError<T>>::UnsupportedOperation);
}

fn approve_from(
Expand All @@ -193,19 +198,24 @@ impl<T: Config> CommonCollectionOperations<T> for NativeFungibleHandle<T> {
_token: TokenId,
_amount: u128,
) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
fail!(<pallet_common::Error<T>>::UnsupportedOperation);
fail!(<CommonError<T>>::UnsupportedOperation);
}

fn transfer_from(
&self,
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(
Expand All @@ -216,7 +226,7 @@ impl<T: Config> CommonCollectionOperations<T> for NativeFungibleHandle<T> {
_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(
Expand All @@ -226,7 +236,7 @@ impl<T: Config> CommonCollectionOperations<T> for NativeFungibleHandle<T> {
_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)) {}
Expand Down Expand Up @@ -332,7 +342,7 @@ impl<T: Config> CommonCollectionOperations<T> for NativeFungibleHandle<T> {
_operator: <T>::CrossAccountId,
_approve: bool,
) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
fail!(<pallet_common::Error<T>>::UnsupportedOperation);
fail!(<CommonError<T>>::UnsupportedOperation);
}

fn allowance_for_all(
Expand All @@ -347,6 +357,6 @@ impl<T: Config> CommonCollectionOperations<T> for NativeFungibleHandle<T> {
&self,
_token: TokenId,
) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
fail!(<pallet_common::Error<T>>::UnsupportedOperation);
fail!(<CommonError<T>>::UnsupportedOperation);
}
}
16 changes: 4 additions & 12 deletions pallets/balances-adapter/src/erc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,8 @@ impl<T: Config> NativeFungibleHandle<T> {
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)
}

Expand All @@ -83,7 +79,7 @@ impl<T: Config> NativeFungibleHandle<T> {
.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)
}
Expand All @@ -106,12 +102,8 @@ where
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)
}
Expand All @@ -137,7 +129,7 @@ where
.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)
Expand Down
18 changes: 6 additions & 12 deletions pallets/balances-adapter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>,
from: &T::CrossAccountId,
to: &T::CrossAccountId,
amount: u128,
_nesting_budget: &dyn Budget,
) -> DispatchResultWithPostInfo {
<PalletCommon<T>>::ensure_correct_receiver(to)?;

Expand All @@ -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<T>,
spender: &T::CrossAccountId,
from: &T::CrossAccountId,
to: &T::CrossAccountId,
Expand All @@ -208,7 +202,7 @@ pub mod pallet {
if allowance < amount {
return Err(<CommonError<T>>::ApprovedValueTooLow.into());
}
Self::transfer(collection, from, to, amount, nesting_budget)
Self::transfer(from, to, amount)
}
}
}
3 changes: 3 additions & 0 deletions pallets/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 7 additions & 7 deletions pallets/fungible/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -169,7 +169,7 @@ impl<T: Config> CommonCollectionOperations<T> for FungibleHandle<T> {
) -> DispatchResultWithPostInfo {
ensure!(
token == TokenId::default(),
<Error<T>>::FungibleItemsHaveNoId
<CommonError<T>>::FungibleItemsHaveNoId
);

with_weight(
Expand All @@ -188,7 +188,7 @@ impl<T: Config> CommonCollectionOperations<T> for FungibleHandle<T> {
) -> DispatchResultWithPostInfo {
ensure!(
token == TokenId::default(),
<Error<T>>::FungibleItemsHaveNoId
<CommonError<T>>::FungibleItemsHaveNoId
);

<Pallet<T>>::transfer(self, &from, &to, amount, nesting_budget)
Expand All @@ -203,7 +203,7 @@ impl<T: Config> CommonCollectionOperations<T> for FungibleHandle<T> {
) -> DispatchResultWithPostInfo {
ensure!(
token == TokenId::default(),
<Error<T>>::FungibleItemsHaveNoId
<CommonError<T>>::FungibleItemsHaveNoId
);

with_weight(
Expand All @@ -222,7 +222,7 @@ impl<T: Config> CommonCollectionOperations<T> for FungibleHandle<T> {
) -> DispatchResultWithPostInfo {
ensure!(
token == TokenId::default(),
<Error<T>>::FungibleItemsHaveNoId
<CommonError<T>>::FungibleItemsHaveNoId
);

with_weight(
Expand All @@ -242,7 +242,7 @@ impl<T: Config> CommonCollectionOperations<T> for FungibleHandle<T> {
) -> DispatchResultWithPostInfo {
ensure!(
token == TokenId::default(),
<Error<T>>::FungibleItemsHaveNoId
<CommonError<T>>::FungibleItemsHaveNoId
);

<Pallet<T>>::transfer_from(self, &sender, &from, &to, amount, nesting_budget)
Expand All @@ -258,7 +258,7 @@ impl<T: Config> CommonCollectionOperations<T> for FungibleHandle<T> {
) -> DispatchResultWithPostInfo {
ensure!(
token == TokenId::default(),
<Error<T>>::FungibleItemsHaveNoId
<CommonError<T>>::FungibleItemsHaveNoId
);

with_weight(
Expand Down
2 changes: 0 additions & 2 deletions pallets/fungible/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,6 @@ pub mod pallet {
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.
Expand Down

0 comments on commit 0a49946

Please sign in to comment.