From 13c0fec53db76b7b35052a8445eaad7eea3c8bc1 Mon Sep 17 00:00:00 2001 From: Juan Ignacio Rios Date: Wed, 1 May 2024 15:06:44 +0200 Subject: [PATCH] accept only ct decimals from 4 to 20 --- pallets/funding/src/tests/1_application.rs | 38 ++++++++++++++++++++++ pallets/funding/src/types.rs | 6 ++++ 2 files changed, 44 insertions(+) diff --git a/pallets/funding/src/tests/1_application.rs b/pallets/funding/src/tests/1_application.rs index db64ff3c6..c779d9cd2 100644 --- a/pallets/funding/src/tests/1_application.rs +++ b/pallets/funding/src/tests/1_application.rs @@ -557,6 +557,44 @@ mod create_project_extrinsic { ); }); } + + #[test] + fn unaccepted_ct_decimals() { + let mut inst = MockInstantiator::new(Some(RefCell::new(new_test_ext()))); + let mut project_metadata = default_project_metadata(ISSUER_1); + + inst.mint_plmc_to(default_plmc_balances()); + let jwt = get_mock_jwt_with_cid( + ISSUER_1, + InvestorType::Institutional, + generate_did_from_account(ISSUER_1), + project_metadata.clone().policy_ipfs_cid.unwrap(), + ); + + let mut fail_with_decimals = |decimals: u8| { + project_metadata.token_information.decimals = decimals; + + inst.execute(|| { + assert_noop!( + Pallet::::create_project( + RuntimeOrigin::signed(ISSUER_1), + jwt.clone(), + project_metadata.clone() + ), + Error::::BadMetadata(MetadataError::BadDecimals) + ); + }); + }; + + // less than 5 should fail + for i in 0..=4 { + fail_with_decimals(i); + } + // more than 20 should fail + for i in 21..30 { + fail_with_decimals(i); + } + } } } diff --git a/pallets/funding/src/types.rs b/pallets/funding/src/types.rs index 1803eb1b1..193f7b22d 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 < 5 || 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.