git.delta.rocks / unique-network / refs/commits / dcd5f668cefb

difftreelog

source

pallets/unique/src/eth/mod.rs4.7 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617use 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 = <pallet_nonfungible::Pallet<T>>::init_collection(caller.clone(), data)96			.map_err(pallet_evm_coder_substrate::dispatch_to_evm::<T>)?;9798		let address = pallet_common::eth::collection_id_to_address(collection_id);99		Ok(address)100	}101102	fn is_collection_exist(&self, _caller: caller, collection_address: address) -> Result<bool> {103		if let Some(id) = pallet_common::eth::map_eth_to_id(&collection_address) {104			let collection_id = id;105			return Ok(<CollectionById<T>>::contains_key(collection_id));106		}107108		Ok(false)109	}110}111112pub struct CollectionHelpersOnMethodCall<T: Config>(PhantomData<*const T>);113impl<T: Config + pallet_nonfungible::Config> OnMethodCall<T> for CollectionHelpersOnMethodCall<T> {114	fn is_reserved(contract: &sp_core::H160) -> bool {115		contract == &T::ContractAddress::get()116	}117118	fn is_used(contract: &sp_core::H160) -> bool {119		contract == &T::ContractAddress::get()120	}121122	fn call(handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {123		if handle.code_address() != T::ContractAddress::get() {124			return None;125		}126127		let helpers =128			EvmCollectionHelpers::<T>(SubstrateRecorder::<T>::new(handle.remaining_gas()));129		pallet_evm_coder_substrate::call(handle, helpers)130	}131132	fn get_code(contract: &sp_core::H160) -> Option<Vec<u8>> {133		(contract == &T::ContractAddress::get())134			.then(|| include_bytes!("./stubs/CollectionHelpers.raw").to_vec())135	}136}137138generate_stubgen!(collection_helper_impl, CollectionHelpersCall<()>, true);139generate_stubgen!(collection_helper_iface, CollectionHelpersCall<()>, false);140141fn error_feild_too_long(feild: &str, bound: u32) -> Error {142	Error::Revert(format!("{} is too long. Max length is {}.", feild, bound))143}