12345678910111213141516171819202122extern crate alloc;2324#[cfg(not(feature = "std"))]25use alloc::format;2627use core::{28 char::{REPLACEMENT_CHARACTER, decode_utf16},29 convert::TryInto,30 ops::Deref,31};32use evm_coder::{33 abi::AbiType, ToLog, execution::*, generate_stubgen, solidity_interface, types::*, weight,34};35use pallet_common::{36 CommonWeightInfo,37 erc::{CommonEvmHandler, PrecompileResult},38 eth::{collection_id_to_address, EthCrossAccount},39};40use pallet_evm::{account::CrossAccountId, PrecompileHandle};41use pallet_evm_coder_substrate::{call, dispatch_to_evm, WithRecorder};42use pallet_structure::{SelfWeightOf as StructureWeight, weights::WeightInfo as _};43use sp_std::vec::Vec;44use up_data_structs::TokenId;4546use crate::{47 Allowance, Balance, common::CommonWeights, Config, Pallet, RefungibleHandle, SelfWeightOf,48 TotalSupply, weights::WeightInfo,49};5051pub 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<uint256> {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: uint256,76 },77 78 79 Approval {80 #[indexed]81 owner: address,82 #[indexed]83 spender: address,84 value: uint256,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<uint256> {108 self.consume_store_reads(1)?;109 Ok(<TotalSupply<T>>::get((self.id, self.1)).into())110 }111112 113 fn decimals(&self) -> Result<uint8> {114 115 Ok(0)116 }117118 119 120 121 fn balance_of(&self, owner: address) -> Result<uint256> {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: uint256) -> 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: uint256,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: uint256) -> 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<uint256> {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 fn burn_from(&mut self, caller: caller, from: address, amount: uint256) -> Result<bool> {212 let caller = T::CrossAccountId::from_eth(caller);213 let from = T::CrossAccountId::from_eth(from);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, self.1, amount, &budget)220 .map_err(dispatch_to_evm::<T>)?;221 Ok(true)222 }223224 225 226 227 228 #[weight(<SelfWeightOf<T>>::burn_from())]229 fn burn_from_cross(230 &mut self,231 caller: caller,232 from: EthCrossAccount,233 amount: uint256,234 ) -> Result<bool> {235 let caller = T::CrossAccountId::from_eth(caller);236 let from = from.into_sub_cross_account::<T>()?;237 let amount = amount.try_into().map_err(|_| "amount overflow")?;238 let budget = self239 .recorder240 .weight_calls_budget(<StructureWeight<T>>::find_parent());241242 <Pallet<T>>::burn_from(self, &caller, &from, self.1, amount, &budget)243 .map_err(dispatch_to_evm::<T>)?;244 Ok(true)245 }246247 248 249 250 251 252 253 254 #[weight(<SelfWeightOf<T>>::approve())]255 fn approve_cross(256 &mut self,257 caller: caller,258 spender: EthCrossAccount,259 amount: uint256,260 ) -> Result<bool> {261 let caller = T::CrossAccountId::from_eth(caller);262 let spender = spender.into_sub_cross_account::<T>()?;263 let amount = amount.try_into().map_err(|_| "amount overflow")?;264265 <Pallet<T>>::set_allowance(self, &caller, &spender, self.1, amount)266 .map_err(dispatch_to_evm::<T>)?;267 Ok(true)268 }269 270 271 272 #[weight(<SelfWeightOf<T>>::repartition_item())]273 fn repartition(&mut self, caller: caller, amount: uint256) -> Result<bool> {274 let caller = T::CrossAccountId::from_eth(caller);275 let amount = amount.try_into().map_err(|_| "amount overflow")?;276277 <Pallet<T>>::repartition(self, &caller, self.1, amount).map_err(dispatch_to_evm::<T>)?;278 Ok(true)279 }280281 282 283 284 #[weight(<CommonWeights<T>>::transfer())]285 fn transfer_cross(286 &mut self,287 caller: caller,288 to: EthCrossAccount,289 amount: uint256,290 ) -> Result<bool> {291 let caller = T::CrossAccountId::from_eth(caller);292 let to = to.into_sub_cross_account::<T>()?;293 let amount = amount.try_into().map_err(|_| "amount overflow")?;294 let budget = self295 .recorder296 .weight_calls_budget(<StructureWeight<T>>::find_parent());297298 <Pallet<T>>::transfer(self, &caller, &to, self.1, amount, &budget)299 .map_err(dispatch_to_evm::<T>)?;300 Ok(true)301 }302303 304 305 306 307 #[weight(<CommonWeights<T>>::transfer_from())]308 fn transfer_from_cross(309 &mut self,310 caller: caller,311 from: EthCrossAccount,312 to: EthCrossAccount,313 amount: uint256,314 ) -> Result<bool> {315 let caller = T::CrossAccountId::from_eth(caller);316 let from = from.into_sub_cross_account::<T>()?;317 let to = to.into_sub_cross_account::<T>()?;318 let amount = amount.try_into().map_err(|_| "amount overflow")?;319 let budget = self320 .recorder321 .weight_calls_budget(<StructureWeight<T>>::find_parent());322323 <Pallet<T>>::transfer_from(self, &caller, &from, &to, self.1, amount, &budget)324 .map_err(dispatch_to_evm::<T>)?;325 Ok(true)326 }327}328329impl<T: Config> RefungibleTokenHandle<T> {330 pub fn into_inner(self) -> RefungibleHandle<T> {331 self.0332 }333 pub fn common_mut(&mut self) -> &mut RefungibleHandle<T> {334 &mut self.0335 }336}337338impl<T: Config> WithRecorder<T> for RefungibleTokenHandle<T> {339 fn recorder(&self) -> &pallet_evm_coder_substrate::SubstrateRecorder<T> {340 self.0.recorder()341 }342 fn into_recorder(self) -> pallet_evm_coder_substrate::SubstrateRecorder<T> {343 self.0.into_recorder()344 }345}346347impl<T: Config> Deref for RefungibleTokenHandle<T> {348 type Target = RefungibleHandle<T>;349350 fn deref(&self) -> &Self::Target {351 &self.0352 }353}354355#[solidity_interface(356 name = UniqueRefungibleToken,357 is(ERC20, ERC20UniqueExtensions, ERC1633)358)]359impl<T: Config> RefungibleTokenHandle<T> where T::AccountId: From<[u8; 32]> {}360361generate_stubgen!(gen_impl, UniqueRefungibleTokenCall<()>, true);362generate_stubgen!(gen_iface, UniqueRefungibleTokenCall<()>, false);363364impl<T: Config> CommonEvmHandler for RefungibleTokenHandle<T>365where366 T::AccountId: From<[u8; 32]>,367{368 const CODE: &'static [u8] = include_bytes!("./stubs/UniqueRefungibleToken.raw");369370 fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {371 call::<T, UniqueRefungibleTokenCall<T>, _, _>(handle, self)372 }373}