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};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::{CollectionById, erc::token_uri_key};28use crate::{SelfWeightOf, Config, weights::WeightInfo};2930use sp_std::vec::Vec;31use alloc::format;3233struct EvmCollectionHelper<T: Config>(SubstrateRecorder<T>);34impl<T: Config> WithRecorder<T> for EvmCollectionHelper<T> {35 fn recorder(&self) -> &SubstrateRecorder<T> {36 &self.037 }3839 fn into_recorder(self) -> SubstrateRecorder<T> {40 self.041 }42}4344#[solidity_interface(name = "CollectionHelper")]45impl<T: Config + pallet_nonfungible::Config> EvmCollectionHelper<T> {46 #[weight(<SelfWeightOf<T>>::create_collection())]47 fn create_nonfungible_collection(48 &self,49 caller: caller,50 name: string,51 description: string,52 token_prefix: string,53 ) -> Result<address> {54 let caller = T::CrossAccountId::from_eth(caller);55 let name = name56 .encode_utf16()57 .collect::<Vec<u16>>()58 .try_into()59 .map_err(|_| error_feild_too_long(stringify!(name), MAX_COLLECTION_NAME_LENGTH))?;60 let description = description61 .encode_utf16()62 .collect::<Vec<u16>>()63 .try_into()64 .map_err(|_| {65 error_feild_too_long(stringify!(description), MAX_COLLECTION_DESCRIPTION_LENGTH)66 })?;67 let token_prefix = token_prefix68 .into_bytes()69 .try_into()70 .map_err(|_| error_feild_too_long(stringify!(token_prefix), MAX_TOKEN_PREFIX_LENGTH))?;7172 let key = token_uri_key();73 let permission = up_data_structs::PropertyPermission {74 mutable: true,75 collection_admin: true,76 token_owner: false,77 };78 let mut token_property_permissions =79 up_data_structs::CollectionPropertiesPermissionsVec::default();80 token_property_permissions81 .try_push(up_data_structs::PropertyKeyPermission { key, permission })82 .map_err(|e| Error::Revert(format!("{:?}", e)))?;8384 let data = CreateCollectionData {85 name,86 description,87 token_prefix,88 token_property_permissions,89 ..Default::default()90 };9192 let collection_id =93 <pallet_nonfungible::Pallet<T>>::init_collection(caller.as_sub().clone(), data)94 .map_err(pallet_evm_coder_substrate::dispatch_to_evm::<T>)?;9596 let address = pallet_common::eth::collection_id_to_address(collection_id);97 Ok(address)98 }99100 fn is_collection_exist(&self, _caller: caller, collection_address: address) -> Result<bool> {101 if let Some(id) = pallet_common::eth::map_eth_to_id(&collection_address) {102 let collection_id = id;103 return Ok(<CollectionById<T>>::contains_key(collection_id));104 }105106 Ok(false)107 }108}109110pub struct CollectionHelperOnMethodCall<T: Config>(PhantomData<*const T>);111impl<T: Config + pallet_nonfungible::Config> OnMethodCall<T> for CollectionHelperOnMethodCall<T> {112 fn is_reserved(contract: &sp_core::H160) -> bool {113 contract == &T::ContractAddress::get()114 }115116 fn is_used(contract: &sp_core::H160) -> bool {117 contract == &T::ContractAddress::get()118 }119120 fn call(121 source: &sp_core::H160,122 target: &sp_core::H160,123 gas_left: u64,124 input: &[u8],125 value: sp_core::U256,126 ) -> Option<PrecompileResult> {127 if target != &T::ContractAddress::get() {128 return None;129 }130131 let helpers = EvmCollectionHelper::<T>(SubstrateRecorder::<T>::new(gas_left));132 pallet_evm_coder_substrate::call(*source, helpers, value, input)133 }134135 fn get_code(contract: &sp_core::H160) -> Option<Vec<u8>> {136 (contract == &T::ContractAddress::get())137 .then(|| include_bytes!("./stubs/CollectionHelper.raw").to_vec())138 }139}140141generate_stubgen!(collection_helper_impl, CollectionHelperCall<()>, true);142generate_stubgen!(collection_helper_iface, CollectionHelperCall<()>, false);143144fn error_feild_too_long(feild: &str, bound: u32) -> Error {145 Error::Revert(format!("{} is too long. Max length is {}.", feild, bound))146}