Skip to content

Commit

Permalink
Merge pull request #2 from Daltonic/completed
Browse files Browse the repository at this point in the history
Completed
  • Loading branch information
Daltonic authored Dec 20, 2024
2 parents 74e3f69 + b72b031 commit 96bbaf7
Show file tree
Hide file tree
Showing 41 changed files with 836 additions and 868 deletions.
2 changes: 1 addition & 1 deletion anchor/Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ resolution = true
skip-lint = false

[programs.localnet]
fundus = "AWMHjJYpfc8iDrHRALDryZtw8X2FQubnwH5ztGnxSatu"
fundus = "8tfuvG2FVPUE41keGjgybaftAiw9UQBK3FGtYxMyzJ5W"

[registry]
url = "https://api.apr.dev"
Expand Down
22 changes: 10 additions & 12 deletions anchor/programs/fundus/src/errors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anchor_lang::prelude::*;

#[error_code]
pub enum ErrorCode {
#[msg("The program has already been initialized.")]
#[msg("Th program has already been initialied.")]
AlreadyInitialized,
#[msg("Title exceeds the maximum length of 64 characters.")]
TitleTooLong,
Expand All @@ -12,24 +12,22 @@ pub enum ErrorCode {
ImageUrlTooLong,
#[msg("Invalid goal amount. Goal must be greater than zero.")]
InvalidGoalAmount,
#[msg("Unauthorized access.")]
Unauthorized,
#[msg("Campaign not found.")]
CampaignNotFound,
#[msg("Campaign is inactive.")]
InactiveCampaign,
#[msg("Donation amount must be at least 1 SOL.")]
InvalidDonationAmount,
#[msg("Unauthorized access.")]
Unauthorized,
#[msg("Withdrawal amount must be greater than zero.")]
#[msg("Campaign goal reached.")]
CampaignGoalActualized,
#[msg("Withdrawal amount must be at least 1 SOL.")]
InvalidWithdrawalAmount,
#[msg("Insufficient funds in the campaign.")]
InsufficientFund,
#[msg("Invalid platform fee percentage.")]
InvalidPlatformFee,
#[msg("The provided platform address is invalid.")]
InvalidPlatformAddress,
#[msg("Campaign not found.")]
CampaignNotFound,
#[msg("Campaign already received funding.")]
CampaignAlreadyFunded,
#[msg("Campaign goal reached.")]
CampaignGoalActualized,
#[msg("Invalid platform fee percentage.")]
InvalidPlatformFee,
}
12 changes: 4 additions & 8 deletions anchor/programs/fundus/src/instructions/create_campaign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,20 @@ pub fn create_campaign(
if title.len() > 64 {
return Err(TitleTooLong.into());
}

if description.len() > 512 {
return Err(DescriptionTooLong.into());
}

if image_url.len() > 256 {
return Err(ImageUrlTooLong.into());
}

if goal <= 0 {
if goal < 1_000_000_000 {
return Err(InvalidGoalAmount.into());
}

state.campaign_count += 1;

campaign.cid = state.campaign_count;
campaign.creator = *ctx.accounts.creator.key;
campaign.creator = ctx.accounts.creator.key();
campaign.title = title;
campaign.description = description;
campaign.image_url = image_url;
Expand All @@ -49,7 +46,7 @@ pub fn create_campaign(
#[derive(Accounts)]
pub struct CreateCampaignCtx<'info> {
#[account(mut)]
pub creator: Signer<'info>,
pub program_state: Account<'info, ProgramState>,

#[account(
init,
Expand All @@ -64,7 +61,6 @@ pub struct CreateCampaignCtx<'info> {
pub campaign: Account<'info, Campaign>,

#[account(mut)]
pub program_state: Account<'info, ProgramState>,

pub creator: Signer<'info>,
pub system_program: Program<'info, System>,
}
7 changes: 4 additions & 3 deletions anchor/programs/fundus/src/instructions/delete_campaign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use anchor_lang::prelude::*;

pub fn delete_campaign(ctx: Context<DeleteCampaignCtx>, cid: u64) -> Result<()> {
let campaign = &mut ctx.accounts.campaign;
let creator = &ctx.accounts.creator;
let creator = &mut ctx.accounts.creator;

if campaign.creator != creator.key() {
return Err(Unauthorized.into());
Expand All @@ -26,8 +26,6 @@ pub fn delete_campaign(ctx: Context<DeleteCampaignCtx>, cid: u64) -> Result<()>
#[derive(Accounts)]
#[instruction(cid: u64)]
pub struct DeleteCampaignCtx<'info> {
#[account(mut)]
pub creator: Signer<'info>,
#[account(
mut,
seeds = [
Expand All @@ -37,5 +35,8 @@ pub struct DeleteCampaignCtx<'info> {
bump
)]
pub campaign: Account<'info, Campaign>,

#[account(mut)]
pub creator: Signer<'info>,
pub system_program: Program<'info, System>,
}
38 changes: 20 additions & 18 deletions anchor/programs/fundus/src/instructions/donate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,25 @@ use anchor_lang::prelude::*;

pub fn donate(ctx: Context<DonateCtx>, cid: u64, amount: u64) -> Result<()> {
let campaign = &mut ctx.accounts.campaign;
let donor = &ctx.accounts.donor;
let contribution = &mut ctx.accounts.contribution;
let donor = &mut ctx.accounts.donor;
let transaction = &mut ctx.accounts.transaction;

// if !campaign.active {
// return Err(InactiveCampaign.into());
// }
if campaign.cid != cid {
return Err(CampaignNotFound.into());
}

if !campaign.active {
return Err(InactiveCampaign.into());
}

if amount < 1_000_000_000 {
return Err(InvalidDonationAmount.into());
return Err(InvalidGoalAmount.into());
}

if campaign.amount_raised >= campaign.goal {
return Err(CampaignGoalActualized.into());
}

// Transfer lamports from the donor to the campaign
let tx_instruction = anchor_lang::solana_program::system_instruction::transfer(
&donor.key(),
&campaign.key(),
Expand All @@ -33,29 +36,26 @@ pub fn donate(ctx: Context<DonateCtx>, cid: u64, amount: u64) -> Result<()> {
);

if let Err(e) = result {
msg!("Donation transfer failed: {:?}", e); // Logging transfer failure
msg!("Donation transfer failed: {:?}", e);
return Err(e.into());
}

campaign.amount_raised += amount;
campaign.balance += amount;
campaign.donors += 1;

contribution.amount = amount;
contribution.cid = cid;
contribution.owner = donor.key();
contribution.timestamp = Clock::get()?.unix_timestamp as u64;
contribution.credited = true;
transaction.amount = amount;
transaction.cid = cid;
transaction.owner = donor.key();
transaction.timestamp = Clock::get()?.unix_timestamp as u64;
transaction.credited = true;

Ok(())
}

#[derive(Accounts)]
#[instruction(cid: u64)]
pub struct DonateCtx<'info> {
#[account(mut)]
pub donor: Signer<'info>,

#[account(
mut,
seeds = [
Expand All @@ -78,7 +78,9 @@ pub struct DonateCtx<'info> {
],
bump
)]
pub contribution: Account<'info, Transaction>,
pub transaction: Account<'info, Transaction>,

#[account(mut)]
pub donor: Signer<'info>,
pub system_program: Program<'info, System>,
}
8 changes: 5 additions & 3 deletions anchor/programs/fundus/src/instructions/initialize.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use anchor_lang::prelude::*;

use crate::constants::ANCHOR_DISCRIMINATOR_SIZE;
use crate::errors::ErrorCode::AlreadyInitialized;
use crate::states::ProgramState;
use anchor_lang::prelude::*;

pub fn initialize(ctx: Context<InitializeCtx>) -> Result<()> {
let state = &mut ctx.accounts.program_state;
Expand All @@ -12,7 +13,7 @@ pub fn initialize(ctx: Context<InitializeCtx>) -> Result<()> {
}

state.campaign_count = 0;
state.platform_fee = 5; // Default platform fee (in %)
state.platform_fee = 5;
state.platform_address = deployer.key();
state.initialized = true;

Expand All @@ -24,11 +25,12 @@ pub struct InitializeCtx<'info> {
#[account(
init,
payer = deployer,
space = ANCHOR_DISCRIMINATOR_SIZE + ProgramState::INIT_SPACE, // Size for Option<u64>
space = ANCHOR_DISCRIMINATOR_SIZE + ProgramState::INIT_SPACE,
seeds = [b"program_state"],
bump
)]
pub program_state: Account<'info, ProgramState>,

#[account(mut)]
pub deployer: Signer<'info>,
pub system_program: Program<'info, System>,
Expand Down
16 changes: 5 additions & 11 deletions anchor/programs/fundus/src/instructions/update_campaign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fn update_campaign(
goal: u64,
) -> Result<()> {
let campaign = &mut ctx.accounts.campaign;
let creator = &ctx.accounts.creator;
let creator = &mut ctx.accounts.creator;

if campaign.creator != creator.key() {
return Err(Unauthorized.into());
Expand All @@ -21,23 +21,16 @@ pub fn update_campaign(
return Err(CampaignNotFound.into());
}

if campaign.amount_raised > 0 {
return Err(CampaignAlreadyFunded.into());
}

if title.len() > 64 {
return Err(TitleTooLong.into());
}

if description.len() > 512 {
return Err(DescriptionTooLong.into());
}

if image_url.len() > 256 {
return Err(ImageUrlTooLong.into());
}

if goal <= 0 {
if goal < 1_000_000_000 {
return Err(InvalidGoalAmount.into());
}

Expand All @@ -52,8 +45,6 @@ pub fn update_campaign(
#[derive(Accounts)]
#[instruction(cid: u64)]
pub struct UpdateCampaignCtx<'info> {
#[account(mut)]
pub creator: Signer<'info>,
#[account(
mut,
seeds = [
Expand All @@ -63,5 +54,8 @@ pub struct UpdateCampaignCtx<'info> {
bump
)]
pub campaign: Account<'info, Campaign>,

#[account(mut)]
pub creator: Signer<'info>,
pub system_program: Program<'info, System>,
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@ pub fn update_platform_settings(
let state = &mut ctx.accounts.program_state;
let updater = &ctx.accounts.updater;

// Ensure the caller is the current platform address
if updater.key() != state.platform_address {
return Err(Unauthorized.into());
}

if !(1..=100).contains(&new_platform_fee) {
if !(1..=15).contains(&new_platform_fee) {
return Err(InvalidPlatformFee.into());
}

// Update platform settings
state.platform_fee = new_platform_fee;

Ok(())
Expand All @@ -27,12 +25,12 @@ pub fn update_platform_settings(
#[derive(Accounts)]
pub struct UpdatePlatformSettingsCtx<'info> {
#[account(mut)]
pub updater: Signer<'info>, // The signer must be the current platform address
pub updater: Signer<'info>,

#[account(
mut,
seeds = [b"program_state"],
bump
)]
pub program_state: Account<'info, ProgramState>,
}
}
Loading

0 comments on commit 96bbaf7

Please sign in to comment.