#[event]
pub struct FundCreated {
pub fund: Pubkey,
pub creator: Pubkey,
pub name: String,
pub goal: u64,
pub timestamp: i64,
}
#[event]
pub struct ContributionMade {
pub fund: Pubkey,
pub contributor: Pubkey,
pub amount: u64,
pub total_raised: u64,
pub timestamp: i64,
}
#[event]
pub struct DeadlineSet {
pub fund: Pubkey,
pub deadline: u64,
pub timestamp: i64,
}
#[event]
pub struct RefundProcessed {
pub fund: Pubkey,
pub contributor: Pubkey,
pub amount: u64,
pub timestamp: i64,
}
#[event]
pub struct FundsWithdrawn {
pub fund: Pubkey,
pub creator: Pubkey,
pub amount: u64,
pub timestamp: i64,
}
pub fn fund_create(ctx: Context<FundCreate>, name: String, description: String, goal: u64) -> Result<()> {
let fund = &mut ctx.accounts.fund;
fund.name = name.clone();
fund.description = description;
fund.goal = goal;
fund.deadline = 0;
fund.creator = ctx.accounts.creator.key();
fund.amount_raised = 0;
fund.deadline_set = false;
emit!(FundCreated {
fund: fund.key(),
creator: fund.creator,
name: name,
goal: goal,
timestamp: Clock::get()?.unix_timestamp,
});
Ok(())
}
pub fn contribute(ctx: Context<FundContribute>, amount: u64) -> Result<()> {
emit!(ContributionMade {
fund: ctx.accounts.fund.key(),
contributor: ctx.accounts.contributor.key(),
amount: amount,
total_raised: ctx.accounts.fund.amount_raised,
timestamp: Clock::get()?.unix_timestamp,
});
Ok(())
}
pub fn set_deadline(ctx: Context<FundSetDeadline>, deadline: u64) -> Result<()> {
emit!(DeadlineSet {
fund: ctx.accounts.fund.key(),
deadline: deadline,
timestamp: Clock::get()?.unix_timestamp,
});
Ok(())
}
pub fn refund(ctx: Context<FundRefund>) -> Result<()> {
let amount = ctx.accounts.contribution.amount;
emit!(RefundProcessed {
fund: ctx.accounts.fund.key(),
contributor: ctx.accounts.contributor.key(),
amount: amount,
timestamp: Clock::get()?.unix_timestamp,
});
Ok(())
}
pub fn withdraw(ctx: Context<FundWithdraw>) -> Result<()> {
let amount = ctx.accounts.fund.amount_raised;
emit!(FundsWithdrawn {
fund: ctx.accounts.fund.key(),
creator: ctx.accounts.creator.key(),
amount: amount,
timestamp: Clock::get()?.unix_timestamp,
});
Ok(())
}