Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,12 @@ impl<'info> Contribute<'info> {
FundraiserError::ContributionTooSmall
);

let max_contribution = (u128::from(self.fundraiser.amount_to_raise)
* u128::from(MAX_CONTRIBUTION_PERCENTAGE)) / u128::from(PERCENTAGE_SCALER);

// Check if the amount to contribute is less than the maximum allowed contribution
require!(
amount <= (self.fundraiser.amount_to_raise * MAX_CONTRIBUTION_PERCENTAGE) / PERCENTAGE_SCALER,
u128::from(amount) <= max_contribution,
FundraiserError::ContributionTooBig
);

Expand All @@ -77,8 +80,7 @@ impl<'info> Contribute<'info> {

// Check if the maximum contributions per contributor have been reached
require!(
(self.contributor_account.amount <= (self.fundraiser.amount_to_raise * MAX_CONTRIBUTION_PERCENTAGE) / PERCENTAGE_SCALER)
&& (self.contributor_account.amount + amount <= (self.fundraiser.amount_to_raise * MAX_CONTRIBUTION_PERCENTAGE) / PERCENTAGE_SCALER),
u128::from(self.contributor_account.amount) + u128::from(amount) <= max_contribution,
FundraiserError::MaximumContributionsReached
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,51 @@ fn check_contributions_rejects_mint_other_than_recorded() {
assert_eq!(state.mint_to_raise, real_mint);
assert_eq!(token_balance(&env.svm, &fake_vault), amount_to_raise);
}

#[test]
fn large_targets_accept_contributions_and_enforce_ten_percent_cap() {
for target in [u64::MAX / 10 + 1, u64::MAX] {
let mut env = setup();
let mint = create_mint(&mut env.svm, &env.payer, &env.payer.pubkey());
let contributor_ata = get_associated_token_address(&env.payer.pubkey(), &mint);
let setup_ixs = [
create_associated_token_account(&env.payer.pubkey(), &env.payer.pubkey(), &mint, &spl_token::ID),
spl_token::instruction::mint_to(
&spl_token::ID,
&mint,
&contributor_ata,
&env.payer.pubkey(),
&[],
u64::MAX,
)
.unwrap(),
];
send(&mut env.svm, &setup_ixs, &env.payer, &[&env.payer]).unwrap();
let ix = initialize(&env, &mint, target, 1);
send(&mut env.svm, &[ix], &env.maker, &[&env.maker]).unwrap();
let campaign = Campaign {
mint,
vault: get_associated_token_address(&env.fundraiser, &mint),
contributor_ata,
contributor_account: Pubkey::find_program_address(
&[b"contributor", env.fundraiser.as_ref(), env.payer.pubkey().as_ref()],
&fundraiser::id(),
)
.0,
};
let ix = campaign.contribute(&env, 1_000_000);
send(&mut env.svm, &[ix], &env.payer, &[&env.payer]).unwrap();
assert_eq!(token_balance(&env.svm, &campaign.vault), 1_000_000);
let cap = target / 10;
let ix = campaign.contribute(&env, cap + 1);
assert_anchor_error(send(&mut env.svm, &[ix], &env.payer, &[&env.payer]), "ContributionTooBig");
let ix = campaign.contribute(&env, cap - 1_000_000);
send(&mut env.svm, &[ix], &env.payer, &[&env.payer]).unwrap();
let ix = campaign.contribute(&env, 1);
assert_anchor_error(send(&mut env.svm, &[ix], &env.payer, &[&env.payer]), "MaximumContributionsReached");
assert_eq!(token_balance(&env.svm, &campaign.vault), cap);
assert_eq!(token_balance(&env.svm, &contributor_ata), u64::MAX - cap);
assert_eq!(read::<Contributor>(&env.svm, &campaign.contributor_account).amount, cap);
assert_eq!(read::<Fundraiser>(&env.svm, &env.fundraiser).current_amount, cap);
}
}
8 changes: 5 additions & 3 deletions tokens/token-fundraiser/anchor/readme.MD
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,18 @@ impl<'info> Contribute<'info> {
FundraiserError::ContributionTooSmall
);

let max_contribution = (u128::from(self.fundraiser.amount_to_raise)
* u128::from(MAX_CONTRIBUTION_PERCENTAGE)) / u128::from(PERCENTAGE_SCALER);

// Check if the amount to contribute is less than the maximum allowed contribution
require!(
amount <= (self.fundraiser.amount_to_raise * MAX_CONTRIBUTION_PERCENTAGE) / PERCENTAGE_SCALER,
u128::from(amount) <= max_contribution,
FundraiserError::ContributionTooBig
);

// Check if the maximum contributions per contributor have been reached
require!(
(self.contributor_account.amount <= (self.fundraiser.amount_to_raise * MAX_CONTRIBUTION_PERCENTAGE) / PERCENTAGE_SCALER)
&& (self.contributor_account.amount + amount <= (self.fundraiser.amount_to_raise * MAX_CONTRIBUTION_PERCENTAGE) / PERCENTAGE_SCALER),
u128::from(self.contributor_account.amount) + u128::from(amount) <= max_contribution,
FundraiserError::MaximumContributionsReached
);

Expand Down
Loading