Skip to content

Commit

Permalink
Refactor (part 2)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiendv89 committed Aug 20, 2024
1 parent e08af9b commit d2b53ae
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 43 deletions.
31 changes: 9 additions & 22 deletions programs/locker/src/instructions/claim.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anchor_spl::token::{Token, TokenAccount, Transfer};
use anchor_spl::token::{Token, TokenAccount};

use crate::*;
use crate::util::token::transfer_to_recipient;

#[event_cpi]
#[derive(Accounts)]
Expand All @@ -23,26 +24,6 @@ pub struct ClaimCtx<'info> {
pub token_program: Program<'info, Token>,
}

impl<'info> ClaimCtx<'info> {
fn transfer_to_recipient(&self, amount: u64) -> Result<()> {
let escrow = self.escrow.load()?;
let escrow_seeds = escrow_seeds!(escrow);
anchor_spl::token::transfer(
CpiContext::new_with_signer(
self.token_program.to_account_info(),
Transfer {
from: self.escrow_token.to_account_info(),
to: self.recipient_token.to_account_info(),
authority: self.escrow.to_account_info(),
},
&[&escrow_seeds[..]],
),
amount,
)?;
Ok(())
}
}

pub fn handle_claim(ctx: Context<ClaimCtx>, max_amount: u64) -> Result<()> {
let mut escrow = ctx.accounts.escrow.load_mut()?;
let escrow_token = anchor_spl::associated_token::get_associated_token_address(
Expand All @@ -58,7 +39,13 @@ pub fn handle_claim(ctx: Context<ClaimCtx>, max_amount: u64) -> Result<()> {
let amount = escrow.claim(max_amount)?;
drop(escrow);

ctx.accounts.transfer_to_recipient(amount)?;
transfer_to_recipient(
&ctx.accounts.escrow,
&ctx.accounts.escrow_token,
&ctx.accounts.recipient_token,
&ctx.accounts.token_program,
amount,
)?;

let current_ts = Clock::get()?.unix_timestamp as u64;
emit_cpi!(EventClaim {
Expand Down
29 changes: 9 additions & 20 deletions programs/locker/src/instructions/create_vesting_escrow.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use anchor_spl::token::{Token, TokenAccount, Transfer};
use anchor_spl::token::{Token, TokenAccount};

use crate::*;
use crate::safe_math::SafeMath;
use crate::util::token::transfer_to_escrow;

#[derive(AnchorSerialize, AnchorDeserialize)]
/// Accounts for [locker::create_vesting_escrow].
Expand Down Expand Up @@ -101,24 +102,6 @@ pub struct CreateVestingEscrowCtx<'info> {
pub system_program: Program<'info, System>,
}

impl<'info> CreateVestingEscrowCtx<'info> {
fn transfer_to_escrow(&self, amount: u64) -> Result<()> {
anchor_spl::token::transfer(
CpiContext::new(
self.token_program.to_account_info(),
Transfer {
from: self.sender_token.to_account_info(),
to: self.escrow_token.to_account_info(),
authority: self.sender.to_account_info(),
},
),
amount,
)?;

Ok(())
}
}

pub fn handle_create_vesting_escrow(
ctx: Context<CreateVestingEscrowCtx>,
params: &CreateVestingEscrowParameters,
Expand All @@ -142,7 +125,13 @@ pub fn handle_create_vesting_escrow(
ctx.bumps.escrow,
)?;

ctx.accounts.transfer_to_escrow(params.get_total_deposit_amount()?)?;
transfer_to_escrow(
&ctx.accounts.sender,
&ctx.accounts.sender_token,
&ctx.accounts.escrow_token,
&ctx.accounts.token_program,
params.get_total_deposit_amount()?,
)?;

let &CreateVestingEscrowParameters {
start_time,
Expand Down
2 changes: 1 addition & 1 deletion programs/locker/src/instructions/v2/claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use anchor_spl::token_interface::{

use crate::*;
use crate::constants::transfer_memo;
use crate::util::{AccountsType, MemoTransferContext, parse_remaining_accounts, ParsedRemainingAccounts, RemainingAccountsSlice, transfer_to_recipient_v2};
use crate::util::{AccountsType, MemoTransferContext, parse_remaining_accounts, ParsedRemainingAccounts, transfer_to_recipient_v2};

#[event_cpi]
#[derive(Accounts)]
Expand Down
1 change: 1 addition & 0 deletions programs/locker/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub use remaining_accounts::*;
pub use role::*;
pub use token2022::*;

pub mod token;
pub mod token2022;
pub mod remaining_accounts;
pub mod role;
Expand Down
51 changes: 51 additions & 0 deletions programs/locker/src/util/token.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use anchor_lang::prelude::*;
use anchor_spl::token::{Token, TokenAccount, Transfer};

use crate::VestingEscrow;

pub fn transfer_to_escrow<'info>(
sender: &Signer<'info>,
sender_token: &Account<'info, TokenAccount>,
escrow_token: &Account<'info, TokenAccount>,
token_program: &Program<'info, Token>,
amount: u64,
) -> Result<()> {
anchor_spl::token::transfer(
CpiContext::new(
token_program.to_account_info(),
Transfer {
from: sender_token.to_account_info(),
to: escrow_token.to_account_info(),
authority: sender.to_account_info(),
},
),
amount,
)?;

Ok(())
}

pub fn transfer_to_recipient<'info>(
escrow: &AccountLoader<'info, VestingEscrow>,
escrow_token: &Account<'info, TokenAccount>,
recipient_token: &Account<'info, TokenAccount>,
token_program: &Program<'info, Token>,
amount: u64,
) -> Result<()> {
let escrow_state = escrow.load()?;
let escrow_seeds = escrow_seeds!(escrow_state);

anchor_spl::token::transfer(
CpiContext::new_with_signer(
token_program.to_account_info(),
Transfer {
from: escrow_token.to_account_info(),
to: recipient_token.to_account_info(),
authority: escrow.to_account_info(),
},
&[&escrow_seeds[..]],
),
amount,
)?;
Ok(())
}

0 comments on commit d2b53ae

Please sign in to comment.