New Solana Anchor developer here and I have a question about how I am currently sending Sol to multiple wallets in my program. Currently im using the code below twice and then invoking two separate transactions.
system_instruction::transfer(from_account.key, to_account.key, amount);
However, I noticed in the Rust documentation there was a system instruction for "transfer_many" using a vec array of instruction.
Can someone explain to me how I would create 1 instruction with transfer_many and than invoke that instruction? The documentation is gibberish to me so if someone would explain in human terms I would appreciate it.
Here is my full code below to send sol to multiple addresses. Please ignore the "feeaccount" as eventually I will change up my code to send a percent of the sol that the program account accepts. (Later down the road once I learn more)
use anchor_lang::prelude::*;
use anchor_spl::token::{self, Token, TokenAccount, Transfer as SplTransfer};
use solana_program::system_instruction;
declare_id!("HBj3u9jbTigqXb47TAF2H44TAoTckVJnjzFc4jxusUN2");
#[derive(Accounts)]
pub struct TransferLamports<'info> {
#[account(mut)]
pub from: Signer<'info>,
#[account(mut)]
pub to: AccountInfo<'info>,
#[account(mut)]
pub feeto: AccountInfo<'info>,
pub system_program: Program<'info, System>,
}
#[program]
pub mod solana_lamport_transfer {
use super::*;
pub fn transfer_lamports(ctx: Context<TransferLamports>, amount: u64) -> Result<()> {
let from_account = &ctx.accounts.from;
let to_account = &ctx.accounts.to;
let fee_account = &ctx.accounts.feeto;
// Create the transfer instruction
let transfer_instruction1 =
system_instruction::transfer(from_account.key, to_account.key, amount);
// Create the transfer instruction
let transfer_instruction2 =
system_instruction::transfer(from_account.key, fee_account.key, amount);
// Invoke the transfer instruction
anchor_lang::solana_program::program::invoke_signed(
&transfer_instruction1,
&[
from_account.to_account_info(),
to_account.clone(),
ctx.accounts.system_program.to_account_info(),
],
&[],
)?;
// Invoke the transfer instruction
anchor_lang::solana_program::program::invoke_signed(
&transfer_instruction2,
&[
from_account.to_account_info(),
fee_account.clone(),
ctx.accounts.system_program.to_account_info(),
],
&[],
)?;
Ok(())
}
}
My code works how I wrote it, however, I'm so lost in using the "transfer_many" snip. Is the code I wrote fine? Or will I run into issues? I noticed that I dont pay a tx fee twice since its in the same call so maybe I dont need to use transfer_many?