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

difftreelog

source

pallets/common/src/erc.rs10.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 evm_coder::{18	solidity_interface, solidity, ToLog,19	types::*,20	execution::{Result, Error},21};22pub use pallet_evm::{PrecompileOutput, PrecompileResult, PrecompileHandle, account::CrossAccountId};23use pallet_evm_coder_substrate::dispatch_to_evm;24use sp_std::vec::Vec;25use up_data_structs::{26	Property, SponsoringRateLimit, OwnerRestrictedSet, AccessMode, CollectionPermissions,27};28use alloc::format;2930use crate::{Pallet, CollectionHandle, Config, CollectionProperties};3132#[derive(ToLog)]33pub enum CollectionHelpersEvents {34	CollectionCreated {35		#[indexed]36		owner: address,37		#[indexed]38		collection_id: address,39	},40}4142/// Does not always represent a full collection, for RFT it is either43/// collection (Implementing ERC721), or specific collection token (Implementing ERC20)44pub trait CommonEvmHandler {45	const CODE: &'static [u8];4647	fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult>;48}4950#[solidity_interface(name = "Collection")]51impl<T: Config> CollectionHandle<T>52where53	T::AccountId: From<[u8; 32]>,54{55	fn set_collection_property(56		&mut self,57		caller: caller,58		key: string,59		value: bytes,60	) -> Result<void> {61		let caller = T::CrossAccountId::from_eth(caller);62		let key = <Vec<u8>>::from(key)63			.try_into()64			.map_err(|_| "key too large")?;65		let value = value.try_into().map_err(|_| "value too large")?;6667		<Pallet<T>>::set_collection_property(self, &caller, Property { key, value })68			.map_err(dispatch_to_evm::<T>)69	}7071	fn delete_collection_property(&mut self, caller: caller, key: string) -> Result<()> {72		let caller = T::CrossAccountId::from_eth(caller);73		let key = <Vec<u8>>::from(key)74			.try_into()75			.map_err(|_| "key too large")?;7677		<Pallet<T>>::delete_collection_property(self, &caller, key).map_err(dispatch_to_evm::<T>)78	}7980	/// Throws error if key not found81	fn collection_property(&self, key: string) -> Result<bytes> {82		let key = <Vec<u8>>::from(key)83			.try_into()84			.map_err(|_| "key too large")?;8586		let props = <CollectionProperties<T>>::get(self.id);87		let prop = props.get(&key).ok_or("key not found")?;8889		Ok(prop.to_vec())90	}9192	fn set_collection_sponsor(&mut self, caller: caller, sponsor: address) -> Result<void> {93		check_is_owner_or_admin(caller, self)?;9495		let sponsor = T::CrossAccountId::from_eth(sponsor);96		self.set_sponsor(sponsor.as_sub().clone())97			.map_err(dispatch_to_evm::<T>)?;98		save(self)99	}100101	fn confirm_collection_sponsorship(&mut self, caller: caller) -> Result<void> {102		let caller = T::CrossAccountId::from_eth(caller);103		if !self104			.confirm_sponsorship(caller.as_sub())105			.map_err(dispatch_to_evm::<T>)?106		{107			return Err("caller is not set as sponsor".into());108		}109		save(self)110	}111112	#[solidity(rename_selector = "setCollectionLimit")]113	fn set_int_limit(&mut self, caller: caller, limit: string, value: uint32) -> Result<void> {114		check_is_owner_or_admin(caller, self)?;115		let mut limits = self.limits.clone();116117		match limit.as_str() {118			"accountTokenOwnershipLimit" => {119				limits.account_token_ownership_limit = Some(value);120			}121			"sponsoredDataSize" => {122				limits.sponsored_data_size = Some(value);123			}124			"sponsoredDataRateLimit" => {125				limits.sponsored_data_rate_limit = Some(SponsoringRateLimit::Blocks(value));126			}127			"tokenLimit" => {128				limits.token_limit = Some(value);129			}130			"sponsorTransferTimeout" => {131				limits.sponsor_transfer_timeout = Some(value);132			}133			"sponsorApproveTimeout" => {134				limits.sponsor_approve_timeout = Some(value);135			}136			_ => {137				return Err(Error::Revert(format!(138					"unknown integer limit \"{}\"",139					limit140				)))141			}142		}143		self.limits = <Pallet<T>>::clamp_limits(self.mode.clone(), &self.limits, limits)144			.map_err(dispatch_to_evm::<T>)?;145		save(self)146	}147148	#[solidity(rename_selector = "setCollectionLimit")]149	fn set_bool_limit(&mut self, caller: caller, limit: string, value: bool) -> Result<void> {150		check_is_owner_or_admin(caller, self)?;151		let mut limits = self.limits.clone();152153		match limit.as_str() {154			"ownerCanTransfer" => {155				limits.owner_can_transfer = Some(value);156			}157			"ownerCanDestroy" => {158				limits.owner_can_destroy = Some(value);159			}160			"transfersEnabled" => {161				limits.transfers_enabled = Some(value);162			}163			_ => {164				return Err(Error::Revert(format!(165					"unknown boolean limit \"{}\"",166					limit167				)))168			}169		}170		self.limits = <Pallet<T>>::clamp_limits(self.mode.clone(), &self.limits, limits)171			.map_err(dispatch_to_evm::<T>)?;172		save(self)173	}174175	fn contract_address(&self, _caller: caller) -> Result<address> {176		Ok(crate::eth::collection_id_to_address(self.id))177	}178179	fn add_collection_admin_substrate(&self, caller: caller, new_admin: uint256) -> Result<void> {180		let caller = T::CrossAccountId::from_eth(caller);181		let mut new_admin_arr: [u8; 32] = Default::default();182		new_admin.to_big_endian(&mut new_admin_arr);183		let account_id = T::AccountId::from(new_admin_arr);184		let new_admin = T::CrossAccountId::from_sub(account_id);185		<Pallet<T>>::toggle_admin(self, &caller, &new_admin, true).map_err(dispatch_to_evm::<T>)?;186		Ok(())187	}188189	fn remove_collection_admin_substrate(190		&self,191		caller: caller,192		new_admin: uint256,193	) -> Result<void> {194		let caller = T::CrossAccountId::from_eth(caller);195		let mut new_admin_arr: [u8; 32] = Default::default();196		new_admin.to_big_endian(&mut new_admin_arr);197		let account_id = T::AccountId::from(new_admin_arr);198		let new_admin = T::CrossAccountId::from_sub(account_id);199		<Pallet<T>>::toggle_admin(self, &caller, &new_admin, false)200			.map_err(dispatch_to_evm::<T>)?;201		Ok(())202	}203204	fn add_collection_admin(&self, caller: caller, new_admin: address) -> Result<void> {205		let caller = T::CrossAccountId::from_eth(caller);206		let new_admin = T::CrossAccountId::from_eth(new_admin);207		<Pallet<T>>::toggle_admin(self, &caller, &new_admin, true).map_err(dispatch_to_evm::<T>)?;208		Ok(())209	}210211	fn remove_collection_admin(&self, caller: caller, admin: address) -> Result<void> {212		let caller = T::CrossAccountId::from_eth(caller);213		let admin = T::CrossAccountId::from_eth(admin);214		<Pallet<T>>::toggle_admin(self, &caller, &admin, false).map_err(dispatch_to_evm::<T>)?;215		Ok(())216	}217218	#[solidity(rename_selector = "setCollectionNesting")]219	fn set_nesting_bool(&mut self, caller: caller, enable: bool) -> Result<void> {220		check_is_owner_or_admin(caller, self)?;221222		let mut permissions = self.collection.permissions.clone();223		let mut nesting = permissions.nesting().clone();224		nesting.token_owner = enable;225		nesting.restricted = None;226		permissions.nesting = Some(nesting);227228		self.collection.permissions = <Pallet<T>>::clamp_permissions(229			self.collection.mode.clone(),230			&self.collection.permissions,231			permissions,232		)233		.map_err(dispatch_to_evm::<T>)?;234235		save(self)236	}237238	#[solidity(rename_selector = "setCollectionNesting")]239	fn set_nesting(240		&mut self,241		caller: caller,242		enable: bool,243		collections: Vec<address>,244	) -> Result<void> {245		if collections.is_empty() {246			return Err("no addresses provided".into());247		}248		check_is_owner_or_admin(caller, self)?;249250		let mut permissions = self.collection.permissions.clone();251		match enable {252			false => {253				let mut nesting = permissions.nesting().clone();254				nesting.token_owner = false;255				nesting.restricted = None;256				permissions.nesting = Some(nesting);257			}258			true => {259				let mut bv = OwnerRestrictedSet::new();260				for i in collections {261					bv.try_insert(crate::eth::map_eth_to_id(&i).ok_or(Error::Revert(262						"Can't convert address into collection id".into(),263					))?)264					.map_err(|_| "too many collections")?;265				}266				let mut nesting = permissions.nesting().clone();267				nesting.token_owner = true;268				nesting.restricted = Some(bv);269				permissions.nesting = Some(nesting);270			}271		};272273		self.collection.permissions = <Pallet<T>>::clamp_permissions(274			self.collection.mode.clone(),275			&self.collection.permissions,276			permissions,277		)278		.map_err(dispatch_to_evm::<T>)?;279280		save(self)281	}282283	fn set_collection_access(&mut self, caller: caller, mode: uint8) -> Result<void> {284		check_is_owner_or_admin(caller, self)?;285		let permissions = CollectionPermissions {286			access: Some(match mode {287				0 => AccessMode::Normal,288				1 => AccessMode::AllowList,289				_ => return Err("not supported access mode".into()),290			}),291			..Default::default()292		};293		self.collection.permissions = <Pallet<T>>::clamp_permissions(294			self.collection.mode.clone(),295			&self.collection.permissions,296			permissions,297		)298		.map_err(dispatch_to_evm::<T>)?;299300		save(self)301	}302303	fn add_to_collection_allow_list(&self, caller: caller, user: address) -> Result<void> {304		let caller = T::CrossAccountId::from_eth(caller);305		let user = T::CrossAccountId::from_eth(user);306		<Pallet<T>>::toggle_allowlist(self, &caller, &user, true).map_err(dispatch_to_evm::<T>)?;307		Ok(())308	}309310	fn remove_from_collection_allow_list(&self, caller: caller, user: address) -> Result<void> {311		let caller = T::CrossAccountId::from_eth(caller);312		let user = T::CrossAccountId::from_eth(user);313		<Pallet<T>>::toggle_allowlist(self, &caller, &user, false).map_err(dispatch_to_evm::<T>)?;314		Ok(())315	}316317	fn set_collection_mint_mode(&mut self, caller: caller, mode: bool) -> Result<void> {318		check_is_owner_or_admin(caller, self)?;319		let permissions = CollectionPermissions {320			mint_mode: Some(mode),321			..Default::default()322		};323		self.collection.permissions = <Pallet<T>>::clamp_permissions(324			self.collection.mode.clone(),325			&self.collection.permissions,326			permissions,327		)328		.map_err(dispatch_to_evm::<T>)?;329330		save(self)331	}332}333334fn check_is_owner_or_admin<T: Config>(335	caller: caller,336	collection: &CollectionHandle<T>,337) -> Result<T::CrossAccountId> {338	let caller = T::CrossAccountId::from_eth(caller);339	collection340		.check_is_owner_or_admin(&caller)341		.map_err(pallet_evm_coder_substrate::dispatch_to_evm::<T>)?;342	Ok(caller)343}344345fn save<T: Config>(collection: &CollectionHandle<T>) -> Result<void> {346	// TODO possibly delete for the lack of transaction347	collection348		.check_is_internal()349		.map_err(dispatch_to_evm::<T>)?;350	<crate::CollectionById<T>>::insert(collection.id, collection.collection.clone());351	Ok(())352}353354pub fn token_uri_key() -> up_data_structs::PropertyKey {355	b"tokenURI"356		.to_vec()357		.try_into()358		.expect("length < limit; qed")359}