-
-
Notifications
You must be signed in to change notification settings - Fork 3
Open
Description
Create Campaign Logic.
Definition of Done:
- A campaign account can be created by a musical artist (creator).
- The campaign includes title, description, funding goal, start/end dates, and status.
- The campaign uses a Program Derived Address (PDA) for unique identification.
Tasks:
- Define the Campaign struct.
- Create an instruction to initialize a new campaign account.
- Use validation for inputs (e.g., end_date > start_date, valid funding goal).
- Write unit tests to verify campaign creation.
Code Example:
#[account]
pub struct Campaign {
pub creator: Pubkey, // Creator's wallet address
pub title: String, // Campaign title
pub description: String, // Campaign description
pub goal_amount: u64, // Funding goal in lamports
pub current_amount: u64, // Current amount pledged
pub start_date: i64, // Campaign start time
pub end_date: i64, // Campaign end time
pub status: u8, // 0 = active, 1 = completed, 2 = cancelled
}
#[derive(Accounts)]
pub struct CreateCampaign<'info> {
#[account(init, payer = creator, space = 8 + Campaign::MAX_SIZE, seeds = [b"campaign", creator.key().as_ref()], bump)]
pub campaign: Account<'info, Campaign>,
#[account(mut)]
pub creator: Signer<'info>,
pub system_program: Program<'info, System>,
}
pub fn create_campaign(
ctx: Context<CreateCampaign>,
title: String,
description: String,
goal_amount: u64,
start_date: i64,
end_date: i64,
) -> Result<()> {
require!(end_date > start_date, ErrorCode::InvalidDates);
require!(goal_amount > 0, ErrorCode::InvalidGoalAmount);
let campaign = &mut ctx.accounts.campaign;
campaign.creator = *ctx.accounts.creator.key;
campaign.title = title;
campaign.description = description;
campaign.goal_amount = goal_amount;
campaign.current_amount = 0;
campaign.start_date = start_date;
campaign.end_date = end_date;
campaign.status = 0; // Active
Ok(())
}
Metadata
Metadata
Assignees
Labels
No labels