difftreelog
NFTPAR-183: Storage Refactoring. Decimal points u32 -> u8. Also extended decimal points limit 4 -> 30.
in: master
2 files changed
pallets/nft/src/lib.rsdiffbeforeafterboth--- a/pallets/nft/src/lib.rs
+++ b/pallets/nft/src/lib.rs
@@ -45,21 +45,25 @@
mod default_weights;
+pub const MAX_DECIMAL_POINTS: DecimalPoints = 30;
+
// Structs
// #region
pub type CollectionId = u32;
pub type TokenId = u32;
+pub type DecimalPoints = u8;
+
#[derive(Encode, Decode, Eq, Debug, Clone, PartialEq)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub enum CollectionMode {
Invalid,
NFT,
// decimal points
- Fungible(u32),
+ Fungible(DecimalPoints),
// decimal points
- ReFungible(u32),
+ ReFungible(DecimalPoints),
}
impl Into<u8> for CollectionMode {
@@ -104,7 +108,7 @@
pub owner: AccountId,
pub mode: CollectionMode,
pub access: AccessMode,
- pub decimal_points: u32,
+ pub decimal_points: DecimalPoints,
pub name: Vec<u16>, // 64 include null escape char
pub description: Vec<u16>, // 256 include null escape char
pub token_prefix: Vec<u8>, // 16 include null escape char
@@ -269,7 +273,7 @@
pub enum Error for Module<T: Trait> {
/// Total collections bound exceeded.
TotalCollectionsLimitExceeded,
- /// Decimal_points parameter must be lower than 4.
+ /// Decimal_points parameter must be lower than MAX_DECIMAL_POINTS constant, currently it is 30.
CollectionDecimalPointLimitExceeded,
/// Collection name can not be longer than 63 char.
CollectionNameLimitExceeded,
@@ -498,7 +502,7 @@
ensure!(CollectionCount::get() < ChainLimit::get().collection_numbers_limit, Error::<T>::TotalCollectionsLimitExceeded);
// check params
- ensure!(decimal_points <= 4, Error::<T>::CollectionDecimalPointLimitExceeded);
+ ensure!(decimal_points <= MAX_DECIMAL_POINTS, Error::<T>::CollectionDecimalPointLimitExceeded);
let mut name = collection_name.to_vec();
name.push(0);
@@ -1461,14 +1465,14 @@
let item = FungibleItemType {
collection: collection_id,
owner,
- value: (10 as u128).pow(collection.decimal_points)
+ value: (10 as u128).pow(collection.decimal_points as u32)
};
Self::add_fungible_item(item)?;
},
CreateItemData::ReFungible(data) => {
let mut owner_list = Vec::new();
- let value = (10 as u128).pow(collection.decimal_points);
+ let value = (10 as u128).pow(collection.decimal_points as u32);
owner_list.push(Ownership {owner: owner.clone(), fraction: value});
let item = ReFungibleItemType {
@@ -1982,8 +1986,8 @@
fn init_collection(item: &CollectionType<T::AccountId>) {
// check params
assert!(
- item.decimal_points <= 4,
- "decimal_points parameter must be lower than 4"
+ item.decimal_points <= MAX_DECIMAL_POINTS,
+ "decimal_points parameter must be lower than MAX_DECIMAL_POINTS"
);
assert!(
item.name.len() <= 64,
pallets/nft/src/tests.rsdiffbeforeafterboth3use crate::mock::*;3use crate::mock::*;4use crate::{AccessMode, ApprovePermissions, CollectionMode,4use crate::{AccessMode, ApprovePermissions, CollectionMode,5 Ownership, ChainLimits, CreateItemData, CreateNftData, CreateFungibleData, CreateReFungibleData,5 Ownership, ChainLimits, CreateItemData, CreateNftData, CreateFungibleData, CreateReFungibleData,6 CollectionId, TokenId}; //Err6 CollectionId, TokenId, MAX_DECIMAL_POINTS}; //Err7use frame_support::{assert_noop, assert_ok};7use frame_support::{assert_noop, assert_ok};8use frame_system::{ RawOrigin };8use frame_system::{ RawOrigin };99777778// Use cases tests region78// Use cases tests region79// #region79// #region80#[test]81fn create_fungible_collection_fails_with_large_decimal_numbers() {82 new_test_ext().execute_with(|| {83 default_limits();8485 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();8889 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}99100#[test]101fn create_re_fungible_collection_fails_with_large_decimal_numbers() {102 new_test_ext().execute_with(|| {103 default_limits();104105 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();108109 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}11980#[test]120#[test]81fn create_nft_item() {121fn create_nft_item() {