12345678910111213141516171819202122use core::{23 char::{REPLACEMENT_CHARACTER, decode_utf16},24 convert::TryInto,25 ops::Deref,26};27use evm_coder::{28 abi::AbiType, ToLog, execution::*, generate_stubgen, solidity_interface, solidity, types::*,29 weight,30};31use pallet_common::{32 CommonWeightInfo,33 erc::{CommonEvmHandler, PrecompileResult},34 eth::collection_id_to_address,35};36use pallet_evm::{account::CrossAccountId, PrecompileHandle};37use pallet_evm_coder_substrate::{call, dispatch_to_evm, WithRecorder};38use pallet_structure::{SelfWeightOf as StructureWeight, weights::WeightInfo as _};39use sp_std::vec::Vec;40use sp_core::U256;41use up_data_structs::TokenId;4243use crate::{44 Allowance, Balance, common::CommonWeights, Config, Pallet, RefungibleHandle, SelfWeightOf,45 TotalSupply, weights::WeightInfo,46};4748495051pub struct RefungibleTokenHandle<T: Config>(pub RefungibleHandle<T>, pub TokenId);5253#[solidity_interface(name = ERC1633)]54impl<T: Config> RefungibleTokenHandle<T> {55 fn parent_token(&self) -> Result<Address> {56 Ok(collection_id_to_address(self.id))57 }5859 fn parent_token_id(&self) -> Result<U256> {60 Ok(self.1.into())61 }62}6364#[derive(ToLog)]65pub enum ERC20Events {66 67 68 69 70 Transfer {71 #[indexed]72 from: Address,73 #[indexed]74 to: Address,75 value: U256,76 },77 78 79 Approval {80 #[indexed]81 owner: Address,82 #[indexed]83 spender: Address,84 value: U256,85 },86}878889909192#[solidity_interface(name = ERC20, events(ERC20Events))]93impl<T: Config> RefungibleTokenHandle<T> {94 95 fn name(&self) -> Result<String> {96 Ok(decode_utf16(self.name.iter().copied())97 .map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))98 .collect::<String>())99 }100101 102 fn symbol(&self) -> Result<String> {103 Ok(String::from_utf8_lossy(&self.token_prefix).into())104 }105106 107 fn total_supply(&self) -> Result<U256> {108 self.consume_store_reads(1)?;109 Ok(<TotalSupply<T>>::get((self.id, self.1)).into())110 }111112 113 fn decimals(&self) -> Result<u8> {114 115 Ok(0)116 }117118 119 120 121 fn balance_of(&self, owner: Address) -> Result<U256> {122 self.consume_store_reads(1)?;123 let owner = T::CrossAccountId::from_eth(owner);124 let balance = <Balance<T>>::get((self.id, self.1, owner));125 Ok(balance.into())126 }127128 129 130 131 #[weight(<CommonWeights<T>>::transfer())]132 fn transfer(&mut self, caller: Caller, to: Address, amount: U256) -> Result<bool> {133 let caller = T::CrossAccountId::from_eth(caller);134 let to = T::CrossAccountId::from_eth(to);135 let amount = amount.try_into().map_err(|_| "amount overflow")?;136 let budget = self137 .recorder138 .weight_calls_budget(<StructureWeight<T>>::find_parent());139140 <Pallet<T>>::transfer(self, &caller, &to, self.1, amount, &budget)141 .map_err(dispatch_to_evm::<T>)?;142 Ok(true)143 }144145 146 147 148 149 #[weight(<CommonWeights<T>>::transfer_from())]150 fn transfer_from(151 &mut self,152 caller: Caller,153 from: Address,154 to: Address,155 amount: U256,156 ) -> Result<bool> {157 let caller = T::CrossAccountId::from_eth(caller);158 let from = T::CrossAccountId::from_eth(from);159 let to = T::CrossAccountId::from_eth(to);160 let amount = amount.try_into().map_err(|_| "amount overflow")?;161 let budget = self162 .recorder163 .weight_calls_budget(<StructureWeight<T>>::find_parent());164165 <Pallet<T>>::transfer_from(self, &caller, &from, &to, self.1, amount, &budget)166 .map_err(dispatch_to_evm::<T>)?;167 Ok(true)168 }169170 171 172 173 174 175 176 177 #[weight(<SelfWeightOf<T>>::approve())]178 fn approve(&mut self, caller: Caller, spender: Address, amount: U256) -> Result<bool> {179 let caller = T::CrossAccountId::from_eth(caller);180 let spender = T::CrossAccountId::from_eth(spender);181 let amount = amount.try_into().map_err(|_| "amount overflow")?;182183 <Pallet<T>>::set_allowance(self, &caller, &spender, self.1, amount)184 .map_err(dispatch_to_evm::<T>)?;185 Ok(true)186 }187188 189 190 191 192 fn allowance(&self, owner: Address, spender: Address) -> Result<U256> {193 self.consume_store_reads(1)?;194 let owner = T::CrossAccountId::from_eth(owner);195 let spender = T::CrossAccountId::from_eth(spender);196197 Ok(<Allowance<T>>::get((self.id, self.1, owner, spender)).into())198 }199}200201#[solidity_interface(name = ERC20UniqueExtensions)]202impl<T: Config> RefungibleTokenHandle<T>203where204 T::AccountId: From<[u8; 32]>,205{206 207 208 209 210 #[weight(<SelfWeightOf<T>>::burn_from())]211 #[solidity(hide)]212 fn burn_from(&mut self, caller: Caller, from: Address, amount: U256) -> Result<bool> {213 let caller = T::CrossAccountId::from_eth(caller);214 let from = T::CrossAccountId::from_eth(from);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, self.1, amount, &budget)221 .map_err(dispatch_to_evm::<T>)?;222 Ok(true)223 }224225 226 227 228 229 #[weight(<SelfWeightOf<T>>::burn_from())]230 fn burn_from_cross(231 &mut self,232 caller: Caller,233 from: pallet_common::eth::CrossAddress,234 amount: U256,235 ) -> Result<bool> {236 let caller = T::CrossAccountId::from_eth(caller);237 let from = from.into_sub_cross_account::<T>()?;238 let amount = amount.try_into().map_err(|_| "amount overflow")?;239 let budget = self240 .recorder241 .weight_calls_budget(<StructureWeight<T>>::find_parent());242243 <Pallet<T>>::burn_from(self, &caller, &from, self.1, amount, &budget)244 .map_err(dispatch_to_evm::<T>)?;245 Ok(true)246 }247248 249 250 251 252 253 254 255 #[weight(<SelfWeightOf<T>>::approve())]256 fn approve_cross(257 &mut self,258 caller: Caller,259 spender: pallet_common::eth::CrossAddress,260 amount: U256,261 ) -> Result<bool> {262 let caller = T::CrossAccountId::from_eth(caller);263 let spender = spender.into_sub_cross_account::<T>()?;264 let amount = amount.try_into().map_err(|_| "amount overflow")?;265266 <Pallet<T>>::set_allowance(self, &caller, &spender, self.1, amount)267 .map_err(dispatch_to_evm::<T>)?;268 Ok(true)269 }270 271 272 273 #[weight(<SelfWeightOf<T>>::repartition_item())]274 fn repartition(&mut self, caller: Caller, amount: U256) -> Result<bool> {275 let caller = T::CrossAccountId::from_eth(caller);276 let amount = amount.try_into().map_err(|_| "amount overflow")?;277278 <Pallet<T>>::repartition(self, &caller, self.1, amount).map_err(dispatch_to_evm::<T>)?;279 Ok(true)280 }281282 283 284 285 #[weight(<CommonWeights<T>>::transfer())]286 fn transfer_cross(287 &mut self,288 caller: Caller,289 to: pallet_common::eth::CrossAddress,290 amount: U256,291 ) -> Result<bool> {292 let caller = T::CrossAccountId::from_eth(caller);293 let to = to.into_sub_cross_account::<T>()?;294 let amount = amount.try_into().map_err(|_| "amount overflow")?;295 let budget = self296 .recorder297 .weight_calls_budget(<StructureWeight<T>>::find_parent());298299 <Pallet<T>>::transfer(self, &caller, &to, self.1, amount, &budget)300 .map_err(dispatch_to_evm::<T>)?;301 Ok(true)302 }303304 305 306 307 308 #[weight(<CommonWeights<T>>::transfer_from())]309 fn transfer_from_cross(310 &mut self,311 caller: Caller,312 from: pallet_common::eth::CrossAddress,313 to: pallet_common::eth::CrossAddress,314 amount: U256,315 ) -> Result<bool> {316 let caller = T::CrossAccountId::from_eth(caller);317 let from = from.into_sub_cross_account::<T>()?;318 let to = to.into_sub_cross_account::<T>()?;319 let amount = amount.try_into().map_err(|_| "amount overflow")?;320 let budget = self321 .recorder322 .weight_calls_budget(<StructureWeight<T>>::find_parent());323324 <Pallet<T>>::transfer_from(self, &caller, &from, &to, self.1, amount, &budget)325 .map_err(dispatch_to_evm::<T>)?;326 Ok(true)327 }328}329330impl<T: Config> RefungibleTokenHandle<T> {331 pub fn into_inner(self) -> RefungibleHandle<T> {332 self.0333 }334 pub fn common_mut(&mut self) -> &mut RefungibleHandle<T> {335 &mut self.0336 }337}338339impl<T: Config> WithRecorder<T> for RefungibleTokenHandle<T> {340 fn recorder(&self) -> &pallet_evm_coder_substrate::SubstrateRecorder<T> {341 self.0.recorder()342 }343 fn into_recorder(self) -> pallet_evm_coder_substrate::SubstrateRecorder<T> {344 self.0.into_recorder()345 }346}347348impl<T: Config> Deref for RefungibleTokenHandle<T> {349 type Target = RefungibleHandle<T>;350351 fn deref(&self) -> &Self::Target {352 &self.0353 }354}355356#[solidity_interface(357 name = UniqueRefungibleToken,358 is(ERC20, ERC20UniqueExtensions, ERC1633)359)]360impl<T: Config> RefungibleTokenHandle<T> where T::AccountId: From<[u8; 32]> {}361362generate_stubgen!(gen_impl, UniqueRefungibleTokenCall<()>, true);363generate_stubgen!(gen_iface, UniqueRefungibleTokenCall<()>, false);364365impl<T: Config> CommonEvmHandler for RefungibleTokenHandle<T>366where367 T::AccountId: From<[u8; 32]>,368{369 const CODE: &'static [u8] = include_bytes!("./stubs/UniqueRefungibleToken.raw");370371 fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {372 call::<T, UniqueRefungibleTokenCall<T>, _, _>(handle, self)373 }374}