-
Notifications
You must be signed in to change notification settings - Fork 523
Fix inverted contribute/refund time gates in token-fundraiser (anchor) #614
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 { | ||
|
|
@@ -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, | ||
|
|
@@ -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; | ||
| } | ||
|
|
||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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 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! |
||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The bare
catch {}setsrejected = truefor any thrown error, not just the expectedFundraiserNotEnded. If the contributor account was never created (e.g. an earlier contribute test failed), the RPC will throw an account-not-found error andrejectedwill still betrue, 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.