Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"How to sign with a PDA" seem not work #457

Open
anvodev opened this issue Sep 29, 2022 · 0 comments
Open

"How to sign with a PDA" seem not work #457

anvodev opened this issue Sep 29, 2022 · 0 comments

Comments

@anvodev
Copy link

anvodev commented Sep 29, 2022

Link: https://solanacookbook.com/references/accounts.html#how-to-sign-with-a-pda

I tested this snippet

invoke_signed(
    &system_instruction::transfer(
        &pda_account_info.key,
        &to_account_info.key,
        100_000_000, // 0.1 SOL
    ),
    &[
        pda_account_info.clone(),
        to_account_info.clone(),
        system_program_account_info.clone(),
    ],
    &[&[b"escrow", &[bump_seed]]],
)?;

And has this error:
instruction spent from the balance of an account it does not own

I assume the reason, in this case, is that: the system_program checks who request the transfer and finds out the PDA, which is illegal because the PDA is not the owner of "itself", PDA cannot modify PDA's balance.
Instead, I run directly the SOL transfer in the program and it works. My theory is that the program is the owner of PDA so it can modify the SOL balance of PDA directly.

**pda_account_info.try_borrow_mut_lamports()? -= 100_000_000;
**to_account_info.try_borrow_mut_lamports()? += 100_000_000;

If my assumption is right, the snippet will be irrelevant when we want to transfer SOL from PDA. My suggestion is, to demonstrate the PDA signing, we can:

  1. Show snippet for transferring token from PDA's token account
  2. Just mention the case creating a PDA account which already has a snippet mentioned before in the previous section.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant