1234567891011121314151617use core::marker::PhantomData;18use evm_coder::{abi::AbiWriter, execution::*, generate_stubgen, solidity_interface, types::*, ToLog};19use ethereum as _;20use pallet_common::CollectionById;21use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder};22use pallet_evm::{23 ExitRevert, OnCreate, OnMethodCall, PrecompileResult, PrecompileFailure,24 account::CrossAccountId, Pallet as PalletEvm,25};26use sp_core::H160;27use up_data_structs::{28 CreateCollectionData, MAX_COLLECTION_DESCRIPTION_LENGTH, MAX_TOKEN_PREFIX_LENGTH,29 MAX_COLLECTION_NAME_LENGTH,30};31use crate::{Config, Pallet};32use frame_support::traits::Get;3334use sp_std::{vec::Vec, rc::Rc};35use alloc::format;3637struct EvmCollection<T: Config>(SubstrateRecorder<T>);38impl<T: Config> WithRecorder<T> for EvmCollection<T> {39 fn recorder(&self) -> &SubstrateRecorder<T> {40 &self.041 }4243 fn into_recorder(self) -> SubstrateRecorder<T> {44 self.045 }46}4748#[derive(ToLog)]49pub enum CollectionEvent {50 CollectionCreated {51 #[indexed]52 owner: address,53 #[indexed]54 collection_id: address,55 },56}5758#[solidity_interface(name = "Collection")]59impl<T: Config> EvmCollection<T> {60 fn create_721_collection(61 &self,62 caller: caller,63 name: string,64 description: string,65 token_prefix: string,66 ) -> Result<address> {67 let caller = T::CrossAccountId::from_eth(caller);68 let name = name69 .encode_utf16()70 .collect::<Vec<u16>>()71 .try_into()72 .map_err(|_| error_feild_too_long("name", MAX_COLLECTION_NAME_LENGTH))?;73 let description = description74 .encode_utf16()75 .collect::<Vec<u16>>()76 .try_into()77 .map_err(|_| error_feild_too_long("description", MAX_COLLECTION_DESCRIPTION_LENGTH))?;78 let token_prefix = token_prefix79 .into_bytes()80 .try_into()81 .map_err(|_| error_feild_too_long("token_prefix", MAX_TOKEN_PREFIX_LENGTH))?;8283 let data = CreateCollectionData {84 name,85 description,86 token_prefix,87 ..Default::default()88 };8990 let collection_id =91 <pallet_nonfungible::Pallet<T>>::init_collection(caller.as_sub().clone(), data)92 .map_err(pallet_evm_coder_substrate::dispatch_to_evm::<T>)?;9394 let address = pallet_common::eth::collection_id_to_address(collection_id);95 <PalletEvm<T>>::deposit_log(96 CollectionEvent::CollectionCreated {97 owner: *caller.as_eth(),98 collection_id: address,99 }100 .to_log(address),101 );102 Ok(address)103 }104105 106 107 108 109 110 111 112 113 114115 116 117 118119 120 121 122123 124 125 126127 128 129 130}131132fn error_feild_too_long(feild: &str, bound: u32) -> Error {133 Error::Revert(format!("{} is too long. Max length is {}.", feild, bound))134}135136pub struct CollectionOnMethodCall<T: Config>(PhantomData<*const T>);137impl<T: Config> OnMethodCall<T> for CollectionOnMethodCall<T> {138 fn is_reserved(contract: &sp_core::H160) -> bool {139 contract == &T::ContractAddress::get()140 }141142 fn is_used(contract: &sp_core::H160) -> bool {143 contract == &T::ContractAddress::get()144 }145146 fn call(147 source: &sp_core::H160,148 target: &sp_core::H160,149 gas_left: u64,150 input: &[u8],151 value: sp_core::U256,152 ) -> Option<PrecompileResult> {153 154 if target != &T::ContractAddress::get() {155 return None;156 }157158 let helpers = EvmCollection::<T>(SubstrateRecorder::<T>::new(gas_left));159 pallet_evm_coder_substrate::call(*source, helpers, value, input)160 }161162 fn get_code(contract: &sp_core::H160) -> Option<Vec<u8>> {163 (contract == &T::ContractAddress::get())164 .then(|| include_bytes!("./stubs/Collection.raw").to_vec())165 }166}167168generate_stubgen!(collection_impl, CollectionCall<()>, true);169generate_stubgen!(collection_iface, CollectionCall<()>, false);