Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: native fungibles collection #1015

Merged
merged 2 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pallets/balances-adapter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pallet-evm = { workspace = true }

#Local
pallet-common = { workspace = true }
pallet-fungible = { workspace = true }
pallet-evm-coder-substrate = { workspace = true }
pallet-evm-transaction-payment = { workspace = true }
up-data-structs = { workspace = true, features = ['serde1'] }
Expand All @@ -36,6 +37,7 @@ std = [
"frame-system/std",
"pallet-balances/std",
"pallet-common/std",
"pallet-fungible/std",
"pallet-evm-coder-substrate/std",
"pallet-evm/std",
"sp-core/std",
Expand Down
22 changes: 16 additions & 6 deletions pallets/balances-adapter/src/common.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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 up_data_structs::TokenId;
Expand Down Expand Up @@ -168,11 +168,16 @@ impl<T: Config> CommonCollectionOperations<T> for NativeFungibleHandle<T> {
&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(),
pallet_fungible::Error::<T>::FungibleItemsHaveNoId
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Возвращать ошибки из других палет неправильно, тем более что этой палете вовсе не нужно знать о fungible
Лучше скопируй эту ошибку сюда, либо перемести её в common

);

<Pallet<T>>::transfer(&sender, &to, amount)
}

fn approve(
Expand Down Expand Up @@ -201,11 +206,16 @@ impl<T: Config> CommonCollectionOperations<T> for NativeFungibleHandle<T> {
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(),
pallet_fungible::Error::<T>::FungibleItemsHaveNoId
);

<Pallet<T>>::transfer_from(&sender, &from, &to, amount, budget)
}

fn burn_from(
Expand Down
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
19 changes: 7 additions & 12 deletions pallets/balances-adapter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ pub mod pallet {
frame_system::Config
+ pallet_evm_coder_substrate::Config
+ pallet_common::Config
+ pallet_fungible::Config
+ pallet_structure::Config
{
/// Inspect from `pallet_balances`
Expand Down Expand Up @@ -155,20 +156,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 +181,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 +203,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)
}
}
}
Loading