From 956d7c163224346019ae7c934e90e6846f40ccf3 Mon Sep 17 00:00:00 2001 From: Trubnikov Sergey Date: Fri, 24 Jun 2022 11:42:45 +0000 Subject: [PATCH] CORE-410 Setup properties at creation items --- --- a/pallets/refungible/src/common.rs +++ b/pallets/refungible/src/common.rs @@ -140,6 +140,7 @@ out.insert(to.clone(), data.pieces); out.try_into().expect("limit > 0") }, + properties: data.properties, }), _ => fail!(>::NotRefungibleDataUsedToMintFungibleCollectionToken), } --- a/pallets/refungible/src/lib.rs +++ b/pallets/refungible/src/lib.rs @@ -87,7 +87,7 @@ #![cfg_attr(not(feature = "std"), no_std)] -use frame_support::{ensure, BoundedVec, transactional}; +use frame_support::{ensure, BoundedVec, transactional, storage::with_transaction}; use up_data_structs::{ AccessMode, CollectionId, CustomDataLimit, MAX_REFUNGIBLE_PIECES, TokenId, CreateCollectionData, CreateRefungibleExData, mapping::TokenAddressMapping, budget::Budget, @@ -96,7 +96,7 @@ use pallet_evm::account::CrossAccountId; use pallet_common::{Error as CommonError, Event as CommonEvent, Pallet as PalletCommon, CommonCollectionOperations as _}; use pallet_structure::Pallet as PalletStructure; -use sp_runtime::{ArithmeticError, DispatchError, DispatchResult}; +use sp_runtime::{ArithmeticError, DispatchError, DispatchResult, TransactionOutcome}; use sp_std::{vec::Vec, vec, collections::btree_map::BTreeMap}; use core::ops::Deref; use codec::{Encode, Decode, MaxEncodedLen}; @@ -804,32 +804,57 @@ // ========= + with_transaction(|| { + for (i, data) in data.iter().enumerate() { + let token_id = first_token_id + i as u32 + 1; + >::insert((collection.id, token_id), totals[i]); + + >::insert( + (collection.id, token_id), + ItemData { + const_data: data.const_data.clone(), + }, + ); + + for (user, amount) in data.users.iter() { + if *amount == 0 { + continue; + } + >::insert((collection.id, token_id, &user), amount); + >::insert((collection.id, &user, TokenId(token_id)), true); + >::nest_if_sent_to_token_unchecked( + user, + collection.id, + TokenId(token_id), + ); + } + + if let Err(e) = Self::set_token_properties( + collection, + sender, + TokenId(token_id), + data.properties.clone().into_inner(), + true, + ) { + return TransactionOutcome::Rollback(Err(e)); + } + } + TransactionOutcome::Commit(Ok(())) + })?; + >::insert(collection.id, tokens_minted); + for (account, balance) in balances { >::insert((collection.id, account), balance); } + for (i, token) in data.into_iter().enumerate() { let token_id = first_token_id + i as u32 + 1; - >::insert((collection.id, token_id), totals[i]); - - >::insert( - (collection.id, token_id), - ItemData { - const_data: token.const_data, - }, - ); for (user, amount) in token.users.into_iter() { if amount == 0 { continue; - } - >::insert((collection.id, token_id, &user), amount); - >::insert((collection.id, &user, TokenId(token_id)), true); - >::nest_if_sent_to_token_unchecked( - &user, - collection.id, - TokenId(token_id), - ); + } // TODO: ERC20 transfer event >::deposit_event(CommonEvent::ItemCreated( --- a/primitives/data-structs/src/lib.rs +++ b/primitives/data-structs/src/lib.rs @@ -534,7 +534,12 @@ #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))] #[derivative(Debug(format_with = "bounded::vec_debug"))] pub const_data: BoundedVec, + pub pieces: u128, + + #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))] + #[derivative(Debug(format_with = "bounded::vec_debug"))] + pub properties: CollectionPropertiesVec, } #[derive(Encode, Decode, Debug, Clone, PartialEq, TypeInfo, MaxEncodedLen)] @@ -568,6 +573,8 @@ pub const_data: BoundedVec, #[derivative(Debug(format_with = "bounded::map_debug"))] pub users: BoundedBTreeMap>, + #[derivative(Debug(format_with = "bounded::vec_debug"))] + pub properties: CollectionPropertiesVec, } #[derive(Encode, Decode, MaxEncodedLen, PartialEq, Clone, TypeInfo, Derivative)] --- a/tests/src/createMultipleItems.test.ts +++ b/tests/src/createMultipleItems.test.ts @@ -376,14 +376,26 @@ // ReFungible const collectionIdReFungible = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}}); - const argsReFungible = [ - {ReFungible: ['1'.repeat(2049), 10]}, - {ReFungible: ['2'.repeat(2049), 10]}, - {ReFungible: ['3'.repeat(2049), 10]}, - ]; - const createMultipleItemsTxFungible = api.tx.unique - .createMultipleItems(collectionIdReFungible, normalizeAccountId(alice.address), argsReFungible); - await expect(submitTransactionExpectFailAsync(alice, createMultipleItemsTxFungible)).to.be.rejected; + { + const argsReFungible = [ + {ReFungible: ['1'.repeat(2049), 10, []]}, + {ReFungible: ['2'.repeat(2049), 10, []]}, + {ReFungible: ['3'.repeat(2049), 10, []]}, + ]; + const createMultipleItemsTxFungible = api.tx.unique + .createMultipleItems(collectionIdReFungible, normalizeAccountId(alice.address), argsReFungible); + await expect(submitTransactionExpectFailAsync(alice, createMultipleItemsTxFungible)).to.be.rejected; + } + { + const argsReFungible = [ + {ReFungible: {properties: [{key: 'key', value: 'A'.repeat(32769)}]}}, + {ReFungible: {properties: [{key: 'key', value: 'B'.repeat(32769)}]}}, + {ReFungible: {properties: [{key: 'key', value: 'C'.repeat(32769)}]}}, + ]; + const createMultipleItemsTxFungible = api.tx.unique + .createMultipleItems(collectionIdReFungible, normalizeAccountId(alice.address), argsReFungible); + await expect(submitTransactionExpectFailAsync(alice, createMultipleItemsTxFungible)).to.be.rejected; + } }); }); -- gitstuff