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::{ToLog, execution::*, generate_stubgen, solidity, solidity_interface, types::*, weight};33use pallet_common::{34 CommonWeightInfo,35 erc::{CommonEvmHandler, PrecompileResult, static_property::key},36 eth::map_eth_to_id,37};38use pallet_evm::{account::CrossAccountId, PrecompileHandle};39use pallet_evm_coder_substrate::{call, dispatch_to_evm, WithRecorder};40use pallet_structure::{SelfWeightOf as StructureWeight, weights::WeightInfo as _};41use sp_core::H160;42use sp_std::vec::Vec;43use up_data_structs::{mapping::TokenAddressMapping, PropertyScope, TokenId};4445use crate::{46 Allowance, Balance, common::CommonWeights, Config, Pallet, RefungibleHandle, SelfWeightOf,47 TokenProperties, TotalSupply, weights::WeightInfo,48};4950pub struct RefungibleTokenHandle<T: Config>(pub RefungibleHandle<T>, pub TokenId);5152#[solidity_interface(name = "ERC1633")]53impl<T: Config> RefungibleTokenHandle<T> {54 fn parent_token(&self) -> Result<address> {55 self.consume_store_reads(2)?;56 let props = <TokenProperties<T>>::get((self.id, self.1));57 let key = key::parent_nft();5859 let key_scoped = PropertyScope::Eth60 .apply(key)61 .expect("property key shouldn't exceed length limit");62 if let Some(value) = props.get(&key_scoped) {63 Ok(H160::from_slice(value.as_slice()))64 } else {65 Ok(*T::CrossTokenAddressMapping::token_to_address(self.id, self.1).as_eth())66 }67 }6869 fn parent_token_id(&self) -> Result<uint256> {70 self.consume_store_reads(2)?;71 let props = <TokenProperties<T>>::get((self.id, self.1));72 let key = key::parent_nft();7374 let key_scoped = PropertyScope::Eth75 .apply(key)76 .expect("property key shouldn't exceed length limit");77 if let Some(value) = props.get(&key_scoped) {78 let nft_token_address = H160::from_slice(value.as_slice());79 let nft_token_account = T::CrossAccountId::from_eth(nft_token_address);80 let (_, token_id) = T::CrossTokenAddressMapping::address_to_token(&nft_token_account)81 .ok_or("parent NFT should contain NFT token address")?;8283 Ok(token_id.into())84 } else {85 Ok(self.1.into())86 }87 }88}8990#[solidity_interface(name = "ERC1633UniqueExtensions")]91impl<T: Config> RefungibleTokenHandle<T> {92 #[solidity(rename_selector = "setParentNFT")]93 #[weight(<CommonWeights<T>>::token_owner() + <SelfWeightOf<T>>::set_parent_nft_unchecked())]94 fn set_parent_nft(95 &mut self,96 caller: caller,97 collection: address,98 nft_id: uint256,99 ) -> Result<bool> {100 self.consume_store_reads(1)?;101 let caller = T::CrossAccountId::from_eth(caller);102 let nft_collection = map_eth_to_id(&collection).ok_or("collection not found")?;103 let nft_token = nft_id.try_into()?;104105 <Pallet<T>>::set_parent_nft(&self.0, self.1, caller, nft_collection, nft_token)106 .map_err(dispatch_to_evm::<T>)?;107108 Ok(true)109 }110}111112#[derive(ToLog)]113pub enum ERC20Events {114 115 116 117 118 Transfer {119 #[indexed]120 from: address,121 #[indexed]122 to: address,123 value: uint256,124 },125 126 127 Approval {128 #[indexed]129 owner: address,130 #[indexed]131 spender: address,132 value: uint256,133 },134}135136137138139140#[solidity_interface(name = "ERC20", events(ERC20Events))]141impl<T: Config> RefungibleTokenHandle<T> {142 143 fn name(&self) -> Result<string> {144 Ok(decode_utf16(self.name.iter().copied())145 .map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))146 .collect::<string>())147 }148149 150 fn symbol(&self) -> Result<string> {151 Ok(string::from_utf8_lossy(&self.token_prefix).into())152 }153154 155 fn total_supply(&self) -> Result<uint256> {156 self.consume_store_reads(1)?;157 Ok(<TotalSupply<T>>::get((self.id, self.1)).into())158 }159160 161 fn decimals(&self) -> Result<uint8> {162 163 Ok(0)164 }165166 167 168 169 fn balance_of(&self, owner: address) -> Result<uint256> {170 self.consume_store_reads(1)?;171 let owner = T::CrossAccountId::from_eth(owner);172 let balance = <Balance<T>>::get((self.id, self.1, owner));173 Ok(balance.into())174 }175176 177 178 179 #[weight(<CommonWeights<T>>::transfer())]180 fn transfer(&mut self, caller: caller, to: address, amount: uint256) -> Result<bool> {181 let caller = T::CrossAccountId::from_eth(caller);182 let to = T::CrossAccountId::from_eth(to);183 let amount = amount.try_into().map_err(|_| "amount overflow")?;184 let budget = self185 .recorder186 .weight_calls_budget(<StructureWeight<T>>::find_parent());187188 <Pallet<T>>::transfer(self, &caller, &to, self.1, amount, &budget)189 .map_err(dispatch_to_evm::<T>)?;190 Ok(true)191 }192193 194 195 196 197 #[weight(<CommonWeights<T>>::transfer_from())]198 fn transfer_from(199 &mut self,200 caller: caller,201 from: address,202 to: address,203 amount: uint256,204 ) -> Result<bool> {205 let caller = T::CrossAccountId::from_eth(caller);206 let from = T::CrossAccountId::from_eth(from);207 let to = T::CrossAccountId::from_eth(to);208 let amount = amount.try_into().map_err(|_| "amount overflow")?;209 let budget = self210 .recorder211 .weight_calls_budget(<StructureWeight<T>>::find_parent());212213 <Pallet<T>>::transfer_from(self, &caller, &from, &to, self.1, amount, &budget)214 .map_err(dispatch_to_evm::<T>)?;215 Ok(true)216 }217218 219 220 221 222 223 224 225 #[weight(<SelfWeightOf<T>>::approve())]226 fn approve(&mut self, caller: caller, spender: address, amount: uint256) -> Result<bool> {227 let caller = T::CrossAccountId::from_eth(caller);228 let spender = T::CrossAccountId::from_eth(spender);229 let amount = amount.try_into().map_err(|_| "amount overflow")?;230231 <Pallet<T>>::set_allowance(self, &caller, &spender, self.1, amount)232 .map_err(dispatch_to_evm::<T>)?;233 Ok(true)234 }235236 237 238 239 240 fn allowance(&self, owner: address, spender: address) -> Result<uint256> {241 self.consume_store_reads(1)?;242 let owner = T::CrossAccountId::from_eth(owner);243 let spender = T::CrossAccountId::from_eth(spender);244245 Ok(<Allowance<T>>::get((self.id, self.1, owner, spender)).into())246 }247}248249#[solidity_interface(name = "ERC20UniqueExtensions")]250impl<T: Config> RefungibleTokenHandle<T> {251 252 253 254 255 #[weight(<SelfWeightOf<T>>::burn_from())]256 fn burn_from(&mut self, caller: caller, from: address, amount: uint256) -> Result<bool> {257 let caller = T::CrossAccountId::from_eth(caller);258 let from = T::CrossAccountId::from_eth(from);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>>::burn_from(self, &caller, &from, self.1, amount, &budget)265 .map_err(dispatch_to_evm::<T>)?;266 Ok(true)267 }268269 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 }280}281282impl<T: Config> RefungibleTokenHandle<T> {283 pub fn into_inner(self) -> RefungibleHandle<T> {284 self.0285 }286 pub fn common_mut(&mut self) -> &mut RefungibleHandle<T> {287 &mut self.0288 }289}290291impl<T: Config> WithRecorder<T> for RefungibleTokenHandle<T> {292 fn recorder(&self) -> &pallet_evm_coder_substrate::SubstrateRecorder<T> {293 self.0.recorder()294 }295 fn into_recorder(self) -> pallet_evm_coder_substrate::SubstrateRecorder<T> {296 self.0.into_recorder()297 }298}299300impl<T: Config> Deref for RefungibleTokenHandle<T> {301 type Target = RefungibleHandle<T>;302303 fn deref(&self) -> &Self::Target {304 &self.0305 }306}307308#[solidity_interface(309 name = "UniqueRefungibleToken",310 is(ERC20, ERC20UniqueExtensions, ERC1633, ERC1633UniqueExtensions)311)]312impl<T: Config> RefungibleTokenHandle<T> where T::AccountId: From<[u8; 32]> {}313314generate_stubgen!(gen_impl, UniqueRefungibleTokenCall<()>, true);315generate_stubgen!(gen_iface, UniqueRefungibleTokenCall<()>, false);316317impl<T: Config> CommonEvmHandler for RefungibleTokenHandle<T>318where319 T::AccountId: From<[u8; 32]>,320{321 const CODE: &'static [u8] = include_bytes!("./stubs/UniqueRefungibleToken.raw");322323 fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {324 call::<T, UniqueRefungibleTokenCall<T>, _, _>(handle, self)325 }326}