I have SOL in my program account. Is it possible to transfer that native (NOT WSOL) SOL to user?
If yes, How?
You can do something like this with an account that the program owns. The syntax used to access accounts is slightly anchor specific but the idea works in for regular solana programs too.
let authority_lamports = ctx.accounts.authority.lamports();
**ctx.accounts.authority.lamports.borrow_mut() = authority_lamports
.checked_add(ctx.accounts.your_program_account.lamports())
.unwrap();
**ctx.accounts.your_program_account.lamports.borrow_mut() = 0;
authority
is used to denote someone who has to sign a transaction to have control over some account.
// creating SOL transfer instruction
let ix = anchor_lang::solana_program::system_instruction::transfer(
from_pubkey,
&to_pubkey,
amount
);
// invoking SOL transfer instruction
anchor_lang::solana_program::program::invoke(
&ix,
&[
// from
ctx.accounts.from_account.to_account_info(),
// to
ctx.accounts.to_account.to_account_info(),
]
)?;
I think this should work for programs aswell. Ofcourse this would fail if amount is higher than balance or the "from" pubkey is not the signer