4

in a nutshell I'm wondering how to send one CPI call with a vector of Instructions.

Here's an example:

I noticed there's a function in the solana_program Rust crate called:

solana_program::system_instruction::transfer_many

It's parameters & return type are as follows:

pub fn transfer_many(
    from_pubkey: &Pubkey,
    to_lamports: &[(Pubkey, u64)]
) -> Vec<Instruction>

This will obviously return to me a vector of Instruction types, but as far as I can tell invoke and invoke_signed are designed for one instruction at a time.

How can I use this vector of instructions dynamically, say if I want my program to transfer 3 different amounts to 3 different accounts in one transaction?

One can infer that you could iterate over the vector and use invoke, but that raises three concerns:

  1. This seems sort of strange.
  2. How do I determine, for each invoke call, what accounts to pass?
  3. How do I determine, for each invoke_signed call, what seeds to sign with?

Is there some way to do a single CPI that will send a transaction with multiple instructions?

Link to function: https://docs.rs/solana-program/latest/solana_program/system_instruction/fn.transfer_many.html

1 Answer 1

1

Invoke and invoke_signed only execute a single ix per execution.

TransferMany does this:

pub fn transfer_many(from_pubkey: &Pubkey, to_lamports: &[(Pubkey, u64)]) -> Vec<Instruction> {
    to_lamports
        .iter()
        .map(|(to_pubkey, lamports)| transfer(from_pubkey, to_pubkey, *lamports))
        .collect() }

So the elements will in order contain the result of solana_program::system_instruction::transfer(from_pubkey, to_pubkey, lamports)

which means the seeds will always be the signer/sender seeds, and accounts will be the corresponding [from_pubkey, to_pubkey] accountInfos.

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.