Created
October 12, 2022 08:05
-
-
Save omgbbqhaxx/7b0d501b218907deefe4105166643db2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use anchor_lang::prelude::*; | |
use anchor_lang::solana_program::entrypoint::ProgramResult; | |
declare_id!("DoV2hRXUUYzyZ7yM9Ttmdv32JLQYJS168sDuugWpU3sQ"); | |
impl DepositeAccount { | |
pub const MAX_SIZE: usize = 2 + (1 + 32) + (4 + 10 * 32); | |
} | |
impl ClaimStatus { | |
pub const MAX_SIZE: usize = 2 + (1 + 32) + (4 + 10 * 32); | |
} | |
#[program] | |
pub mod claimsol { | |
use super::*; | |
pub fn create(ctx: Context<Create>, amount: u64) -> Result<()> { | |
let campaign = &mut ctx.accounts.campaign; | |
campaign.amount_deposited = amount; | |
campaign.admin = *ctx.accounts.user.key; | |
let ix = anchor_lang::solana_program::system_instruction::transfer( | |
&ctx.accounts.user.key(), | |
&ctx.accounts.campaign.key(), | |
amount, | |
); | |
anchor_lang::solana_program::program::invoke( | |
&ix, | |
&[ | |
ctx.accounts.user.to_account_info(), | |
ctx.accounts.campaign.to_account_info(), | |
], | |
); | |
(&mut ctx.accounts.campaign).amount_deposited += amount; | |
Ok(()) | |
} | |
pub fn withdraw(ctx: Context<Withdraw>, amount: u64) -> ProgramResult { | |
let campaign = &mut ctx.accounts.campaign; | |
let user = &mut ctx.accounts.user; | |
let statify = &mut ctx.accounts.claimstatus; | |
msg!("statify here, {:?}", statify.cstat); | |
//if campaign.admin != *user.key { return Err(ProgramError::IncorrectProgramId);} | |
if amount > 100000000 { | |
msg!("You can claim maximum 0.1 sol bro"); | |
return Err(ProgramError::InsufficientFunds); | |
} | |
if statify.cstat == true { | |
msg!("You already claimed 0.1 sol bro"); | |
return Err(ProgramError::InsufficientFunds); | |
} | |
let rent_balance = Rent::get()?.minimum_balance(campaign.to_account_info().data_len()); | |
if **campaign.to_account_info().lamports.borrow() - rent_balance < amount { | |
return Err(ProgramError::InsufficientFunds); | |
} | |
msg!("0.1 successfully claimed."); | |
**campaign.to_account_info().try_borrow_mut_lamports()? -= amount; | |
**user.to_account_info().try_borrow_mut_lamports()? += amount; | |
statify.cstat = true; | |
Ok(()) | |
} | |
} | |
#[derive(Accounts)] | |
pub struct Create<'info> { | |
#[account(init, payer = user, space = 8 + DepositeAccount::MAX_SIZE, seeds=[b"claimsol".as_ref(), user.key().as_ref()], bump)] | |
pub campaign: Account<'info, DepositeAccount>, | |
#[account(mut)] | |
pub user: Signer<'info>, | |
pub system_program: Program<'info, System>, | |
} | |
#[derive(Accounts)] | |
pub struct Withdraw<'info> { | |
#[account(mut)] | |
pub campaign: Account<'info, DepositeAccount>, | |
#[account(init, payer = user, space = 8 + ClaimStatus::MAX_SIZE)] | |
pub claimstatus: Account<'info, ClaimStatus>, | |
#[account(mut)] | |
pub user: Signer<'info>, | |
pub system_program: Program<'info, System>, | |
} | |
#[account] | |
pub struct DepositeAccount { | |
pub admin: Pubkey, | |
pub amount_deposited: u64, | |
} | |
#[account] | |
pub struct ClaimStatus { | |
pub cstat: bool, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment