Can someone point out what I'm doing wrong? I'm trying to transfer SOL
from a PDA
to the to_account
but for some reason when I call it, fails with the following error message: Error processing Instruction 0: invalid program argument
. According the solna cookbook (https://solanacookbook.com/references/accounts.html#how-to-sign-with-a-pda) it suppose to work, but I'm really not sure what I'm doing wrong. Thanks.
use solana_program::{
account_info::next_account_info, account_info::AccountInfo, entrypoint,
entrypoint::ProgramResult, program::invoke_signed, pubkey::Pubkey, system_instruction,
};
use solana_program::rent::Rent;
use solana_program::sysvar::Sysvar;
entrypoint!(process_instruction);
fn process_instruction(
_program_id: &Pubkey,
accounts: &[AccountInfo],
_instruction_data: &[u8],
) -> ProgramResult {
let account_info_iter = &mut accounts.iter();
let _initializer = next_account_info(account_info_iter)?;
let pda = next_account_info(account_info_iter)?;
let to_account = next_account_info(account_info_iter)?;
let system_program = next_account_info(account_info_iter)?;
invoke_signed(
&system_instruction::transfer(
&pda.key,
&to_account.key,
100_000_000, // 0.1 SOL
),
&[
pda.clone(),
to_account.clone(),
system_program.clone(),
],
&[&[b"wallet".as_ref(), &[255]]],
)?;
Ok(())
}
On the client side looks something like this:
const transaction = new Transaction().add(
new TransactionInstruction({
keys: [
{pubkey: signer.publicKey, isSigner: true, isWritable: false},
{pubkey: programDerivedAddress, isSigner: false, isWritable: true},
{pubkey: toAddress, isSigner: false, isWritable: true},
{pubkey: SystemProgram.programId, isSigner: false, isWritable: false}
],
data: Buffer.from(new Uint8Array([])),
programId: programId,
})
);
await sendAndConfirmTransaction(
connection,
transaction,
[signer],
);