difftreelog
feat replace Currency with fungible traits
in: master
4 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 crate::{Config, NativeFungibleHandle, Pallet};
-use frame_support::{fail, traits::Currency, weights::Weight};
+use frame_support::{fail, weights::Weight};
use pallet_balances::{weights::SubstrateWeight as BalancesWeight, WeightInfo};
-use pallet_common::{erc::CrossAccountId, CommonCollectionOperations, CommonWeightInfo};
+use pallet_common::{CommonCollectionOperations, CommonWeightInfo};
use up_data_structs::TokenId;
pub struct CommonWeights<T: Config>(PhantomData<T>);
@@ -250,8 +250,7 @@
fn unnest(&self, _under: TokenId, _to_nest: (up_data_structs::CollectionId, TokenId)) {}
fn account_tokens(&self, account: <T>::CrossAccountId) -> Vec<TokenId> {
- let balance = <T as Config>::Currency::total_balance(account.as_sub());
- let balance: u128 = balance.into();
+ let balance = <Pallet<T>>::total_balance(&account);
if balance != 0 {
vec![TokenId::default()]
} else {
@@ -302,24 +301,23 @@
1
}
- fn account_balance(&self, account: <T>::CrossAccountId) -> u32 {
- let balance: u128 = <T as Config>::Currency::free_balance(account.as_sub()).into();
+ fn account_balance(&self, account: T::CrossAccountId) -> u32 {
+ let balance = <Pallet<T>>::balance_of(&account);
(balance != 0).into()
}
- fn balance(&self, account: <T>::CrossAccountId, token: TokenId) -> u128 {
+ fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {
if token != TokenId::default() {
return 0;
}
- <T as Config>::Currency::free_balance(account.as_sub()).into()
+ <Pallet<T>>::balance_of(&account)
}
fn total_pieces(&self, token: TokenId) -> Option<u128> {
if token != TokenId::default() {
return None;
}
- let total = <T as Config>::Currency::total_issuance();
- Some(total.into())
+ Some(<Pallet<T>>::total_issuance())
}
fn allowance(
pallets/balances-adapter/src/erc.rsdiffbeforeafterboth1use crate::{Config, NativeFungibleHandle, Pallet, SelfWeightOf};2use evm_coder::{abi::AbiType, generate_stubgen, solidity_interface, types::*};3use frame_support::traits::{Currency};4use pallet_balances::WeightInfo;5use pallet_common::{6 erc::{CommonEvmHandler, CrossAccountId, PrecompileHandle, PrecompileResult},7 eth::CrossAddress,8};9use pallet_evm_coder_substrate::{10 call, dispatch_to_evm,11 execution::{PreDispatch, Result},12 frontier_contract, WithRecorder,13};14use pallet_structure::{SelfWeightOf as StructureWeight, weights::WeightInfo as _};15use sp_core::{U256, Get};1617frontier_contract! {18 macro_rules! NativeFungibleHandle_result {...}19 impl<T: Config> Contract for NativeFungibleHandle<T> {...}20}2122#[solidity_interface(name = ERC20, enum(derive(PreDispatch)), enum_attr(weight), expect_selector = 0x942e8b22)]23impl<T: Config> NativeFungibleHandle<T> {24 fn allowance(&self, _owner: Address, _spender: Address) -> Result<U256> {25 Ok(U256::zero())26 }2728 fn approve(&mut self, _caller: Caller, _spender: Address, _amount: U256) -> Result<bool> {29 Err("Approve not supported".into())30 }3132 fn balance_of(&self, owner: Address) -> Result<U256> {33 self.consume_store_reads(1)?;34 let owner = T::CrossAccountId::from_eth(owner);35 let balance = <T as Config>::Currency::free_balance(owner.as_sub());36 Ok(balance.into())37 }3839 fn decimals(&self) -> Result<u8> {40 Ok(T::Decimals::get())41 }4243 fn name(&self) -> Result<String> {44 Ok(T::Name::get())45 }4647 fn symbol(&self) -> Result<String> {48 Ok(T::Symbol::get())49 }5051 fn total_supply(&self) -> Result<U256> {52 self.consume_store_reads(1)?;53 let total = <T as Config>::Currency::total_issuance();54 Ok(total.into())55 }5657 #[weight(<SelfWeightOf<T>>::transfer_allow_death())]58 fn transfer(&mut self, caller: Caller, to: Address, amount: U256) -> Result<bool> {59 let caller = T::CrossAccountId::from_eth(caller);60 let to = T::CrossAccountId::from_eth(to);61 let amount = amount.try_into().map_err(|_| "amount overflow")?;62 let budget = self63 .recorder()64 .weight_calls_budget(<StructureWeight<T>>::find_parent());6566 <Pallet<T>>::transfer(self, &caller, &to, amount, &budget)67 .map_err(|e| dispatch_to_evm::<T>(e.error))?;68 Ok(true)69 }7071 #[weight(<SelfWeightOf<T>>::transfer_allow_death())]72 fn transfer_from(73 &mut self,74 caller: Caller,75 from: Address,76 to: Address,77 amount: U256,78 ) -> Result<bool> {79 let caller = T::CrossAccountId::from_eth(caller);80 let from = T::CrossAccountId::from_eth(from);81 let to = T::CrossAccountId::from_eth(to);82 let amount = amount.try_into().map_err(|_| "amount overflow")?;83 let budget = self84 .recorder()85 .weight_calls_budget(<StructureWeight<T>>::find_parent());8687 <Pallet<T>>::transfer_from(self, &caller, &from, &to, amount, &budget)88 .map_err(|e| dispatch_to_evm::<T>(e.error))?;89 Ok(true)90 }91}9293#[solidity_interface(name = ERC20UniqueExtensions, enum(derive(PreDispatch)), enum_attr(weight))]94impl<T: Config> NativeFungibleHandle<T>95where96 T::AccountId: From<[u8; 32]>,97{98 fn balance_of_cross(&self, owner: CrossAddress) -> Result<U256> {99 self.consume_store_reads(1)?;100 let owner = owner.into_sub_cross_account::<T>()?;101 let balance = <T as Config>::Currency::free_balance(owner.as_sub());102 Ok(balance.into())103 }104105 #[weight(<SelfWeightOf<T>>::transfer_allow_death())]106 fn transfer_cross(&mut self, caller: Caller, to: CrossAddress, amount: U256) -> Result<bool> {107 let caller = T::CrossAccountId::from_eth(caller);108 let to = to.into_sub_cross_account::<T>()?;109 let amount = amount.try_into().map_err(|_| "amount overflow")?;110 let budget = self111 .recorder()112 .weight_calls_budget(<StructureWeight<T>>::find_parent());113114 <Pallet<T>>::transfer(self, &caller, &to, amount, &budget)115 .map_err(|e| dispatch_to_evm::<T>(e.error))?;116117 Ok(true)118 }119120 #[weight(<SelfWeightOf<T>>::transfer_allow_death())]121 fn transfer_from_cross(122 &mut self,123 caller: Caller,124 from: CrossAddress,125 to: CrossAddress,126 amount: U256,127 ) -> Result<bool> {128 let caller = T::CrossAccountId::from_eth(caller);129 let from = from.into_sub_cross_account::<T>()?;130 let to = to.into_sub_cross_account::<T>()?;131 let amount = amount.try_into().map_err(|_| "amount overflow")?;132133 if from != caller {134 return Err("no permission".into());135 }136137 let budget = self138 .recorder()139 .weight_calls_budget(<StructureWeight<T>>::find_parent());140141 <Pallet<T>>::transfer_from(self, &caller, &from, &to, amount, &budget)142 .map_err(|e| dispatch_to_evm::<T>(e.error))?;143144 Ok(true)145 }146}147148#[solidity_interface(149 name = UniqueNativeFungible,150 is(ERC20, ERC20UniqueExtensions),151 enum(derive(PreDispatch))152)]153impl<T: Config> NativeFungibleHandle<T> where T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]> {}154155generate_stubgen!(gen_impl, UniqueNativeFungibleCall<()>, true);156generate_stubgen!(gen_iface, UniqueNativeFungibleCall<()>, false);157158impl<T: Config> CommonEvmHandler for NativeFungibleHandle<T>159where160 T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]>,161{162 const CODE: &'static [u8] = include_bytes!("./stubs/UniqueNativeFungible.raw");163164 fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {165 call::<T, UniqueNativeFungibleCall<T>, _, _>(handle, self)166 }167}1use crate::{Config, NativeFungibleHandle, Pallet, SelfWeightOf};2use evm_coder::{abi::AbiType, generate_stubgen, solidity_interface, types::*};3use pallet_balances::WeightInfo;4use pallet_common::{5 erc::{CommonEvmHandler, CrossAccountId, PrecompileHandle, PrecompileResult},6 eth::CrossAddress,7};8use pallet_evm_coder_substrate::{9 call, dispatch_to_evm,10 execution::{PreDispatch, Result},11 frontier_contract, WithRecorder,12};13use pallet_structure::{SelfWeightOf as StructureWeight, weights::WeightInfo as _};14use sp_core::{U256, Get};1516frontier_contract! {17 macro_rules! NativeFungibleHandle_result {...}18 impl<T: Config> Contract for NativeFungibleHandle<T> {...}19}2021#[solidity_interface(name = ERC20, enum(derive(PreDispatch)), enum_attr(weight), expect_selector = 0x942e8b22)]22impl<T: Config> NativeFungibleHandle<T> {23 fn allowance(&self, _owner: Address, _spender: Address) -> Result<U256> {24 Ok(U256::zero())25 }2627 fn approve(&mut self, _caller: Caller, _spender: Address, _amount: U256) -> Result<bool> {28 Err("Approve not supported".into())29 }3031 fn balance_of(&self, owner: Address) -> Result<U256> {32 self.consume_store_reads(1)?;33 let owner = T::CrossAccountId::from_eth(owner);34 let balance = <Pallet<T>>::balance_of(&owner);35 Ok(balance.into())36 }3738 fn decimals(&self) -> Result<u8> {39 Ok(T::Decimals::get())40 }4142 fn name(&self) -> Result<String> {43 Ok(T::Name::get())44 }4546 fn symbol(&self) -> Result<String> {47 Ok(T::Symbol::get())48 }4950 fn total_supply(&self) -> Result<U256> {51 self.consume_store_reads(1)?;52 Ok(<Pallet<T>>::total_issuance().into())53 }5455 #[weight(<SelfWeightOf<T>>::transfer_allow_death())]56 fn transfer(&mut self, caller: Caller, to: Address, amount: U256) -> Result<bool> {57 let caller = T::CrossAccountId::from_eth(caller);58 let to = T::CrossAccountId::from_eth(to);59 let amount = amount.try_into().map_err(|_| "amount overflow")?;60 let budget = self61 .recorder()62 .weight_calls_budget(<StructureWeight<T>>::find_parent());6364 <Pallet<T>>::transfer(self, &caller, &to, amount, &budget)65 .map_err(|e| dispatch_to_evm::<T>(e.error))?;66 Ok(true)67 }6869 #[weight(<SelfWeightOf<T>>::transfer_allow_death())]70 fn transfer_from(71 &mut self,72 caller: Caller,73 from: Address,74 to: Address,75 amount: U256,76 ) -> Result<bool> {77 let caller = T::CrossAccountId::from_eth(caller);78 let from = T::CrossAccountId::from_eth(from);79 let to = T::CrossAccountId::from_eth(to);80 let amount = amount.try_into().map_err(|_| "amount overflow")?;81 let budget = self82 .recorder()83 .weight_calls_budget(<StructureWeight<T>>::find_parent());8485 <Pallet<T>>::transfer_from(self, &caller, &from, &to, amount, &budget)86 .map_err(|e| dispatch_to_evm::<T>(e.error))?;87 Ok(true)88 }89}9091#[solidity_interface(name = ERC20UniqueExtensions, enum(derive(PreDispatch)), enum_attr(weight))]92impl<T: Config> NativeFungibleHandle<T>93where94 T::AccountId: From<[u8; 32]>,95{96 fn balance_of_cross(&self, owner: CrossAddress) -> Result<U256> {97 self.consume_store_reads(1)?;98 let owner = owner.into_sub_cross_account::<T>()?;99 let balance = <Pallet<T>>::balance_of(&owner);100 Ok(balance.into())101 }102103 #[weight(<SelfWeightOf<T>>::transfer_allow_death())]104 fn transfer_cross(&mut self, caller: Caller, to: CrossAddress, amount: U256) -> Result<bool> {105 let caller = T::CrossAccountId::from_eth(caller);106 let to = to.into_sub_cross_account::<T>()?;107 let amount = amount.try_into().map_err(|_| "amount overflow")?;108 let budget = self109 .recorder()110 .weight_calls_budget(<StructureWeight<T>>::find_parent());111112 <Pallet<T>>::transfer(self, &caller, &to, amount, &budget)113 .map_err(|e| dispatch_to_evm::<T>(e.error))?;114115 Ok(true)116 }117118 #[weight(<SelfWeightOf<T>>::transfer_allow_death())]119 fn transfer_from_cross(120 &mut self,121 caller: Caller,122 from: CrossAddress,123 to: CrossAddress,124 amount: U256,125 ) -> Result<bool> {126 let caller = T::CrossAccountId::from_eth(caller);127 let from = from.into_sub_cross_account::<T>()?;128 let to = to.into_sub_cross_account::<T>()?;129 let amount = amount.try_into().map_err(|_| "amount overflow")?;130131 if from != caller {132 return Err("no permission".into());133 }134135 let budget = self136 .recorder()137 .weight_calls_budget(<StructureWeight<T>>::find_parent());138139 <Pallet<T>>::transfer_from(self, &caller, &from, &to, amount, &budget)140 .map_err(|e| dispatch_to_evm::<T>(e.error))?;141142 Ok(true)143 }144}145146#[solidity_interface(147 name = UniqueNativeFungible,148 is(ERC20, ERC20UniqueExtensions),149 enum(derive(PreDispatch))150)]151impl<T: Config> NativeFungibleHandle<T> where T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]> {}152153generate_stubgen!(gen_impl, UniqueNativeFungibleCall<()>, true);154generate_stubgen!(gen_iface, UniqueNativeFungibleCall<()>, false);155156impl<T: Config> CommonEvmHandler for NativeFungibleHandle<T>157where158 T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]>,159{160 const CODE: &'static [u8] = include_bytes!("./stubs/UniqueNativeFungible.raw");161162 fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {163 call::<T, UniqueNativeFungibleCall<T>, _, _>(handle, self)164 }165}pallets/balances-adapter/src/lib.rsdiffbeforeafterboth--- a/pallets/balances-adapter/src/lib.rs
+++ b/pallets/balances-adapter/src/lib.rs
@@ -55,7 +55,11 @@
dispatch::PostDispatchInfo,
ensure,
pallet_prelude::{DispatchResultWithPostInfo, Pays},
- traits::{Currency, ExistenceRequirement, Get},
+ traits::{
+ Get,
+ fungible::{Inspect, Mutate},
+ tokens::Preservation,
+ },
};
use pallet_balances::WeightInfo;
use pallet_common::{erc::CrossAccountId, Error as CommonError, Pallet as PalletCommon};
@@ -71,11 +75,18 @@
+ pallet_common::Config
+ pallet_structure::Config
{
- /// Currency from `pallet_balances`
- type Currency: frame_support::traits::Currency<
+ /// Inspect from `pallet_balances`
+ type Inspect: frame_support::traits::tokens::fungible::Inspect<
+ Self::AccountId,
+ Balance = Self::CurrencyBalance,
+ >;
+
+ /// Mutate from `pallet_balances`
+ type Mutate: frame_support::traits::tokens::fungible::Mutate<
Self::AccountId,
Balance = Self::CurrencyBalance,
>;
+
/// Balance type of chain
type CurrencyBalance: Into<U256> + TryFrom<U256> + TryFrom<u128> + Into<u128>;
@@ -93,6 +104,18 @@
pub struct Pallet<T>(_);
impl<T: Config> Pallet<T> {
+ pub fn balance_of(account: &T::CrossAccountId) -> u128 {
+ T::Inspect::balance(account.as_sub()).into()
+ }
+
+ pub fn total_balance(account: &T::CrossAccountId) -> u128 {
+ T::Inspect::total_balance(account.as_sub()).into()
+ }
+
+ pub fn total_issuance() -> u128 {
+ T::Inspect::total_issuance().into()
+ }
+
/// Checks if a non-owner has (enough) allowance from the owner to perform operations on the tokens.
/// Returns the expected remaining allowance - it should be set manually if the transaction proceeds.
///
@@ -121,7 +144,7 @@
return Ok(0);
}
- Ok(<T as Config>::Currency::free_balance(from.as_sub()).into())
+ Ok(Self::balance_of(from))
}
/// Transfers the specified amount of tokens. Will check that
@@ -142,14 +165,10 @@
<PalletCommon<T>>::ensure_correct_receiver(to)?;
if from != to && amount != 0 {
- <T as Config>::Currency::transfer(
- from.as_sub(),
- to.as_sub(),
- amount
- .try_into()
- .map_err(|_| sp_runtime::ArithmeticError::Overflow)?,
- ExistenceRequirement::AllowDeath,
- )?;
+ let amount = amount
+ .try_into()
+ .map_err(|_| sp_runtime::ArithmeticError::Overflow)?;
+ T::Mutate::transfer(from.as_sub(), to.as_sub(), amount, Preservation::Expendable)?;
};
Ok(PostDispatchInfo {
runtime/common/config/pallets/mod.rsdiffbeforeafterboth--- a/runtime/common/config/pallets/mod.rs
+++ b/runtime/common/config/pallets/mod.rs
@@ -92,7 +92,8 @@
pub Symbol: String = TOKEN_SYMBOL.to_string();
}
impl pallet_balances_adapter::Config for Runtime {
- type Currency = Balances;
+ type Inspect = Balances;
+ type Mutate = Balances;
type CurrencyBalance = <Balances as Currency<Self::AccountId>>::Balance;
type Decimals = Decimals;
type Name = Name;