2

Sorry for a noob question but im writing a solana program without anchor. And I'm having a problem with transfering sol from pda to a wallet in a program.

pub fn process_instruction(
    program_id: &Pubkey,
    accounts: &[AccountInfo],
    instruction_data: &[u8]
) -> ProgramResult {
    let accounts_iter = &mut accounts.iter();
    let account_user = next_account_info(accounts_iter)?;
    let account_pda = next_account_info(accounts_iter)?;

    let (pda, bump_seed) = Pubkey::find_program_address(&[b"stack"], program_id);

    let cost = Rent::get()?.minimum_balance(300 as usize);

    let create_pda_ix = system_instruction::create_account(
        &account_user.key,
        &account_pda,
        cost,
        300,
        program_id,
    );

    invoke_signed(
        &create_pda_ix,
        &[account_user.clone(), account_pda.clone()],
        &[&[b"stack", &[bump_seed]]], 
    )?;

    // How do i transfer money from the pda to a user wallet

    Ok(())
}

invoking a normal transaction didnt work. What do i do i cant find a native example of this problem.

1 Answer 1

3

Since the PDA is owned by your program, subtract the lamports from the PDA account and add an equal amount to another account.

For example:

    **account_pda.try_borrow_mut_lamports()? -= cost;
    **account_user.try_borrow_mut_lamports()? += cost;

https://github.com/solana-developers/program-examples/blob/main/basics/transfer-sol/native/program/src/instruction.rs#L23-L36

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.