Skip to content

Instantly share code, notes, and snippets.

@QuadradS
Created January 14, 2025 08:52
Show Gist options
  • Select an option

  • Save QuadradS/02f87b1e5242b44332f50a36c1cdebcd to your computer and use it in GitHub Desktop.

Select an option

Save QuadradS/02f87b1e5242b44332f50a36c1cdebcd to your computer and use it in GitHub Desktop.
Create pda and send token back
#[derive(Accounts)]
pub struct SendBack<'info> {
#[account(
mut,
seeds = [constants::CONFIG_ACCOUNT_SEED.as_bytes()],
bump
)]
/// CHECK only read
sender: AccountInfo<'info>,
#[account(mut)]
/// CHECK only read
sender_ata: AccountInfo<'info>,
#[account(mut)]
/// CHECK only read
receiver_ata: Account<'info, TokenAccount>,
token_program: Program<'info, Token>,
system_program: Program<'info, System>,
}
#[derive(Accounts)]
pub struct CreateSplAccount<'info> {
#[account(
init,
space = 8,
payer = user,
seeds = [constants::CONFIG_ACCOUNT_SEED.as_bytes()],
bump
)]
/// CHECK: TEST
spl_tokens_acc: AccountInfo<'info>,
#[account(mut)]
user: Signer<'info>,
system_program: Program<'info, System>,
}
// ---
pub fn send_back<'a, 'b, 'c, 'info>(
ctx: Context<'a, 'b, 'c, 'info, SendBack<'info>>,
) -> Result<()> {
msg!("send_back");
let bump = ctx.bumps.sender;
let config_seed = constants::CONFIG_ACCOUNT_SEED.as_bytes();
let bump_slice = [bump];
let seeds = [config_seed.as_ref(), &bump_slice];
create_close_spl_signed(
ctx.accounts.sender_ata.to_account_info(),
ctx.accounts.receiver_ata.to_account_info(),
ctx.accounts.sender.to_account_info(),
ctx.accounts.token_program.to_account_info(),
&seeds,
)?;
Ok(())
}
pub fn create_spl_accounts<'a, 'b, 'c, 'info>(
ctx: Context<'a, 'b, 'c, 'info, CreateSplAccount<'info>>,
) -> Result<()> {
msg!("ACCOUNT CREATED: {:#?}", &ctx.accounts.spl_tokens_acc.key);
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment