difftreelog
fix PR
in: master
2 files changed
pallets/balances-adapter/src/lib.rsdiffbeforeafterboth1#![cfg_attr(not(feature = "std"), no_std)]23extern crate alloc;4use core::ops::Deref;56use frame_support::sp_runtime::DispatchResult;7use pallet_evm_coder_substrate::{WithRecorder, SubstrateRecorder};8pub use pallet::*;910pub mod common;11pub mod erc;1213pub(crate) type SelfWeightOf<T> = <T as Config>::WeightInfo;1415/// Handle for native fungible collection16pub struct NativeFungibleHandle<T: Config>(SubstrateRecorder<T>);17impl<T: Config> NativeFungibleHandle<T> {18 /// Creates a handle19 pub fn new() -> NativeFungibleHandle<T> {20 Self(SubstrateRecorder::new(u64::MAX))21 }2223 /// Check if the collection is internal24 pub fn check_is_internal(&self) -> DispatchResult {25 Ok(())26 }27}2829impl<T: Config> WithRecorder<T> for NativeFungibleHandle<T> {30 fn recorder(&self) -> &pallet_evm_coder_substrate::SubstrateRecorder<T> {31 &self.032 }33 fn into_recorder(self) -> pallet_evm_coder_substrate::SubstrateRecorder<T> {34 self.035 }36}3738impl<T: Config> Deref for NativeFungibleHandle<T> {39 type Target = SubstrateRecorder<T>;4041 fn deref(&self) -> &Self::Target {42 &self.043 }44}45#[frame_support::pallet]46pub mod pallet {47 use super::*;48 use alloc::string::String;49 use frame_support::{50 dispatch::PostDispatchInfo,51 ensure,52 pallet_prelude::{DispatchResultWithPostInfo, Pays},53 traits::{Currency, ExistenceRequirement, Get},54 };55 use pallet_balances::WeightInfo;56 use pallet_common::{57 erc::CrossAccountId, Error as CommonError, Pallet as PalletCommon,58 NATIVE_FUNGIBLE_COLLECTION_ID,59 };60 use pallet_structure::Pallet as PalletStructure;61 use sp_core::U256;62 use sp_runtime::DispatchError;63 use up_data_structs::{budget::Budget, mapping::TokenAddressMapping, TokenId};6465 #[pallet::config]66 pub trait Config:67 frame_system::Config68 + pallet_evm_coder_substrate::Config69 + pallet_common::Config70 + pallet_structure::Config71 {72 /// Currency from `pallet_balances`73 type Currency: frame_support::traits::Currency<74 Self::AccountId,75 Balance = Self::CurrencyBalance,76 >;77 /// Balance type of chain78 type CurrencyBalance: Into<U256> + TryFrom<U256> + TryFrom<u128> + Into<u128>;7980 /// Decimals of balance81 type Decimals: Get<u8>;82 /// Collection name83 type Name: Get<String>;84 /// Collection symbol85 type Symbol: Get<String>;8687 /// Weight information88 type WeightInfo: WeightInfo;89 }90 #[pallet::pallet]91 pub struct Pallet<T>(_);9293 impl<T: Config> Pallet<T> {94 /// Checks if a non-owner has (enough) allowance from the owner to perform operations on the tokens.95 /// Returns the expected remaining allowance - it should be set manually if the transaction proceeds.96 ///97 /// - `spender`: CrossAccountId who has the allowance rights.98 /// - `from`: The owner of the tokens who sets the allowance.99 /// - `nesting_budget`: Limit for searching parents in-depth to check ownership.100 fn check_allowed(101 spender: &T::CrossAccountId,102 from: &T::CrossAccountId,103 nesting_budget: &dyn Budget,104 ) -> Result<u128, DispatchError> {105 if let Some((collection_id, token_id)) =106 T::CrossTokenAddressMapping::address_to_token(from)107 {108 ensure!(109 <PalletStructure<T>>::check_indirectly_owned(110 spender.clone(),111 collection_id,112 token_id,113 None,114 nesting_budget115 )?,116 <CommonError<T>>::ApprovedValueTooLow,117 );118 } else if !spender.conv_eq(from) {119 return Ok(0);120 }121122 Ok(<T as Config>::Currency::free_balance(from.as_sub()).into())123 }124125 /// Transfers the specified amount of tokens. Will check that126 /// the transfer is allowed for the token.127 ///128 /// - `collection`: Collection that contains the token.129 /// - `from`: Owner of tokens to transfer.130 /// - `to`: Recepient of transfered tokens.131 /// - `amount`: Amount of tokens to transfer.132 /// - `nesting_budget`: Limit for searching parents in-depth to check ownership.133 pub fn transfer(134 _collection: &NativeFungibleHandle<T>,135 from: &T::CrossAccountId,136 to: &T::CrossAccountId,137 amount: u128,138 nesting_budget: &dyn Budget,139 ) -> DispatchResultWithPostInfo {140 <PalletCommon<T>>::ensure_correct_receiver(to)?;141142 if from != to && amount != 0 {143 <T as Config>::Currency::transfer(144 from.as_sub(),145 to.as_sub(),146 amount147 .try_into()148 .map_err(|_| sp_runtime::ArithmeticError::Overflow)?,149 ExistenceRequirement::AllowDeath,150 )?;151 };152153 Ok(PostDispatchInfo {154 actual_weight: Some(<SelfWeightOf<T>>::transfer()),155 pays_fee: Pays::Yes,156 })157 }158159 /// Transfer NFT token from one account to another.160 ///161 /// Same as the [`Self::transfer`] but spender doesn't needs to be the owner of the token.162 /// The owner should set allowance for the spender to transfer token.163 ///164 /// - `collection`: Collection that contains the token.165 /// - `spender`: Account that spend the money.166 /// - `from`: Owner of tokens to transfer.167 /// - `to`: Recepient of transfered tokens.168 /// - `amount`: Amount of tokens to transfer.169 /// - `nesting_budget`: Limit for searching parents in-depth to check ownership.170 pub fn transfer_from(171 collection: &NativeFungibleHandle<T>,172 spender: &T::CrossAccountId,173 from: &T::CrossAccountId,174 to: &T::CrossAccountId,175 amount: u128,176 nesting_budget: &dyn Budget,177 ) -> DispatchResultWithPostInfo {178 let allowance = Self::check_allowed(spender, from, nesting_budget)?;179 if allowance < amount {180 return Err(<CommonError<T>>::ApprovedValueTooLow.into());181 }182 Self::transfer(collection, from, to, amount, nesting_budget)183 }184 }185}1#![cfg_attr(not(feature = "std"), no_std)]23extern crate alloc;4use core::ops::Deref;56use frame_support::sp_runtime::DispatchResult;7use pallet_evm_coder_substrate::{WithRecorder, SubstrateRecorder};8pub use pallet::*;910pub mod common;11pub mod erc;1213pub(crate) type SelfWeightOf<T> = <T as Config>::WeightInfo;1415/// Handle for native fungible collection16pub struct NativeFungibleHandle<T: Config>(SubstrateRecorder<T>);17impl<T: Config> NativeFungibleHandle<T> {18 /// Creates a handle19 pub fn new() -> NativeFungibleHandle<T> {20 Self(SubstrateRecorder::new(u64::MAX))21 }2223 /// Check if the collection is internal24 pub fn check_is_internal(&self) -> DispatchResult {25 Ok(())26 }27}2829impl<T: Config> WithRecorder<T> for NativeFungibleHandle<T> {30 fn recorder(&self) -> &pallet_evm_coder_substrate::SubstrateRecorder<T> {31 &self.032 }33 fn into_recorder(self) -> pallet_evm_coder_substrate::SubstrateRecorder<T> {34 self.035 }36}3738impl<T: Config> Deref for NativeFungibleHandle<T> {39 type Target = SubstrateRecorder<T>;4041 fn deref(&self) -> &Self::Target {42 &self.043 }44}45#[frame_support::pallet]46pub mod pallet {47 use super::*;48 use alloc::string::String;49 use frame_support::{50 dispatch::PostDispatchInfo,51 ensure,52 pallet_prelude::{DispatchResultWithPostInfo, Pays},53 traits::{Currency, ExistenceRequirement, Get},54 };55 use pallet_balances::WeightInfo;56 use pallet_common::{erc::CrossAccountId, Error as CommonError, Pallet as PalletCommon};57 use pallet_structure::Pallet as PalletStructure;58 use sp_core::U256;59 use sp_runtime::DispatchError;60 use up_data_structs::{budget::Budget, mapping::TokenAddressMapping};6162 #[pallet::config]63 pub trait Config:64 frame_system::Config65 + pallet_evm_coder_substrate::Config66 + pallet_common::Config67 + pallet_structure::Config68 {69 /// Currency from `pallet_balances`70 type Currency: frame_support::traits::Currency<71 Self::AccountId,72 Balance = Self::CurrencyBalance,73 >;74 /// Balance type of chain75 type CurrencyBalance: Into<U256> + TryFrom<U256> + TryFrom<u128> + Into<u128>;7677 /// Decimals of balance78 type Decimals: Get<u8>;79 /// Collection name80 type Name: Get<String>;81 /// Collection symbol82 type Symbol: Get<String>;8384 /// Weight information85 type WeightInfo: WeightInfo;86 }87 #[pallet::pallet]88 pub struct Pallet<T>(_);8990 impl<T: Config> Pallet<T> {91 /// Checks if a non-owner has (enough) allowance from the owner to perform operations on the tokens.92 /// Returns the expected remaining allowance - it should be set manually if the transaction proceeds.93 ///94 /// - `spender`: CrossAccountId who has the allowance rights.95 /// - `from`: The owner of the tokens who sets the allowance.96 /// - `nesting_budget`: Limit for searching parents in-depth to check ownership.97 fn check_allowed(98 spender: &T::CrossAccountId,99 from: &T::CrossAccountId,100 nesting_budget: &dyn Budget,101 ) -> Result<u128, DispatchError> {102 if let Some((collection_id, token_id)) =103 T::CrossTokenAddressMapping::address_to_token(from)104 {105 ensure!(106 <PalletStructure<T>>::check_indirectly_owned(107 spender.clone(),108 collection_id,109 token_id,110 None,111 nesting_budget112 )?,113 <CommonError<T>>::ApprovedValueTooLow,114 );115 } else if !spender.conv_eq(from) {116 return Ok(0);117 }118119 Ok(<T as Config>::Currency::free_balance(from.as_sub()).into())120 }121122 /// Transfers the specified amount of tokens. Will check that123 /// the transfer is allowed for the token.124 ///125 /// - `collection`: Collection that contains the token.126 /// - `from`: Owner of tokens to transfer.127 /// - `to`: Recepient of transfered tokens.128 /// - `amount`: Amount of tokens to transfer.129 /// - `nesting_budget`: Limit for searching parents in-depth to check ownership.130 pub fn transfer(131 _collection: &NativeFungibleHandle<T>,132 from: &T::CrossAccountId,133 to: &T::CrossAccountId,134 amount: u128,135 _nesting_budget: &dyn Budget,136 ) -> DispatchResultWithPostInfo {137 <PalletCommon<T>>::ensure_correct_receiver(to)?;138139 if from != to && amount != 0 {140 <T as Config>::Currency::transfer(141 from.as_sub(),142 to.as_sub(),143 amount144 .try_into()145 .map_err(|_| sp_runtime::ArithmeticError::Overflow)?,146 ExistenceRequirement::AllowDeath,147 )?;148 };149150 Ok(PostDispatchInfo {151 actual_weight: Some(<SelfWeightOf<T>>::transfer()),152 pays_fee: Pays::Yes,153 })154 }155156 /// Transfer NFT token from one account to another.157 ///158 /// Same as the [`Self::transfer`] but spender doesn't needs to be the owner of the token.159 /// The owner should set allowance for the spender to transfer token.160 ///161 /// - `collection`: Collection that contains the token.162 /// - `spender`: Account that spend the money.163 /// - `from`: Owner of tokens to transfer.164 /// - `to`: Recepient of transfered tokens.165 /// - `amount`: Amount of tokens to transfer.166 /// - `nesting_budget`: Limit for searching parents in-depth to check ownership.167 pub fn transfer_from(168 collection: &NativeFungibleHandle<T>,169 spender: &T::CrossAccountId,170 from: &T::CrossAccountId,171 to: &T::CrossAccountId,172 amount: u128,173 nesting_budget: &dyn Budget,174 ) -> DispatchResultWithPostInfo {175 let allowance = Self::check_allowed(spender, from, nesting_budget)?;176 if allowance < amount {177 return Err(<CommonError<T>>::ApprovedValueTooLow.into());178 }179 Self::transfer(collection, from, to, amount, nesting_budget)180 }181 }182}tests/src/eth/nativeRpc/estimateGas.test.tsdiffbeforeafterboth--- a/tests/src/eth/nativeRpc/estimateGas.test.ts
+++ b/tests/src/eth/nativeRpc/estimateGas.test.ts
@@ -28,7 +28,7 @@
});
});
- itEth.only('estimate gas', async ({helper}) => {
+ itEth.skip('estimate gas', async ({helper}) => {
const BALANCE = 100n;
const BALANCE_TO_TRANSFER = 90n;