git.delta.rocks / unique-network / refs/commits / 71e3eed44117

difftreelog

Merge pull request #31 from usetech-llc/feature/NFTPAR-183

usetech-llc2020-12-11parents: #68e6834 #fc4b45c.patch.diff
in: master
Feature/NFTPAR-183 Storage Refactoring.

3 files changed

modifiedREADME.mddiffbeforeafterboth
164 "WhiteList"164 "WhiteList"
165 ]165 ]
166 },166 },
167 "DecimalPoints": "u8",
167 "CollectionMode": {168 "CollectionMode": {
168 "_enum": {169 "_enum": {
169 "Invalid": null,170 "Invalid": null,
170 "NFT": null,171 "NFT": null,
171 "Fungible": "u32",172 "Fungible": "DecimalPoints",
172 "ReFungible": "u32"173 "ReFungible": "DecimalPoints"
173 }174 }
174 },175 },
175 "Ownership": {176 "Ownership": {
176 "Owner": "AccountId",177 "Owner": "AccountId",
177 "Fraction": "u128"178 "Fraction": "u128"
178 },179 },
179 "FungibleItemType": {180 "FungibleItemType": {
180 "Collection": "u64",181 "Collection": "CollectionId",
181 "Owner": "AccountId",182 "Owner": "AccountId",
182 "Value": "u128"183 "Value": "u128"
183 },184 },
184 "ReFungibleItemType": {185 "ReFungibleItemType": {
185 "Collection": "u64",186 "Collection": "CollectionId",
186 "Owner": "Vec<Ownership>",187 "Owner": "Vec<Ownership>",
187 "Data": "Vec<u8>"188 "Data": "Vec<u8>"
188 },189 },
189 "NftItemType": {190 "NftItemType": {
190 "Collection": "u64",191 "Collection": "CollectionId",
191 "Owner": "AccountId",192 "Owner": "AccountId",
192 "ConstData": "Vec<u8>",193 "ConstData": "Vec<u8>",
193 "VariableData": "Vec<u8>"194 "VariableData": "Vec<u8>"
197 "fraction": "u128"198 "fraction": "u128"
198 },199 },
199 "ReFungibleItemType": {200 "ReFungibleItemType": {
200 "Collection": "u64",201 "Collection": "CollectionId",
201 "Owner": "Vec<Ownership<AccountId>>",202 "Owner": "Vec<Ownership<AccountId>>",
202 "ConstData": "Vec<u8>",203 "ConstData": "Vec<u8>",
203 "VariableData": "Vec<u8>"204 "VariableData": "Vec<u8>"
206 "Owner": "AccountId",207 "Owner": "AccountId",
207 "Mode": "CollectionMode",208 "Mode": "CollectionMode",
208 "Access": "AccessMode",209 "Access": "AccessMode",
209 "DecimalPoints": "u32",210 "DecimalPoints": "DecimalPoints",
210 "Name": "Vec<u16>",211 "Name": "Vec<u16>",
211 "Description": "Vec<u16>",212 "Description": "Vec<u16>",
212 "TokenPrefix": "Vec<u8>",213 "TokenPrefix": "Vec<u8>",
219 },220 },
220 "ApprovePermissions": {221 "ApprovePermissions": {
221 "Approved": "AccountId",222 "Approved": "AccountId",
222 "Amount": "u64"223 "Amount": "u128"
223 },224 },
224 "RawData": "Vec<u8>",225 "RawData": "Vec<u8>",
225 "Address": "AccountId",226 "Address": "AccountId",
240 "Fungible": "CreateFungibleData",241 "Fungible": "CreateFungibleData",
241 "ReFungible": "CreateReFungibleData"242 "ReFungible": "CreateReFungibleData"
242 }243 }
243 }244 },
245 "CollectionId": "u32",
246 "TokenId": "u32"
244}247}
245248
246```249```
modifiedpallets/nft/src/lib.rsdiffbeforeafterboth
4545
46mod default_weights;46mod default_weights;
47
48pub const MAX_DECIMAL_POINTS: DecimalPoints = 30;
4749
48// Structs50// Structs
49// #region51// #region
52
53pub type CollectionId = u32;
54pub type TokenId = u32;
55
56pub type DecimalPoints = u8;
5057
51#[derive(Encode, Decode, Eq, Debug, Clone, PartialEq)]58#[derive(Encode, Decode, Eq, Debug, Clone, PartialEq)]
52#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]59#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
53pub enum CollectionMode {60pub enum CollectionMode {
54 Invalid,61 Invalid,
55 NFT,62 NFT,
56 // decimal points63 // decimal points
57 Fungible(u32),64 Fungible(DecimalPoints),
58 // decimal points65 // decimal points
59 ReFungible(u32),66 ReFungible(DecimalPoints),
60}67}
6168
62impl Into<u8> for CollectionMode {69impl Into<u8> for CollectionMode {
101 pub owner: AccountId,108 pub owner: AccountId,
102 pub mode: CollectionMode,109 pub mode: CollectionMode,
103 pub access: AccessMode,110 pub access: AccessMode,
104 pub decimal_points: u32,111 pub decimal_points: DecimalPoints,
105 pub name: Vec<u16>, // 64 include null escape char112 pub name: Vec<u16>, // 64 include null escape char
106 pub description: Vec<u16>, // 256 include null escape char113 pub description: Vec<u16>, // 256 include null escape char
107 pub token_prefix: Vec<u8>, // 16 include null escape char114 pub token_prefix: Vec<u8>, // 16 include null escape char
116#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]123#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]
117#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]124#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
118pub struct NftItemType<AccountId> {125pub struct NftItemType<AccountId> {
119 pub collection: u64,126 pub collection: CollectionId,
120 pub owner: AccountId,127 pub owner: AccountId,
121 pub const_data: Vec<u8>,128 pub const_data: Vec<u8>,
122 pub variable_data: Vec<u8>,129 pub variable_data: Vec<u8>,
125#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]132#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]
126#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]133#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
127pub struct FungibleItemType<AccountId> {134pub struct FungibleItemType<AccountId> {
128 pub collection: u64,135 pub collection: CollectionId,
129 pub owner: AccountId,136 pub owner: AccountId,
130 pub value: u128,137 pub value: u128,
131}138}
132139
133#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]140#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]
134#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]141#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
135pub struct ReFungibleItemType<AccountId> {142pub struct ReFungibleItemType<AccountId> {
136 pub collection: u64,143 pub collection: CollectionId,
137 pub owner: Vec<Ownership<AccountId>>,144 pub owner: Vec<Ownership<AccountId>>,
138 pub const_data: Vec<u8>,145 pub const_data: Vec<u8>,
139 pub variable_data: Vec<u8>,146 pub variable_data: Vec<u8>,
143#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]150#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
144pub struct ApprovePermissions<AccountId> {151pub struct ApprovePermissions<AccountId> {
145 pub approved: AccountId,152 pub approved: AccountId,
146 pub amount: u64,153 pub amount: u128,
147}154}
148155
149#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]156#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]
150#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]157#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
151pub struct VestingItem<AccountId, Moment> {158pub struct VestingItem<AccountId, Moment> {
152 pub sender: AccountId,159 pub sender: AccountId,
153 pub recipient: AccountId,160 pub recipient: AccountId,
154 pub collection_id: u64,161 pub collection_id: CollectionId,
155 pub item_id: u64,162 pub item_id: TokenId,
156 pub amount: u64,163 pub amount: u64,
157 pub vesting_date: Moment,164 pub vesting_date: Moment,
158}165}
167#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]174#[derive(Encode, Decode, Default, Debug, Clone, PartialEq)]
168#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]175#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
169pub struct ChainLimits {176pub struct ChainLimits {
170 pub collection_numbers_limit: u64,177 pub collection_numbers_limit: u32,
171 pub account_token_ownership_limit: u64,178 pub account_token_ownership_limit: u32,
172 pub collections_admins_limit: u64,179 pub collections_admins_limit: u64,
173 pub custom_data_limit: u32,180 pub custom_data_limit: u32,
174181
266 pub enum Error for Module<T: Trait> {273 pub enum Error for Module<T: Trait> {
267 /// Total collections bound exceeded.274 /// Total collections bound exceeded.
268 TotalCollectionsLimitExceeded,275 TotalCollectionsLimitExceeded,
269 /// Decimal_points parameter must be lower than 4.276 /// Decimal_points parameter must be lower than MAX_DECIMAL_POINTS constant, currently it is 30.
270 CollectionDecimalPointLimitExceeded, 277 CollectionDecimalPointLimitExceeded,
271 /// Collection name can not be longer than 63 char.278 /// Collection name can not be longer than 63 char.
272 CollectionNameLimitExceeded, 279 CollectionNameLimitExceeded,
341 trait Store for Module<T: Trait> as Nft {348 trait Store for Module<T: Trait> as Nft {
342349
343 // Private members350 // Private members
344 NextCollectionID: u64;351 NextCollectionID: CollectionId;
345 CreatedCollectionCount: u64;352 CreatedCollectionCount: u32;
346 ChainVersion: u64;353 ChainVersion: u64;
347 ItemListIndex: map hasher(blake2_128_concat) u64 => u64;354 ItemListIndex: map hasher(identity) CollectionId => TokenId;
348355
349 // Chain limits struct356 // Chain limits struct
350 pub ChainLimit get(fn chain_limit) config(): ChainLimits;357 pub ChainLimit get(fn chain_limit) config(): ChainLimits;
351358
352 // Bound counters359 // Bound counters
353 CollectionCount: u64;360 CollectionCount: u32;
354 pub AccountItemCount get(fn account_item_count): map hasher(identity) T::AccountId => u64;361 pub AccountItemCount get(fn account_item_count): map hasher(twox_64_concat) T::AccountId => u32;
355362
356 // Basic collections363 // Basic collections
357 pub Collection get(fn collection) config(): map hasher(identity) u64 => CollectionType<T::AccountId>;364 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>;365 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>;366 pub WhiteList get(fn white_list): map hasher(identity) CollectionId => Vec<T::AccountId>;
360367
361 /// Balance owner per collection map368 /// 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;369 pub Balance get(fn balance_count): double_map hasher(identity) CollectionId, hasher(twox_64_concat) T::AccountId => u128;
363370
364 /// second parameter: item id + owner account id371 /// 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>>;372 pub ApprovedList get(fn approved): double_map hasher(identity) CollectionId, hasher(twox_64_concat) (TokenId, T::AccountId) => Vec<ApprovePermissions<T::AccountId>>;
366373
367 /// Item collections374 /// 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>;375 pub NftItemList get(fn nft_item_id) config(): double_map hasher(identity) CollectionId, hasher(identity) 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>;376 pub FungibleItemList get(fn fungible_item_id) config(): double_map hasher(identity) CollectionId, hasher(identity) 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>;377 pub ReFungibleItemList get(fn refungible_item_id) config(): double_map hasher(identity) CollectionId, hasher(identity) TokenId => ReFungibleItemType<T::AccountId>;
371378
372 /// Index list379 /// Index list
373 pub AddressTokens get(fn address_tokens): double_map hasher(blake2_128_concat) u64, hasher(blake2_128_concat) T::AccountId => Vec<u64>;380 pub AddressTokens get(fn address_tokens): double_map hasher(identity) CollectionId, hasher(twox_64_concat) T::AccountId => Vec<TokenId>;
374381
375 /// Tokens transfer baskets382 /// 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;383 pub NftTransferBasket get(fn nft_transfer_basket): double_map hasher(identity) CollectionId, hasher(identity) 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>>;384 pub FungibleTransferBasket get(fn fungible_transfer_basket): double_map hasher(identity) CollectionId, hasher(identity) 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;385 pub ReFungibleTransferBasket get(fn refungible_transfer_basket): double_map hasher(identity) CollectionId, hasher(identity) TokenId => T::BlockNumber;
379386
380 // Contract Sponsorship and Ownership387 // Contract Sponsorship and Ownership
381 pub ContractOwner get(fn contract_owner): map hasher(twox_64_concat) T::AccountId => T::AccountId;388 pub ContractOwner get(fn contract_owner): map hasher(twox_64_concat) T::AccountId => T::AccountId;
419 /// * mode: [CollectionMode] converted into u8.426 /// * mode: [CollectionMode] converted into u8.
420 /// 427 ///
421 /// * account_id: Collection owner.428 /// * account_id: Collection owner.
422 Created(u64, u8, AccountId),429 Created(CollectionId, u8, AccountId),
423430
424 /// New item was created.431 /// New item was created.
425 /// 432 ///
428 /// * collection_id: Id of the collection where item was created.435 /// * collection_id: Id of the collection where item was created.
429 /// 436 ///
430 /// * item_id: Id of an item. Unique within the collection.437 /// * item_id: Id of an item. Unique within the collection.
431 ItemCreated(u64, u64),438 ItemCreated(CollectionId, TokenId),
432439
433 /// Collection item was burned.440 /// Collection item was burned.
434 /// 441 ///
437 /// collection_id.444 /// collection_id.
438 /// 445 ///
439 /// item_id: Identifier of burned NFT.446 /// item_id: Identifier of burned NFT.
440 ItemDestroyed(u64, u64),447 ItemDestroyed(CollectionId, TokenId),
441 }448 }
442);449);
443450
495 ensure!(CollectionCount::get() < ChainLimit::get().collection_numbers_limit, Error::<T>::TotalCollectionsLimitExceeded);502 ensure!(CollectionCount::get() < ChainLimit::get().collection_numbers_limit, Error::<T>::TotalCollectionsLimitExceeded);
496503
497 // check params504 // check params
498 ensure!(decimal_points <= 4, Error::<T>::CollectionDecimalPointLimitExceeded);505 ensure!(decimal_points <= MAX_DECIMAL_POINTS, Error::<T>::CollectionDecimalPointLimitExceeded);
499506
500 let mut name = collection_name.to_vec();507 let mut name = collection_name.to_vec();
501 name.push(0);508 name.push(0);
558 /// 565 ///
559 /// * collection_id: collection to destroy.566 /// * collection_id: collection to destroy.
560 #[weight = T::WeightInfo::destroy_collection()]567 #[weight = T::WeightInfo::destroy_collection()]
561 pub fn destroy_collection(origin, collection_id: u64) -> DispatchResult {568 pub fn destroy_collection(origin, collection_id: CollectionId) -> DispatchResult {
562569
563 let sender = ensure_signed(origin)?;570 let sender = ensure_signed(origin)?;
564 Self::check_owner_permissions(collection_id, sender)?;571 Self::check_owner_permissions(collection_id, sender)?;
605 /// 612 ///
606 /// * address.613 /// * address.
607 #[weight = T::WeightInfo::add_to_white_list()]614 #[weight = T::WeightInfo::add_to_white_list()]
608 pub fn add_to_white_list(origin, collection_id: u64, address: T::AccountId) -> DispatchResult{615 pub fn add_to_white_list(origin, collection_id: CollectionId, address: T::AccountId) -> DispatchResult{
609616
610 let sender = ensure_signed(origin)?;617 let sender = ensure_signed(origin)?;
611 Self::check_owner_or_admin_permissions(collection_id, sender)?;618 Self::check_owner_or_admin_permissions(collection_id, sender)?;
640 /// 647 ///
641 /// * address.648 /// * address.
642 #[weight = T::WeightInfo::remove_from_white_list()]649 #[weight = T::WeightInfo::remove_from_white_list()]
643 pub fn remove_from_white_list(origin, collection_id: u64, address: T::AccountId) -> DispatchResult{650 pub fn remove_from_white_list(origin, collection_id: CollectionId, address: T::AccountId) -> DispatchResult{
644651
645 let sender = ensure_signed(origin)?;652 let sender = ensure_signed(origin)?;
646 Self::check_owner_or_admin_permissions(collection_id, sender)?;653 Self::check_owner_or_admin_permissions(collection_id, sender)?;
669 /// 676 ///
670 /// * mode: [AccessMode]677 /// * mode: [AccessMode]
671 #[weight = T::WeightInfo::set_public_access_mode()]678 #[weight = T::WeightInfo::set_public_access_mode()]
672 pub fn set_public_access_mode(origin, collection_id: u64, mode: AccessMode) -> DispatchResult679 pub fn set_public_access_mode(origin, collection_id: CollectionId, mode: AccessMode) -> DispatchResult
673 {680 {
674 let sender = ensure_signed(origin)?;681 let sender = ensure_signed(origin)?;
675682
695 /// 702 ///
696 /// * mint_permission: Boolean parameter. If True, allows minting to Anyone with conditions above.703 /// * mint_permission: Boolean parameter. If True, allows minting to Anyone with conditions above.
697 #[weight = T::WeightInfo::set_mint_permission()]704 #[weight = T::WeightInfo::set_mint_permission()]
698 pub fn set_mint_permission(origin, collection_id: u64, mint_permission: bool) -> DispatchResult705 pub fn set_mint_permission(origin, collection_id: CollectionId, mint_permission: bool) -> DispatchResult
699 {706 {
700 let sender = ensure_signed(origin)?;707 let sender = ensure_signed(origin)?;
701708
719 /// 726 ///
720 /// * new_owner.727 /// * new_owner.
721 #[weight = T::WeightInfo::change_collection_owner()]728 #[weight = T::WeightInfo::change_collection_owner()]
722 pub fn change_collection_owner(origin, collection_id: u64, new_owner: T::AccountId) -> DispatchResult {729 pub fn change_collection_owner(origin, collection_id: CollectionId, new_owner: T::AccountId) -> DispatchResult {
723730
724 let sender = ensure_signed(origin)?;731 let sender = ensure_signed(origin)?;
725 Self::check_owner_permissions(collection_id, sender)?;732 Self::check_owner_permissions(collection_id, sender)?;
744 /// 751 ///
745 /// * new_admin_id: Address of new admin to add.752 /// * new_admin_id: Address of new admin to add.
746 #[weight = T::WeightInfo::add_collection_admin()]753 #[weight = T::WeightInfo::add_collection_admin()]
747 pub fn add_collection_admin(origin, collection_id: u64, new_admin_id: T::AccountId) -> DispatchResult {754 pub fn add_collection_admin(origin, collection_id: CollectionId, new_admin_id: T::AccountId) -> DispatchResult {
748755
749 let sender = ensure_signed(origin)?;756 let sender = ensure_signed(origin)?;
750 Self::check_owner_or_admin_permissions(collection_id, sender)?;757 Self::check_owner_or_admin_permissions(collection_id, sender)?;
778 /// 785 ///
779 /// * account_id: Address of admin to remove.786 /// * account_id: Address of admin to remove.
780 #[weight = T::WeightInfo::remove_collection_admin()]787 #[weight = T::WeightInfo::remove_collection_admin()]
781 pub fn remove_collection_admin(origin, collection_id: u64, account_id: T::AccountId) -> DispatchResult {788 pub fn remove_collection_admin(origin, collection_id: CollectionId, account_id: T::AccountId) -> DispatchResult {
782789
783 let sender = ensure_signed(origin)?;790 let sender = ensure_signed(origin)?;
784 Self::check_owner_or_admin_permissions(collection_id, sender)?;791 Self::check_owner_or_admin_permissions(collection_id, sender)?;
803 /// 810 ///
804 /// * new_sponsor.811 /// * new_sponsor.
805 #[weight = T::WeightInfo::set_collection_sponsor()]812 #[weight = T::WeightInfo::set_collection_sponsor()]
806 pub fn set_collection_sponsor(origin, collection_id: u64, new_sponsor: T::AccountId) -> DispatchResult {813 pub fn set_collection_sponsor(origin, collection_id: CollectionId, new_sponsor: T::AccountId) -> DispatchResult {
807814
808 let sender = ensure_signed(origin)?;815 let sender = ensure_signed(origin)?;
809 ensure!(<Collection<T>>::contains_key(collection_id), Error::<T>::CollectionNotFound);816 ensure!(<Collection<T>>::contains_key(collection_id), Error::<T>::CollectionNotFound);
825 /// 832 ///
826 /// * collection_id.833 /// * collection_id.
827 #[weight = T::WeightInfo::confirm_sponsorship()]834 #[weight = T::WeightInfo::confirm_sponsorship()]
828 pub fn confirm_sponsorship(origin, collection_id: u64) -> DispatchResult {835 pub fn confirm_sponsorship(origin, collection_id: CollectionId) -> DispatchResult {
829836
830 let sender = ensure_signed(origin)?;837 let sender = ensure_signed(origin)?;
831 ensure!(<Collection<T>>::contains_key(collection_id), Error::<T>::CollectionNotFound);838 ensure!(<Collection<T>>::contains_key(collection_id), Error::<T>::CollectionNotFound);
850 /// 857 ///
851 /// * collection_id.858 /// * collection_id.
852 #[weight = T::WeightInfo::remove_collection_sponsor()]859 #[weight = T::WeightInfo::remove_collection_sponsor()]
853 pub fn remove_collection_sponsor(origin, collection_id: u64) -> DispatchResult {860 pub fn remove_collection_sponsor(origin, collection_id: CollectionId) -> DispatchResult {
854861
855 let sender = ensure_signed(origin)?;862 let sender = ensure_signed(origin)?;
856 ensure!(<Collection<T>>::contains_key(collection_id), Error::<T>::CollectionNotFound);863 ensure!(<Collection<T>>::contains_key(collection_id), Error::<T>::CollectionNotFound);
889 // .saturating_add(RocksDbWeight::get().writes(8 as Weight))]896 // .saturating_add(RocksDbWeight::get().writes(8 as Weight))]
890897
891 #[weight = T::WeightInfo::create_item(data.len())]898 #[weight = T::WeightInfo::create_item(data.len())]
892 pub fn create_item(origin, collection_id: u64, owner: T::AccountId, data: CreateItemData) -> DispatchResult {899 pub fn create_item(origin, collection_id: CollectionId, owner: T::AccountId, data: CreateItemData) -> DispatchResult {
893900
894 let sender = ensure_signed(origin)?;901 let sender = ensure_signed(origin)?;
895902
925 #[weight = T::WeightInfo::create_item(items_data.into_iter()932 #[weight = T::WeightInfo::create_item(items_data.into_iter()
926 .map(|data| { data.len() })933 .map(|data| { data.len() })
927 .sum())]934 .sum())]
928 pub fn create_multiple_items(origin, collection_id: u64, owner: T::AccountId, items_data: Vec<CreateItemData>) -> DispatchResult {935 pub fn create_multiple_items(origin, collection_id: CollectionId, owner: T::AccountId, items_data: Vec<CreateItemData>) -> DispatchResult {
929936
930 ensure!(items_data.len() > 0, Error::<T>::EmptyArgument);937 ensure!(items_data.len() > 0, Error::<T>::EmptyArgument);
931 let sender = ensure_signed(origin)?;938 let sender = ensure_signed(origin)?;
959 /// 966 ///
960 /// * item_id: ID of NFT to burn.967 /// * item_id: ID of NFT to burn.
961 #[weight = T::WeightInfo::burn_item()]968 #[weight = T::WeightInfo::burn_item()]
962 pub fn burn_item(origin, collection_id: u64, item_id: u64) -> DispatchResult {969 pub fn burn_item(origin, collection_id: CollectionId, item_id: TokenId) -> DispatchResult {
963970
964 let sender = ensure_signed(origin)?;971 let sender = ensure_signed(origin)?;
965 Self::collection_exists(collection_id)?;972 Self::collection_exists(collection_id)?;
1012 /// * Fungible Mode: Must specify transferred amount1019 /// * Fungible Mode: Must specify transferred amount
1013 /// * Re-Fungible Mode: Must specify transferred portion (between 0 and 1)1020 /// * Re-Fungible Mode: Must specify transferred portion (between 0 and 1)
1014 #[weight = T::WeightInfo::transfer()]1021 #[weight = T::WeightInfo::transfer()]
1015 pub fn transfer(origin, recipient: T::AccountId, collection_id: u64, item_id: u64, value: u64) -> DispatchResult {1022 pub fn transfer(origin, recipient: T::AccountId, collection_id: CollectionId, item_id: TokenId, value: u128) -> DispatchResult {
10161023
1017 let sender = ensure_signed(origin)?;1024 let sender = ensure_signed(origin)?;
10181025
1054 /// 1061 ///
1055 /// * item_id: ID of the item.1062 /// * item_id: ID of the item.
1056 #[weight = T::WeightInfo::approve()]1063 #[weight = T::WeightInfo::approve()]
1057 pub fn approve(origin, approved: T::AccountId, collection_id: u64, item_id: u64) -> DispatchResult {1064 pub fn approve(origin, approved: T::AccountId, collection_id: CollectionId, item_id: TokenId) -> DispatchResult {
10581065
1059 let sender = ensure_signed(origin)?;1066 let sender = ensure_signed(origin)?;
10601067
1112 /// 1119 ///
1113 /// * value: Amount to transfer.1120 /// * value: Amount to transfer.
1114 #[weight = T::WeightInfo::transfer_from()]1121 #[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 {1122 pub fn transfer_from(origin, from: T::AccountId, recipient: T::AccountId, collection_id: CollectionId, item_id: TokenId, value: u128 ) -> DispatchResult {
11161123
1117 let sender = ensure_signed(origin)?;1124 let sender = ensure_signed(origin)?;
1118 let mut appoved_transfer = false;1125 let mut appoved_transfer = false;
11571164
1158 ///1165 ///
1159 #[weight = 0]1166 #[weight = 0]
1160 pub fn safe_transfer_from(origin, collection_id: u64, item_id: u64, new_owner: T::AccountId) -> DispatchResult {1167 pub fn safe_transfer_from(origin, collection_id: CollectionId, item_id: TokenId, new_owner: T::AccountId) -> DispatchResult {
11611168
1162 // let no_perm_mes = "You do not have permissions to modify this collection";1169 // 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);1170 // ensure!(<ApprovedList<T>>::contains_key((collection_id, item_id)), no_perm_mes);
1186 #[weight = T::WeightInfo::set_variable_meta_data()]1193 #[weight = T::WeightInfo::set_variable_meta_data()]
1187 pub fn set_variable_meta_data (1194 pub fn set_variable_meta_data (
1188 origin,1195 origin,
1189 collection_id: u64,1196 collection_id: CollectionId,
1190 item_id: u64,1197 item_id: TokenId,
1191 data: Vec<u8>1198 data: Vec<u8>
1192 ) -> DispatchResult {1199 ) -> DispatchResult {
1193 let sender = ensure_signed(origin)?;1200 let sender = ensure_signed(origin)?;
1231 #[weight = T::WeightInfo::set_offchain_schema()]1238 #[weight = T::WeightInfo::set_offchain_schema()]
1232 pub fn set_offchain_schema(1239 pub fn set_offchain_schema(
1233 origin,1240 origin,
1234 collection_id: u64,1241 collection_id: CollectionId,
1235 schema: Vec<u8>1242 schema: Vec<u8>
1236 ) -> DispatchResult {1243 ) -> DispatchResult {
1237 let sender = ensure_signed(origin)?;1244 let sender = ensure_signed(origin)?;
1259 #[weight = T::WeightInfo::set_const_on_chain_schema()]1266 #[weight = T::WeightInfo::set_const_on_chain_schema()]
1260 pub fn set_const_on_chain_schema (1267 pub fn set_const_on_chain_schema (
1261 origin,1268 origin,
1262 collection_id: u64,1269 collection_id: CollectionId,
1263 schema: Vec<u8>1270 schema: Vec<u8>
1264 ) -> DispatchResult {1271 ) -> DispatchResult {
1265 let sender = ensure_signed(origin)?;1272 let sender = ensure_signed(origin)?;
1287 #[weight = T::WeightInfo::set_const_on_chain_schema()]1294 #[weight = T::WeightInfo::set_const_on_chain_schema()]
1288 pub fn set_variable_on_chain_schema (1295 pub fn set_variable_on_chain_schema (
1289 origin,1296 origin,
1290 collection_id: u64,1297 collection_id: CollectionId,
1291 schema: Vec<u8>1298 schema: Vec<u8>
1292 ) -> DispatchResult {1299 ) -> DispatchResult {
1293 let sender = ensure_signed(origin)?;1300 let sender = ensure_signed(origin)?;
13961403
1397impl<T: Trait> Module<T> {1404impl<T: Trait> Module<T> {
13981405
1399 fn can_create_items_in_collection(collection_id: u64, collection: &CollectionType<T::AccountId>, sender: &T::AccountId, owner: &T::AccountId) -> DispatchResult {1406 fn can_create_items_in_collection(collection_id: CollectionId, collection: &CollectionType<T::AccountId>, sender: &T::AccountId, owner: &T::AccountId) -> DispatchResult {
14001407
1401 if !Self::is_owner_or_admin_permissions(collection_id, sender.clone()) {1408 if !Self::is_owner_or_admin_permissions(collection_id, sender.clone()) {
1402 ensure!(collection.mint_mode == true, Error::<T>::PublicMintingNotAllowed);1409 ensure!(collection.mint_mode == true, Error::<T>::PublicMintingNotAllowed);
1441 Ok(())1448 Ok(())
1442 }1449 }
14431450
1444 fn create_item_no_validation(collection_id: u64, collection: &CollectionType<T::AccountId>, owner: T::AccountId, data: CreateItemData) -> DispatchResult {1451 fn create_item_no_validation(collection_id: CollectionId, collection: &CollectionType<T::AccountId>, owner: T::AccountId, data: CreateItemData) -> DispatchResult {
1445 match data1452 match data
1446 {1453 {
1447 CreateItemData::NFT(data) => {1454 CreateItemData::NFT(data) => {
1458 let item = FungibleItemType {1465 let item = FungibleItemType {
1459 collection: collection_id,1466 collection: collection_id,
1460 owner,1467 owner,
1461 value: (10 as u128).pow(collection.decimal_points)1468 value: (10 as u128).pow(collection.decimal_points as u32)
1462 };1469 };
14631470
1464 Self::add_fungible_item(item)?;1471 Self::add_fungible_item(item)?;
1465 },1472 },
1466 CreateItemData::ReFungible(data) => {1473 CreateItemData::ReFungible(data) => {
1467 let mut owner_list = Vec::new();1474 let mut owner_list = Vec::new();
1468 let value = (10 as u128).pow(collection.decimal_points);1475 let value = (10 as u128).pow(collection.decimal_points as u32);
1469 owner_list.push(Ownership {owner: owner.clone(), fraction: value});1476 owner_list.push(Ownership {owner: owner.clone(), fraction: value});
14701477
1471 let item = ReFungibleItemType {1478 let item = ReFungibleItemType {
1492 .ok_or(Error::<T>::NumOverflow)?;1499 .ok_or(Error::<T>::NumOverflow)?;
1493 let itemcopy = item.clone();1500 let itemcopy = item.clone();
1494 let owner = item.owner.clone();1501 let owner = item.owner.clone();
1495 let value = item.value as u64;
14961502
1497 Self::add_token_index(item.collection, current_index, owner.clone())?;1503 Self::add_token_index(item.collection, current_index, owner.clone())?;
14981504
1505 1511
1506 // Update balance1512 // Update balance
1507 let new_balance = <Balance<T>>::get(item.collection, owner.clone())1513 let new_balance = <Balance<T>>::get(item.collection, owner.clone())
1508 .checked_add(value)1514 .checked_add(item.value)
1509 .ok_or(Error::<T>::NumOverflow)?;1515 .ok_or(Error::<T>::NumOverflow)?;
1510 <Balance<T>>::insert(item.collection, owner.clone(), new_balance);1516 <Balance<T>>::insert(item.collection, owner.clone(), new_balance);
15111517
1518 .ok_or(Error::<T>::NumOverflow)?;1524 .ok_or(Error::<T>::NumOverflow)?;
1519 let itemcopy = item.clone();1525 let itemcopy = item.clone();
15201526
1521 let value = item.owner.first().unwrap().fraction as u64;1527 let value = item.owner.first().unwrap().fraction;
1522 let owner = item.owner.first().unwrap().owner.clone();1528 let owner = item.owner.first().unwrap().owner.clone();
15231529
1524 Self::add_token_index(item.collection, current_index, owner.clone())?;1530 Self::add_token_index(item.collection, current_index, owner.clone())?;
1565 }1571 }
15661572
1567 fn burn_refungible_item(1573 fn burn_refungible_item(
1568 collection_id: u64,1574 collection_id: CollectionId,
1569 item_id: u64,1575 item_id: TokenId,
1570 owner: T::AccountId,1576 owner: T::AccountId,
1571 ) -> DispatchResult {1577 ) -> DispatchResult {
1572 ensure!(1578 ensure!(
15871593
1588 // update balance1594 // update balance
1589 let new_balance = <Balance<T>>::get(collection_id, item.owner.clone())1595 let new_balance = <Balance<T>>::get(collection_id, item.owner.clone())
1590 .checked_sub(item.fraction as u64)1596 .checked_sub(item.fraction)
1591 .ok_or(Error::<T>::NumOverflow)?;1597 .ok_or(Error::<T>::NumOverflow)?;
1592 <Balance<T>>::insert(collection_id, item.owner.clone(), new_balance);1598 <Balance<T>>::insert(collection_id, item.owner.clone(), new_balance);
15931599
1596 Ok(())1602 Ok(())
1597 }1603 }
15981604
1599 fn burn_nft_item(collection_id: u64, item_id: u64) -> DispatchResult {1605 fn burn_nft_item(collection_id: CollectionId, item_id: TokenId) -> DispatchResult {
1600 ensure!(1606 ensure!(
1601 <NftItemList<T>>::contains_key(collection_id, item_id),1607 <NftItemList<T>>::contains_key(collection_id, item_id),
1602 Error::<T>::TokenNotFound1608 Error::<T>::TokenNotFound
1617 Ok(())1623 Ok(())
1618 }1624 }
16191625
1620 fn burn_fungible_item(collection_id: u64, item_id: u64) -> DispatchResult {1626 fn burn_fungible_item(collection_id: CollectionId, item_id: TokenId) -> DispatchResult {
1621 ensure!(1627 ensure!(
1622 <FungibleItemList<T>>::contains_key(collection_id, item_id),1628 <FungibleItemList<T>>::contains_key(collection_id, item_id),
1623 Error::<T>::TokenNotFound1629 Error::<T>::TokenNotFound
16301636
1631 // update balance1637 // update balance
1632 let new_balance = <Balance<T>>::get(collection_id, item.owner.clone())1638 let new_balance = <Balance<T>>::get(collection_id, item.owner.clone())
1633 .checked_sub(item.value as u64)1639 .checked_sub(item.value)
1634 .ok_or(Error::<T>::NumOverflow)?;1640 .ok_or(Error::<T>::NumOverflow)?;
1635 <Balance<T>>::insert(collection_id, item.owner.clone(), new_balance);1641 <Balance<T>>::insert(collection_id, item.owner.clone(), new_balance);
16361642
1639 Ok(())1645 Ok(())
1640 }1646 }
16411647
1642 fn collection_exists(collection_id: u64) -> DispatchResult {1648 fn collection_exists(collection_id: CollectionId) -> DispatchResult {
1643 ensure!(1649 ensure!(
1644 <Collection<T>>::contains_key(collection_id),1650 <Collection<T>>::contains_key(collection_id),
1645 Error::<T>::CollectionNotFound1651 Error::<T>::CollectionNotFound
1646 );1652 );
1647 Ok(())1653 Ok(())
1648 }1654 }
16491655
1650 fn check_owner_permissions(collection_id: u64, subject: T::AccountId) -> DispatchResult {1656 fn check_owner_permissions(collection_id: CollectionId, subject: T::AccountId) -> DispatchResult {
1651 Self::collection_exists(collection_id)?;1657 Self::collection_exists(collection_id)?;
16521658
1653 let target_collection = <Collection<T>>::get(collection_id);1659 let target_collection = <Collection<T>>::get(collection_id);
1659 Ok(())1665 Ok(())
1660 }1666 }
16611667
1662 fn is_owner_or_admin_permissions(collection_id: u64, subject: T::AccountId) -> bool {1668 fn is_owner_or_admin_permissions(collection_id: CollectionId, subject: T::AccountId) -> bool {
1663 let target_collection = <Collection<T>>::get(collection_id);1669 let target_collection = <Collection<T>>::get(collection_id);
1664 let mut result: bool = subject == target_collection.owner;1670 let mut result: bool = subject == target_collection.owner;
1665 let exists = <AdminList<T>>::contains_key(collection_id);1671 let exists = <AdminList<T>>::contains_key(collection_id);
1674 }1680 }
16751681
1676 fn check_owner_or_admin_permissions(1682 fn check_owner_or_admin_permissions(
1677 collection_id: u64,1683 collection_id: CollectionId,
1678 subject: T::AccountId,1684 subject: T::AccountId,
1679 ) -> DispatchResult {1685 ) -> DispatchResult {
1680 Self::collection_exists(collection_id)?;1686 Self::collection_exists(collection_id)?;
1687 Ok(())1693 Ok(())
1688 }1694 }
16891695
1690 fn is_item_owner(subject: T::AccountId, collection_id: u64, item_id: u64) -> bool {1696 fn is_item_owner(subject: T::AccountId, collection_id: CollectionId, item_id: TokenId) -> bool {
1691 let target_collection = <Collection<T>>::get(collection_id);1697 let target_collection = <Collection<T>>::get(collection_id);
16921698
1693 match target_collection.mode {1699 match target_collection.mode {
1707 }1713 }
1708 }1714 }
17091715
1710 fn check_white_list(collection_id: u64, address: &T::AccountId) -> DispatchResult {1716 fn check_white_list(collection_id: CollectionId, address: &T::AccountId) -> DispatchResult {
1711 let mes = Error::<T>::AddresNotInWhiteList;1717 let mes = Error::<T>::AddresNotInWhiteList;
1712 ensure!(<WhiteList<T>>::contains_key(collection_id), mes);1718 ensure!(<WhiteList<T>>::contains_key(collection_id), mes);
1713 let wl = <WhiteList<T>>::get(collection_id);1719 let wl = <WhiteList<T>>::get(collection_id);
1717 }1723 }
17181724
1719 fn transfer_fungible(1725 fn transfer_fungible(
1720 collection_id: u64,1726 collection_id: CollectionId,
1721 item_id: u64,1727 item_id: TokenId,
1722 value: u64,1728 value: u128,
1723 owner: T::AccountId,1729 owner: T::AccountId,
1724 new_owner: T::AccountId,1730 new_owner: T::AccountId,
1725 ) -> DispatchResult {1731 ) -> DispatchResult {
1731 let full_item = <FungibleItemList<T>>::get(collection_id, item_id);1737 let full_item = <FungibleItemList<T>>::get(collection_id, item_id);
1732 let amount = full_item.value;1738 let amount = full_item.value;
17331739
1734 ensure!(amount >= value.into(), Error::<T>::TokenValueTooLow);1740 ensure!(amount >= value, Error::<T>::TokenValueTooLow);
17351741
1736 // update balance1742 // update balance
1737 let balance_old_owner = <Balance<T>>::get(collection_id, owner.clone())1743 let balance_old_owner = <Balance<T>>::get(collection_id, owner.clone())
1745 new_owner_account_id = new_owner_items[0];1751 new_owner_account_id = new_owner_items[0];
1746 }1752 }
1747
1748 let val64 = value.into();
17491753
1750 // transfer1754 // transfer
1751 if amount == val64 && new_owner_account_id == 0 {1755 if amount == value && new_owner_account_id == 0 {
1752 // change owner1756 // change owner
1753 // new owner do not have account1757 // new owner do not have account
1754 let mut new_full_item = full_item.clone();1758 let mut new_full_item = full_item.clone();
1765 Self::move_token_index(collection_id, item_id, owner.clone(), new_owner.clone())?;1769 Self::move_token_index(collection_id, item_id, owner.clone(), new_owner.clone())?;
1766 } else {1770 } else {
1767 let mut new_full_item = full_item.clone();1771 let mut new_full_item = full_item.clone();
1768 new_full_item.value -= val64;1772 new_full_item.value -= value;
17691773
1770 // separate amount1774 // separate amount
1771 if new_owner_account_id > 0 {1775 if new_owner_account_id > 0 {
1772 // new owner has account1776 // new owner has account
1773 let mut item = <FungibleItemList<T>>::get(collection_id, new_owner_account_id);1777 let mut item = <FungibleItemList<T>>::get(collection_id, new_owner_account_id);
1774 item.value += val64;1778 item.value += value;
17751779
1776 // update balance1780 // update balance
1777 let balance_new_owner = <Balance<T>>::get(collection_id, new_owner.clone())1781 let balance_new_owner = <Balance<T>>::get(collection_id, new_owner.clone())
1785 let item = FungibleItemType {1789 let item = FungibleItemType {
1786 collection: collection_id,1790 collection: collection_id,
1787 owner: new_owner.clone(),1791 owner: new_owner.clone(),
1788 value: val64,1792 value
1789 };1793 };
17901794
1791 Self::add_fungible_item(item)?;1795 Self::add_fungible_item(item)?;
1792 }1796 }
17931797
1794 if amount == val64 {1798 if amount == value {
1795 Self::remove_token_index(collection_id, item_id, full_item.owner.clone())?;1799 Self::remove_token_index(collection_id, item_id, full_item.owner.clone())?;
17961800
1797 // remove approve list1801 // remove approve list
1806 }1810 }
18071811
1808 fn transfer_refungible(1812 fn transfer_refungible(
1809 collection_id: u64,1813 collection_id: CollectionId,
1810 item_id: u64,1814 item_id: TokenId,
1811 value: u64,1815 value: u128,
1812 owner: T::AccountId,1816 owner: T::AccountId,
1813 new_owner: T::AccountId,1817 new_owner: T::AccountId,
1814 ) -> DispatchResult {1818 ) -> DispatchResult {
1826 .ok_or(Error::<T>::NumOverflow)?;1830 .ok_or(Error::<T>::NumOverflow)?;
1827 let amount = item.fraction;1831 let amount = item.fraction;
18281832
1829 ensure!(amount >= value.into(), Error::<T>::TokenValueTooLow);1833 ensure!(amount >= value, Error::<T>::TokenValueTooLow);
18301834
1831 // update balance1835 // update balance
1832 let balance_old_owner = <Balance<T>>::get(collection_id, item.owner.clone())1836 let balance_old_owner = <Balance<T>>::get(collection_id, item.owner.clone())
18411845
1842 let old_owner = item.owner.clone();1846 let old_owner = item.owner.clone();
1843 let new_owner_has_account = full_item.owner.iter().any(|i| i.owner == new_owner);1847 let new_owner_has_account = full_item.owner.iter().any(|i| i.owner == new_owner);
1844 let val64 = value.into();
18451848
1846 // transfer1849 // transfer
1847 if amount == val64 && !new_owner_has_account {1850 if amount == value && !new_owner_has_account {
1848 // change owner1851 // change owner
1849 // new owner do not have account1852 // new owner do not have account
1850 let mut new_full_item = full_item.clone();1853 let mut new_full_item = full_item.clone();
1865 .iter_mut()1868 .iter_mut()
1866 .find(|i| i.owner == owner)1869 .find(|i| i.owner == owner)
1867 .unwrap()1870 .unwrap()
1868 .fraction -= val64;1871 .fraction -= value;
18691872
1870 // separate amount1873 // separate amount
1871 if new_owner_has_account {1874 if new_owner_has_account {
1875 .iter_mut()1878 .iter_mut()
1876 .find(|i| i.owner == new_owner)1879 .find(|i| i.owner == new_owner)
1877 .unwrap()1880 .unwrap()
1878 .fraction += val64;1881 .fraction += value;
1879 } else {1882 } else {
1880 // new owner do not have account1883 // new owner do not have account
1881 new_full_item.owner.push(Ownership {1884 new_full_item.owner.push(Ownership {
1882 owner: new_owner.clone(),1885 owner: new_owner.clone(),
1883 fraction: val64,1886 fraction: value,
1884 });1887 });
1885 Self::add_token_index(collection_id, item_id, new_owner.clone())?;1888 Self::add_token_index(collection_id, item_id, new_owner.clone())?;
1886 }1889 }
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);
1979 fn init_collection(item: &CollectionType<T::AccountId>) {1982 fn init_collection(item: &CollectionType<T::AccountId>) {
1980 // check params1983 // check params
1981 assert!(1984 assert!(
1982 item.decimal_points <= 4,1985 item.decimal_points <= MAX_DECIMAL_POINTS,
1983 "decimal_points parameter must be lower than 4"1986 "decimal_points parameter must be lower than MAX_DECIMAL_POINTS"
1984 );1987 );
1985 assert!(1988 assert!(
1986 item.name.len() <= 64,1989 item.name.len() <= 64,
2026 .checked_add(1)2029 .checked_add(1)
2027 .unwrap();2030 .unwrap();
2028 let owner = item.owner.clone();2031 let owner = item.owner.clone();
2029 let value = item.value as u64;
20302032
2031 Self::add_token_index(item.collection, current_index, owner.clone()).unwrap();2033 Self::add_token_index(item.collection, current_index, owner.clone()).unwrap();
20322034
2033 <ItemListIndex>::insert(item.collection, current_index);2035 <ItemListIndex>::insert(item.collection, current_index);
20342036
2035 // Update balance2037 // Update balance
2036 let new_balance = <Balance<T>>::get(item.collection, owner.clone())2038 let new_balance = <Balance<T>>::get(item.collection, owner.clone())
2037 .checked_add(value)2039 .checked_add(item.value)
2038 .unwrap();2040 .unwrap();
2039 <Balance<T>>::insert(item.collection, owner.clone(), new_balance);2041 <Balance<T>>::insert(item.collection, owner.clone(), new_balance);
2040 }2042 }
2044 .checked_add(1)2046 .checked_add(1)
2045 .unwrap();2047 .unwrap();
20462048
2047 let value = item.owner.first().unwrap().fraction as u64;2049 let value = item.owner.first().unwrap().fraction;
2048 let owner = item.owner.first().unwrap().owner.clone();2050 let owner = item.owner.first().unwrap().owner.clone();
20492051
2050 Self::add_token_index(item.collection, current_index, owner.clone()).unwrap();2052 Self::add_token_index(item.collection, current_index, owner.clone()).unwrap();
2058 <Balance<T>>::insert(item.collection, owner.clone(), new_balance);2060 <Balance<T>>::insert(item.collection, owner.clone(), new_balance);
2059 }2061 }
20602062
2061 fn add_token_index(collection_id: u64, item_index: u64, owner: T::AccountId) -> DispatchResult {2063 fn add_token_index(collection_id: CollectionId, item_index: TokenId, owner: T::AccountId) -> DispatchResult {
20622064
2063 // add to account limit2065 // add to account limit
2064 if <AccountItemCount<T>>::contains_key(owner.clone()) {2066 if <AccountItemCount<T>>::contains_key(owner.clone()) {
2096 }2098 }
20972099
2098 fn remove_token_index(2100 fn remove_token_index(
2099 collection_id: u64,2101 collection_id: CollectionId,
2100 item_index: u64,2102 item_index: TokenId,
2101 owner: T::AccountId,2103 owner: T::AccountId,
2102 ) -> DispatchResult {2104 ) -> DispatchResult {
21032105
2123 }2125 }
21242126
2125 fn move_token_index(2127 fn move_token_index(
2126 collection_id: u64,2128 collection_id: CollectionId,
2127 item_index: u64,2129 item_index: TokenId,
2128 old_owner: T::AccountId,2130 old_owner: T::AccountId,
2129 new_owner: T::AccountId,2131 new_owner: T::AccountId,
2130 ) -> DispatchResult {2132 ) -> 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, MAX_DECIMAL_POINTS}; //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(),
7677
77// Use cases tests region78// Use cases tests region
78// #region79// #region
80#[test]
81fn create_fungible_collection_fails_with_large_decimal_numbers() {
82 new_test_ext().execute_with(|| {
83 default_limits();
84
85 let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();
86 let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();
87 let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();
88
89 let origin1 = Origin::signed(1);
90 assert_noop!(TemplateModule::create_collection(
91 origin1,
92 col_name1,
93 col_desc1,
94 token_prefix1,
95 CollectionMode::Fungible(MAX_DECIMAL_POINTS + 1)
96 ), Error::<Test>::CollectionDecimalPointLimitExceeded);
97 });
98}
99
100#[test]
101fn create_re_fungible_collection_fails_with_large_decimal_numbers() {
102 new_test_ext().execute_with(|| {
103 default_limits();
104
105 let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();
106 let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();
107 let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();
108
109 let origin1 = Origin::signed(1);
110 assert_noop!(TemplateModule::create_collection(
111 origin1,
112 col_name1,
113 col_desc1,
114 token_prefix1,
115 CollectionMode::ReFungible(MAX_DECIMAL_POINTS + 1)
116 ), Error::<Test>::CollectionDecimalPointLimitExceeded);
117 });
118}
119
79#[test]120#[test]
80fn create_nft_item() {121fn create_nft_item() {
109 items_data.clone().into_iter().map(|d| { d.into() }).collect()150 items_data.clone().into_iter().map(|d| { d.into() }).collect()
110 ));151 ));
111 for (index, data) in items_data.iter().enumerate() {152 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);153 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);154 assert_eq!(TemplateModule::nft_item_id(1, (index + 1) as TokenId).variable_data.to_vec(), data.variable_data);
114 }155 }
115 });156 });
116}157}
160 ));201 ));
161 for (index, data) in items_data.iter().enumerate() {202 for (index, data) in items_data.iter().enumerate() {
162203
163 let item = TemplateModule::refungible_item_id(1, (index + 1) as u64);204 let item = TemplateModule::refungible_item_id(1, (index + 1) as TokenId);
164 assert_eq!(item.const_data.to_vec(), data.const_data);205 assert_eq!(item.const_data.to_vec(), data.const_data);
165 assert_eq!(item.variable_data.to_vec(), data.variable_data);206 assert_eq!(item.variable_data.to_vec(), data.variable_data);
166 assert_eq!(207 assert_eq!(
207 ));248 ));
208 249
209 for (index, _) in items_data.iter().enumerate() {250 for (index, _) in items_data.iter().enumerate() {
210 assert_eq!(TemplateModule::fungible_item_id(1, (index + 1) as u64).owner, 1);251 assert_eq!(TemplateModule::fungible_item_id(1, (index + 1) as TokenId).owner, 1);
211 }252 }
212 assert_eq!(TemplateModule::balance_count(1, 1), 3000);253 assert_eq!(TemplateModule::balance_count(1, 1), 3000);
213 assert_eq!(TemplateModule::address_tokens(1, 1), [1, 2, 3]);254 assert_eq!(TemplateModule::address_tokens(1, 1), [1, 2, 3]);