solana-labs / solana-program-library

A collection of Solana programs maintained by Solana Labs
https://solanalabs.com
Apache License 2.0
3.56k stars 2.08k forks source link

How to send tokens to multiple accountsin bulk #6524

Closed bigbulb closed 7 months ago

bigbulb commented 7 months ago

Hello, I am following the tutorial to send a token to a specific recipient (https://spl.solana.com/token#example-transferring-tokens-to-an-explicit-recipient-token-account). Below is the code that works where I can send the transactions 1 by one, but it would take forever for large amounts of wallets.

const walletsTo = [wallet1, 'wallet2, 'wallet3'...]

  walletsTo.forEach(async function (addressTo) {

    const toWallet = new PublicKey(addressTo);

    const toTokenAccount = await getOrCreateAssociatedTokenAccount(
      connection,
      fromWallet,
      new PublicKey(mintToken),
      toWallet
    );

    let signature = await transfer(
      connection,
      fromWallet,
      fromTokenAccount.address,
      toTokenAccount.address,
      fromWallet.publicKey,
      1
    );

    console.log("Signature tx: ", signature);
  });

Is there a way to do the same but send the token to dozens or hundreds of recipients by batching the transactions?

github-actions[bot] commented 7 months ago

Hi @bigbulb,

Thanks for your question!

We want to make sure to keep signal strong in the GitHub issue tracker – to make sure that it remains the best place to track issues that affect the development of Solana itself.

Questions like yours deserve a purpose-built Q&A forum. Unless there exists evidence that this is a bug with Solana itself, please post your question to the Solana Stack Exchange using this link: https://solana.stackexchange.com/questions/ask


This automated message is a result of having added the ‘question’ tag.