1use 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())]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())]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())]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())]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}