#[derive(Accounts)]
#[instruction(idx:u64)]
pub struct Initialize<'info> {
// Derived PDAs
#[account(
init,
payer= user_sending,
seeds=[b"state".as_ref(), user_sending.key().as_ref(), mint_of_token_sent.key().as_ref(), idx.to_le_bytes().as_ref()],
bump,
space =8 +8 +32+ 32+ 32+ 8 +1
)]
state: Account<'info, State>,
#[account(
init,
payer= user_sending,
seeds=[b"wallet".as_ref(), user_sending.key().as_ref(), mint_of_token_sent.key().as_ref(), bridge_idx.to_le_bytes().as_ref()],
bump,
token::mint= mint_of_token_sent,
token::authority=state,
)]
vault: Account<'info, TokenAccount>,
#[account(mut)]
user_sending: Signer<'info>,
mint_of_token_sent: Account<'info, Mint>,
#[account(mut,
constraint=user_sending_token_account.owner==user_sending.key(),
constraint=user_sending_token_account.mint==mint_of_token_sent.key(),
)]
user_sending_token_account: Account<'info, TokenAccount>,
system_program: Program<'info, System>,
token_program: Program<'info, Token>,
rent: Sysvar<'info, Rent>,
}
#[derive(Accounts)]
#[instruction(idx:u64)]
pub struct BurnTx<'info> {
#[account(
mut,
seeds=[b"state".as_ref(), user_sending.key().as_ref(), mint_of_token_sent.key().as_ref(), idx.to_le_bytes().as_ref()],
bump,
has_one= user_sending,
has_one=mint_of_token_sent,
)]
state: Account<'info, State>,
#[account(
mut,
seeds=[b"wallet".as_ref(), user_sending.key().as_ref(), mint_of_token_sent.key().as_ref(), idx.to_le_bytes().as_ref()],
bump,
)]
vault: Account<'info, TokenAccount>,
#[account(mut)]
user_sending: Signer<'info>,
mint_of_token_sent: Account<'info, Mint>,
system_program: Program<'info, System>,
token_program: Program<'info, Token>,
associated_token_program: Program<'info, AssociatedToken>,
rent: Sysvar<'info, Rent>,
}
My endpoint:
pub fn burn_tx(ctx: Context<BurnTx>, idx: u64) -> Result<()> {
let bump = ctx.bumps.state.to_le_bytes();
let binding = ctx.accounts.user_sending.key();
let mint_of_token_sent_pk = ctx.accounts.mint_of_token_sent.key();
let idx_bytes = idx.to_le_bytes();
let inner = vec![
b"state".as_ref(),
binding.as_ref(),
mint_of_token_sent_pk.as_ref(),
idx_bytes.as_ref(),
bump.as_ref(),
];
let outer = vec![inner.as_slice()];
let burn_instruction = Burn {
mint: ctx.accounts.mint_of_token_sent.to_account_info(),
from: ctx.accounts.vault.to_account_info(),
authority: ctx.accounts.state.to_account_info(),
};
let cpi_ctx = CpiContext::new_with_signer(
ctx.accounts.token_program.to_account_info(),
burn_instruction,
outer.as_slice(),
);
anchor_spl::token::burn(cpi_ctx, ctx.accounts.state.amount)?;
let should_close = {
ctx.accounts.escrow_wallet.reload()?;
ctx.accounts.escrow_wallet.amount == 0
};
if should_close {
let ca = CloseAccount {
account: ctx.accounts.vault.to_account_info(),
destination: ctx.accounts.user_sending.to_account_info(),
authority: ctx.accounts.state.to_account_info(),
};
let cpi_ctx = CpiContext::new_with_signer(
ctx.accounts.token_program.to_account_info(),
ca,
outer.as_slice(),
);
anchor_spl::token::close_account(cpi_ctx)?;
}
Ok(())
}
I got this error:
Cross-program invocation with unauthorized signer or writable account.
I am using anchor framework verison 0.29.0.