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

difftreelog

CORE-346 Rewrite tokenURL as property

Trubnikov Sergey2022-05-26parent: #7ed3eb8.patch.diff
in: master

2 files changed

modifiedpallets/nonfungible/src/erc.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/erc.rs
+++ b/pallets/nonfungible/src/erc.rs
@@ -21,13 +21,13 @@
 };
 use evm_coder::{ToLog, execution::*, generate_stubgen, solidity, solidity_interface, types::*, weight};
 use frame_support::BoundedVec;
-use up_data_structs::{TokenId, SchemaVersion, PropertyPermission, PropertyKeyPermission, Property};
+use up_data_structs::{TokenId, SchemaVersion, PropertyPermission, PropertyKeyPermission, Property, CollectionId, PropertyKey, CollectionPropertiesVec};
 use pallet_evm_coder_substrate::dispatch_to_evm;
 use sp_core::{H160, U256};
 use sp_std::vec::Vec;
 use pallet_common::{
 	erc::{CommonEvmHandler, PrecompileResult, CollectionCall},
-	CollectionHandle,
+	CollectionHandle, CollectionPropertyPermissions,
 };
 use pallet_evm::account::CrossAccountId;
 use pallet_evm_coder_substrate::call;
@@ -158,6 +158,14 @@
 	/// 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 permission = get_permission::<T>(self.id, &key)?;
+		if !permission.collection_admin {
+			return Err("Operation is not allowed".into());
+		}
+
 		self.consume_store_reads(1)?;
 		let _token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;
 		Ok(string::from_utf8_lossy(
@@ -350,6 +358,14 @@
 		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 permission = get_permission::<T>(self.id, &key)?;
+		if !permission.collection_admin {
+			return Err("Operation is not allowed".into());
+		}
+
 		let caller = T::CrossAccountId::from_eth(caller);
 		let to = T::CrossAccountId::from_eth(to);
 		let token_id: u32 = token_id.try_into().map_err(|_| "amount overflow")?;
@@ -365,13 +381,18 @@
 			return Err("item id should be next".into());
 		}
 
-		todo!("token uri");
+		let mut properties = CollectionPropertiesVec::default();
+		properties.try_push(Property{
+			key,
+			value: token_uri.into_bytes().try_into()
+				.map_err(|_| "token uri is too long")?
+		}).map_err(|e| Error::Revert(alloc::format!("Can't add property: {:?}", e)))?;
 
 		<Pallet<T>>::create_item(
 			self,
 			&caller,
 			CreateItemData::<T> {
-				properties: BoundedVec::default(),
+				properties,
 				owner: to,
 			},
 			&budget,
@@ -386,6 +407,14 @@
 	}
 }
 
+fn get_permission<T: Config>(collection_id: CollectionId, key: &PropertyKey) -> Result<PropertyPermission> {
+	let token_property_permissions = CollectionPropertyPermissions::<T>::try_get(collection_id)
+		.map_err(|_| Error::Revert("No permissions for collection".into()))?;
+	Ok(token_property_permissions.get(key)
+		.map(|p| p.clone())
+		.ok_or_else(|| Error::Revert("No permission for tokenURI".into()))?)
+}
+
 #[solidity_interface(name = "ERC721UniqueExtensions")]
 impl<T: Config> NonfungibleHandle<T> {
 	#[weight(<SelfWeightOf<T>>::transfer())]
modifiedpallets/unique/src/eth/mod.rsdiffbeforeafterboth
78 .try_into()78 .try_into()
79 .map_err(|_| error_feild_too_long(stringify!(token_prefix), MAX_TOKEN_PREFIX_LENGTH))?;79 .map_err(|_| error_feild_too_long(stringify!(token_prefix), MAX_TOKEN_PREFIX_LENGTH))?;
80 80
81 let key: string = "tokenURI".into(); //TODO: make static
82 let key: up_data_structs::PropertyKey = key.into_bytes().try_into().map_err(|_| Error::Revert("".into()))?;
83 let permission = up_data_structs::PropertyPermission {
84 mutable: true,
85 collection_admin: true,
86 token_owner: false,
87 };
88 let mut token_property_permissions = up_data_structs::CollectionPropertiesPermissionsVec::default();
89 token_property_permissions.try_push(up_data_structs::PropertyKeyPermission{
90 key,
91 permission,
92 }).map_err(|e| Error::Revert(format!("{:?}", e)))?;
93
81 let data = CreateCollectionData {94 let data = CreateCollectionData {
82 name,95 name,
83 description,96 description,
84 token_prefix,97 token_prefix,
98 token_property_permissions,
85 ..Default::default()99 ..Default::default()
86 };100 };
87 101