If once a PDA is created as an associate token address, it can not transfer SOL from the PDA to another account?
I'd like to transfer both SOL and SPL-Token using a single PDA account.
I tried both solana_program::program::invoke_signed
with system_instruction::transfer
and also try_borrow_mut_lamports()
.
But, it is not working.
Whenever transfer with system_program, I got Transfer:
from must not carry data
, the other way was instruction spent from the balance of an account it does not own
.
What am I missing?
Can I get some advice?
Thx.
the associate token account in Struct
#[account(init,
seeds = [authority.to_account_info().key.as_ref()],
bump,
payer = authority,
token::mint = mint,
token::authority = authority)
]
pub vault: Account<'info, TokenAccount>,
Create another PDA, authority for when trasfering spl-token from the vault account
let (vault_authority, vault_authority_bump) = Pubkey::find_program_address(
&[ctx.accounts.vault.to_account_info().key.as_ref()], ctx.program_id
);
let cpi_accounts = SetAuthority {
account_or_mint: ctx.accounts.vault.to_account_info().clone(),
current_authority: ctx.accounts.authority.to_account_info().clone()
};
let cpi_context = CpiContext::new(ctx.accounts.token_program.to_account_info().clone(), cpi_accounts);
token::set_authority(cpi_context, AuthorityType::AccountOwner, Some(vault_authority))?;
Failed case 1, transfer SOL from vault to owner using system_program
// Error message : Transfer: `from` must not carry data
let vault_key = ctx.accounts.authority.to_account_info().key.as_ref();
let (_vault, bump) = Pubkey::find_program_address(&[vault_key], ctx.program_id);
let seeds = &[vault_key, &[bump]];
let signer = &[&seeds[..]];
let owner_key = &ctx.accounts.owner.key();
let vault_key = &ctx.accounts.vault.key();
let ix = system_instruction::transfer(vault_key, owner_key, amount);
let owner_account = ctx.accounts.owner.to_account_info();
let vault_account = ctx.accounts.vault.to_account_info();
solana_program::program::invoke_signed(&ix, &[vault_account, owner_account], signer)?;
Failed case 2, transfer SOL from vault to owner using 'try_borrow_mut_lamports'
// Error message : instruction spent from the balance of an account it does not own
**ctx.accounts.vault.to_account_info().try_borrow_mut_lamports()? -= amount;
**ctx.accounts.owner.to_account_info().try_borrow_mut_lamports()? += amount;