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 pallet_common::eth::convert_tuple_to_cross_account;32use up_data_structs::CollectionMode;33use pallet_common::erc::{CommonEvmHandler, PrecompileResult};34use sp_std::vec::Vec;35use pallet_evm::{account::CrossAccountId, PrecompileHandle};36use pallet_evm_coder_substrate::{call, dispatch_to_evm};37use pallet_structure::{SelfWeightOf as StructureWeight, weights::WeightInfo as _};38use pallet_common::{CollectionHandle, erc::CollectionCall};3940use crate::{41 Allowance, Balance, Config, FungibleHandle, Pallet, SelfWeightOf, TotalSupply,42 weights::WeightInfo,43};4445#[derive(ToLog)]46pub enum ERC20Events {47 Transfer {48 #[indexed]49 from: address,50 #[indexed]51 to: address,52 value: uint256,53 },54 Approval {55 #[indexed]56 owner: address,57 #[indexed]58 spender: address,59 value: uint256,60 },61}6263#[solidity_interface(name = ERC20, events(ERC20Events))]64impl<T: Config> FungibleHandle<T> {65 fn name(&self) -> Result<string> {66 Ok(decode_utf16(self.name.iter().copied())67 .map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))68 .collect::<string>())69 }70 fn symbol(&self) -> Result<string> {71 Ok(string::from_utf8_lossy(&self.token_prefix).into())72 }73 fn total_supply(&self) -> Result<uint256> {74 self.consume_store_reads(1)?;75 Ok(<TotalSupply<T>>::get(self.id).into())76 }7778 fn decimals(&self) -> Result<uint8> {79 Ok(if let CollectionMode::Fungible(decimals) = &self.mode {80 *decimals81 } else {82 unreachable!()83 })84 }85 fn balance_of(&self, owner: address) -> Result<uint256> {86 self.consume_store_reads(1)?;87 let owner = T::CrossAccountId::from_eth(owner);88 let balance = <Balance<T>>::get((self.id, owner));89 Ok(balance.into())90 }91 #[weight(<SelfWeightOf<T>>::transfer())]92 fn transfer(&mut self, caller: caller, to: address, amount: uint256) -> Result<bool> {93 let caller = T::CrossAccountId::from_eth(caller);94 let to = T::CrossAccountId::from_eth(to);95 let amount = amount.try_into().map_err(|_| "amount overflow")?;96 let budget = self97 .recorder98 .weight_calls_budget(<StructureWeight<T>>::find_parent());99100 <Pallet<T>>::transfer(self, &caller, &to, amount, &budget).map_err(|_| "transfer error")?;101 Ok(true)102 }103 #[weight(<SelfWeightOf<T>>::transfer_from())]104 fn transfer_from(105 &mut self,106 caller: caller,107 from: address,108 to: address,109 amount: uint256,110 ) -> Result<bool> {111 let caller = T::CrossAccountId::from_eth(caller);112 let from = T::CrossAccountId::from_eth(from);113 let to = T::CrossAccountId::from_eth(to);114 let amount = amount.try_into().map_err(|_| "amount overflow")?;115 let budget = self116 .recorder117 .weight_calls_budget(<StructureWeight<T>>::find_parent());118119 <Pallet<T>>::transfer_from(self, &caller, &from, &to, amount, &budget)120 .map_err(dispatch_to_evm::<T>)?;121 Ok(true)122 }123 #[weight(<SelfWeightOf<T>>::approve())]124 fn approve(&mut self, caller: caller, spender: address, amount: uint256) -> Result<bool> {125 let caller = T::CrossAccountId::from_eth(caller);126 let spender = T::CrossAccountId::from_eth(spender);127 let amount = amount.try_into().map_err(|_| "amount overflow")?;128129 <Pallet<T>>::set_allowance(self, &caller, &spender, amount)130 .map_err(dispatch_to_evm::<T>)?;131 Ok(true)132 }133 fn allowance(&self, owner: address, spender: address) -> Result<uint256> {134 self.consume_store_reads(1)?;135 let owner = T::CrossAccountId::from_eth(owner);136 let spender = T::CrossAccountId::from_eth(spender);137138 Ok(<Allowance<T>>::get((self.id, owner, spender)).into())139 }140}141142#[solidity_interface(name = ERC20Mintable)]143impl<T: Config> FungibleHandle<T> {144 145 146 147 #[weight(<SelfWeightOf<T>>::create_item())]148 fn mint(&mut self, caller: caller, to: address, amount: uint256) -> Result<bool> {149 let caller = T::CrossAccountId::from_eth(caller);150 let to = T::CrossAccountId::from_eth(to);151 let amount = amount.try_into().map_err(|_| "amount overflow")?;152 let budget = self153 .recorder154 .weight_calls_budget(<StructureWeight<T>>::find_parent());155 <Pallet<T>>::create_item(&self, &caller, (to, amount), &budget)156 .map_err(dispatch_to_evm::<T>)?;157 Ok(true)158 }159}160161#[solidity_interface(name = ERC20UniqueExtensions)]162impl<T: Config> FungibleHandle<T>163where164 T::AccountId: From<[u8; 32]>,165{166 #[weight(<SelfWeightOf<T>>::approve())]167 fn approve_cross(168 &mut self,169 caller: caller,170 spender: EthCrossAccount,171 amount: uint256,172 ) -> Result<bool> {173 let caller = T::CrossAccountId::from_eth(caller);174 let spender = spender.into_sub_cross_account::<T>()?;175 let amount = amount.try_into().map_err(|_| "amount overflow")?;176177 <Pallet<T>>::set_allowance(self, &caller, &spender, amount)178 .map_err(dispatch_to_evm::<T>)?;179 Ok(true)180 }181182 183 184 185 186 187 #[weight(<SelfWeightOf<T>>::burn_from())]188 fn burn_from(&mut self, caller: caller, from: address, amount: uint256) -> Result<bool> {189 let caller = T::CrossAccountId::from_eth(caller);190 let from = T::CrossAccountId::from_eth(from);191 let amount = amount.try_into().map_err(|_| "amount overflow")?;192 let budget = self193 .recorder194 .weight_calls_budget(<StructureWeight<T>>::find_parent());195196 <Pallet<T>>::burn_from(self, &caller, &from, amount, &budget)197 .map_err(dispatch_to_evm::<T>)?;198 Ok(true)199 }200201 202 203 204 205 206 #[weight(<SelfWeightOf<T>>::burn_from())]207 fn burn_from_cross(208 &mut self,209 caller: caller,210 from: EthCrossAccount,211 amount: uint256,212 ) -> Result<bool> {213 let caller = T::CrossAccountId::from_eth(caller);214 let from = from.into_sub_cross_account::<T>()?;215 let amount = amount.try_into().map_err(|_| "amount overflow")?;216 let budget = self217 .recorder218 .weight_calls_budget(<StructureWeight<T>>::find_parent());219220 <Pallet<T>>::burn_from(self, &caller, &from, amount, &budget)221 .map_err(dispatch_to_evm::<T>)?;222 Ok(true)223 }224225 226 227 #[weight(<SelfWeightOf<T>>::create_multiple_items_ex(amounts.len() as u32))]228 fn mint_bulk(&mut self, caller: caller, amounts: Vec<(address, uint256)>) -> Result<bool> {229 let caller = T::CrossAccountId::from_eth(caller);230 let budget = self231 .recorder232 .weight_calls_budget(<StructureWeight<T>>::find_parent());233 let amounts = amounts234 .into_iter()235 .map(|(to, amount)| {236 Ok((237 T::CrossAccountId::from_eth(to),238 amount.try_into().map_err(|_| "amount overflow")?,239 ))240 })241 .collect::<Result<_>>()?;242243 <Pallet<T>>::create_multiple_items(&self, &caller, amounts, &budget)244 .map_err(dispatch_to_evm::<T>)?;245 Ok(true)246 }247248 #[weight(<SelfWeightOf<T>>::transfer_from())]249 fn transfer_from_cross(250 &mut self,251 caller: caller,252 from: EthCrossAccount,253 to: EthCrossAccount,254 amount: uint256,255 ) -> Result<bool> {256 let caller = T::CrossAccountId::from_eth(caller);257 let from = from.into_sub_cross_account::<T>()?;258 let to = to.into_sub_cross_account::<T>()?;259 let amount = amount.try_into().map_err(|_| "amount overflow")?;260 let budget = self261 .recorder262 .weight_calls_budget(<StructureWeight<T>>::find_parent());263264 <Pallet<T>>::transfer_from(self, &caller, &from, &to, amount, &budget)265 .map_err(dispatch_to_evm::<T>)?;266 Ok(true)267 }268}269270#[solidity_interface(271 name = UniqueFungible,272 is(273 ERC20,274 ERC20Mintable,275 ERC20UniqueExtensions,276 Collection(via(common_mut returns CollectionHandle<T>)),277 )278)]279impl<T: Config> FungibleHandle<T> where T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]> {}280281generate_stubgen!(gen_impl, UniqueFungibleCall<()>, true);282generate_stubgen!(gen_iface, UniqueFungibleCall<()>, false);283284impl<T: Config> CommonEvmHandler for FungibleHandle<T>285where286 T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]>,287{288 const CODE: &'static [u8] = include_bytes!("./stubs/UniqueFungible.raw");289290 fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {291 call::<T, UniqueFungibleCall<T>, _, _>(handle, self)292 }293}