diff --git a/pallets/funding/src/tests/1_application.rs b/pallets/funding/src/tests/1_application.rs index db64ff3c6..1ca0596bc 100644 --- a/pallets/funding/src/tests/1_application.rs +++ b/pallets/funding/src/tests/1_application.rs @@ -557,6 +557,68 @@ mod create_project_extrinsic { ); }); } + + #[test] + fn unaccepted_decimal_ranges() { + let mut inst = MockInstantiator::new(Some(RefCell::new(new_test_ext()))); + + let mut fail_with_decimals = |decimals: u8| { + let mut project_metadata = default_project_metadata(ISSUER_1); + project_metadata.token_information.decimals = decimals; + let jwt = get_mock_jwt_with_cid( + ISSUER_1, + InvestorType::Institutional, + generate_did_from_account(ISSUER_1), + project_metadata.clone().policy_ipfs_cid.unwrap(), + ); + inst.execute(|| { + assert_noop!( + Pallet::::create_project( + RuntimeOrigin::signed(ISSUER_1), + jwt.clone(), + project_metadata.clone() + ), + Error::::BadMetadata(MetadataError::BadDecimals) + ); + }); + }; + + // less than 4 should fail + for i in 0..=3 { + fail_with_decimals(i); + } + + // more than 20 should fail + for i in 21..=30 { + fail_with_decimals(i); + } + + let mut issuer = ISSUER_2; + let mut succeed_with_decimals = |decimals: u8| { + let mut project_metadata = default_project_metadata(issuer); + project_metadata.token_information.decimals = decimals; + let jwt = get_mock_jwt_with_cid( + issuer, + InvestorType::Institutional, + generate_did_from_account(issuer), + project_metadata.clone().policy_ipfs_cid.unwrap(), + ); + + inst.mint_plmc_to(vec![(issuer, 1000 * PLMC).into()]); + inst.execute(|| { + assert_ok!(Pallet::::create_project( + RuntimeOrigin::signed(issuer), + jwt.clone(), + project_metadata.clone() + )); + }); + issuer +=1 ; + }; + // 5 to 20 succeeds + for i in 5..=20 { + succeed_with_decimals(i); + } + } } } diff --git a/pallets/funding/src/tests/4_community.rs b/pallets/funding/src/tests/4_community.rs index e7ff2e3bb..dde7fde70 100644 --- a/pallets/funding/src/tests/4_community.rs +++ b/pallets/funding/src/tests/4_community.rs @@ -292,7 +292,7 @@ mod round_flow { for decimals in 0..25 { decimal_test(decimals); } -gt + // Since we use the same original price and allocation size and adjust for decimals, // the USD and PLMC amounts should be the same assert!(total_fundings_usd.iter().all(|x| *x == total_fundings_usd[0])); diff --git a/pallets/funding/src/types.rs b/pallets/funding/src/types.rs index 1803eb1b1..4b761ad21 100644 --- a/pallets/funding/src/types.rs +++ b/pallets/funding/src/types.rs @@ -244,6 +244,10 @@ pub mod storage_types { if target_funding < (1000u64 * 10u64.pow(USD_DECIMALS.into())).into() { return Err(MetadataError::FundingTargetTooLow); } + + if self.token_information.decimals < 4 || self.token_information.decimals > 20 { + return Err(MetadataError::BadDecimals); + } Ok(()) } } @@ -758,6 +762,8 @@ pub mod inner_types { FundingTargetTooLow, /// The project's metadata hash is not provided while starting the evaluation round. CidNotProvided, + /// The ct decimals specified for the CT is outside the 4 to 20 range. + BadDecimals, } /// Errors related to the project's migration process.