3

I want to transfer some lamports (100) froma a PDA to an account. This is the Rust code:

pub fn test(ctx: Context<ApproveEscrow>) -> Result<()> {
    // PDA already initilized in another function and with 1 SOL amount
    let pda: &mut Account<ApproverAccount> = &mut ctx.accounts.pda;

    let seed: Pubkey = ctx.accounts.receiver.key();
    let bump_seed: u8 = ctx.bumps.pda;
    let signer_seeds: &[&[&[u8]]] = &[&[b"pda", seed.as_ref(), &[bump_seed]]];

    let cpi_context: CpiContext<Transfer> = CpiContext::new(
        ctx.accounts.system_program.to_account_info(),
        Transfer {
            from: pda.to_account_info(),
            to: ctx.accounts.receiver.to_account_info(),
        }
    ).with_signer(signer_seeds);

    transfer(cpi_context, 100)?;

    Ok(())
}

and this ts test code:

it("Test", async () => {    
        await program.methods
            .test()
            .accounts({ receiver: receiver.publicKey })
            .signers([ receiver ])
            .rpc();
}

Everything seems to work correctly, I logged every public keys and they are correct, yet I cannot understand why the transfer function returns: Error: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: invalid program argument

I'm also sure the PDA account has enough balance to make the transfer. I think it's a signer problem but I really cannot get the problem and the error message does not help so much

1 Answer 1

3

If the program owns the PDA account a simple way to make the transfer is

  pda.sub_lamports(100)?;
  ctx.accounts.receiver.add_lamports(params100)?;
1
  • 1
    Wow man thank you so much for this! I had struggled on it for an entire week.... Anyway I've not be able to find out the reason for this problem...
    – Francesco
    Commented Jun 2 at 21:53

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.