Crowdfunding has one simple promise: if the campaign reaches its goal by the deadline, the creator gets the money; if it does not, everyone gets their money back. Simple to say, but somebody has to hold the money in the meantime, and both sides have to trust that somebody.
A program can be that somebody. The rules are few and fully mechanical, which makes all-or-nothing crowdfunding a good exercise: a goal, a deadline, two outcomes, and a list of things nobody should be able to do. crowdfund is a SOL crowdfunding program in Anchor 1.2 built around exactly that.
The warning from the README first: not audited, devnet / localnet only. This is a learning project. Do not deploy it to mainnet or use it with real funds.
What it does
Four instructions cover the whole life of a campaign:
| instruction | who | when | effect |
|---|---|---|---|
create(id, goal, deadline) |
creator | deadline in the future, goal > 0 | campaign PDA ["campaign", creator, id]
|
contribute(amount) |
anyone | now < deadline |
lamports → campaign PDA; receipt PDA ["receipt", campaign, contributor] accumulates |
withdraw() |
creator (has_one) |
now >= deadline and raised >= goal, once |
raised lamports → creator |
refund() |
each contributor |
now >= deadline and raised < goal
|
their amount back; receipt closed (close = contributor) so a second refund fails |
Read as a timeline:
- Before the deadline, anyone can contribute. Nobody can withdraw and nobody can refund.
- At or after the deadline, goal reached: the creator withdraws what was raised, once. Refunds are closed.
- At or after the deadline, goal missed: each contributor takes their own amount back. The creator cannot withdraw.
Each contribution also emits a Contributed event, and all arithmetic is checked: a checked_add that fails returns an Overflow error.
How to use it
Clone the repository, build, and run the tests:
git clone https://github.com/AbdeldjalilChougui/crowdfund.git
cd crowdfund
anchor build # or: anchor build --tools-version v1.58
cargo test # LiteSVM, no validator; the deadline is driven through the Clock sysvar
There is no front end. The tests in programs/crowdfund/tests/crowdfund.rs are the usage example: they create a campaign, contribute from several wallets, move the clock to the deadline, and try every allowed and forbidden action on both outcomes.
What I built from scratch
Two design choices carry most of the program.
Funds live in the campaign account itself. There is no separate vault. The campaign PDA is owned by the program, so withdraw and refund move lamports directly with sub_lamports and add_lamports. No CPI is needed, and the account keeps its rent-exempt minimum. The campaign account is both the record of the campaign and the box holding the SOL.
Every contributor has a receipt. A receipt is a PDA derived from ["receipt", campaign, contributor] that accumulates what that contributor has put in. Receipts are init_if_needed, so a contributor can top up several times and still have one receipt. On refund, the receipt is what proves how much to give back, and close = contributor closes it, so a second refund fails.
What I learned
A program can move lamports out of accounts it owns, without a CPI. For SPL tokens you call the token program. For SOL sitting in an account your own program owns, you subtract from one balance and add to another. The catch is rent: the campaign account must keep its rent-exempt minimum. That is why withdraw sends exactly raised lamports, not the whole balance, and why one test checks that after refunds the campaign ends at exactly its rent-exempt minimum.
"Once" has to be designed in. "The creator withdraws once" and "a contributor refunds once" are two different mechanisms here. For the creator, the campaign remembers that it was withdrawn, and a second call fails with AlreadyWithdrawn. For contributors, the receipt is closed on refund, so there is nothing left to redeem; a second refund fails with AccountNotInitialized. Closing the account that proves a claim is a clean way to make the claim single-use.
The receipt needs three locks. has_one = campaign and has_one = contributor, plus the seeds, stop anyone from redeeming another contributor's receipt. Each check covers a different substitution: a receipt from another campaign, a receipt that belongs to someone else, or an account at the wrong address altogether.
Seeds and has_one can back each other up. The campaign's seeds include the creator's key. When someone else tries to withdraw, the seeds derived from that signer do not match the campaign, so ConstraintSeeds fires first. has_one = creator is still there as a second line. I kept both: two independent reasons for an attack to fail is not redundancy I want to remove.
Time conditions need both sides tested. The rules use now < deadline for contributing and now >= deadline for the outcome. Those must be exact complements, or there is an instant where both or neither apply. The tests drive the deadline through the LiteSVM Clock sysvar and check before, at, and after it.
init_if_needed deserves a reason. It lets a contributor top up with the same instruction and the same receipt. It is a feature to use deliberately; here the receipt's address is fully determined by the campaign and the contributor, and the receipt only ever accumulates.
The full suite:
-
goal_met_creator_withdraws: three contributions, early withdraw rejected (CampaignRunning), withdraw at the deadline, second withdraw rejected (AlreadyWithdrawn), refunds closed (GoalReached) -
goal_missed_contributors_refund: refund rejected while running, withdraw rejected (GoalNotReached), refund pays back, a second refund fails (AccountNotInitialized), and the campaign ends at exactly its rent-exempt minimum -
contribute_after_deadline_fails: the deadline -
only_creator_can_withdraw: wrong signer. The campaign seeds use the signer key, soConstraintSeedsfires (has_one = creatorbacks it up) -
cannot_refund_someone_elses_receipt: wrong owner of the receipt, rejected by its seeds andhas_one = contributor -
create_validates_inputs: zero goal, past deadline, zero contribution
Each error has a name that says what state the campaign is in: CampaignRunning, GoalReached, GoalNotReached, AlreadyWithdrawn. Writing the tests around those names turned the rules table at the top into something executable.
Things to know
- Not audited. Devnet / localnet only. Do not deploy to mainnet or use with real funds.
- SOL only. There are no SPL token campaigns.
- There are no stretch goals, no cancel-before-deadline, and no fee.
- The campaign is all-or-nothing: contributors cannot pull out before the deadline, and can refund only if the goal was missed.
- The tests run in LiteSVM with no validator. The README describes no devnet deployment.
Why I built it
Crowdfunding is a small state machine with money in it: running, succeeded, failed. Every rule is a condition on time, on an amount, or on who is signing, and every rule has a matching attack: withdraw early, withdraw twice, refund twice, refund someone else's contribution, contribute late. That makes it a compact way to practise the things a Solana program has to get right, with SOL held directly in a program-owned account instead of a token vault. I wanted a version where each of those attacks is a named test and each rejection is a named error.
Get the code
The program and the LiteSVM tests are on GitHub: https://github.com/AbdeldjalilChougui/crowdfund. It is MIT licensed. Issues and pull requests are welcome.
Originally published at abdeldjalilchougui.biz.
Top comments (0)