1

I am trying to write a simple anchor program that will burn the SPL tokens that the user sends to my anchor program. I followed the guide below on QuickNode to build a new program that allows minting.

QuickNode Guide I followed

I then modified the code a bit so that when the user sends SOL to the program, it calculates how much of the SPL Token to mint and sends it back to the users ATA account they provded.

I am now trying to do it the other way around. I want to have the user send the SPL token to the function in the program. Burn the token, and then send sol back.

I followed a couple guides, however, I cant seem to find if it is possible to burn the SPL token right then and there, or if I have to have them transfer the SPL to another program owned PDA and then burn it there.

Is there a way to just have instruction for the SPL token to be burned right when its sent to the program function without first sending it to a program owned ata? Just a simple burn with the sender signature?

I followed a couple guides including Burning Guide but they all seem to need the program to own the token before they are burned.

I also checked out this documentation and it just seems like gibberish to me without someone to explain it. Burn Information Solana

1 Answer 1

1

Here is a simple snippet to achieve this

#[program]
pub mod anchor_basics_spl {
    use super::*;

    pub fn burn_spl_tokens(ctx: Context<BurnSPLToken>, amount: u64) -> Result<()> {
        let cpi_program = ctx.accounts.token_program.to_account_info();
        let cpi_accounts = Burn {
            mint: ctx.accounts.mint.to_account_info(),
            from: ctx.accounts.token_account.to_account_info(),
            authority: ctx.accounts.payer.to_account_info(),
        };

        let bump = &[ctx.bumps.mint];
        let signer_seeds: &[&[&[u8]]] = &[&[b"mint", bump]];

        let cpi_ctx = CpiContext::new_with_signer(cpi_program, cpi_accounts, signer_seeds);
        burn(cpi_ctx, amount)?;

        // todo: calc and transfer sol back to user

        Ok(())
    }
}

#[derive(Accounts)]
pub struct BurnSPLToken<'info> {
    #[account(
        mut,
        seeds = [b"mint"],
        bump,
        mint::authority = mint,
    )]
    pub mint: Account<'info, Mint>,
    #[account(mut)]
    pub token_account: Account<'info, TokenAccount>,
    #[account(mut)]
    pub payer: Signer<'info>,
    pub rent: Sysvar<'info, Rent>,
    pub system_program: Program<'info, System>,
    pub token_program: Program<'info, Token>,
    pub associated_token_program: Program<'info, AssociatedToken>,
}

In this guide, the mint is a PDA and it maintains authority over itself. This means that any operation that you need to perform must happen through this program.

5
  • Thank you for the good information! A couple questions on this. Why did I have to change the authority in the "authority: ctx.accounts.mint.to_account_info()," to authority: ctx.accounts.payer.to_account_info(), for this to work? Is it because the signer has authority over their own ATA? I followed that QuickNode guide attached in original question and the token account has authority over itself? solscan.io/token/… Is this bad practice? Or is this safe? Commented Nov 20 at 17:08
  • In the guide, they use update_authority: ctx.accounts.mint.to_account_info(), to initialize the token. Is this where I am getting confused? Should this actually be owned by the program? A program PDA? Or or is this safe? I see in your snippet the program has to sign it with the seeds and bump, does this automatically use the program id in it as well? Is that how users would not just be able to mint or burn if they copied the seed and bump? Commented Nov 20 at 17:14
  • aaah, nice catch. yes, that's it for the first question. update_authority is for the Metaplex metadata account and it determines who will be able to update the token metadata. It is correct because in the guide, it seems the are giving the authorities to the mint. However most folks give the authority to the payer account and it depends with preference I guess.
    – Jimii
    Commented Nov 20 at 17:36
  • using the mint as a PDA doesn't change anything except that any token operation that needs to happen will need to go through your program and you''ll have to write the instructions for them. As you've noted, you'll also need to sign with the seeds for the mint account any ix that requires a signer .
    – Jimii
    Commented Nov 20 at 17:37
  • 1
    Thank you! This was extremely helpful. Commented Nov 20 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.