difftreelog
CORE-441 Add some properties atcollection creation
in: master
4 files changed
pallets/common/src/erc.rsdiffbeforeafterboth--- a/pallets/common/src/erc.rs
+++ b/pallets/common/src/erc.rs
@@ -430,10 +430,53 @@
Ok(())
}
-/// Get the "tokenURI" key as [PropertyKey](up_data_structs::PropertyKey).
-pub fn token_uri_key() -> up_data_structs::PropertyKey {
- b"tokenURI"
- .to_vec()
- .try_into()
- .expect("length < limit; qed")
+pub mod static_property_key_value {
+ use evm_coder::{
+ execution::{Result, Error},
+ };
+ use alloc::format;
+
+ const EXPECT_CONVERT_ERROR: &str = "length < limit";
+ /// Get the "tokenURI" key as [PropertyKey](up_data_structs::PropertyKey).
+ pub fn token_uri_key() -> up_data_structs::PropertyKey {
+ property_key_from_bytes(b"tokenURI").expect(EXPECT_CONVERT_ERROR)
+ }
+
+ pub fn schema_name_key() -> up_data_structs::PropertyKey {
+ property_key_from_bytes(b"schemaName").expect(EXPECT_CONVERT_ERROR)
+ }
+
+ pub fn base_uri_key() -> up_data_structs::PropertyKey {
+ property_key_from_bytes(b"baseURI").expect(EXPECT_CONVERT_ERROR)
+ }
+
+ pub fn u_key() -> up_data_structs::PropertyKey {
+ property_key_from_bytes(b"u").expect(EXPECT_CONVERT_ERROR)
+ }
+
+ pub fn s_key() -> up_data_structs::PropertyKey {
+ property_key_from_bytes(b"s").expect(EXPECT_CONVERT_ERROR)
+ }
+
+ pub fn erc721_value() -> up_data_structs::PropertyValue {
+ property_value_from_bytes(b"ERC721").expect(EXPECT_CONVERT_ERROR)
+ }
+
+ pub fn property_key_from_bytes(bytes: &[u8]) -> Result<up_data_structs::PropertyKey> {
+ bytes.to_vec().try_into().map_err(|_| {
+ Error::Revert(format!(
+ "Property key is too long. Max length is {}.",
+ up_data_structs::PropertyKey::bound()
+ ))
+ })
+ }
+
+ pub fn property_value_from_bytes(bytes: &[u8]) -> Result<up_data_structs::PropertyValue> {
+ bytes.to_vec().try_into().map_err(|_| {
+ Error::Revert(format!(
+ "Property key is too long. Max length is {}.",
+ up_data_structs::PropertyKey::bound()
+ ))
+ })
+ }
}
pallets/nonfungible/src/erc.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/erc.rs
+++ b/pallets/nonfungible/src/erc.rs
@@ -33,7 +33,7 @@
use pallet_evm_coder_substrate::dispatch_to_evm;
use sp_std::vec::Vec;
use pallet_common::{
- erc::{CommonEvmHandler, PrecompileResult, CollectionCall, token_uri_key},
+ erc::{CommonEvmHandler, PrecompileResult, CollectionCall, static_property_key_value::*},
CollectionHandle, CollectionPropertyPermissions,
};
use pallet_evm::{account::CrossAccountId, PrecompileHandle};
pallets/unique/src/eth/mod.rsdiffbeforeafterboth17//! Implementation of CollectionHelpers contract.17//! Implementation of CollectionHelpers contract.181819use core::marker::PhantomData;19use core::marker::PhantomData;20use evm_coder::{execution::*, generate_stubgen, solidity_interface, weight, types::*};20use evm_coder::{execution::*, generate_stubgen, solidity_interface, solidity, weight, types::*};21use ethereum as _;21use ethereum as _;22use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder};22use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder};23use pallet_evm::{OnMethodCall, PrecompileResult, account::CrossAccountId, PrecompileHandle};23use pallet_evm::{OnMethodCall, PrecompileResult, account::CrossAccountId, PrecompileHandle};24use up_data_structs::{24use up_data_structs::{25 CreateCollectionData, MAX_COLLECTION_DESCRIPTION_LENGTH, MAX_TOKEN_PREFIX_LENGTH,25 CollectionName, CollectionDescription, CollectionTokenPrefix, CreateCollectionData,26 MAX_COLLECTION_NAME_LENGTH,26 CollectionMode, PropertyValue,27};27};28use frame_support::traits::Get;28use frame_support::traits::Get;29use pallet_common::{29use pallet_common::{30 CollectionById,30 CollectionById,31 erc::{token_uri_key, CollectionHelpersEvents},31 erc::{static_property_key_value::*, CollectionHelpersEvents},32};32};33use crate::{SelfWeightOf, Config, weights::WeightInfo};33use crate::{SelfWeightOf, Config, weights::WeightInfo};343447 }47 }48}48}4950fn convert_data<T: Config>(51 caller: caller,52 name: string,53 description: string,54 token_prefix: string,55 base_uri: string,56) -> Result<(57 T::CrossAccountId,58 CollectionName,59 CollectionDescription,60 CollectionTokenPrefix,61 PropertyValue,62)> {63 let caller = T::CrossAccountId::from_eth(caller);64 let name = name65 .encode_utf16()66 .collect::<Vec<u16>>()67 .try_into()68 .map_err(|_| error_feild_too_long(stringify!(name), CollectionName::bound()))?;69 let description = description70 .encode_utf16()71 .collect::<Vec<u16>>()72 .try_into()73 .map_err(|_| {74 error_feild_too_long(stringify!(description), CollectionDescription::bound())75 })?;76 let token_prefix = token_prefix.into_bytes().try_into().map_err(|_| {77 error_feild_too_long(stringify!(token_prefix), CollectionTokenPrefix::bound())78 })?;79 let base_uri_value = base_uri80 .into_bytes()81 .try_into()82 .map_err(|_| error_feild_too_long(stringify!(token_prefix), PropertyValue::bound()))?;83 Ok((caller, name, description, token_prefix, base_uri_value))84}8586fn make_data<T: Config>(87 name: CollectionName,88 mode: CollectionMode,89 description: CollectionDescription,90 token_prefix: CollectionTokenPrefix,91 base_uri_value: PropertyValue,92 add_properties: bool,93) -> Result<CreateCollectionData<T::AccountId>> {94 let mut collection_properties = up_data_structs::CollectionPropertiesVec::default();95 let mut token_property_permissions =96 up_data_structs::CollectionPropertiesPermissionsVec::default();9798 if add_properties {99 token_property_permissions100 .try_push(up_data_structs::PropertyKeyPermission {101 key: token_uri_key(),102 permission: up_data_structs::PropertyPermission {103 mutable: true,104 collection_admin: true,105 token_owner: false,106 },107 })108 .map_err(|e| Error::Revert(format!("{:?}", e)))?;109110 token_property_permissions111 .try_push(up_data_structs::PropertyKeyPermission {112 key: u_key(),113 permission: up_data_structs::PropertyPermission {114 mutable: false,115 collection_admin: true,116 token_owner: false,117 },118 })119 .map_err(|e| Error::Revert(format!("{:?}", e)))?;120121 token_property_permissions122 .try_push(up_data_structs::PropertyKeyPermission {123 key: s_key(),124 permission: up_data_structs::PropertyPermission {125 mutable: false,126 collection_admin: true,127 token_owner: false,128 },129 })130 .map_err(|e| Error::Revert(format!("{:?}", e)))?;131132 collection_properties133 .try_push(up_data_structs::Property {134 key: schema_name_key(),135 value: erc721_value(),136 })137 .map_err(|e| Error::Revert(format!("{:?}", e)))?;138139 if !base_uri_value.is_empty() {140 collection_properties141 .try_push(up_data_structs::Property {142 key: base_uri_key(),143 value: base_uri_value,144 })145 .map_err(|e| Error::Revert(format!("{:?}", e)))?;146 }147 }148149 let data = CreateCollectionData {150 name,151 mode,152 description,153 token_prefix,154 token_property_permissions,155 ..Default::default()156 };157 Ok(data)158}4915950/// @title Contract, which allows users to operate with collections160/// @title Contract, which allows users to operate with collections51#[solidity_interface(name = "CollectionHelpers", events(CollectionHelpersEvents))]161#[solidity_interface(name = "CollectionHelpers", events(CollectionHelpersEvents))]63 description: string,173 description: string,64 token_prefix: string,174 token_prefix: string,65 ) -> Result<address> {175 ) -> Result<address> {66 let caller = T::CrossAccountId::from_eth(caller);176 let (caller, name, description, token_prefix, _base_uri_value) =67 let name = name68 .encode_utf16()69 .collect::<Vec<u16>>()70 .try_into()71 .map_err(|_| error_feild_too_long(stringify!(name), MAX_COLLECTION_NAME_LENGTH))?;72 let description = description73 .encode_utf16()74 .collect::<Vec<u16>>()75 .try_into()76 .map_err(|_| {77 error_feild_too_long(stringify!(description), MAX_COLLECTION_DESCRIPTION_LENGTH)78 })?;79 let token_prefix = token_prefix80 .into_bytes()81 .try_into()82 .map_err(|_| error_feild_too_long(stringify!(token_prefix), MAX_TOKEN_PREFIX_LENGTH))?;8384 let key = token_uri_key();85 let permission = up_data_structs::PropertyPermission {86 mutable: true,87 collection_admin: true,88 token_owner: false,89 };90 let mut token_property_permissions =91 up_data_structs::CollectionPropertiesPermissionsVec::default();92 token_property_permissions93 .try_push(up_data_structs::PropertyKeyPermission { key, permission })177 convert_data::<T>(caller, name, description, token_prefix, "".into())?;94 .map_err(|e| Error::Revert(format!("{:?}", e)))?;9596 let data = CreateCollectionData {178 let data = make_data::<T>(name, CollectionMode::NFT, description, token_prefix, Default::default(), false)?;97 name,98 description,99 token_prefix,100 token_property_permissions,101 ..Default::default()102 };103104 let collection_id =179 let collection_id =105 <pallet_nonfungible::Pallet<T>>::init_collection(caller.clone(), data, false)180 <pallet_nonfungible::Pallet<T>>::init_collection(caller.clone(), data, false)109 Ok(address)184 Ok(address)110 }185 }186187 #[weight(<SelfWeightOf<T>>::create_collection())]188 #[solidity(rename_selector = "createERC721MetadataCompatibleCollection")]189 fn create_nonfungible_collection_with_properties(190 &mut self,191 caller: caller,192 name: string,193 description: string,194 token_prefix: string,195 base_uri: string,196 ) -> Result<address> {197 let (caller, name, description, token_prefix, base_uri_value) =198 convert_data::<T>(caller, name, description, token_prefix, base_uri)?;199 let data = make_data::<T>(name, CollectionMode::NFT, description, token_prefix, base_uri_value, true)?;200 let collection_id =201 <pallet_nonfungible::Pallet<T>>::init_collection(caller.clone(), data, false)202 .map_err(pallet_evm_coder_substrate::dispatch_to_evm::<T>)?;203204 let address = pallet_common::eth::collection_id_to_address(collection_id);205 Ok(address)206 }111207112 /// Check if a collection exists208 /// Check if a collection exists113 /// @param collection_address Address of the collection in question209 /// @param collection_address Address of the collection in question152generate_stubgen!(collection_helper_impl, CollectionHelpersCall<()>, true);248generate_stubgen!(collection_helper_impl, CollectionHelpersCall<()>, true);153generate_stubgen!(collection_helper_iface, CollectionHelpersCall<()>, false);249generate_stubgen!(collection_helper_iface, CollectionHelpersCall<()>, false);154250155fn error_feild_too_long(feild: &str, bound: u32) -> Error {251fn error_feild_too_long(feild: &str, bound: usize) -> Error {156 Error::Revert(format!("{} is too long. Max length is {}.", feild, bound))252 Error::Revert(format!("{} is too long. Max length is {}.", feild, bound))157}253}158254primitives/data-structs/src/lib.rsdiffbeforeafterboth--- a/primitives/data-structs/src/lib.rs
+++ b/primitives/data-structs/src/lib.rs
@@ -286,6 +286,10 @@
}
}
+pub type CollectionName = BoundedVec<u16, ConstU32<MAX_COLLECTION_NAME_LENGTH>>;
+pub type CollectionDescription = BoundedVec<u16, ConstU32<MAX_COLLECTION_DESCRIPTION_LENGTH>>;
+pub type CollectionTokenPrefix = BoundedVec<u8, ConstU32<MAX_TOKEN_PREFIX_LENGTH>>;
+
/// Collection parameters, used in storage (see [`RpcCollection`] for the RPC version).
#[struct_versioning::versioned(version = 2, upper)]
#[derive(Encode, Decode, Clone, PartialEq, TypeInfo, MaxEncodedLen)]