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
21};21};
22use evm_coder::{ToLog, execution::*, generate_stubgen, solidity, solidity_interface, types::*, weight};22use evm_coder::{ToLog, execution::*, generate_stubgen, solidity, solidity_interface, types::*, weight};
23use frame_support::BoundedVec;23use frame_support::BoundedVec;
24use up_data_structs::{TokenId, SchemaVersion, PropertyPermission, PropertyKeyPermission, Property};24use up_data_structs::{TokenId, SchemaVersion, PropertyPermission, PropertyKeyPermission, Property, CollectionId, PropertyKey, CollectionPropertiesVec};
25use pallet_evm_coder_substrate::dispatch_to_evm;25use pallet_evm_coder_substrate::dispatch_to_evm;
26use sp_core::{H160, U256};26use sp_core::{H160, U256};
27use sp_std::vec::Vec;27use sp_std::vec::Vec;
28use pallet_common::{28use pallet_common::{
29 erc::{CommonEvmHandler, PrecompileResult, CollectionCall},29 erc::{CommonEvmHandler, PrecompileResult, CollectionCall},
30 CollectionHandle,30 CollectionHandle, CollectionPropertyPermissions,
31};31};
32use pallet_evm::account::CrossAccountId;32use pallet_evm::account::CrossAccountId;
33use pallet_evm_coder_substrate::call;33use pallet_evm_coder_substrate::call;
158 /// Returns token's const_metadata158 /// Returns token's const_metadata
159 #[solidity(rename_selector = "tokenURI")]159 #[solidity(rename_selector = "tokenURI")]
160 fn token_uri(&self, token_id: uint256) -> Result<string> {160 fn token_uri(&self, token_id: uint256) -> Result<string> {
161 let key: string = "tokenURI".into(); //TODO: make static
162 let key: up_data_structs::PropertyKey = key.into_bytes().try_into()
163 .map_err(|_| Error::Revert("".into()))?;
164 let permission = get_permission::<T>(self.id, &key)?;
165 if !permission.collection_admin {
166 return Err("Operation is not allowed".into());
167 }
168
161 self.consume_store_reads(1)?;169 self.consume_store_reads(1)?;
162 let _token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;170 let _token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;
350 token_id: uint256,358 token_id: uint256,
351 token_uri: string,359 token_uri: string,
352 ) -> Result<bool> {360 ) -> Result<bool> {
361 let key: string = "tokenURI".into(); //TODO: make static
362 let key: up_data_structs::PropertyKey = key.into_bytes().try_into()
363 .map_err(|_| Error::Revert("".into()))?;
364 let permission = get_permission::<T>(self.id, &key)?;
365 if !permission.collection_admin {
366 return Err("Operation is not allowed".into());
367 }
368
353 let caller = T::CrossAccountId::from_eth(caller);369 let caller = T::CrossAccountId::from_eth(caller);
354 let to = T::CrossAccountId::from_eth(to);370 let to = T::CrossAccountId::from_eth(to);
365 return Err("item id should be next".into());381 return Err("item id should be next".into());
366 }382 }
367383
384 let mut properties = CollectionPropertiesVec::default();
385 properties.try_push(Property{
386 key,
387 value: token_uri.into_bytes().try_into()
388 .map_err(|_| "token uri is too long")?
368 todo!("token uri");389 }).map_err(|e| Error::Revert(alloc::format!("Can't add property: {:?}", e)))?;
369390
370 <Pallet<T>>::create_item(391 <Pallet<T>>::create_item(
371 self,392 self,
372 &caller,393 &caller,
373 CreateItemData::<T> {394 CreateItemData::<T> {
374 properties: BoundedVec::default(),395 properties,
375 owner: to,396 owner: to,
376 },397 },
377 &budget,398 &budget,
386 }407 }
387}408}
409
410fn get_permission<T: Config>(collection_id: CollectionId, key: &PropertyKey) -> Result<PropertyPermission> {
411 let token_property_permissions = CollectionPropertyPermissions::<T>::try_get(collection_id)
412 .map_err(|_| Error::Revert("No permissions for collection".into()))?;
413 Ok(token_property_permissions.get(key)
414 .map(|p| p.clone())
415 .ok_or_else(|| Error::Revert("No permission for tokenURI".into()))?)
416}
388417
389#[solidity_interface(name = "ERC721UniqueExtensions")]418#[solidity_interface(name = "ERC721UniqueExtensions")]
390impl<T: Config> NonfungibleHandle<T> {419impl<T: Config> NonfungibleHandle<T> {
modifiedpallets/unique/src/eth/mod.rsdiffbeforeafterboth
--- a/pallets/unique/src/eth/mod.rs
+++ b/pallets/unique/src/eth/mod.rs
@@ -78,10 +78,24 @@
 				.try_into()
 				.map_err(|_| error_feild_too_long(stringify!(token_prefix), MAX_TOKEN_PREFIX_LENGTH))?;
 	
+			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 = up_data_structs::PropertyPermission {
+				mutable: true,
+				collection_admin: true,
+				token_owner: false,
+			};
+			let mut token_property_permissions = up_data_structs::CollectionPropertiesPermissionsVec::default();
+			token_property_permissions.try_push(up_data_structs::PropertyKeyPermission{
+				key,
+				permission,
+			}).map_err(|e| Error::Revert(format!("{:?}", e)))?;
+
 			let data = CreateCollectionData {
 				name,
 				description,
 				token_prefix,
+				token_property_permissions,
 				..Default::default()
 			};