@@ -108,7 +108,7 @@ pub mod pallet {
108108 CollectionId ,
109109 Blake2_128Concat ,
110110 ItemId , // FruniqueId
111- HierarchicalInfo , // ParentId and flag if it inherit attributes
111+ Option < HierarchicalInfo > , // ParentId and flag if it inherit attributes
112112 ValueQuery
113113 > ;
114114
@@ -155,44 +155,6 @@ pub mod pallet {
155155 Ok ( ( ) )
156156 }
157157
158- /// Issue a new frunique from a public origin.
159- ///
160- /// A new NFT (unique) is created and reserved,
161- /// a fungible token (asset) is created and minted to the owner.
162- ///
163- /// The origin must be Signed and the sender must have sufficient funds free.
164- ///
165- /// `AssetDeposit` funds of sender are reserved.
166- ///
167- /// Parameters:
168- /// - `asset_id`: The identifier of the new asset. This must not be currently in use to identify
169- /// an existing asset.
170- /// - `class`: The identifier of the new asset class. This must not be currently in use.
171- /// - `admin`: The admin of this class of assets. The admin is the initial address of each
172- /// member of the asset class's admin team.
173- ///
174- /// Emits `FruniqueCreated` event when successful.
175- ///
176- /// Weight: `O(1)`
177- #[ pallet:: weight( 10_000 + T :: DbWeight :: get( ) . writes( 1 ) ) ]
178- pub fn create (
179- origin : OriginFor < T > ,
180- class_id : T :: CollectionId ,
181- instance_id : T :: ItemId ,
182- numeric_value : Option < Permill > ,
183- admin : <T :: Lookup as sp_runtime:: traits:: StaticLookup >:: Source ,
184- ) -> DispatchResult {
185- let owner = ensure_signed ( origin. clone ( ) ) ?;
186-
187- // create an NFT in the uniques pallet
188- Self :: do_create ( origin. clone ( ) , class_id, instance_id, numeric_value, admin. clone ( ) ) ?;
189-
190- let admin = T :: Lookup :: lookup ( admin) ?;
191- Self :: deposit_event ( Event :: FruniqueCreated ( owner, admin, class_id, instance_id) ) ;
192-
193- Ok ( ( ) )
194- }
195-
196158 #[ pallet:: weight( 10_000 + T :: DbWeight :: get( ) . writes( 4 ) ) ]
197159 pub fn instance_exists (
198160 _origin : OriginFor < T > ,
@@ -244,49 +206,6 @@ pub mod pallet {
244206 Ok ( ( ) )
245207 }
246208
247- /// ## Create a frunique with given attributes.
248- /// `origin` must be signed by the owner of the frunique.
249- /// - `attributes` must be a list of pairs of `key` and `value`.
250- /// `key` must be a valid key for the asset class.
251- /// `value` must be a valid value for the asset class.
252- /// `attributes` must not be empty.
253- /// - `instance_id` must be a valid instance of the asset class.
254- /// - `class_id` must be a valid class of the asset class.
255- /// - `numeric_value` must be a valid value for the asset class.
256- /// - `admin` must be a valid admin of the asset class.
257- /// - `instance_id` must not be already in use.
258- /// - `class_id` must not be already in use.
259- /// - `numeric_value` must not be already in use.
260- #[ pallet:: weight( 10_000 + T :: DbWeight :: get( ) . writes( 1 ) ) ]
261- pub fn create_with_attributes (
262- origin : OriginFor < T > ,
263- class_id : T :: CollectionId ,
264- instance_id : T :: ItemId ,
265- numeric_value : Option < Permill > ,
266- admin : <T :: Lookup as sp_runtime:: traits:: StaticLookup >:: Source ,
267- attributes : Vec < ( BoundedVec < u8 , T :: KeyLimit > , BoundedVec < u8 , T :: ValueLimit > ) > ,
268- ) -> DispatchResult {
269- // ! Ensure the admin is the one who can add attributes to the frunique.
270- ensure ! ( !attributes. is_empty( ) , Error :: <T >:: AttributesEmpty ) ;
271-
272- let owner = ensure_signed ( origin. clone ( ) ) ?;
273- // create an NFT in the uniques pallet
274- Self :: do_create ( origin. clone ( ) , class_id, instance_id, numeric_value, admin. clone ( ) ) ?;
275- for attribute in & attributes {
276- Self :: set_attribute (
277- origin. clone ( ) ,
278- & class_id. clone ( ) ,
279- Self :: u32_to_instance_id ( instance_id. clone ( ) ) ,
280- attribute. 0 . clone ( ) ,
281- attribute. 1 . clone ( ) ,
282- ) ?;
283- }
284-
285- let admin = T :: Lookup :: lookup ( admin) ?;
286- Self :: deposit_event ( Event :: FruniqueCreated ( owner, admin, class_id, instance_id) ) ;
287- Ok ( ( ) )
288- }
289-
290209 /// ## NFT Division
291210 ///
292211 /// PD: the Key/value length limits are inherited from the uniques pallet,
@@ -328,115 +247,17 @@ pub mod pallet {
328247 attributes
329248 ) ?;
330249
250+ Self :: deposit_event ( Event :: FruniqueCreated ( owner. clone ( ) , owner, class_id, instance_id) ) ;
251+
331252 if let Some ( parent_info) = parent_info {
332253 // ! check if parent exists
333- <FruniqueParent < T > >:: insert ( class_id, instance_id, parent_info) ;
254+ ensure ! ( Self :: item_exists( & class_id, & parent_info. 0 ) , <Error <T >>:: CollectionNotFound ) ;
255+ <FruniqueParent < T > >:: insert ( class_id, instance_id, Some ( parent_info) ) ;
334256 // Self::do_hierarchical_division(origin.clone(), class_id, instance_id, parent_info)?;
335257 }
336258
337259 Ok ( ( ) )
338260 }
339- // Old one
340- // #[pallet::weight(10_000 + T::DbWeight::get().writes(4))]
341- // pub fn spawn(
342- // origin: OriginFor<T>,
343- // class_id: T::CollectionId,
344- // instance_id: T::ItemId,
345- // inherit_attrs: bool,
346- // _p: Permill,
347- // admin: <T::Lookup as sp_runtime::traits::StaticLookup>::Source,
348- // ) -> DispatchResult
349- // where
350- // <T as pallet_uniques::Config>::ItemId: From<u32>,
351- // {
352- // // Boilerplate (setup, conversions, ensure_signed)
353- // let owner = ensure_signed(origin.clone())?;
354- // let encoded_id = instance_id.encode();
355- // // TODO: Check if the instance_id exists?
356- // let parent_id_key = BoundedVec::<u8, T::KeyLimit>::try_from(r#"parent_id"#.encode())
357- // .expect("Error on encoding the parent_id key to BoundedVec");
358- // let mut parent_id_val: BoundedVec<u8, T::ValueLimit>;
359- // // Instance n number of nfts (with the respective parentId)
360- // let new_instance_id = Self::frunique_cnt().try_into().unwrap();
361- // // Mint a unique
362- // Self::mint(origin.clone(), &class_id, new_instance_id, admin.clone())?;
363- // // Set the respective attributtes
364- // if inherit_attrs {
365- // // TODO: Check all the parent's instance attributes
366- // // Let's start with some static attributes check (does parent_id exist?)
367- // // Options:
368- // // 1.- Constant &str array containing the keys
369- // // 2.- Set a whole single attribute as bytes, containing all the fruniques metadata (parent_id, numerical_value, etc..)
370- // // 3.- Keep our own metadata (or whole nfts) storage on
371- // // 3.1.- Consider the 3 above but with interfaces/traits
372- // // I'm assuming doing it via scripts on the front-end isn't viable option
373- // let parent_id =
374- // Self::get_nft_attribute(&class_id, &instance_id, &"parent_id".encode());
375- // if parent_id.len() > 0 {
376- // parent_id_val = parent_id;
377- // } else {
378- // parent_id_val = BoundedVec::<u8, T::ValueLimit>::try_from(encoded_id.clone())
379- // .expect("Error on converting the parent_id to BoundedVec");
380- // }
381- // let num_value =
382- // Self::get_nft_attribute(&class_id, &instance_id, &"num_value".encode());
383- // if num_value.len() > 0 {
384- // let num_value_key =
385- // BoundedVec::<u8, T::KeyLimit>::try_from(r#"num_value"#.encode())
386- // .expect("Error on encoding the num_value key to BoundedVec");
387- // // TODO: Call bytes_to_u32 & divide the numeric value before setting it
388- // Self::set_attribute(
389- // origin.clone(),
390- // &class_id,
391- // Self::u32_to_instance_id(new_instance_id),
392- // num_value_key,
393- // num_value,
394- // )?;
395- // }
396- // if let Some(parent_attr) = pallet_uniques::Pallet::<T>::attribute(
397- // &class_id,
398- // &instance_id,
399- // &"parent_id".encode(),
400- // ) {
401- // //println!(" Instance number {:?} parent_id (parent's parent): {:#?}", instance_id, Self::bytes_to_u32( parent_attr.clone() ));
402- // parent_id_val = BoundedVec::<u8, T::ValueLimit>::try_from(parent_attr)
403- // .expect("Error on converting the parent_id to BoundedVec");
404- // } else {
405- // //println!("The parent doesn't have a parent_id");
406- // parent_id_val = BoundedVec::<u8, T::ValueLimit>::try_from(encoded_id)
407- // .expect("Error on converting the parent_id to BoundedVec");
408- // }
409- // } else {
410- // parent_id_val = BoundedVec::<u8, T::ValueLimit>::try_from(encoded_id)
411- // .expect("Error on converting the parent_id to BoundedVec");
412- // }
413- // // (for encoding reasons the parentId is stored on hex format as a secondary side-effect, I hope it's not too much of a problem).
414- // Self::set_attribute(
415- // origin.clone(),
416- // &class_id,
417- // Self::u32_to_instance_id(new_instance_id),
418- // parent_id_key,
419- // parent_id_val,
420- // )?;
421- // let _e = Self::instance_exists(origin, class_id, instance_id);
422- // //let final_test = pallet_uniques::Pallet::<T>::attribute(&class_id, &Self::u16_to_instance_id(new_instance_id ), &r#"parent_id"#.encode() );
423- // //println!("The parent_id of {} is now {:?}",new_instance_id, Self::bytes_to_u32(final_test.unwrap()) );
424- // // TODO: set the divided value attribute. Numbers, divisions and floating points are giving a lot of problems
425- // let admin = T::Lookup::lookup(admin)?;
426- // Self::deposit_event(Event::FruniqueDivided(owner, admin, class_id, instance_id));
427- // // Freeze the nft to prevent trading it? Burn it? Not clear, so nothing at the moment
428- // // Freeze parent
429-
430- // // unique -> n parts of parent
431- // // unique is freezed
432- // // 1, 2, 3, 4, ..., n
433- // // alice 1 & bob 1 & carol 1 & ... & n
434- // // (n - 5) -> m parts of parent
435- // // m parts of the new frunique
436- // // n
437-
438- // Ok(())
439- // }
440261
441262 /// Kill all the stored data.
442263 ///
0 commit comments