Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Accept only CT decimals from 4 to 20 #279

Merged
merged 1 commit into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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