From a14082e85bb95c42a9ea1d4248268fb4db08c3b6 Mon Sep 17 00:00:00 2001 From: bry Date: Fri, 3 Jan 2025 13:29:40 -0600 Subject: [PATCH] init_if_needed ata --- .../src/instructions/withdraw_v0.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/programs/voter-stake-registry/src/instructions/withdraw_v0.rs b/programs/voter-stake-registry/src/instructions/withdraw_v0.rs index dfaa8cf39..a77ee63f5 100644 --- a/programs/voter-stake-registry/src/instructions/withdraw_v0.rs +++ b/programs/voter-stake-registry/src/instructions/withdraw_v0.rs @@ -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 { @@ -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( @@ -27,6 +31,8 @@ pub struct WithdrawV0<'info> { pub position: Box>, pub mint: Box>, #[account( + init_if_needed, + payer = payer, token::mint = mint, token::authority = position_authority, constraint = position_token_account.amount > 0 @@ -47,13 +53,15 @@ pub struct WithdrawV0<'info> { )] pub destination: Box>, + 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(), @@ -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, 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;