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

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::{Property, SponsoringRateLimit, OwnerRestrictedSet, AccessMode, CollectionPermissions};26use alloc::format;2728use crate::{Pallet, CollectionHandle, Config, CollectionProperties};2930#[derive(ToLog)]31pub enum CollectionHelpersEvents {32	CollectionCreated {33		#[indexed]34		owner: address,35		#[indexed]36		collection_id: address,37	},38}3940/// Does not always represent a full collection, for RFT it is either41/// collection (Implementing ERC721), or specific collection token (Implementing ERC20)42pub trait CommonEvmHandler {43	const CODE: &'static [u8];4445	fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult>;46}4748#[solidity_interface(name = "Collection")]49impl<T: Config> CollectionHandle<T>50where51	T::AccountId: From<[u8; 32]>,52{53	fn set_collection_property(54		&mut self,55		caller: caller,56		key: string,57		value: bytes,58	) -> Result<void> {59		let caller = T::CrossAccountId::from_eth(caller);60		let key = <Vec<u8>>::from(key)61			.try_into()62			.map_err(|_| "key too large")?;63		let value = value.try_into().map_err(|_| "value too large")?;6465		<Pallet<T>>::set_collection_property(self, &caller, Property { key, value })66			.map_err(dispatch_to_evm::<T>)67	}6869	fn delete_collection_property(&mut self, caller: caller, key: string) -> Result<()> {70		let caller = T::CrossAccountId::from_eth(caller);71		let key = <Vec<u8>>::from(key)72			.try_into()73			.map_err(|_| "key too large")?;7475		<Pallet<T>>::delete_collection_property(self, &caller, key).map_err(dispatch_to_evm::<T>)76	}7778	/// Throws error if key not found79	fn collection_property(&self, key: string) -> Result<bytes> {80		let key = <Vec<u8>>::from(key)81			.try_into()82			.map_err(|_| "key too large")?;8384		let props = <CollectionProperties<T>>::get(self.id);85		let prop = props.get(&key).ok_or("key not found")?;8687		Ok(prop.to_vec())88	}8990	fn set_collection_sponsor(&mut self, caller: caller, sponsor: address) -> Result<void> {91		check_is_owner_or_admin(caller, self)?;9293		let sponsor = T::CrossAccountId::from_eth(sponsor);94		self.set_sponsor(sponsor.as_sub().clone())95			.map_err(dispatch_to_evm::<T>)?;96		save(self)97	}9899	fn confirm_collection_sponsorship(&mut self, caller: caller) -> Result<void> {100		let caller = T::CrossAccountId::from_eth(caller);101		if !self102			.confirm_sponsorship(caller.as_sub())103			.map_err(dispatch_to_evm::<T>)?104		{105			return Err("caller is not set as sponsor".into());106		}107		save(self)108	}109110	#[solidity(rename_selector = "setCollectionLimit")]111	fn set_int_limit(&mut self, caller: caller, limit: string, value: uint32) -> Result<void> {112		check_is_owner_or_admin(caller, self)?;113		let mut limits = self.limits.clone();114115		match limit.as_str() {116			"accountTokenOwnershipLimit" => {117				limits.account_token_ownership_limit = Some(value);118			}119			"sponsoredDataSize" => {120				limits.sponsored_data_size = Some(value);121			}122			"sponsoredDataRateLimit" => {123				limits.sponsored_data_rate_limit = Some(SponsoringRateLimit::Blocks(value));124			}125			"tokenLimit" => {126				limits.token_limit = Some(value);127			}128			"sponsorTransferTimeout" => {129				limits.sponsor_transfer_timeout = Some(value);130			}131			"sponsorApproveTimeout" => {132				limits.sponsor_approve_timeout = Some(value);133			}134			_ => {135				return Err(Error::Revert(format!(136					"unknown integer limit \"{}\"",137					limit138				)))139			}140		}141		self.limits = <Pallet<T>>::clamp_limits(self.mode.clone(), &self.limits, limits)142			.map_err(dispatch_to_evm::<T>)?;143		save(self)144	}145146	#[solidity(rename_selector = "setCollectionLimit")]147	fn set_bool_limit(&mut self, caller: caller, limit: string, value: bool) -> Result<void> {148		check_is_owner_or_admin(caller, self)?;149		let mut limits = self.limits.clone();150151		match limit.as_str() {152			"ownerCanTransfer" => {153				limits.owner_can_transfer = Some(value);154			}155			"ownerCanDestroy" => {156				limits.owner_can_destroy = Some(value);157			}158			"transfersEnabled" => {159				limits.transfers_enabled = Some(value);160			}161			_ => {162				return Err(Error::Revert(format!(163					"unknown boolean limit \"{}\"",164					limit165				)))166			}167		}168		self.limits = <Pallet<T>>::clamp_limits(self.mode.clone(), &self.limits, limits)169			.map_err(dispatch_to_evm::<T>)?;170		save(self)171	}172173	fn contract_address(&self, _caller: caller) -> Result<address> {174		Ok(crate::eth::collection_id_to_address(self.id))175	}176177	fn add_collection_admin_substrate(&self, caller: caller, new_admin: uint256) -> Result<void> {178		let caller = T::CrossAccountId::from_eth(caller);179		let mut new_admin_arr: [u8; 32] = Default::default();180		new_admin.to_big_endian(&mut new_admin_arr);181		let account_id = T::AccountId::from(new_admin_arr);182		let new_admin = T::CrossAccountId::from_sub(account_id);183		<Pallet<T>>::toggle_admin(self, &caller, &new_admin, true).map_err(dispatch_to_evm::<T>)?;184		Ok(())185	}186187	fn remove_collection_admin_substrate(188		&self,189		caller: caller,190		new_admin: uint256,191	) -> Result<void> {192		let caller = T::CrossAccountId::from_eth(caller);193		let mut new_admin_arr: [u8; 32] = Default::default();194		new_admin.to_big_endian(&mut new_admin_arr);195		let account_id = T::AccountId::from(new_admin_arr);196		let new_admin = T::CrossAccountId::from_sub(account_id);197		<Pallet<T>>::toggle_admin(self, &caller, &new_admin, false)198			.map_err(dispatch_to_evm::<T>)?;199		Ok(())200	}201202	fn add_collection_admin(&self, caller: caller, new_admin: address) -> Result<void> {203		let caller = T::CrossAccountId::from_eth(caller);204		let new_admin = T::CrossAccountId::from_eth(new_admin);205		<Pallet<T>>::toggle_admin(self, &caller, &new_admin, true).map_err(dispatch_to_evm::<T>)?;206		Ok(())207	}208209	fn remove_collection_admin(&self, caller: caller, admin: address) -> Result<void> {210		let caller = T::CrossAccountId::from_eth(caller);211		let admin = T::CrossAccountId::from_eth(admin);212		<Pallet<T>>::toggle_admin(self, &caller, &admin, false).map_err(dispatch_to_evm::<T>)?;213		Ok(())214	}215216	#[solidity(rename_selector = "setCollectionNesting")]217	fn set_nesting_bool(&mut self, caller: caller, enable: bool) -> Result<void> {218		check_is_owner_or_admin(caller, self)?;219220		let mut permissions = self.collection.permissions.clone();221		let mut nesting = permissions.nesting().clone();222		nesting.token_owner = enable;223		nesting.restricted = None;224		permissions.nesting = Some(nesting);225226		self.collection.permissions = <Pallet<T>>::clamp_permissions(227			self.collection.mode.clone(),228			&self.collection.permissions,229			permissions,230		)231		.map_err(dispatch_to_evm::<T>)?;232233		save(self)234	}235236	#[solidity(rename_selector = "setCollectionNesting")]237	fn set_nesting(238		&mut self,239		caller: caller,240		enable: bool,241		collections: Vec<address>,242	) -> Result<void> {243		if collections.is_empty() {244			return Err("no addresses provided".into());245		}246		check_is_owner_or_admin(caller, self)?;247248		let mut permissions = self.collection.permissions.clone();249		match enable {250			false => {251				let mut nesting = permissions.nesting().clone();252				nesting.token_owner = false;253				nesting.restricted = None;254				permissions.nesting = Some(nesting);255			}256			true => {257				let mut bv = OwnerRestrictedSet::new();258				for i in collections {259					bv.try_insert(crate::eth::map_eth_to_id(&i).ok_or(Error::Revert(260						"Can't convert address into collection id".into(),261					))?)262					.map_err(|_| "too many collections")?;263				}264				let mut nesting = permissions.nesting().clone();265				nesting.token_owner = true;266				nesting.restricted = Some(bv);267				permissions.nesting = Some(nesting);268			}269		};270271		self.collection.permissions = <Pallet<T>>::clamp_permissions(272			self.collection.mode.clone(),273			&self.collection.permissions,274			permissions,275		)276		.map_err(dispatch_to_evm::<T>)?;277278		save(self)279	}280281	fn set_collection_access(&mut self, caller: caller, mode: uint8) -> Result<void> {282		check_is_owner_or_admin(caller, self)?;283		let permissions = CollectionPermissions {284			access: Some(match mode {285				0 => AccessMode::Normal,286				1 => AccessMode::AllowList,287				_ => return Err("not supported access mode".into()),288			}),289			..Default::default()290		};291		self.collection.permissions = <Pallet<T>>::clamp_permissions(292			self.collection.mode.clone(),293			&self.collection.permissions,294			permissions,295		)296		.map_err(dispatch_to_evm::<T>)?;297298		save(self)299	}300301	fn add_to_collection_allow_list(&self, caller: caller, user: address) -> Result<void> {302		let caller = T::CrossAccountId::from_eth(caller);303		let user = T::CrossAccountId::from_eth(user);304		<Pallet<T>>::toggle_allowlist(self, &caller, &user, true).map_err(dispatch_to_evm::<T>)?;305		Ok(())306	}307308	fn remove_from_collection_allow_list(&self, caller: caller, user: address) -> Result<void> {309		let caller = T::CrossAccountId::from_eth(caller);310		let user = T::CrossAccountId::from_eth(user);311		<Pallet<T>>::toggle_allowlist(self, &caller, &user, false).map_err(dispatch_to_evm::<T>)?;312		Ok(())313	}314315	fn set_collection_mint_mode(&mut self, caller: caller, mode: bool) -> Result<void> {316		check_is_owner_or_admin(caller, self)?;317		let permissions = CollectionPermissions {318			mint_mode: Some(mode),319			..Default::default()320		};321		self.collection.permissions = <Pallet<T>>::clamp_permissions(322			self.collection.mode.clone(),323			&self.collection.permissions,324			permissions,325		)326		.map_err(dispatch_to_evm::<T>)?;327328		save(self)329	}330}331332fn check_is_owner_or_admin<T: Config>(333	caller: caller,334	collection: &CollectionHandle<T>,335) -> Result<T::CrossAccountId> {336	let caller = T::CrossAccountId::from_eth(caller);337	collection338		.check_is_owner_or_admin(&caller)339		.map_err(pallet_evm_coder_substrate::dispatch_to_evm::<T>)?;340	Ok(caller)341}342343fn save<T: Config>(collection: &CollectionHandle<T>) -> Result<void> {344	// TODO possibly delete for the lack of transaction345	collection346		.check_is_internal()347		.map_err(dispatch_to_evm::<T>)?;348	<crate::CollectionById<T>>::insert(collection.id, collection.collection.clone());349	Ok(())350}351352pub fn token_uri_key() -> up_data_structs::PropertyKey {353	b"tokenURI"354		.to_vec()355		.try_into()356		.expect("length < limit; qed")357}