Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create atas prior to claiming rewards #763

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading