0

I'm trying to airdrop to my users, but 9 out of 10 of the transactions I send fail, I can't understand the reason, does anyone have any suggestions?

Example Error:

Error sending tokens to J7R6efQq89vFoX7xK8aVEePQB9XteaoEzRwGdt2PXjhJ: TransactionExpiredBlockheightExceededError: Signature 4hdbtQ944EuWdDt9hJK8dCzTPBPHadQh2kzDaWRTwv3oMPuBeCdocP4abNY2dLpPruqmWajF3UTbn7YytFwN4HnJ has expired: block height exceeded.
    at Connection.confirmTransactionUsingBlockHeightExceedanceStrategy (C:\Users\mertm\Desktop\airdrop2\node_modules\@solana\web3.js\lib\index.cjs.js:6542:15)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Connection.confirmTransaction (C:\Users\mertm\Desktop\airdrop2\node_modules\@solana\web3.js\lib\index.cjs.js:6370:14)
    at async sendAndConfirmTransaction (C:\Users\mertm\Desktop\airdrop2\node_modules\@solana\web3.js\lib\index.cjs.js:2158:15) {
  signature: '4hdbtQ944EuWdDt9hJK8dCzTPBPHadQh2kzDaWRTwv3oMPuBeCdocP4abNY2dLpPruqmWajF3UTbn7YytFwN4HnJ'
}

This is my code:

const { Connection, PublicKey, Keypair } = require('@solana/web3.js');
const { getOrCreateAssociatedTokenAccount, transfer } = require('@solana/spl-token');
const fs = require('fs');
const bs58 = require('bs58');

// Adjust these variables according to your needs
const base58SeedPhrase = 'redacted';
const tokenMintAddress = 'redacted';
const recipientsPath = 'recipients.json'; // Path to your JSON file
const decimals = 9; // Token decimals

async function airdropTokens() {
  // Use the custom RPC endpoint
  const connection = new Connection('redacted', 'confirmed');

  const seedBytes = bs58.decode(base58SeedPhrase);
  const wallet = Keypair.fromSeed(seedBytes.slice(0, 32));
  const tokenMint = new PublicKey(tokenMintAddress);
  const recipients = JSON.parse(fs.readFileSync(recipientsPath));

  for (const { address, amount } of recipients) {
    try {
      const recipientPublicKey = new PublicKey(address);

      const senderTokenAccount = await getOrCreateAssociatedTokenAccount(
        connection,
        wallet,
        tokenMint,
        wallet.publicKey
      );

      const recipientTokenAccount = await getOrCreateAssociatedTokenAccount(
        connection,
        wallet,
        tokenMint,
        recipientPublicKey
      );

      console.log(`Sending ${amount} tokens to ${address}...`);

      await transfer(
        connection,
        wallet,
        senderTokenAccount.address,
        recipientTokenAccount.address,
        wallet.publicKey,
        amount * Math.pow(10, decimals),
        []
      );

      console.log(`Successfully sent ${amount} tokens to ${address}`);
    } catch (error) {
      console.error(`Error sending tokens to ${address}:`, error);
    }
  }

  console.log('Airdrop complete');
}

// Start the airdrop
airdropTokens().catch(console.error);
0

1 Answer 1

1

The reason is the network is currently quite congested, so it's not rare that transactions fail, especially if you're not adding priority fees like in your case. Check out this question to learn more about transaction failure and how to handle it.

Not the answer you're looking for? Browse other questions tagged or ask your own question.