Skip to content

Commit

Permalink
init_if_needed ata
Browse files Browse the repository at this point in the history
  • Loading branch information
bryzettler committed Jan 3, 2025
1 parent 734e5aa commit a14082e
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions programs/voter-stake-registry/src/instructions/withdraw_v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ use crate::error::*;
use crate::position_seeds;
use crate::state::*;
use anchor_lang::prelude::*;
use anchor_spl::token::Mint;
use anchor_spl::token::{self, Token, TokenAccount};
use anchor_spl::{
associated_token::AssociatedToken,
token::{transfer, Mint, Token, TokenAccount, Transfer},
};

#[derive(AnchorSerialize, AnchorDeserialize, Clone, Default)]
pub struct WithdrawArgsV0 {
Expand All @@ -13,6 +15,8 @@ pub struct WithdrawArgsV0 {
#[derive(Accounts)]
#[instruction(args: WithdrawArgsV0)]
pub struct WithdrawV0<'info> {
#[account(mut)]
pub payer: Signer<'info>,
pub registrar: Box<Account<'info, Registrar>>,

#[account(
Expand All @@ -27,6 +31,8 @@ pub struct WithdrawV0<'info> {
pub position: Box<Account<'info, PositionV0>>,
pub mint: Box<Account<'info, Mint>>,
#[account(
init_if_needed,
payer = payer,
token::mint = mint,
token::authority = position_authority,
constraint = position_token_account.amount > 0
Expand All @@ -47,13 +53,15 @@ pub struct WithdrawV0<'info> {
)]
pub destination: Box<Account<'info, TokenAccount>>,

pub associated_token_program: Program<'info, AssociatedToken>,
pub token_program: Program<'info, Token>,
pub system_program: Program<'info, System>,
}

impl<'info> WithdrawV0<'info> {
pub fn transfer_ctx(&self) -> CpiContext<'_, '_, '_, 'info, token::Transfer<'info>> {
pub fn transfer_ctx(&self) -> CpiContext<'_, '_, '_, 'info, Transfer<'info>> {
let program = self.token_program.to_account_info();
let accounts = token::Transfer {
let accounts = Transfer {
from: self.vault.to_account_info(),
to: self.destination.to_account_info(),
authority: self.position.to_account_info(),
Expand All @@ -65,13 +73,12 @@ impl<'info> WithdrawV0<'info> {
/// Withdraws tokens from a deposit entry, if they are unlocked
///
/// `deposit_entry_index`: The deposit entry to withdraw from.
/// `amount` is in units of the native currency being withdrawn.
pub fn handler(ctx: Context<WithdrawV0>, args: WithdrawArgsV0) -> Result<()> {
let amount = args.amount;

// Transfer the tokens to withdraw.
let seeds = position_seeds!(ctx.accounts.position);
token::transfer(ctx.accounts.transfer_ctx().with_signer(&[seeds]), amount)?;
transfer(ctx.accounts.transfer_ctx().with_signer(&[seeds]), amount)?;

let position = &mut ctx.accounts.position;
let registrar = &ctx.accounts.registrar;
Expand Down

0 comments on commit a14082e

Please sign in to comment.