12345678910111213141516171819extern crate alloc;20use core::char::{REPLACEMENT_CHARACTER, decode_utf16};21use core::convert::TryInto;22use evm_coder::{23 ToLog,24 execution::*,25 generate_stubgen, solidity_interface,26 types::*,27 weight,28 custom_signature::{SignatureUnit, FunctionSignature, SignaturePreferences},29 make_signature,30};31use up_data_structs::CollectionMode;32use pallet_common::erc::{CommonEvmHandler, PrecompileResult};33use sp_std::vec::Vec;34use pallet_evm::{account::CrossAccountId, PrecompileHandle};35use pallet_evm_coder_substrate::{call, dispatch_to_evm};36use pallet_structure::{SelfWeightOf as StructureWeight, weights::WeightInfo as _};37use pallet_common::{CollectionHandle, erc::CollectionCall};3839use crate::{40 Allowance, Balance, Config, FungibleHandle, Pallet, SelfWeightOf, TotalSupply,41 weights::WeightInfo,42};4344#[derive(ToLog)]45pub enum ERC20Events {46 Transfer {47 #[indexed]48 from: address,49 #[indexed]50 to: address,51 value: uint256,52 },53 Approval {54 #[indexed]55 owner: address,56 #[indexed]57 spender: address,58 value: uint256,59 },60}6162#[solidity_interface(name = ERC20, events(ERC20Events))]63impl<T: Config> FungibleHandle<T> {64 fn name(&self) -> Result<string> {65 Ok(decode_utf16(self.name.iter().copied())66 .map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))67 .collect::<string>())68 }69 fn symbol(&self) -> Result<string> {70 Ok(string::from_utf8_lossy(&self.token_prefix).into())71 }72 fn total_supply(&self) -> Result<uint256> {73 self.consume_store_reads(1)?;74 Ok(<TotalSupply<T>>::get(self.id).into())75 }7677 fn decimals(&self) -> Result<uint8> {78 Ok(if let CollectionMode::Fungible(decimals) = &self.mode {79 *decimals80 } else {81 unreachable!()82 })83 }84 fn balance_of(&self, owner: address) -> Result<uint256> {85 self.consume_store_reads(1)?;86 let owner = T::CrossAccountId::from_eth(owner);87 let balance = <Balance<T>>::get((self.id, owner));88 Ok(balance.into())89 }90 #[weight(<SelfWeightOf<T>>::transfer())]91 fn transfer(&mut self, caller: caller, to: address, amount: uint256) -> Result<bool> {92 let caller = T::CrossAccountId::from_eth(caller);93 let to = T::CrossAccountId::from_eth(to);94 let amount = amount.try_into().map_err(|_| "amount overflow")?;95 let budget = self96 .recorder97 .weight_calls_budget(<StructureWeight<T>>::find_parent());9899 <Pallet<T>>::transfer(self, &caller, &to, amount, &budget).map_err(|_| "transfer error")?;100 Ok(true)101 }102 #[weight(<SelfWeightOf<T>>::transfer_from())]103 fn transfer_from(104 &mut self,105 caller: caller,106 from: address,107 to: address,108 amount: uint256,109 ) -> Result<bool> {110 let caller = T::CrossAccountId::from_eth(caller);111 let from = T::CrossAccountId::from_eth(from);112 let to = T::CrossAccountId::from_eth(to);113 let amount = amount.try_into().map_err(|_| "amount overflow")?;114 let budget = self115 .recorder116 .weight_calls_budget(<StructureWeight<T>>::find_parent());117118 <Pallet<T>>::transfer_from(self, &caller, &from, &to, amount, &budget)119 .map_err(dispatch_to_evm::<T>)?;120 Ok(true)121 }122 #[weight(<SelfWeightOf<T>>::approve())]123 fn approve(&mut self, caller: caller, spender: address, amount: uint256) -> Result<bool> {124 let caller = T::CrossAccountId::from_eth(caller);125 let spender = T::CrossAccountId::from_eth(spender);126 let amount = amount.try_into().map_err(|_| "amount overflow")?;127128 <Pallet<T>>::set_allowance(self, &caller, &spender, amount)129 .map_err(dispatch_to_evm::<T>)?;130 Ok(true)131 }132 fn allowance(&self, owner: address, spender: address) -> Result<uint256> {133 self.consume_store_reads(1)?;134 let owner = T::CrossAccountId::from_eth(owner);135 let spender = T::CrossAccountId::from_eth(spender);136137 Ok(<Allowance<T>>::get((self.id, owner, spender)).into())138 }139}140141#[solidity_interface(name = ERC20Mintable)]142impl<T: Config> FungibleHandle<T> {143 144 145 146 #[weight(<SelfWeightOf<T>>::create_item())]147 fn mint(&mut self, caller: caller, to: address, amount: uint256) -> Result<bool> {148 let caller = T::CrossAccountId::from_eth(caller);149 let to = T::CrossAccountId::from_eth(to);150 let amount = amount.try_into().map_err(|_| "amount overflow")?;151 let budget = self152 .recorder153 .weight_calls_budget(<StructureWeight<T>>::find_parent());154 <Pallet<T>>::create_item(&self, &caller, (to, amount), &budget)155 .map_err(dispatch_to_evm::<T>)?;156 Ok(true)157 }158}159160#[solidity_interface(name = ERC20UniqueExtensions)]161impl<T: Config> FungibleHandle<T>162where163 T::AccountId: From<[u8; 32]>,164{165 #[weight(<SelfWeightOf<T>>::approve())]166 fn approve_cross(167 &mut self,168 caller: caller,169 spender: EthCrossAccount,170 amount: uint256,171 ) -> Result<bool> {172 let caller = T::CrossAccountId::from_eth(caller);173 let spender = spender.into_sub_cross_account::<T>()?;174 let amount = amount.try_into().map_err(|_| "amount overflow")?;175176 <Pallet<T>>::set_allowance(self, &caller, &spender, amount)177 .map_err(dispatch_to_evm::<T>)?;178 Ok(true)179 }180181 182 183 184 185 186 #[weight(<SelfWeightOf<T>>::burn_from())]187 fn burn_from(&mut self, caller: caller, from: address, amount: uint256) -> Result<bool> {188 let caller = T::CrossAccountId::from_eth(caller);189 let from = T::CrossAccountId::from_eth(from);190 let amount = amount.try_into().map_err(|_| "amount overflow")?;191 let budget = self192 .recorder193 .weight_calls_budget(<StructureWeight<T>>::find_parent());194195 <Pallet<T>>::burn_from(self, &caller, &from, amount, &budget)196 .map_err(dispatch_to_evm::<T>)?;197 Ok(true)198 }199200 201 202 203 204 205 #[weight(<SelfWeightOf<T>>::burn_from())]206 fn burn_from_cross(207 &mut self,208 caller: caller,209 from: EthCrossAccount,210 amount: uint256,211 ) -> Result<bool> {212 let caller = T::CrossAccountId::from_eth(caller);213 let from = from.into_sub_cross_account::<T>()?;214 let amount = amount.try_into().map_err(|_| "amount overflow")?;215 let budget = self216 .recorder217 .weight_calls_budget(<StructureWeight<T>>::find_parent());218219 <Pallet<T>>::burn_from(self, &caller, &from, amount, &budget)220 .map_err(dispatch_to_evm::<T>)?;221 Ok(true)222 }223224 225 226 #[weight(<SelfWeightOf<T>>::create_multiple_items_ex(amounts.len() as u32))]227 fn mint_bulk(&mut self, caller: caller, amounts: Vec<(address, uint256)>) -> Result<bool> {228 let caller = T::CrossAccountId::from_eth(caller);229 let budget = self230 .recorder231 .weight_calls_budget(<StructureWeight<T>>::find_parent());232 let amounts = amounts233 .into_iter()234 .map(|(to, amount)| {235 Ok((236 T::CrossAccountId::from_eth(to),237 amount.try_into().map_err(|_| "amount overflow")?,238 ))239 })240 .collect::<Result<_>>()?;241242 <Pallet<T>>::create_multiple_items(&self, &caller, amounts, &budget)243 .map_err(dispatch_to_evm::<T>)?;244 Ok(true)245 }246247 #[weight(<SelfWeightOf<T>>::transfer_from())]248 fn transfer_from_cross(249 &mut self,250 caller: caller,251 from: EthCrossAccount,252 to: EthCrossAccount,253 amount: uint256,254 ) -> Result<bool> {255 let caller = T::CrossAccountId::from_eth(caller);256 let from = from.into_sub_cross_account::<T>()?;257 let to = to.into_sub_cross_account::<T>()?;258 let amount = amount.try_into().map_err(|_| "amount overflow")?;259 let budget = self260 .recorder261 .weight_calls_budget(<StructureWeight<T>>::find_parent());262263 <Pallet<T>>::transfer_from(self, &caller, &from, &to, amount, &budget)264 .map_err(dispatch_to_evm::<T>)?;265 Ok(true)266 }267}268269#[solidity_interface(270 name = UniqueFungible,271 is(272 ERC20,273 ERC20Mintable,274 ERC20UniqueExtensions,275 Collection(via(common_mut returns CollectionHandle<T>)),276 )277)]278impl<T: Config> FungibleHandle<T> where T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]> {}279280generate_stubgen!(gen_impl, UniqueFungibleCall<()>, true);281generate_stubgen!(gen_iface, UniqueFungibleCall<()>, false);282283impl<T: Config> CommonEvmHandler for FungibleHandle<T>284where285 T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]>,286{287 const CODE: &'static [u8] = include_bytes!("./stubs/UniqueFungible.raw");288289 fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {290 call::<T, UniqueFungibleCall<T>, _, _>(handle, self)291 }292}