1234567891011121314151617use core::marker::PhantomData;18use evm_coder::{execution::*, generate_stubgen, solidity_interface, weight, types::*};19use ethereum as _;20use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder};21use pallet_evm::{OnMethodCall, PrecompileResult, account::CrossAccountId, PrecompileHandle};22use up_data_structs::{23 CreateCollectionData, MAX_COLLECTION_DESCRIPTION_LENGTH, MAX_TOKEN_PREFIX_LENGTH,24 MAX_COLLECTION_NAME_LENGTH,25};26use frame_support::traits::Get;27use pallet_common::{28 CollectionById,29 erc::{token_uri_key, CollectionHelpersEvents},30};31use crate::{SelfWeightOf, Config, weights::WeightInfo};3233use sp_std::vec::Vec;34use alloc::format;3536struct EvmCollectionHelpers<T: Config>(SubstrateRecorder<T>);37impl<T: Config> WithRecorder<T> for EvmCollectionHelpers<T> {38 fn recorder(&self) -> &SubstrateRecorder<T> {39 &self.040 }4142 fn into_recorder(self) -> SubstrateRecorder<T> {43 self.044 }45}4647#[solidity_interface(name = "CollectionHelpers", events(CollectionHelpersEvents))]48impl<T: Config + pallet_nonfungible::Config> EvmCollectionHelpers<T> {49 #[weight(<SelfWeightOf<T>>::create_collection())]50 fn create_nonfungible_collection(51 &self,52 caller: caller,53 name: string,54 description: string,55 token_prefix: string,56 ) -> Result<address> {57 let caller = T::CrossAccountId::from_eth(caller);58 let name = name59 .encode_utf16()60 .collect::<Vec<u16>>()61 .try_into()62 .map_err(|_| error_feild_too_long(stringify!(name), MAX_COLLECTION_NAME_LENGTH))?;63 let description = description64 .encode_utf16()65 .collect::<Vec<u16>>()66 .try_into()67 .map_err(|_| {68 error_feild_too_long(stringify!(description), MAX_COLLECTION_DESCRIPTION_LENGTH)69 })?;70 let token_prefix = token_prefix71 .into_bytes()72 .try_into()73 .map_err(|_| error_feild_too_long(stringify!(token_prefix), MAX_TOKEN_PREFIX_LENGTH))?;7475 let key = token_uri_key();76 let permission = up_data_structs::PropertyPermission {77 mutable: true,78 collection_admin: true,79 token_owner: false,80 };81 let mut token_property_permissions =82 up_data_structs::CollectionPropertiesPermissionsVec::default();83 token_property_permissions84 .try_push(up_data_structs::PropertyKeyPermission { key, permission })85 .map_err(|e| Error::Revert(format!("{:?}", e)))?;8687 let data = CreateCollectionData {88 name,89 description,90 token_prefix,91 token_property_permissions,92 ..Default::default()93 };9495 let collection_id =96 <pallet_nonfungible::Pallet<T>>::init_collection(caller.clone(), data, false)97 .map_err(pallet_evm_coder_substrate::dispatch_to_evm::<T>)?;9899 let address = pallet_common::eth::collection_id_to_address(collection_id);100 Ok(address)101 }102103 fn is_collection_exist(&self, _caller: caller, collection_address: address) -> Result<bool> {104 if let Some(id) = pallet_common::eth::map_eth_to_id(&collection_address) {105 let collection_id = id;106 return Ok(<CollectionById<T>>::contains_key(collection_id));107 }108109 Ok(false)110 }111}112113pub struct CollectionHelpersOnMethodCall<T: Config>(PhantomData<*const T>);114impl<T: Config + pallet_nonfungible::Config> OnMethodCall<T> for CollectionHelpersOnMethodCall<T> {115 fn is_reserved(contract: &sp_core::H160) -> bool {116 contract == &T::ContractAddress::get()117 }118119 fn is_used(contract: &sp_core::H160) -> bool {120 contract == &T::ContractAddress::get()121 }122123 fn call(handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {124 if handle.code_address() != T::ContractAddress::get() {125 return None;126 }127128 let helpers =129 EvmCollectionHelpers::<T>(SubstrateRecorder::<T>::new(handle.remaining_gas()));130 pallet_evm_coder_substrate::call(handle, helpers)131 }132133 fn get_code(contract: &sp_core::H160) -> Option<Vec<u8>> {134 (contract == &T::ContractAddress::get())135 .then(|| include_bytes!("./stubs/CollectionHelpers.raw").to_vec())136 }137}138139generate_stubgen!(collection_helper_impl, CollectionHelpersCall<()>, true);140generate_stubgen!(collection_helper_iface, CollectionHelpersCall<()>, false);141142fn error_feild_too_long(feild: &str, bound: u32) -> Error {143 Error::Revert(format!("{} is too long. Max length is {}.", feild, bound))144}