Skip to content

Commit

Permalink
accept only ct decimals from 4 to 20
Browse files Browse the repository at this point in the history
  • Loading branch information
JuaniRios committed May 2, 2024
1 parent e08e405 commit edd2894
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
62 changes: 62 additions & 0 deletions pallets/funding/src/tests/1_application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<TestRuntime>::create_project(
RuntimeOrigin::signed(ISSUER_1),
jwt.clone(),
project_metadata.clone()
),
Error::<TestRuntime>::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::<TestRuntime>::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);
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion pallets/funding/src/tests/4_community.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
Expand Down
6 changes: 6 additions & 0 deletions pallets/funding/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
}
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit edd2894

Please sign in to comment.