difftreelog
feat add nesting
in: master
5 files changed
Cargo.lockdiffbeforeafterboth--- a/Cargo.lock
+++ b/Cargo.lock
@@ -6264,9 +6264,11 @@
"pallet-evm",
"pallet-evm-coder-substrate",
"pallet-evm-transaction-payment",
+ "pallet-structure",
"parity-scale-codec",
"scale-info",
"sp-core",
+ "sp-runtime",
"sp-std",
"up-data-structs",
]
pallets/balances-adapter/Cargo.tomldiffbeforeafterboth--- a/pallets/balances-adapter/Cargo.toml
+++ b/pallets/balances-adapter/Cargo.toml
@@ -9,7 +9,9 @@
frame-support = { workspace = true }
frame-system = { workspace = true }
pallet-balances = { workspace = true }
+pallet-structure = { workspace = true }
sp-core = { workspace = true }
+sp-runtime = { workspace = true }
sp-std = { workspace = true }
#Parity
pallets/balances-adapter/src/common.rsdiffbeforeafterboth--- a/pallets/balances-adapter/src/common.rs
+++ b/pallets/balances-adapter/src/common.rs
@@ -1,13 +1,9 @@
use alloc::{vec, vec::Vec};
use core::marker::PhantomData;
-use crate::{Config, NativeFungibleHandle};
-use frame_support::{
- fail,
- traits::{Currency, ExistenceRequirement},
- weights::Weight,
-};
+use crate::{Config, NativeFungibleHandle, Pallet};
+use frame_support::{fail, traits::Currency, weights::Weight};
use pallet_balances::{weights::SubstrateWeight as BalancesWeight, WeightInfo};
-use pallet_common::{erc::CrossAccountId, CommonCollectionOperations, CommonWeightInfo, with_weight};
+use pallet_common::{erc::CrossAccountId, CommonCollectionOperations, CommonWeightInfo};
use up_data_structs::TokenId;
pub struct CommonWeights<T: Config>(PhantomData<T>);
@@ -188,17 +184,9 @@
to: <T>::CrossAccountId,
_token: TokenId,
amount: u128,
- _budget: &dyn up_data_structs::budget::Budget,
+ budget: &dyn up_data_structs::budget::Budget,
) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
- with_weight(
- <T as Config>::Currency::transfer(
- sender.as_sub(),
- to.as_sub(),
- amount.into(),
- ExistenceRequirement::KeepAlive,
- ),
- Default::default(),
- )
+ <Pallet<T>>::transfer(self, &sender, &to, amount, budget)
}
fn approve(
@@ -229,20 +217,9 @@
to: <T>::CrossAccountId,
_token: TokenId,
amount: u128,
- _budget: &dyn up_data_structs::budget::Budget,
+ budget: &dyn up_data_structs::budget::Budget,
) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
- if sender != from {
- fail!(<pallet_common::Error<T>>::NoPermission);
- }
- with_weight(
- <T as Config>::Currency::transfer(
- from.as_sub(),
- to.as_sub(),
- amount.into(),
- ExistenceRequirement::KeepAlive,
- ),
- Default::default(),
- )
+ <Pallet<T>>::transfer_from(self, &sender, &from, &to, amount, budget)
}
fn burn_from(
pallets/balances-adapter/src/erc.rsdiffbeforeafterboth--- a/pallets/balances-adapter/src/erc.rs
+++ b/pallets/balances-adapter/src/erc.rs
@@ -1,4 +1,4 @@
-use crate::{Config, NativeFungibleHandle, SelfWeightOf};
+use crate::{Config, NativeFungibleHandle, Pallet, SelfWeightOf};
use evm_coder::{abi::AbiType, ToLog, generate_stubgen, solidity_interface, types::*};
use frame_support::traits::{Currency, ExistenceRequirement};
use pallet_balances::WeightInfo;
@@ -10,8 +10,9 @@
use pallet_evm_coder_substrate::{
call, dispatch_to_evm,
execution::{PreDispatch, Result},
- frontier_contract,
+ frontier_contract, WithRecorder,
};
+use pallet_structure::{SelfWeightOf as StructureWeight, weights::WeightInfo as _};
use sp_core::{U256, Get};
use sp_std::vec::Vec;
@@ -73,18 +74,12 @@
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());
+ let budget = self
+ .recorder()
+ .weight_calls_budget(<StructureWeight<T>>::find_parent());
- // <Pallet<T>>::transfer(self, &caller, &to, amount, &budget).map_err(|_| "transfer error")?;
- <T as Config>::Currency::transfer(
- caller.as_sub(),
- to.as_sub(),
- amount,
- ExistenceRequirement::KeepAlive,
- )
- .map_err(dispatch_to_evm::<T>)?;
+ <Pallet<T>>::transfer(self, &caller, &to, amount, &budget)
+ .map_err(|e| dispatch_to_evm::<T>(e.error))?;
Ok(true)
}
@@ -100,23 +95,12 @@
let from = T::CrossAccountId::from_eth(from);
let to = T::CrossAccountId::from_eth(to);
let amount = amount.try_into().map_err(|_| "amount overflow")?;
-
- if from != caller {
- return Err("no permission".into());
- }
- // let budget = self
- // .recorder
- // .weight_calls_budget(<StructureWeight<T>>::find_parent());
+ let budget = self
+ .recorder()
+ .weight_calls_budget(<StructureWeight<T>>::find_parent());
- // <Pallet<T>>::transfer_from(self, &caller, &from, &to, amount, &budget)
- // .map_err(dispatch_to_evm::<T>)?;
- <T as Config>::Currency::transfer(
- caller.as_sub(),
- to.as_sub(),
- amount,
- ExistenceRequirement::KeepAlive,
- )
- .map_err(dispatch_to_evm::<T>)?;
+ <Pallet<T>>::transfer_from(self, &caller, &from, &to, amount, &budget)
+ .map_err(|e| dispatch_to_evm::<T>(e.error))?;
Ok(true)
}
}
pallets/balances-adapter/src/lib.rsdiffbeforeafterboth5use core::ops::Deref;5use core::ops::Deref;667use frame_support::sp_runtime::DispatchResult;7use frame_support::sp_runtime::DispatchResult;8pub use pallet::*;9use pallet_evm_coder_substrate::{WithRecorder, SubstrateRecorder};8use pallet_evm_coder_substrate::{WithRecorder, SubstrateRecorder};9pub use pallet::*;101011pub mod common;11pub mod common;12pub mod erc;12pub mod erc;131314pub(crate) type SelfWeightOf<T> = <T as Config>::WeightInfo;14pub(crate) type SelfWeightOf<T> = <T as Config>::WeightInfo;1516const NATIVE_FUNGIBLE_COLLECTION_ID: up_data_structs::CollectionId =17 up_data_structs::CollectionId(0);151816/// Handle for native fungible collection19/// Handle for native fungible collection17pub struct NativeFungibleHandle<T: Config>(SubstrateRecorder<T>);20pub struct NativeFungibleHandle<T: Config>(SubstrateRecorder<T>);45}48}46#[frame_support::pallet]49#[frame_support::pallet]47pub mod pallet {50pub mod pallet {51 use super::*;48 use alloc::string::String;52 use alloc::string::String;49 use frame_support::{traits::Get};53 use frame_support::{54 dispatch::PostDispatchInfo,55 ensure,56 pallet_prelude::{DispatchResultWithPostInfo, Pays},57 traits::{Currency, ExistenceRequirement, Get},58 };50 use pallet_balances::WeightInfo;59 use pallet_balances::WeightInfo;60 use pallet_common::{erc::CrossAccountId, Error as CommonError, Pallet as PalletCommon};61 use pallet_structure::Pallet as PalletStructure;51 use sp_core::U256;62 use sp_core::U256;63 use sp_runtime::DispatchError;64 use up_data_structs::{budget::Budget, mapping::TokenAddressMapping, TokenId};526553 #[pallet::config]66 #[pallet::config]54 pub trait Config:67 pub trait Config:55 frame_system::Config + pallet_evm_coder_substrate::Config + pallet_common::Config68 frame_system::Config69 + pallet_evm_coder_substrate::Config70 + pallet_common::Config71 + pallet_structure::Config56 {72 {57 /// Currency from `pallet_balances`73 /// Currency from `pallet_balances`58 type Currency: frame_support::traits::Currency<74 type Currency: frame_support::traits::Currency<75 #[pallet::pallet]91 #[pallet::pallet]76 pub struct Pallet<T>(_);92 pub struct Pallet<T>(_);9394 impl<T: Config> Pallet<T> {95 /// Checks if a non-owner has (enough) allowance from the owner to perform operations on the tokens.96 /// Returns the expected remaining allowance - it should be set manually if the transaction proceeds.97 ///98 /// - `collection`: Collection that contains the token.99 /// - `spender`: CrossAccountId who has the allowance rights.100 /// - `from`: The owner of the tokens who sets the allowance.101 /// - `amount`: Amount of tokens by which the allowance sholud be reduced.102 fn check_allowed(103 spender: &T::CrossAccountId,104 from: &T::CrossAccountId,105 nesting_budget: &dyn Budget,106 ) -> Result<u128, DispatchError> {107 if spender.conv_eq(from) {108 return Ok(0);109 }110111 if let Some(source) = T::CrossTokenAddressMapping::address_to_token(from) {112 ensure!(113 <PalletStructure<T>>::check_indirectly_owned(114 spender.clone(),115 source.0,116 source.1,117 None,118 nesting_budget119 )?,120 <CommonError<T>>::ApprovedValueTooLow,121 );122 } else if spender != from {123 return Ok(0);124 }125126 Ok(<T as Config>::Currency::free_balance(from.as_sub()).into())127 }128129 /// Transfers the specified amount of tokens. Will check that130 /// the transfer is allowed for the token.131 ///132 /// - `from`: Owner of tokens to transfer.133 /// - `to`: Recepient of transfered tokens.134 /// - `amount`: Amount of tokens to transfer.135 /// - `collection`: Collection that contains the token136 pub fn transfer(137 _collection: &NativeFungibleHandle<T>,138 from: &T::CrossAccountId,139 to: &T::CrossAccountId,140 amount: u128,141 nesting_budget: &dyn Budget,142 ) -> DispatchResultWithPostInfo {143 <PalletCommon<T>>::ensure_correct_receiver(to)?;144145 if from != to && amount != 0 {146 <T as Config>::Currency::transfer(147 from.as_sub(),148 to.as_sub(),149 amount.into(),150 ExistenceRequirement::KeepAlive,151 )?;152153 <PalletStructure<T>>::nest_if_sent_to_token(154 from.clone(),155 to,156 NATIVE_FUNGIBLE_COLLECTION_ID,157 TokenId::default(),158 nesting_budget,159 )?;160161 let balance_from: u128 =162 <T as Config>::Currency::free_balance(from.as_sub()).into();163 if balance_from == 0 {164 <PalletStructure<T>>::unnest_if_nested(165 from,166 NATIVE_FUNGIBLE_COLLECTION_ID,167 TokenId::default(),168 );169 }170 };171172 Ok(PostDispatchInfo {173 actual_weight: Some(<SelfWeightOf<T>>::transfer()),174 pays_fee: Pays::Yes,175 })176 }177178 pub fn transfer_from(179 collection: &NativeFungibleHandle<T>,180 spender: &T::CrossAccountId,181 from: &T::CrossAccountId,182 to: &T::CrossAccountId,183 amount: u128,184 nesting_budget: &dyn Budget,185 ) -> DispatchResultWithPostInfo {186 let allowance = Self::check_allowed(collection, spender, from, amount, nesting_budget)?;187 if allowance < amount {188 return Err(<CommonError<T>>::ApprovedValueTooLow.into());189 }190 Self::transfer(collection, from, to, amount, nesting_budget)191 }192 }77}193}78194