1

Let's say I have PDA A, PDA B, and PDA C. PDA A has lamports. And I like to split lamports to B and C. Is it possible in a single anchor function to transfer lamports from A to B and C?

// TRANSACTION 1 (From A to B)
**ctx.accounts.account_b.to_account_info().lamports.borrow_mut() = account_b_lamports
            .checked_add(lamports_to_b)
            .unwrap();

**ctx.accounts.account_a.to_account_info().lamports.borrow_mut() = account_a_lamports
            .checked_sub(lamports_to_b)
            .unwrap();

 // TRANSACTION 2 (From A to C)
**ctx.accounts.account_c.to_account_info().lamports.borrow_mut() = account_c_lamports
            .checked_add(lamports_to_c)
            .unwrap();

**ctx.accounts.account_a.to_account_info().lamports.borrow_mut() = account_a_lamports
            .checked_sub(lamports_to_c)
            .unwrap();

Is this possible? I'm running into a program error: Error processing Instruction 0

1 Answer 1

0

I was able to solve this by setting up the transaction like this..

// TRANSACTION 1 (From A to B)
**ctx.accounts.account_b.to_account_info().lamports.borrow_mut() = account_b_lamports
            .checked_add(lamports_to_b)
            .unwrap();

// TRANSACTION 2 (From A to C)
**ctx.accounts.account_c.to_account_info().lamports.borrow_mut() = account_c_lamports
            .checked_add(lamports_to_c)
            .unwrap();

// SINGLE TRANSFER OUT FROM A
**ctx.accounts.account_a.to_account_info().lamports.borrow_mut() = account_a_lamports
            .checked_sub(total_lamports_to_c_and_b)
            .unwrap();

I'm not sure the reason why but this ended up working.

1
  • In your first version, account_a_lamports changes after the second line to account_a_lamports - lamports_to_b, but then you later set it to account_a_lamports - lamports_to_c, undoing the deduction of lamports_to_b from account a.
    – Jon C
    Commented Jan 30 at 1:02

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.