git.delta.rocks / unique-network / refs/commits / 956d7c163224

difftreelog

CORE-410 Setup properties at creation items

Trubnikov Sergey2022-06-24parent: #bd8881f.patch.diff
in: master

4 files changed

modifiedpallets/refungible/src/common.rsdiffbeforeafterboth
140 out.insert(to.clone(), data.pieces);140 out.insert(to.clone(), data.pieces);
141 out.try_into().expect("limit > 0")141 out.try_into().expect("limit > 0")
142 },142 },
143 properties: data.properties,
143 }),144 }),
144 _ => fail!(<Error<T>>::NotRefungibleDataUsedToMintFungibleCollectionToken),145 _ => fail!(<Error<T>>::NotRefungibleDataUsedToMintFungibleCollectionToken),
145 }146 }
modifiedpallets/refungible/src/lib.rsdiffbeforeafterboth
8787
88#![cfg_attr(not(feature = "std"), no_std)]88#![cfg_attr(not(feature = "std"), no_std)]
8989
90use frame_support::{ensure, BoundedVec, transactional};90use frame_support::{ensure, BoundedVec, transactional, storage::with_transaction};
91use up_data_structs::{91use up_data_structs::{
92 AccessMode, CollectionId, CustomDataLimit, MAX_REFUNGIBLE_PIECES, TokenId,92 AccessMode, CollectionId, CustomDataLimit, MAX_REFUNGIBLE_PIECES, TokenId,
93 CreateCollectionData, CreateRefungibleExData, mapping::TokenAddressMapping, budget::Budget,93 CreateCollectionData, CreateRefungibleExData, mapping::TokenAddressMapping, budget::Budget,
96use pallet_evm::account::CrossAccountId;96use pallet_evm::account::CrossAccountId;
97use pallet_common::{Error as CommonError, Event as CommonEvent, Pallet as PalletCommon, CommonCollectionOperations as _};97use pallet_common::{Error as CommonError, Event as CommonEvent, Pallet as PalletCommon, CommonCollectionOperations as _};
98use pallet_structure::Pallet as PalletStructure;98use pallet_structure::Pallet as PalletStructure;
99use sp_runtime::{ArithmeticError, DispatchError, DispatchResult};99use sp_runtime::{ArithmeticError, DispatchError, DispatchResult, TransactionOutcome};
100use sp_std::{vec::Vec, vec, collections::btree_map::BTreeMap};100use sp_std::{vec::Vec, vec, collections::btree_map::BTreeMap};
101use core::ops::Deref;101use core::ops::Deref;
102use codec::{Encode, Decode, MaxEncodedLen};102use codec::{Encode, Decode, MaxEncodedLen};
804804
805 // =========805 // =========
806
807 with_transaction(|| {
808 for (i, data) in data.iter().enumerate() {
809 let token_id = first_token_id + i as u32 + 1;
810 <TotalSupply<T>>::insert((collection.id, token_id), totals[i]);
811
812 <TokenData<T>>::insert(
813 (collection.id, token_id),
814 ItemData {
815 const_data: data.const_data.clone(),
816 },
817 );
818
819 for (user, amount) in data.users.iter() {
820 if *amount == 0 {
821 continue;
822 }
823 <Balance<T>>::insert((collection.id, token_id, &user), amount);
824 <Owned<T>>::insert((collection.id, &user, TokenId(token_id)), true);
825 <PalletStructure<T>>::nest_if_sent_to_token_unchecked(
826 user,
827 collection.id,
828 TokenId(token_id),
829 );
830 }
831
832 if let Err(e) = Self::set_token_properties(
833 collection,
834 sender,
835 TokenId(token_id),
836 data.properties.clone().into_inner(),
837 true,
838 ) {
839 return TransactionOutcome::Rollback(Err(e));
840 }
841 }
842 TransactionOutcome::Commit(Ok(()))
843 })?;
806844
807 <TokensMinted<T>>::insert(collection.id, tokens_minted);845 <TokensMinted<T>>::insert(collection.id, tokens_minted);
846
808 for (account, balance) in balances {847 for (account, balance) in balances {
809 <AccountBalance<T>>::insert((collection.id, account), balance);848 <AccountBalance<T>>::insert((collection.id, account), balance);
810 }849 }
850
811 for (i, token) in data.into_iter().enumerate() {851 for (i, token) in data.into_iter().enumerate() {
812 let token_id = first_token_id + i as u32 + 1;852 let token_id = first_token_id + i as u32 + 1;
813 <TotalSupply<T>>::insert((collection.id, token_id), totals[i]);
814
815 <TokenData<T>>::insert(
816 (collection.id, token_id),
817 ItemData {
818 const_data: token.const_data,
819 },
820 );
821853
822 for (user, amount) in token.users.into_iter() {854 for (user, amount) in token.users.into_iter() {
823 if amount == 0 {855 if amount == 0 {
824 continue;856 continue;
825 }857 }
826 <Balance<T>>::insert((collection.id, token_id, &user), amount);
827 <Owned<T>>::insert((collection.id, &user, TokenId(token_id)), true);
828 <PalletStructure<T>>::nest_if_sent_to_token_unchecked(
829 &user,
830 collection.id,
831 TokenId(token_id),
832 );
833858
834 // TODO: ERC20 transfer event859 // TODO: ERC20 transfer event
835 <PalletCommon<T>>::deposit_event(CommonEvent::ItemCreated(860 <PalletCommon<T>>::deposit_event(CommonEvent::ItemCreated(
modifiedprimitives/data-structs/src/lib.rsdiffbeforeafterboth
537
537 pub pieces: u128,538 pub pieces: u128,
539
540 #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]
541 #[derivative(Debug(format_with = "bounded::vec_debug"))]
542 pub properties: CollectionPropertiesVec,
538}543}
539544
540#[derive(Encode, Decode, Debug, Clone, PartialEq, TypeInfo, MaxEncodedLen)]545#[derive(Encode, Decode, Debug, Clone, PartialEq, TypeInfo, MaxEncodedLen)]
568 pub const_data: BoundedVec<u8, CustomDataLimit>,573 pub const_data: BoundedVec<u8, CustomDataLimit>,
569 #[derivative(Debug(format_with = "bounded::map_debug"))]574 #[derivative(Debug(format_with = "bounded::map_debug"))]
570 pub users: BoundedBTreeMap<CrossAccountId, u128, ConstU32<MAX_ITEMS_PER_BATCH>>,575 pub users: BoundedBTreeMap<CrossAccountId, u128, ConstU32<MAX_ITEMS_PER_BATCH>>,
576 #[derivative(Debug(format_with = "bounded::vec_debug"))]
577 pub properties: CollectionPropertiesVec,
571}578}
572579
573#[derive(Encode, Decode, MaxEncodedLen, PartialEq, Clone, TypeInfo, Derivative)]580#[derive(Encode, Decode, MaxEncodedLen, PartialEq, Clone, TypeInfo, Derivative)]
modifiedtests/src/createMultipleItems.test.tsdiffbeforeafterboth
376 // ReFungible376 // ReFungible
377 const collectionIdReFungible =377 const collectionIdReFungible =
378 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});378 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});
379 {
379 const argsReFungible = [380 const argsReFungible = [
380 {ReFungible: ['1'.repeat(2049), 10]},381 {ReFungible: ['1'.repeat(2049), 10, []]},
381 {ReFungible: ['2'.repeat(2049), 10]},382 {ReFungible: ['2'.repeat(2049), 10, []]},
382 {ReFungible: ['3'.repeat(2049), 10]},383 {ReFungible: ['3'.repeat(2049), 10, []]},
383 ];384 ];
384 const createMultipleItemsTxFungible = api.tx.unique385 const createMultipleItemsTxFungible = api.tx.unique
385 .createMultipleItems(collectionIdReFungible, normalizeAccountId(alice.address), argsReFungible);386 .createMultipleItems(collectionIdReFungible, normalizeAccountId(alice.address), argsReFungible);
386 await expect(submitTransactionExpectFailAsync(alice, createMultipleItemsTxFungible)).to.be.rejected;387 await expect(submitTransactionExpectFailAsync(alice, createMultipleItemsTxFungible)).to.be.rejected;
388 }
389 {
390 const argsReFungible = [
391 {ReFungible: {properties: [{key: 'key', value: 'A'.repeat(32769)}]}},
392 {ReFungible: {properties: [{key: 'key', value: 'B'.repeat(32769)}]}},
393 {ReFungible: {properties: [{key: 'key', value: 'C'.repeat(32769)}]}},
394 ];
395 const createMultipleItemsTxFungible = api.tx.unique
396 .createMultipleItems(collectionIdReFungible, normalizeAccountId(alice.address), argsReFungible);
397 await expect(submitTransactionExpectFailAsync(alice, createMultipleItemsTxFungible)).to.be.rejected;
398 }
387 });399 });
388 });400 });
389401