Skip to main content
added 1 character in body
Source Link
Jimii
  • 7.7k
  • 1
  • 8
  • 22

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.mintpayer.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.

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.mint.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.

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.

Source Link
Jimii
  • 7.7k
  • 1
  • 8
  • 22

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.mint.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.