1234567891011121314151617use evm_coder::{18 solidity_interface,19 types::*,20 execution::{Result, Error},21};22pub use pallet_evm::{PrecompileOutput, PrecompileResult, account::CrossAccountId};23use pallet_evm_coder_substrate::dispatch_to_evm;24use sp_core::{H160, U256};25use sp_std::vec::Vec;26use up_data_structs::{Property, SponsoringRateLimit};27use alloc::format;2829use crate::{Pallet, CollectionHandle, Config, CollectionProperties};30313233pub trait CommonEvmHandler {34 const CODE: &'static [u8];3536 fn call(self, source: &H160, input: &[u8], value: U256) -> Option<PrecompileResult>;37}3839#[solidity_interface(name = "Collection")]40impl<T: Config> CollectionHandle<T> {41 fn set_collection_property(&mut self, caller: caller, key: string, value: bytes) -> Result<()> {42 let caller = T::CrossAccountId::from_eth(caller);43 let key = <Vec<u8>>::from(key)44 .try_into()45 .map_err(|_| "key too large")?;46 let value = value.try_into().map_err(|_| "value too large")?;4748 <Pallet<T>>::set_collection_property(self, &caller, Property { key, value })49 .map_err(dispatch_to_evm::<T>)50 }5152 fn delete_collection_property(&mut self, caller: caller, key: string) -> Result<()> {53 let caller = T::CrossAccountId::from_eth(caller);54 let key = <Vec<u8>>::from(key)55 .try_into()56 .map_err(|_| "key too large")?;5758 <Pallet<T>>::delete_collection_property(self, &caller, key).map_err(dispatch_to_evm::<T>)59 }6061 62 fn collection_property(&self, key: string) -> Result<bytes> {63 let key = <Vec<u8>>::from(key)64 .try_into()65 .map_err(|_| "key too large")?;6667 let props = <CollectionProperties<T>>::get(self.id);68 let prop = props.get(&key).ok_or("key not found")?;6970 Ok(prop.to_vec())71 }7273 fn eth_set_sponsor(&mut self, caller: caller, sponsor: address) -> Result<void> {74 check_is_owner(caller, self)?;7576 let sponsor = T::CrossAccountId::from_eth(sponsor);77 self.set_sponsor(sponsor.as_sub().clone());78 save(self);79 Ok(())80 }8182 fn eth_confirm_sponsorship(&mut self, caller: caller) -> Result<void> {83 let caller = T::CrossAccountId::from_eth(caller);84 if !self.confirm_sponsorship(caller.as_sub()) {85 return Err(Error::Revert("Caller is not set as sponsor".into()));86 }87 save(self);88 Ok(())89 }9091 fn set_limit(&mut self, caller: caller, limit: string, value: string) -> Result<void> {92 check_is_owner(caller, self)?;93 let mut limits = self.limits.clone();9495 match limit.as_str() {96 "accountTokenOwnershipLimit" => {97 limits.account_token_ownership_limit = parse_int(value)?;98 }99 "sponsoredDataSize" => {100 limits.sponsored_data_size = parse_int(value)?;101 }102 "sponsoredDataRateLimit" => {103 limits.sponsored_data_rate_limit =104 Some(SponsoringRateLimit::Blocks(parse_int(value)?.unwrap()));105 }106 "tokenLimit" => {107 limits.token_limit = parse_int(value)?;108 }109 "sponsorTransferTimeout" => {110 limits.sponsor_transfer_timeout = parse_int(value)?;111 }112 "sponsorApproveTimeout" => {113 limits.sponsor_approve_timeout = parse_int(value)?;114 }115 "ownerCanTransfer" => {116 limits.owner_can_transfer = parse_bool(value)?;117 }118 "ownerCanDestroy" => {119 limits.owner_can_destroy = parse_bool(value)?;120 }121 "transfersEnabled" => {122 limits.transfers_enabled = parse_bool(value)?;123 }124 _ => return Err(Error::Revert(format!("Unknown limit \"{}\"", limit))),125 }126 self.limits = <Pallet<T>>::clamp_limits(self.mode.clone(), &self.limits, limits)127 .map_err(dispatch_to_evm::<T>)?;128 save(self);129 Ok(())130 }131132 fn contract_address(&self, _caller: caller) -> Result<address> {133 Ok(crate::eth::collection_id_to_address(self.id))134 }135}136137fn check_is_owner<T: Config>(caller: caller, collection: &CollectionHandle<T>) -> Result<()> {138 let caller = T::CrossAccountId::from_eth(caller);139 collection140 .check_is_owner(&caller)141 .map_err(pallet_evm_coder_substrate::dispatch_to_evm::<T>)?;142 Ok(())143}144145fn save<T: Config>(collection: &CollectionHandle<T>) {146 <crate::CollectionById<T>>::insert(collection.id, collection.collection.clone());147}148149fn parse_int(value: string) -> Result<Option<u32>> {150 value151 .parse::<u32>()152 .map_err(|e| Error::Revert(format!("Int value \"{}\" parse error: {}", value, e)))153 .map(|value| Some(value))154}155156fn parse_bool(value: string) -> Result<Option<bool>> {157 value158 .parse::<bool>()159 .map_err(|e| Error::Revert(format!("Bool value \"{}\" parse error: {}", value, e)))160 .map(|value| Some(value))161}