git.delta.rocks / unique-network / refs/commits / 7d68ab6c6ac0

difftreelog

CORE-346 Add static tokenURI key

Trubnikov Sergey2022-05-27parent: #ffa590c.patch.diff
in: master

5 files changed

modifiedCargo.lockdiffbeforeafterboth
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -4290,6 +4290,9 @@
 version = "1.4.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
+dependencies = [
+ "spin",
+]
 
 [[package]]
 name = "lazycell"
@@ -5917,6 +5920,7 @@
  "frame-benchmarking",
  "frame-support",
  "frame-system",
+ "lazy_static",
  "pallet-evm",
  "pallet-evm-coder-substrate",
  "parity-scale-codec 3.1.2",
modifiedpallets/common/Cargo.tomldiffbeforeafterboth
--- a/pallets/common/Cargo.toml
+++ b/pallets/common/Cargo.toml
@@ -16,16 +16,18 @@
 sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.21" }
 sp-std = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.21" }
 sp-core = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.21" }
+frame-benchmarking = { default-features = false, optional = true, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.21" }
 fp-evm-mapping = { default-features = false, git = "https://github.com/uniquenetwork/frontier", branch = "unique-polkadot-v0.9.21-logs" }
 up-data-structs = { default-features = false, path = '../../primitives/data-structs' }
 pallet-evm-coder-substrate = { default-features = false, path = '../../pallets/evm-coder-substrate' }
 evm-coder = { default-features = false, path = '../../crates/evm-coder' }
 pallet-evm = { default-features = false, git = "https://github.com/uniquenetwork/frontier", branch = "unique-polkadot-v0.9.21-logs" }
+
 serde = { version = "1.0.130", default-features = false }
 scale-info = { version = "2.0.1", default-features = false, features = [
     "derive",
 ] }
-frame-benchmarking = { default-features = false, optional = true, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.21" }
+lazy_static = { version = "1.4.0", default-features = false, features = ["spin_no_std"] }
 
 [features]
 default = ["std"]
modifiedpallets/common/src/eth.rsdiffbeforeafterboth
--- a/pallets/common/src/eth.rs
+++ b/pallets/common/src/eth.rs
@@ -17,6 +17,14 @@
 use up_data_structs::CollectionId;
 use sp_core::H160;
 
+lazy_static::lazy_static! {
+	pub static ref KEY_TOKEN_URI: up_data_structs::PropertyKey = {
+		let key: evm_coder::types::string = "tokenURI".into(); //TODO: make static
+		let key: up_data_structs::PropertyKey = key.into_bytes().try_into().expect("Can't create \"tokenURI\" key");
+		key
+	};
+}
+
 // 0x17c4e6453Cc49AAAaEACA894e6D9683e00000001 - collection 1
 // TODO: Unhardcode prefix
 const ETH_COLLECTION_PREFIX: [u8; 16] = [
modifiedpallets/nonfungible/src/erc.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/erc.rs
+++ b/pallets/nonfungible/src/erc.rs
@@ -158,9 +158,7 @@
 	/// Returns token's const_metadata
 	#[solidity(rename_selector = "tokenURI")]
 	fn token_uri(&self, token_id: uint256) -> Result<string> {
-		let key: string = "tokenURI".into(); //TODO: make static
-		let key: up_data_structs::PropertyKey = key.into_bytes().try_into()
-			.map_err(|_| Error::Revert("".into()))?;
+		let key = pallet_common::eth::KEY_TOKEN_URI.clone();
 		let permission = get_token_permission::<T>(self.id, &key)?;
 		if !permission.collection_admin {
 			return Err("Operation is not allowed".into());
@@ -362,9 +360,7 @@
 		token_id: uint256,
 		token_uri: string,
 	) -> Result<bool> {
-		let key: string = "tokenURI".into(); //TODO: make static
-		let key: up_data_structs::PropertyKey = key.into_bytes().try_into()
-			.map_err(|_| Error::Revert("".into()))?;
+		let key = pallet_common::eth::KEY_TOKEN_URI.clone();
 		let permission = get_token_permission::<T>(self.id, &key)?;
 		if !permission.collection_admin {
 			return Err("Operation is not allowed".into());
modifiedpallets/unique/src/eth/mod.rsdiffbeforeafterboth
before · pallets/unique/src/eth/mod.rs
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, types::*, ToLog};19use ethereum as _;20use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder};21use pallet_evm::{OnMethodCall, PrecompileResult, account::CrossAccountId, Pallet as PalletEvm};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 sp_core::H160;28use pallet_common::CollectionById;2930use sp_std::vec::Vec;31use alloc::format;3233pub trait Config:34	frame_system::Config35	+ pallet_evm_coder_substrate::Config36	+ pallet_evm::account::Config37	+ pallet_nonfungible::Config38{39	type ContractAddress: Get<H160>;40}4142struct EvmCollectionHelper<T: Config>(SubstrateRecorder<T>);43impl<T: Config> WithRecorder<T> for EvmCollectionHelper<T> {44	fn recorder(&self) -> &SubstrateRecorder<T> {45		&self.046	}4748	fn into_recorder(self) -> SubstrateRecorder<T> {49		self.050	}51}5253#[solidity_interface(name = "CollectionHelper")]54impl<T: Config> EvmCollectionHelper<T> {55	fn create_721_collection(56		&self,57		caller: caller,58		name: string,59		description: string,60		token_prefix: string,61	) -> Result<address> {62		let caller = T::CrossAccountId::from_eth(caller);63		let name = name64			.encode_utf16()65			.collect::<Vec<u16>>()66			.try_into()67			.map_err(|_| error_feild_too_long(stringify!(name), MAX_COLLECTION_NAME_LENGTH))?;68		let description = description69			.encode_utf16()70			.collect::<Vec<u16>>()71			.try_into()72			.map_err(|_| {73				error_feild_too_long(stringify!(description), MAX_COLLECTION_DESCRIPTION_LENGTH)74			})?;75		let token_prefix = token_prefix76			.into_bytes()77			.try_into()78			.map_err(|_| error_feild_too_long(stringify!(token_prefix), MAX_TOKEN_PREFIX_LENGTH))?;7980		let key: string = "tokenURI".into(); //TODO: make static81		let key: up_data_structs::PropertyKey = key.into_bytes().try_into().map_err(|_| Error::Revert("".into()))?;82		let permission = up_data_structs::PropertyPermission {83			mutable: true,84			collection_admin: true,85			token_owner: false,86		};87		let mut token_property_permissions = up_data_structs::CollectionPropertiesPermissionsVec::default();88		token_property_permissions.try_push(up_data_structs::PropertyKeyPermission{89			key,90			permission,91		}).map_err(|e| Error::Revert(format!("{:?}", e)))?;9293		let data = CreateCollectionData {94			name,95			description,96			token_prefix,97			token_property_permissions,98			..Default::default()99		};100101		let collection_id =102			<pallet_nonfungible::Pallet<T>>::init_collection(caller.as_sub().clone(), data)103				.map_err(pallet_evm_coder_substrate::dispatch_to_evm::<T>)?;104105		let address = pallet_common::eth::collection_id_to_address(collection_id);106		<PalletEvm<T>>::deposit_log(107			EthCollectionEvent::CollectionCreated {108				owner: *caller.as_eth(),109				collection_id: address,110			}111			.to_log(address),112		);113		Ok(address)114	}115116	fn is_collection_exist(&self, _caller: caller, collection_address: address) -> Result<bool> {117		if let Some(id) = pallet_common::eth::map_eth_to_id(&collection_address) {118			let collection_id = id;119			return Ok(<CollectionById<T>>::contains_key(collection_id));120		}121122		Ok(false)123	}124}125126#[derive(ToLog)]127pub enum EthCollectionEvent {128	CollectionCreated {129		#[indexed]130		owner: address,131		#[indexed]132		collection_id: address,133	},134}135136pub struct CollectionHelperOnMethodCall<T: Config>(PhantomData<*const T>);137impl<T: Config> OnMethodCall<T> for CollectionHelperOnMethodCall<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		if target != &T::ContractAddress::get() {154			return None;155		}156157		let helpers = EvmCollectionHelper::<T>(SubstrateRecorder::<T>::new(gas_left));158		pallet_evm_coder_substrate::call(*source, helpers, value, input)159	}160161	fn get_code(contract: &sp_core::H160) -> Option<Vec<u8>> {162		(contract == &T::ContractAddress::get())163			.then(|| include_bytes!("./stubs/CollectionHelper.raw").to_vec())164	}165}166167generate_stubgen!(collection_helper_impl, CollectionHelperCall<()>, true);168generate_stubgen!(collection_helper_iface, CollectionHelperCall<()>, false);169170fn error_feild_too_long(feild: &str, bound: u32) -> Error {171	Error::Revert(format!("{} is too long. Max length is {}.", feild, bound))172}