Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -68,10 +68,11 @@ impl<'info> Contribute<'info> {
FundraiserError::ContributionTooBig
);

// Check if the fundraising duration has been reached
// Contributions are only accepted while the campaign is still open,
// i.e. before `duration` days have elapsed since it started.
let current_time = Clock::get()?.unix_timestamp;
require!(
self.fundraiser.duration <= ((current_time - self.fundraiser.time_started) / SECONDS_TO_DAYS) as u16,
(((current_time - self.fundraiser.time_started) / SECONDS_TO_DAYS) as u16) < self.fundraiser.duration,
crate::FundraiserError::FundraiserEnded
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ pub struct Refund<'info> {
impl<'info> Refund<'info> {
pub fn refund(&mut self) -> Result<()> {

// Check if the fundraising duration has been reached
// Refunds are only allowed once the campaign has ended, i.e. after
// `duration` days have elapsed since it started.
let current_time = Clock::get()?.unix_timestamp;

require!(
self.fundraiser.duration >= ((current_time - self.fundraiser.time_started) / SECONDS_TO_DAYS) as u16,
(((current_time - self.fundraiser.time_started) / SECONDS_TO_DAYS) as u16) >= self.fundraiser.duration,
crate::FundraiserError::FundraiserNotEnded
);

Expand Down
51 changes: 28 additions & 23 deletions tokens/token-fundraiser/anchor/tests/bankrun.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import assert from "node:assert";
import { describe, it } from "node:test";
import * as anchor from "@anchor-lang/core";
import {
Expand Down Expand Up @@ -82,7 +83,7 @@ describe("fundraiser bankrun", async () => {
const vault = getAssociatedTokenAddressSync(mint, fundraiser, true);

const tx = await program.methods
.initialize(new BN(30000000), 0)
.initialize(new BN(30000000), 5)
.accountsPartial({
maker: maker.publicKey,
fundraiser,
Expand Down Expand Up @@ -200,30 +201,34 @@ describe("fundraiser bankrun", async () => {
}
});

it("Refund Contributions", async () => {
it("Refund is rejected while the campaign is still open", async () => {
// The campaign was created with a 5-day duration and has only just started,
// so a refund must be rejected until the campaign has ended. (A successful
// refund requires advancing the validator clock past `duration`, e.g. with
// bankrun's `setClock`.)
const vault = getAssociatedTokenAddressSync(mint, fundraiser, true);

const contributorAccount = await program.account.contributor.fetch(contributor);
console.log("\nContributor balance", contributorAccount.amount.toString());

const tx = await program.methods
.refund()
.accountsPartial({
contributor: provider.publicKey,
maker: maker.publicKey,
mintToRaise: mint,
fundraiser,
contributorAccount: contributor,
contributorAta: contributorATA,
vault,
tokenProgram: TOKEN_PROGRAM_ID,
systemProgram: anchor.web3.SystemProgram.programId,
})
.rpc()
.then(confirm);
let rejected = false;
try {
await program.methods
.refund()
.accountsPartial({
contributor: provider.publicKey,
maker: maker.publicKey,
mintToRaise: mint,
fundraiser,
contributorAccount: contributor,
contributorAta: contributorATA,
vault,
tokenProgram: TOKEN_PROGRAM_ID,
systemProgram: anchor.web3.SystemProgram.programId,
})
.rpc()
.then(confirm);
} catch {
rejected = true;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Catch block too broad — masks unrelated failures

The bare catch {} sets rejected = true for any thrown error, not just the expected FundraiserNotEnded. If the contributor account was never created (e.g. an earlier contribute test failed), the RPC will throw an account-not-found error and rejected will still be true, making the assertion pass while the business-logic gate was never actually exercised. Narrowing the catch to check for the specific error code keeps the test meaningful even when the fixture is in a bad state.


console.log("\nRefunded contributions", tx);
console.log("Your transaction signature", tx);
console.log("Vault balance", (await provider.connection.getTokenAccountBalance(vault)).value.amount);
assert.ok(rejected, "refund should be rejected while the campaign is still open");
});
Comment on lines +204 to 237

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 No positive-path test for a post-campaign refund

The test suite now only verifies that a refund is rejected while the campaign is open; there is no test that confirms a refund succeeds after the campaign ends. The PR description notes this requires advancing bankrun's clock via setClock, but without that test the happy path of the fixed refund gate is never exercised, leaving the most critical behavior change unverified.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

});