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

difftreelog

source

pallets/common/src/erc.rs5.5 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};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> {50	fn set_collection_property(&mut self, caller: caller, key: string, value: bytes) -> Result<()> {51		let caller = T::CrossAccountId::from_eth(caller);52		let key = <Vec<u8>>::from(key)53			.try_into()54			.map_err(|_| "key too large")?;55		let value = value.try_into().map_err(|_| "value too large")?;5657		<Pallet<T>>::set_collection_property(self, &caller, Property { key, value })58			.map_err(dispatch_to_evm::<T>)59	}6061	fn delete_collection_property(&mut self, caller: caller, key: string) -> Result<()> {62		let caller = T::CrossAccountId::from_eth(caller);63		let key = <Vec<u8>>::from(key)64			.try_into()65			.map_err(|_| "key too large")?;6667		<Pallet<T>>::delete_collection_property(self, &caller, key).map_err(dispatch_to_evm::<T>)68	}6970	/// Throws error if key not found71	fn collection_property(&self, key: string) -> Result<bytes> {72		let key = <Vec<u8>>::from(key)73			.try_into()74			.map_err(|_| "key too large")?;7576		let props = <CollectionProperties<T>>::get(self.id);77		let prop = props.get(&key).ok_or("key not found")?;7879		Ok(prop.to_vec())80	}8182	fn eth_set_sponsor(&mut self, caller: caller, sponsor: address) -> Result<void> {83		check_is_owner(caller, self)?;8485		let sponsor = T::CrossAccountId::from_eth(sponsor);86		self.set_sponsor(sponsor.as_sub().clone());87		save(self);88		Ok(())89	}9091	fn eth_confirm_sponsorship(&mut self, caller: caller) -> Result<void> {92		let caller = T::CrossAccountId::from_eth(caller);93		if !self.confirm_sponsorship(caller.as_sub()) {94			return Err(Error::Revert("Caller is not set as sponsor".into()));95		}96		save(self);97		Ok(())98	}99100	#[solidity(rename_selector = "setLimit")]101	fn set_int_limit(&mut self, caller: caller, limit: string, value: uint32) -> Result<void> {102		check_is_owner(caller, self)?;103		let mut limits = self.limits.clone();104105		match limit.as_str() {106			"accountTokenOwnershipLimit" => {107				limits.account_token_ownership_limit = Some(value);108			}109			"sponsoredDataSize" => {110				limits.sponsored_data_size = Some(value);111			}112			"sponsoredDataRateLimit" => {113				limits.sponsored_data_rate_limit = Some(SponsoringRateLimit::Blocks(value));114			}115			"tokenLimit" => {116				limits.token_limit = Some(value);117			}118			"sponsorTransferTimeout" => {119				limits.sponsor_transfer_timeout = Some(value);120			}121			"sponsorApproveTimeout" => {122				limits.sponsor_approve_timeout = Some(value);123			}124			_ => {125				return Err(Error::Revert(format!(126					"Unknown integer limit \"{}\"",127					limit128				)))129			}130		}131		self.limits = <Pallet<T>>::clamp_limits(self.mode.clone(), &self.limits, limits)132			.map_err(dispatch_to_evm::<T>)?;133		save(self);134		Ok(())135	}136137	#[solidity(rename_selector = "setLimit")]138	fn set_bool_limit(&mut self, caller: caller, limit: string, value: bool) -> Result<void> {139		check_is_owner(caller, self)?;140		let mut limits = self.limits.clone();141142		match limit.as_str() {143			"ownerCanTransfer" => {144				limits.owner_can_transfer = Some(value);145			}146			"ownerCanDestroy" => {147				limits.owner_can_destroy = Some(value);148			}149			"transfersEnabled" => {150				limits.transfers_enabled = Some(value);151			}152			_ => {153				return Err(Error::Revert(format!(154					"Unknown boolean limit \"{}\"",155					limit156				)))157			}158		}159		self.limits = <Pallet<T>>::clamp_limits(self.mode.clone(), &self.limits, limits)160			.map_err(dispatch_to_evm::<T>)?;161		save(self);162		Ok(())163	}164165	fn contract_address(&self, _caller: caller) -> Result<address> {166		Ok(crate::eth::collection_id_to_address(self.id))167	}168}169170fn check_is_owner<T: Config>(caller: caller, collection: &CollectionHandle<T>) -> Result<()> {171	let caller = T::CrossAccountId::from_eth(caller);172	collection173		.check_is_owner(&caller)174		.map_err(pallet_evm_coder_substrate::dispatch_to_evm::<T>)?;175	Ok(())176}177178fn save<T: Config>(collection: &CollectionHandle<T>) {179	<crate::CollectionById<T>>::insert(collection.id, collection.collection.clone());180}181182pub fn token_uri_key() -> up_data_structs::PropertyKey {183	b"tokenURI"184		.to_vec()185		.try_into()186		.expect("length < limit; qed")187}