1

I'm struggling to send SOL to another address(fixed one when initialize) on Rust level along logging the sender's account address on the program.

Do I need PDA for this? Also I have to log the sender's account address on the program.

I tried to write some code by investigating code example. Please let me know if this would work. :)

use anchor_lang::prelude::*;

declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");

#[program]
pub mod transfer_sol {
    use super::*;
    pub fn initialize(ctx: Context<Initialize>) -> ProgramResult {
        Ok(())
    }

    pub fn buy_ticket(ctx: Context<BuyTicket>, amount: u64) -> Result <()> {
        let base_account = &mut ctx.accounts.base_account;
        let user = &mut ctx.accounts.user;

        // Transfer Sol
        let transfer_sol = transfer(
            &ctx.accounts.user.key(),
            base_account.dest,
            amount
        );

        invoke(
            &transfer_sol,
            &[
                *user.to_account_info(),
                ctx.accounts.dest.to_account_info(),
                ctx.accounts.system_program.to_account_info(),
            ],
        )?;

        base_account.users.push(*user.to_account_info().key);

        Ok(())
    }
}

#[derive(Accounts)]
pub struct Initialize<'info> {
    #[account(mut)]
    user: Signer<'info>,
    #[account(
        init,
        payer = user,
        space = 8 + 100,
        seeds = [b"tickets".as_ref()],
        bump
    )]
    base_account: Account<'info, BaseAccount>,
    system_program: Program<'info, System>,
}

#[derive(Accounts)]
pub struct BuyTicket<'info> {
    #[account(mut)]
    pub base_account: Account<'info, BaseAccount>,
    #[account(mut)]
    pub user: Signer<'info>,
    pub system_program: Program<'info, System>,
}

#[account]
pub struct BaseAccount {
    pub users: Vec<PublicKey>,
    pub dest: PublicKey
}

2 Answers 2

2

Since the user's wallet is a signer in the instruction, your example will work! You can propagate their signature down to any other program, which is what you're doing with the invoke into the system program's transfer instruction.

2
  • Thanks for the answer. What do you mean by propagate their signature down to any other program?
    – sunnydev
    Sep 16, 2022 at 4:47
  • If I sign your instruction, I can also be a signer for any CPI that you call from your instruction. So if your instruction calls transfer to move my lamports, that's fine. If your instruction withdraws from my stake account, that's also OK. You can read more about it at docs.solana.com/developing/programming-model/…
    – Jon C
    Sep 16, 2022 at 11:35
2

Simple answer is that you don't need a PDA to transfer Solana from one account to another.

If your goal is to transfer Solana from a system program owned account to another account (owned by any program), then you just need to pass in the signature of the sender account. That signature could come from a PDA or a key pair private key.

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.