git.delta.rocks / unique-network / refs/commits / e7d3521284b7

difftreelog

NFTPAR-183: Storage Refactoring. Reduced collection id and token id from u64 to u32.

sotmorskiy2020-12-10parent: #68e6834.patch.diff
in: master

2 files changed

modifiedpallets/nft/src/lib.rsdiffbeforeafterboth
48// Structs48// Structs
49// #region49// #region
50
51pub type CollectionId = u32;
52pub type TokenId = u32;
5053
51#[derive(Encode, Decode, Eq, Debug, Clone, PartialEq)]54#[derive(Encode, Decode, Eq, Debug, Clone, PartialEq)]
52#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]55#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
116#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]119#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]
117#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]120#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
118pub struct NftItemType<AccountId> {121pub struct NftItemType<AccountId> {
119 pub collection: u64,122 pub collection: CollectionId,
120 pub owner: AccountId,123 pub owner: AccountId,
121 pub const_data: Vec<u8>,124 pub const_data: Vec<u8>,
122 pub variable_data: Vec<u8>,125 pub variable_data: Vec<u8>,
125#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]128#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]
126#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]129#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
127pub struct FungibleItemType<AccountId> {130pub struct FungibleItemType<AccountId> {
128 pub collection: u64,131 pub collection: CollectionId,
129 pub owner: AccountId,132 pub owner: AccountId,
130 pub value: u128,133 pub value: u128,
131}134}
132135
133#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]136#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]
134#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]137#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
135pub struct ReFungibleItemType<AccountId> {138pub struct ReFungibleItemType<AccountId> {
136 pub collection: u64,139 pub collection: CollectionId,
137 pub owner: Vec<Ownership<AccountId>>,140 pub owner: Vec<Ownership<AccountId>>,
138 pub const_data: Vec<u8>,141 pub const_data: Vec<u8>,
139 pub variable_data: Vec<u8>,142 pub variable_data: Vec<u8>,
151pub struct VestingItem<AccountId, Moment> {154pub struct VestingItem<AccountId, Moment> {
152 pub sender: AccountId,155 pub sender: AccountId,
153 pub recipient: AccountId,156 pub recipient: AccountId,
154 pub collection_id: u64,157 pub collection_id: CollectionId,
155 pub item_id: u64,158 pub item_id: TokenId,
156 pub amount: u64,159 pub amount: u64,
157 pub vesting_date: Moment,160 pub vesting_date: Moment,
158}161}
167#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]170#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]
168#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]171#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
169pub struct ChainLimits {172pub struct ChainLimits {
170 pub collection_numbers_limit: u64,173 pub collection_numbers_limit: u32,
171 pub account_token_ownership_limit: u64,174 pub account_token_ownership_limit: u32,
172 pub collections_admins_limit: u64,175 pub collections_admins_limit: u64,
173 pub custom_data_limit: u32,176 pub custom_data_limit: u32,
174177
341 trait Store for Module<T: Trait> as Nft {344 trait Store for Module<T: Trait> as Nft {
342345
343 // Private members346 // Private members
344 NextCollectionID: u64;347 NextCollectionID: CollectionId;
345 CreatedCollectionCount: u64;348 CreatedCollectionCount: u32;
346 ChainVersion: u64;349 ChainVersion: u64;
347 ItemListIndex: map hasher(blake2_128_concat) u64 => u64;350 ItemListIndex: map hasher(blake2_128_concat) CollectionId => TokenId;
348351
349 // Chain limits struct352 // Chain limits struct
350 pub ChainLimit get(fn chain_limit) config(): ChainLimits;353 pub ChainLimit get(fn chain_limit) config(): ChainLimits;
351354
352 // Bound counters355 // Bound counters
353 CollectionCount: u64;356 CollectionCount: u32;
354 pub AccountItemCount get(fn account_item_count): map hasher(identity) T::AccountId => u64;357 pub AccountItemCount get(fn account_item_count): map hasher(identity) T::AccountId => u32;
355358
356 // Basic collections359 // Basic collections
357 pub Collection get(fn collection) config(): map hasher(identity) u64 => CollectionType<T::AccountId>;360 pub Collection get(fn collection) config(): map hasher(identity) CollectionId => CollectionType<T::AccountId>;
358 pub AdminList get(fn admin_list_collection): map hasher(identity) u64 => Vec<T::AccountId>;361 pub AdminList get(fn admin_list_collection): map hasher(identity) CollectionId => Vec<T::AccountId>;
359 pub WhiteList get(fn white_list): map hasher(identity) u64 => Vec<T::AccountId>;362 pub WhiteList get(fn white_list): map hasher(identity) CollectionId => Vec<T::AccountId>;
360363
361 /// Balance owner per collection map364 /// Balance owner per collection map
362 pub Balance get(fn balance_count): double_map hasher(blake2_128_concat) u64, hasher(blake2_128_concat) T::AccountId => u64;365 pub Balance get(fn balance_count): double_map hasher(blake2_128_concat) CollectionId, hasher(blake2_128_concat) T::AccountId => u64;
363366
364 /// second parameter: item id + owner account id367 /// second parameter: item id + owner account id
365 pub ApprovedList get(fn approved): double_map hasher(blake2_128_concat) u64, hasher(blake2_128_concat) (u64, T::AccountId) => Vec<ApprovePermissions<T::AccountId>>;368 pub ApprovedList get(fn approved): double_map hasher(blake2_128_concat) CollectionId, hasher(blake2_128_concat) (TokenId, T::AccountId) => Vec<ApprovePermissions<T::AccountId>>;
366369
367 /// Item collections370 /// Item collections
368 pub NftItemList get(fn nft_item_id) config(): double_map hasher(blake2_128_concat) u64, hasher(blake2_128_concat) u64 => NftItemType<T::AccountId>;371 pub NftItemList get(fn nft_item_id) config(): double_map hasher(blake2_128_concat) CollectionId, hasher(blake2_128_concat) TokenId => NftItemType<T::AccountId>;
369 pub FungibleItemList get(fn fungible_item_id) config(): double_map hasher(blake2_128_concat) u64, hasher(blake2_128_concat) u64 => FungibleItemType<T::AccountId>;372 pub FungibleItemList get(fn fungible_item_id) config(): double_map hasher(blake2_128_concat) CollectionId, hasher(blake2_128_concat) TokenId => FungibleItemType<T::AccountId>;
370 pub ReFungibleItemList get(fn refungible_item_id) config(): double_map hasher(blake2_128_concat) u64, hasher(blake2_128_concat) u64 => ReFungibleItemType<T::AccountId>;373 pub ReFungibleItemList get(fn refungible_item_id) config(): double_map hasher(blake2_128_concat) CollectionId, hasher(blake2_128_concat) TokenId => ReFungibleItemType<T::AccountId>;
371374
372 /// Index list375 /// Index list
373 pub AddressTokens get(fn address_tokens): double_map hasher(blake2_128_concat) u64, hasher(blake2_128_concat) T::AccountId => Vec<u64>;376 pub AddressTokens get(fn address_tokens): double_map hasher(blake2_128_concat) CollectionId, hasher(blake2_128_concat) T::AccountId => Vec<TokenId>;
374377
375 /// Tokens transfer baskets378 /// Tokens transfer baskets
376 pub NftTransferBasket get(fn nft_transfer_basket): double_map hasher(blake2_128_concat) u64, hasher(blake2_128_concat) u64 => T::BlockNumber;379 pub NftTransferBasket get(fn nft_transfer_basket): double_map hasher(blake2_128_concat) CollectionId, hasher(blake2_128_concat) TokenId => T::BlockNumber;
377 pub FungibleTransferBasket get(fn fungible_transfer_basket): double_map hasher(blake2_128_concat) u64, hasher(blake2_128_concat) u64 => Vec<BasketItem<T::AccountId, T::BlockNumber>>;380 pub FungibleTransferBasket get(fn fungible_transfer_basket): double_map hasher(blake2_128_concat) CollectionId, hasher(blake2_128_concat) TokenId => Vec<BasketItem<T::AccountId, T::BlockNumber>>;
378 pub ReFungibleTransferBasket get(fn refungible_transfer_basket): double_map hasher(blake2_128_concat) u64, hasher(blake2_128_concat) u64 => T::BlockNumber;381 pub ReFungibleTransferBasket get(fn refungible_transfer_basket): double_map hasher(blake2_128_concat) CollectionId, hasher(blake2_128_concat) TokenId => T::BlockNumber;
379382
380 // Contract Sponsorship and Ownership383 // Contract Sponsorship and Ownership
381 pub ContractOwner get(fn contract_owner): map hasher(twox_64_concat) T::AccountId => T::AccountId;384 pub ContractOwner get(fn contract_owner): map hasher(twox_64_concat) T::AccountId => T::AccountId;
419 /// * mode: [CollectionMode] converted into u8.422 /// * mode: [CollectionMode] converted into u8.
420 /// 423 ///
421 /// * account_id: Collection owner.424 /// * account_id: Collection owner.
422 Created(u64, u8, AccountId),425 Created(CollectionId, u8, AccountId),
423426
424 /// New item was created.427 /// New item was created.
425 /// 428 ///
428 /// * collection_id: Id of the collection where item was created.431 /// * collection_id: Id of the collection where item was created.
429 /// 432 ///
430 /// * item_id: Id of an item. Unique within the collection.433 /// * item_id: Id of an item. Unique within the collection.
431 ItemCreated(u64, u64),434 ItemCreated(CollectionId, TokenId),
432435
433 /// Collection item was burned.436 /// Collection item was burned.
434 /// 437 ///
437 /// collection_id.440 /// collection_id.
438 /// 441 ///
439 /// item_id: Identifier of burned NFT.442 /// item_id: Identifier of burned NFT.
440 ItemDestroyed(u64, u64),443 ItemDestroyed(CollectionId, TokenId),
441 }444 }
442);445);
443446
558 /// 561 ///
559 /// * collection_id: collection to destroy.562 /// * collection_id: collection to destroy.
560 #[weight = T::WeightInfo::destroy_collection()]563 #[weight = T::WeightInfo::destroy_collection()]
561 pub fn destroy_collection(origin, collection_id: u64) -> DispatchResult {564 pub fn destroy_collection(origin, collection_id: CollectionId) -> DispatchResult {
562565
563 let sender = ensure_signed(origin)?;566 let sender = ensure_signed(origin)?;
564 Self::check_owner_permissions(collection_id, sender)?;567 Self::check_owner_permissions(collection_id, sender)?;
605 /// 608 ///
606 /// * address.609 /// * address.
607 #[weight = T::WeightInfo::add_to_white_list()]610 #[weight = T::WeightInfo::add_to_white_list()]
608 pub fn add_to_white_list(origin, collection_id: u64, address: T::AccountId) -> DispatchResult{611 pub fn add_to_white_list(origin, collection_id: CollectionId, address: T::AccountId) -> DispatchResult{
609612
610 let sender = ensure_signed(origin)?;613 let sender = ensure_signed(origin)?;
611 Self::check_owner_or_admin_permissions(collection_id, sender)?;614 Self::check_owner_or_admin_permissions(collection_id, sender)?;
640 /// 643 ///
641 /// * address.644 /// * address.
642 #[weight = T::WeightInfo::remove_from_white_list()]645 #[weight = T::WeightInfo::remove_from_white_list()]
643 pub fn remove_from_white_list(origin, collection_id: u64, address: T::AccountId) -> DispatchResult{646 pub fn remove_from_white_list(origin, collection_id: CollectionId, address: T::AccountId) -> DispatchResult{
644647
645 let sender = ensure_signed(origin)?;648 let sender = ensure_signed(origin)?;
646 Self::check_owner_or_admin_permissions(collection_id, sender)?;649 Self::check_owner_or_admin_permissions(collection_id, sender)?;
669 /// 672 ///
670 /// * mode: [AccessMode]673 /// * mode: [AccessMode]
671 #[weight = T::WeightInfo::set_public_access_mode()]674 #[weight = T::WeightInfo::set_public_access_mode()]
672 pub fn set_public_access_mode(origin, collection_id: u64, mode: AccessMode) -> DispatchResult675 pub fn set_public_access_mode(origin, collection_id: CollectionId, mode: AccessMode) -> DispatchResult
673 {676 {
674 let sender = ensure_signed(origin)?;677 let sender = ensure_signed(origin)?;
675678
695 /// 698 ///
696 /// * mint_permission: Boolean parameter. If True, allows minting to Anyone with conditions above.699 /// * mint_permission: Boolean parameter. If True, allows minting to Anyone with conditions above.
697 #[weight = T::WeightInfo::set_mint_permission()]700 #[weight = T::WeightInfo::set_mint_permission()]
698 pub fn set_mint_permission(origin, collection_id: u64, mint_permission: bool) -> DispatchResult701 pub fn set_mint_permission(origin, collection_id: CollectionId, mint_permission: bool) -> DispatchResult
699 {702 {
700 let sender = ensure_signed(origin)?;703 let sender = ensure_signed(origin)?;
701704
719 /// 722 ///
720 /// * new_owner.723 /// * new_owner.
721 #[weight = T::WeightInfo::change_collection_owner()]724 #[weight = T::WeightInfo::change_collection_owner()]
722 pub fn change_collection_owner(origin, collection_id: u64, new_owner: T::AccountId) -> DispatchResult {725 pub fn change_collection_owner(origin, collection_id: CollectionId, new_owner: T::AccountId) -> DispatchResult {
723726
724 let sender = ensure_signed(origin)?;727 let sender = ensure_signed(origin)?;
725 Self::check_owner_permissions(collection_id, sender)?;728 Self::check_owner_permissions(collection_id, sender)?;
744 /// 747 ///
745 /// * new_admin_id: Address of new admin to add.748 /// * new_admin_id: Address of new admin to add.
746 #[weight = T::WeightInfo::add_collection_admin()]749 #[weight = T::WeightInfo::add_collection_admin()]
747 pub fn add_collection_admin(origin, collection_id: u64, new_admin_id: T::AccountId) -> DispatchResult {750 pub fn add_collection_admin(origin, collection_id: CollectionId, new_admin_id: T::AccountId) -> DispatchResult {
748751
749 let sender = ensure_signed(origin)?;752 let sender = ensure_signed(origin)?;
750 Self::check_owner_or_admin_permissions(collection_id, sender)?;753 Self::check_owner_or_admin_permissions(collection_id, sender)?;
778 /// 781 ///
779 /// * account_id: Address of admin to remove.782 /// * account_id: Address of admin to remove.
780 #[weight = T::WeightInfo::remove_collection_admin()]783 #[weight = T::WeightInfo::remove_collection_admin()]
781 pub fn remove_collection_admin(origin, collection_id: u64, account_id: T::AccountId) -> DispatchResult {784 pub fn remove_collection_admin(origin, collection_id: CollectionId, account_id: T::AccountId) -> DispatchResult {
782785
783 let sender = ensure_signed(origin)?;786 let sender = ensure_signed(origin)?;
784 Self::check_owner_or_admin_permissions(collection_id, sender)?;787 Self::check_owner_or_admin_permissions(collection_id, sender)?;
803 /// 806 ///
804 /// * new_sponsor.807 /// * new_sponsor.
805 #[weight = T::WeightInfo::set_collection_sponsor()]808 #[weight = T::WeightInfo::set_collection_sponsor()]
806 pub fn set_collection_sponsor(origin, collection_id: u64, new_sponsor: T::AccountId) -> DispatchResult {809 pub fn set_collection_sponsor(origin, collection_id: CollectionId, new_sponsor: T::AccountId) -> DispatchResult {
807810
808 let sender = ensure_signed(origin)?;811 let sender = ensure_signed(origin)?;
809 ensure!(<Collection<T>>::contains_key(collection_id), Error::<T>::CollectionNotFound);812 ensure!(<Collection<T>>::contains_key(collection_id), Error::<T>::CollectionNotFound);
825 /// 828 ///
826 /// * collection_id.829 /// * collection_id.
827 #[weight = T::WeightInfo::confirm_sponsorship()]830 #[weight = T::WeightInfo::confirm_sponsorship()]
828 pub fn confirm_sponsorship(origin, collection_id: u64) -> DispatchResult {831 pub fn confirm_sponsorship(origin, collection_id: CollectionId) -> DispatchResult {
829832
830 let sender = ensure_signed(origin)?;833 let sender = ensure_signed(origin)?;
831 ensure!(<Collection<T>>::contains_key(collection_id), Error::<T>::CollectionNotFound);834 ensure!(<Collection<T>>::contains_key(collection_id), Error::<T>::CollectionNotFound);
850 /// 853 ///
851 /// * collection_id.854 /// * collection_id.
852 #[weight = T::WeightInfo::remove_collection_sponsor()]855 #[weight = T::WeightInfo::remove_collection_sponsor()]
853 pub fn remove_collection_sponsor(origin, collection_id: u64) -> DispatchResult {856 pub fn remove_collection_sponsor(origin, collection_id: CollectionId) -> DispatchResult {
854857
855 let sender = ensure_signed(origin)?;858 let sender = ensure_signed(origin)?;
856 ensure!(<Collection<T>>::contains_key(collection_id), Error::<T>::CollectionNotFound);859 ensure!(<Collection<T>>::contains_key(collection_id), Error::<T>::CollectionNotFound);
889 // .saturating_add(RocksDbWeight::get().writes(8 as Weight))]892 // .saturating_add(RocksDbWeight::get().writes(8 as Weight))]
890893
891 #[weight = T::WeightInfo::create_item(data.len())]894 #[weight = T::WeightInfo::create_item(data.len())]
892 pub fn create_item(origin, collection_id: u64, owner: T::AccountId, data: CreateItemData) -> DispatchResult {895 pub fn create_item(origin, collection_id: CollectionId, owner: T::AccountId, data: CreateItemData) -> DispatchResult {
893896
894 let sender = ensure_signed(origin)?;897 let sender = ensure_signed(origin)?;
895898
925 #[weight = T::WeightInfo::create_item(items_data.into_iter()928 #[weight = T::WeightInfo::create_item(items_data.into_iter()
926 .map(|data| { data.len() })929 .map(|data| { data.len() })
927 .sum())]930 .sum())]
928 pub fn create_multiple_items(origin, collection_id: u64, owner: T::AccountId, items_data: Vec<CreateItemData>) -> DispatchResult {931 pub fn create_multiple_items(origin, collection_id: CollectionId, owner: T::AccountId, items_data: Vec<CreateItemData>) -> DispatchResult {
929932
930 ensure!(items_data.len() > 0, Error::<T>::EmptyArgument);933 ensure!(items_data.len() > 0, Error::<T>::EmptyArgument);
931 let sender = ensure_signed(origin)?;934 let sender = ensure_signed(origin)?;
959 /// 962 ///
960 /// * item_id: ID of NFT to burn.963 /// * item_id: ID of NFT to burn.
961 #[weight = T::WeightInfo::burn_item()]964 #[weight = T::WeightInfo::burn_item()]
962 pub fn burn_item(origin, collection_id: u64, item_id: u64) -> DispatchResult {965 pub fn burn_item(origin, collection_id: CollectionId, item_id: TokenId) -> DispatchResult {
963966
964 let sender = ensure_signed(origin)?;967 let sender = ensure_signed(origin)?;
965 Self::collection_exists(collection_id)?;968 Self::collection_exists(collection_id)?;
1012 /// * Fungible Mode: Must specify transferred amount1015 /// * Fungible Mode: Must specify transferred amount
1013 /// * Re-Fungible Mode: Must specify transferred portion (between 0 and 1)1016 /// * Re-Fungible Mode: Must specify transferred portion (between 0 and 1)
1014 #[weight = T::WeightInfo::transfer()]1017 #[weight = T::WeightInfo::transfer()]
1015 pub fn transfer(origin, recipient: T::AccountId, collection_id: u64, item_id: u64, value: u64) -> DispatchResult {1018 pub fn transfer(origin, recipient: T::AccountId, collection_id: CollectionId, item_id: TokenId, value: u64) -> DispatchResult {
10161019
1017 let sender = ensure_signed(origin)?;1020 let sender = ensure_signed(origin)?;
10181021
1054 /// 1057 ///
1055 /// * item_id: ID of the item.1058 /// * item_id: ID of the item.
1056 #[weight = T::WeightInfo::approve()]1059 #[weight = T::WeightInfo::approve()]
1057 pub fn approve(origin, approved: T::AccountId, collection_id: u64, item_id: u64) -> DispatchResult {1060 pub fn approve(origin, approved: T::AccountId, collection_id: CollectionId, item_id: TokenId) -> DispatchResult {
10581061
1059 let sender = ensure_signed(origin)?;1062 let sender = ensure_signed(origin)?;
10601063
1112 /// 1115 ///
1113 /// * value: Amount to transfer.1116 /// * value: Amount to transfer.
1114 #[weight = T::WeightInfo::transfer_from()]1117 #[weight = T::WeightInfo::transfer_from()]
1115 pub fn transfer_from(origin, from: T::AccountId, recipient: T::AccountId, collection_id: u64, item_id: u64, value: u64 ) -> DispatchResult {1118 pub fn transfer_from(origin, from: T::AccountId, recipient: T::AccountId, collection_id: CollectionId, item_id: TokenId, value: u64 ) -> DispatchResult {
11161119
1117 let sender = ensure_signed(origin)?;1120 let sender = ensure_signed(origin)?;
1118 let mut appoved_transfer = false;1121 let mut appoved_transfer = false;
11571160
1158 ///1161 ///
1159 #[weight = 0]1162 #[weight = 0]
1160 pub fn safe_transfer_from(origin, collection_id: u64, item_id: u64, new_owner: T::AccountId) -> DispatchResult {1163 pub fn safe_transfer_from(origin, collection_id: CollectionId, item_id: TokenId, new_owner: T::AccountId) -> DispatchResult {
11611164
1162 // let no_perm_mes = "You do not have permissions to modify this collection";1165 // let no_perm_mes = "You do not have permissions to modify this collection";
1163 // ensure!(<ApprovedList<T>>::contains_key((collection_id, item_id)), no_perm_mes);1166 // ensure!(<ApprovedList<T>>::contains_key((collection_id, item_id)), no_perm_mes);
1186 #[weight = T::WeightInfo::set_variable_meta_data()]1189 #[weight = T::WeightInfo::set_variable_meta_data()]
1187 pub fn set_variable_meta_data (1190 pub fn set_variable_meta_data (
1188 origin,1191 origin,
1189 collection_id: u64,1192 collection_id: CollectionId,
1190 item_id: u64,1193 item_id: TokenId,
1191 data: Vec<u8>1194 data: Vec<u8>
1192 ) -> DispatchResult {1195 ) -> DispatchResult {
1193 let sender = ensure_signed(origin)?;1196 let sender = ensure_signed(origin)?;
1231 #[weight = T::WeightInfo::set_offchain_schema()]1234 #[weight = T::WeightInfo::set_offchain_schema()]
1232 pub fn set_offchain_schema(1235 pub fn set_offchain_schema(
1233 origin,1236 origin,
1234 collection_id: u64,1237 collection_id: CollectionId,
1235 schema: Vec<u8>1238 schema: Vec<u8>
1236 ) -> DispatchResult {1239 ) -> DispatchResult {
1237 let sender = ensure_signed(origin)?;1240 let sender = ensure_signed(origin)?;
1259 #[weight = T::WeightInfo::set_const_on_chain_schema()]1262 #[weight = T::WeightInfo::set_const_on_chain_schema()]
1260 pub fn set_const_on_chain_schema (1263 pub fn set_const_on_chain_schema (
1261 origin,1264 origin,
1262 collection_id: u64,1265 collection_id: CollectionId,
1263 schema: Vec<u8>1266 schema: Vec<u8>
1264 ) -> DispatchResult {1267 ) -> DispatchResult {
1265 let sender = ensure_signed(origin)?;1268 let sender = ensure_signed(origin)?;
1287 #[weight = T::WeightInfo::set_const_on_chain_schema()]1290 #[weight = T::WeightInfo::set_const_on_chain_schema()]
1288 pub fn set_variable_on_chain_schema (1291 pub fn set_variable_on_chain_schema (
1289 origin,1292 origin,
1290 collection_id: u64,1293 collection_id: CollectionId,
1291 schema: Vec<u8>1294 schema: Vec<u8>
1292 ) -> DispatchResult {1295 ) -> DispatchResult {
1293 let sender = ensure_signed(origin)?;1296 let sender = ensure_signed(origin)?;
13961399
1397impl<T: Trait> Module<T> {1400impl<T: Trait> Module<T> {
13981401
1399 fn can_create_items_in_collection(collection_id: u64, collection: &CollectionType<T::AccountId>, sender: &T::AccountId, owner: &T::AccountId) -> DispatchResult {1402 fn can_create_items_in_collection(collection_id: CollectionId, collection: &CollectionType<T::AccountId>, sender: &T::AccountId, owner: &T::AccountId) -> DispatchResult {
14001403
1401 if !Self::is_owner_or_admin_permissions(collection_id, sender.clone()) {1404 if !Self::is_owner_or_admin_permissions(collection_id, sender.clone()) {
1402 ensure!(collection.mint_mode == true, Error::<T>::PublicMintingNotAllowed);1405 ensure!(collection.mint_mode == true, Error::<T>::PublicMintingNotAllowed);
1441 Ok(())1444 Ok(())
1442 }1445 }
14431446
1444 fn create_item_no_validation(collection_id: u64, collection: &CollectionType<T::AccountId>, owner: T::AccountId, data: CreateItemData) -> DispatchResult {1447 fn create_item_no_validation(collection_id: CollectionId, collection: &CollectionType<T::AccountId>, owner: T::AccountId, data: CreateItemData) -> DispatchResult {
1445 match data1448 match data
1446 {1449 {
1447 CreateItemData::NFT(data) => {1450 CreateItemData::NFT(data) => {
1565 }1568 }
15661569
1567 fn burn_refungible_item(1570 fn burn_refungible_item(
1568 collection_id: u64,1571 collection_id: CollectionId,
1569 item_id: u64,1572 item_id: TokenId,
1570 owner: T::AccountId,1573 owner: T::AccountId,
1571 ) -> DispatchResult {1574 ) -> DispatchResult {
1572 ensure!(1575 ensure!(
1596 Ok(())1599 Ok(())
1597 }1600 }
15981601
1599 fn burn_nft_item(collection_id: u64, item_id: u64) -> DispatchResult {1602 fn burn_nft_item(collection_id: CollectionId, item_id: TokenId) -> DispatchResult {
1600 ensure!(1603 ensure!(
1601 <NftItemList<T>>::contains_key(collection_id, item_id),1604 <NftItemList<T>>::contains_key(collection_id, item_id),
1602 Error::<T>::TokenNotFound1605 Error::<T>::TokenNotFound
1617 Ok(())1620 Ok(())
1618 }1621 }
16191622
1620 fn burn_fungible_item(collection_id: u64, item_id: u64) -> DispatchResult {1623 fn burn_fungible_item(collection_id: CollectionId, item_id: TokenId) -> DispatchResult {
1621 ensure!(1624 ensure!(
1622 <FungibleItemList<T>>::contains_key(collection_id, item_id),1625 <FungibleItemList<T>>::contains_key(collection_id, item_id),
1623 Error::<T>::TokenNotFound1626 Error::<T>::TokenNotFound
1639 Ok(())1642 Ok(())
1640 }1643 }
16411644
1642 fn collection_exists(collection_id: u64) -> DispatchResult {1645 fn collection_exists(collection_id: CollectionId) -> DispatchResult {
1643 ensure!(1646 ensure!(
1644 <Collection<T>>::contains_key(collection_id),1647 <Collection<T>>::contains_key(collection_id),
1645 Error::<T>::CollectionNotFound1648 Error::<T>::CollectionNotFound
1646 );1649 );
1647 Ok(())1650 Ok(())
1648 }1651 }
16491652
1650 fn check_owner_permissions(collection_id: u64, subject: T::AccountId) -> DispatchResult {1653 fn check_owner_permissions(collection_id: CollectionId, subject: T::AccountId) -> DispatchResult {
1651 Self::collection_exists(collection_id)?;1654 Self::collection_exists(collection_id)?;
16521655
1653 let target_collection = <Collection<T>>::get(collection_id);1656 let target_collection = <Collection<T>>::get(collection_id);
1659 Ok(())1662 Ok(())
1660 }1663 }
16611664
1662 fn is_owner_or_admin_permissions(collection_id: u64, subject: T::AccountId) -> bool {1665 fn is_owner_or_admin_permissions(collection_id: CollectionId, subject: T::AccountId) -> bool {
1663 let target_collection = <Collection<T>>::get(collection_id);1666 let target_collection = <Collection<T>>::get(collection_id);
1664 let mut result: bool = subject == target_collection.owner;1667 let mut result: bool = subject == target_collection.owner;
1665 let exists = <AdminList<T>>::contains_key(collection_id);1668 let exists = <AdminList<T>>::contains_key(collection_id);
1674 }1677 }
16751678
1676 fn check_owner_or_admin_permissions(1679 fn check_owner_or_admin_permissions(
1677 collection_id: u64,1680 collection_id: CollectionId,
1678 subject: T::AccountId,1681 subject: T::AccountId,
1679 ) -> DispatchResult {1682 ) -> DispatchResult {
1680 Self::collection_exists(collection_id)?;1683 Self::collection_exists(collection_id)?;
1687 Ok(())1690 Ok(())
1688 }1691 }
16891692
1690 fn is_item_owner(subject: T::AccountId, collection_id: u64, item_id: u64) -> bool {1693 fn is_item_owner(subject: T::AccountId, collection_id: CollectionId, item_id: TokenId) -> bool {
1691 let target_collection = <Collection<T>>::get(collection_id);1694 let target_collection = <Collection<T>>::get(collection_id);
16921695
1693 match target_collection.mode {1696 match target_collection.mode {
1707 }1710 }
1708 }1711 }
17091712
1710 fn check_white_list(collection_id: u64, address: &T::AccountId) -> DispatchResult {1713 fn check_white_list(collection_id: CollectionId, address: &T::AccountId) -> DispatchResult {
1711 let mes = Error::<T>::AddresNotInWhiteList;1714 let mes = Error::<T>::AddresNotInWhiteList;
1712 ensure!(<WhiteList<T>>::contains_key(collection_id), mes);1715 ensure!(<WhiteList<T>>::contains_key(collection_id), mes);
1713 let wl = <WhiteList<T>>::get(collection_id);1716 let wl = <WhiteList<T>>::get(collection_id);
1717 }1720 }
17181721
1719 fn transfer_fungible(1722 fn transfer_fungible(
1720 collection_id: u64,1723 collection_id: CollectionId,
1721 item_id: u64,1724 item_id: TokenId,
1722 value: u64,1725 value: u64,
1723 owner: T::AccountId,1726 owner: T::AccountId,
1724 new_owner: T::AccountId,1727 new_owner: T::AccountId,
1806 }1809 }
18071810
1808 fn transfer_refungible(1811 fn transfer_refungible(
1809 collection_id: u64,1812 collection_id: CollectionId,
1810 item_id: u64,1813 item_id: TokenId,
1811 value: u64,1814 value: u64,
1812 owner: T::AccountId,1815 owner: T::AccountId,
1813 new_owner: T::AccountId,1816 new_owner: T::AccountId,
1892 }1895 }
18931896
1894 fn transfer_nft(1897 fn transfer_nft(
1895 collection_id: u64,1898 collection_id: CollectionId,
1896 item_id: u64,1899 item_id: TokenId,
1897 sender: T::AccountId,1900 sender: T::AccountId,
1898 new_owner: T::AccountId,1901 new_owner: T::AccountId,
1899 ) -> DispatchResult {1902 ) -> DispatchResult {
1934 }1937 }
1935 1938
1936 fn item_exists(1939 fn item_exists(
1937 collection_id: u64,1940 collection_id: CollectionId,
1938 item_id: u64,1941 item_id: TokenId,
1939 mode: &CollectionMode1942 mode: &CollectionMode
1940 ) -> DispatchResult {1943 ) -> DispatchResult {
1941 match mode {1944 match mode {
1949 }1952 }
19501953
1951 fn set_re_fungible_variable_data(1954 fn set_re_fungible_variable_data(
1952 collection_id: u64,1955 collection_id: CollectionId,
1953 item_id: u64,1956 item_id: TokenId,
1954 data: Vec<u8>1957 data: Vec<u8>
1955 ) -> DispatchResult {1958 ) -> DispatchResult {
1956 let mut item = <ReFungibleItemList<T>>::get(collection_id, item_id);1959 let mut item = <ReFungibleItemList<T>>::get(collection_id, item_id);
1963 }1966 }
19641967
1965 fn set_nft_variable_data(1968 fn set_nft_variable_data(
1966 collection_id: u64,1969 collection_id: CollectionId,
1967 item_id: u64,1970 item_id: TokenId,
1968 data: Vec<u8>1971 data: Vec<u8>
1969 ) -> DispatchResult {1972 ) -> DispatchResult {
1970 let mut item = <NftItemList<T>>::get(collection_id, item_id);1973 let mut item = <NftItemList<T>>::get(collection_id, item_id);
2058 <Balance<T>>::insert(item.collection, owner.clone(), new_balance);2061 <Balance<T>>::insert(item.collection, owner.clone(), new_balance);
2059 }2062 }
20602063
2061 fn add_token_index(collection_id: u64, item_index: u64, owner: T::AccountId) -> DispatchResult {2064 fn add_token_index(collection_id: CollectionId, item_index: TokenId, owner: T::AccountId) -> DispatchResult {
20622065
2063 // add to account limit2066 // add to account limit
2064 if <AccountItemCount<T>>::contains_key(owner.clone()) {2067 if <AccountItemCount<T>>::contains_key(owner.clone()) {
2096 }2099 }
20972100
2098 fn remove_token_index(2101 fn remove_token_index(
2099 collection_id: u64,2102 collection_id: CollectionId,
2100 item_index: u64,2103 item_index: TokenId,
2101 owner: T::AccountId,2104 owner: T::AccountId,
2102 ) -> DispatchResult {2105 ) -> DispatchResult {
21032106
2123 }2126 }
21242127
2125 fn move_token_index(2128 fn move_token_index(
2126 collection_id: u64,2129 collection_id: CollectionId,
2127 item_index: u64,2130 item_index: TokenId,
2128 old_owner: T::AccountId,2131 old_owner: T::AccountId,
2129 new_owner: T::AccountId,2132 new_owner: T::AccountId,
2130 ) -> DispatchResult {2133 ) -> DispatchResult {
modifiedpallets/nft/src/tests.rsdiffbeforeafterboth
2use super::*;2use super::*;
3use crate::mock::*;3use crate::mock::*;
4use crate::{AccessMode, ApprovePermissions, CollectionMode,4use crate::{AccessMode, ApprovePermissions, CollectionMode,
5 Ownership, ChainLimits, CreateItemData, CreateNftData, CreateFungibleData, CreateReFungibleData}; //Err5 Ownership, ChainLimits, CreateItemData, CreateNftData, CreateFungibleData, CreateReFungibleData,
6 CollectionId, TokenId}; //Err
6use frame_support::{assert_noop, assert_ok};7use frame_support::{assert_noop, assert_ok};
7use frame_system::{ RawOrigin };8use frame_system::{ RawOrigin };
89
9fn default_collection_numbers_limit() -> u64 {10fn default_collection_numbers_limit() -> u32 {
10 1011 10
11}12}
1213
34 CreateReFungibleData { const_data: vec![1, 2, 3], variable_data: vec![3, 2, 1] }35 CreateReFungibleData { const_data: vec![1, 2, 3], variable_data: vec![3, 2, 1] }
35}36}
3637
37fn create_test_collection_for_owner(mode: &CollectionMode, owner: u64, id: u64) -> u64 {38fn create_test_collection_for_owner(mode: &CollectionMode, owner: u64, id: CollectionId) -> CollectionId {
38 let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();39 let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();
39 let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();40 let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();
40 let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();41 let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();
59 id60 id
60}61}
6162
62fn create_test_collection(mode: &CollectionMode, id: u64) -> u64 {63fn create_test_collection(mode: &CollectionMode, id: CollectionId) -> CollectionId {
63 create_test_collection_for_owner(&mode, 1, id)64 create_test_collection_for_owner(&mode, 1, id)
64}65}
6566
66fn create_test_item(collection_id: u64, data: &CreateItemData) {67fn create_test_item(collection_id: CollectionId, data: &CreateItemData) {
67 let origin1 = Origin::signed(1);68 let origin1 = Origin::signed(1);
68 assert_ok!(TemplateModule::create_item(69 assert_ok!(TemplateModule::create_item(
69 origin1.clone(),70 origin1.clone(),
109 items_data.clone().into_iter().map(|d| { d.into() }).collect()110 items_data.clone().into_iter().map(|d| { d.into() }).collect()
110 ));111 ));
111 for (index, data) in items_data.iter().enumerate() {112 for (index, data) in items_data.iter().enumerate() {
112 assert_eq!(TemplateModule::nft_item_id(1, (index + 1) as u64).const_data.to_vec(), data.const_data);113 assert_eq!(TemplateModule::nft_item_id(1, (index + 1) as TokenId).const_data.to_vec(), data.const_data);
113 assert_eq!(TemplateModule::nft_item_id(1, (index + 1) as u64).variable_data.to_vec(), data.variable_data);114 assert_eq!(TemplateModule::nft_item_id(1, (index + 1) as TokenId).variable_data.to_vec(), data.variable_data);
114 }115 }
115 });116 });
116}117}
160 ));161 ));
161 for (index, data) in items_data.iter().enumerate() {162 for (index, data) in items_data.iter().enumerate() {
162163
163 let item = TemplateModule::refungible_item_id(1, (index + 1) as u64);164 let item = TemplateModule::refungible_item_id(1, (index + 1) as TokenId);
164 assert_eq!(item.const_data.to_vec(), data.const_data);165 assert_eq!(item.const_data.to_vec(), data.const_data);
165 assert_eq!(item.variable_data.to_vec(), data.variable_data);166 assert_eq!(item.variable_data.to_vec(), data.variable_data);
166 assert_eq!(167 assert_eq!(
207 ));208 ));
208 209
209 for (index, _) in items_data.iter().enumerate() {210 for (index, _) in items_data.iter().enumerate() {
210 assert_eq!(TemplateModule::fungible_item_id(1, (index + 1) as u64).owner, 1);211 assert_eq!(TemplateModule::fungible_item_id(1, (index + 1) as TokenId).owner, 1);
211 }212 }
212 assert_eq!(TemplateModule::balance_count(1, 1), 3000);213 assert_eq!(TemplateModule::balance_count(1, 1), 3000);
213 assert_eq!(TemplateModule::address_tokens(1, 1), [1, 2, 3]);214 assert_eq!(TemplateModule::address_tokens(1, 1), [1, 2, 3]);