I am working on an Anchor Solana program, and I've encountered an error that I'm having trouble resolving. The error is:
"Cross-program invocation with unauthorized signer or writable account." This error occurs when I try to execute a send_sol instruction in my program. Below is the relevant part of my code for context:
Program Structure:
#[program]
mod hello_anchor {
use super::*;
pub fn initialize(ctx: Context<Initialize>, data: u64) -> Result<()> {
let new_account = &mut ctx.accounts.new_account;
new_account.data = data;
new_account.bump = ctx.bumps.new_account;
msg!("Changed data to: {}!", data);
Ok(())
}
pub fn send_sol(ctx: Context<SendSol>) -> Result<()> {
let sender = &mut ctx.accounts.sender;
let reciever = &mut ctx.accounts.reciever;
let system_program = &mut ctx.accounts.system_program;
invoke_signed(
&transfer(&sender.key(), &reciever.key(), 100000),
&[
sender.to_account_info(),
reciever.to_account_info(),
system_program.to_account_info(),
],
&[&[b"sender", &[sender.bump]]],
)?;
Ok(())
}
}
#[derive(Accounts)]
pub struct SendSol<'info> {
#[account(mut)]
pub sender: Account<'info, SigningAccount>,
pub reciever: AccountInfo<'info>,
pub system_program: Program<'info, System>,
}
#[derive(Accounts)]
pub struct Initialize<'info> {
#[account(init, payer = signer, space = 8 + 8 + 1, seeds = [b"sender"], bump)]
pub new_account: Account<'info, SigningAccount>,
#[account(mut)]
pub signer: Signer<'info>,
pub system_program: Program<'info, System>,
}
#[account]
pub struct SigningAccount {
data: u64,
bump: u8,
}
The initialize function works as expected, but the send_sol function, which is supposed to send SOL from a PDA (sender) to a receiver, triggers the error.
Key Points:
I am using a PDA as the sender in the send_sol function. I have ensured that the seeds and bump used for the PDA are consistent. The PDA (sender) should have sufficient lamports for the transaction. I suspect the issue might be related to how I'm handling the PDA or the accounts in the send_sol function, but I'm not sure what I'm missing.
Has anyone encountered a similar issue or can spot what might be going wrong in my approach? Any insights or suggestions would be greatly appreciated.
Thank you in advance!