Example 3: Crowdfunding (Fundraising) dApp
Scenario: A crowdfunding platform (like a decentralized Kickstarter on Solana) where project creators can create a campaign, and users can contribute SOL to back the project. If a funding goal is met, the creator can withdraw funds; if not, contributors can get refunds.
Prompt Example:
markdownKopierenBearbeitenAct as a Solana Anchor developer and full-stack dApp builder.
**Task:** Build a **crowdfunding dApp on Solana**.
**Specifications:**
- **On-Chain Program (Anchor):** We need a program to manage crowdfunding campaigns. Implement the following:
- A **Campaign** account (Anchor struct) that holds: campaign creator pubkey, title & description (strings), goal amount (u64, in lamports), deadline timestamp, total funds raised, and a boolean `funded` flag.
- Instruction `create_campaign(title, description, goal, deadline)` which initializes a new Campaign account (use a PDA derived from creator pubkey and campaign title or an incrementing ID). Only the campaign creator pays the rent for this account.
- Instruction `contribute(campaign_pubkey, amount)` where any user can send SOL to contribute. This should transfer the specified SOL from the contributor to the Campaign account (use the system program to allocate lamports to the Campaign account). Track the contribution by increasing `total_funds` in the Campaign state. Possibly also have a separate Contributions record or log event, but main logic is updating total.
- Instruction `withdraw_funds(campaign_pubkey)` that allows the campaign creator to withdraw the funds *only if* `total_funds >= goal` *and* the deadline has passed (i.e., campaign succeeded). This will transfer the Campaign account lamports to the creator and mark `funded=true`. If the campaign failed (not enough funds by deadline), perhaps allow contributors to reclaim their contributions via a `refund` instruction (optional).
- Include necessary checks: contributors cannot contribute after the deadline, only the creator can withdraw, and refunds only to actual contributors if implemented.
- **Front-End (React/Vue/etc.):** Create a simple web interface for this. Users should be able to:
- Connect their wallet.
- Create a new campaign by inputting title, description, goal, deadline → call `create_campaign`.
- View a list of campaigns (fetch from on-chain accounts, maybe by using the connection to get all Campaign accounts).
- For each campaign, show its details (title, description, goal, current funds, deadline, creator).
- If a campaign is still ongoing and not funded, allow the user to contribute by entering an amount and sending a transaction (`contribute` instruction).
- If a campaign succeeded and the user is the creator, show a "Withdraw" button to withdraw funds (`withdraw_funds`).
- If a campaign failed and refunds are implemented, show a "Refund" option for contributors.
- **General:** Use Anchor’s client or @solana/web3.js in the front-end to interact with the program. Provide clear error messages in the UI (e.g., “Campaign not found” or “Contribution too high”). Include code comments, especially in the on-chain logic for tricky parts like checking timestamps and handling lamport transfers.
This prompt outlines a full mini-application with multiple instructions and a UI with various states. It’s quite detailed (which is good), and showcases a reusable pattern: breaking down the specification by component (state, each instruction, front-end features). The structure makes it easier for the AI to not forget any part.
Last updated