0

I am creating accounts for my application using custodial wallets while creating account I am getting above error.

import {
  Connection,
  Keypair,
  LAMPORTS_PER_SOL,
  SystemProgram,
  Transaction,
  VersionedTransaction,
} from "@solana/web3.js";

const generateKeyPair = async () => {
  try {
    const connection = new Connection(
      RPC_DEVNET_URL_FROM_ALCHEMY,
    );

    const payer = Keypair.generate();
    console.log("Payer:", payer.publicKey.toBase58());

    const keypair = Keypair.generate();
    console.log("New Account:", keypair.publicKey.toBase58());

    await connection.requestAirdrop(payer.publicKey, 5000 * LAMPORTS_PER_SOL);
    waitForAirdropConfirmation(connection, payer.publicKey);
    const lamports = await connection.getMinimumBalanceForRentExemption(0);

    const createAccountIx = SystemProgram.createAccount({
      fromPubkey: payer.publicKey,
      newAccountPubkey: keypair.publicKey,
      lamports,
      space: 0,
      programId: SystemProgram.programId,
    });

    const recentBlockhash = await connection.getLatestBlockhash();

    const transaction = new Transaction({
      recentBlockhash: recentBlockhash.blockhash,
      feePayer: payer.publicKey,
    });

    transaction.add(createAccountIx);

    transaction.sign(payer, keypair);

    console.log("Transaction after signing:", transaction);

    await new Promise(resolve => setTimeout(resolve, 6000)); 
    const signature = await connection.sendTransaction(transaction, [payer, keypair]);

    console.log("Transaction completed. Signature:", signature);
  } catch (error) {
    console.error("Error:", error);
  }
};

const waitForAirdropConfirmation = async (connection, publicKey) => {
  while (true) {
    const balance = await connection.getBalance(publicKey);
    if (balance > 0) {
      console.log(`Airdrop confirmed for ${publicKey.toBase58()}`);
      break;
    }
    await sleep(5000); // Check balance every 5 seconds
  }
};

const sleep = ms => {
  return new Promise(resolve => setTimeout(resolve, ms));
};

generateKeyPair();

Have also tried following code :

import {
  Connection,
  Keypair,
  LAMPORTS_PER_SOL,
  SystemProgram,
  TransactionMessage,
  VersionedTransaction,
} from "@solana/web3.js";

const generateKeyPair = async () => {
  try {
    const connection = new Connection(
      RPC_URL_FROM_ALCHEMY_OF_DEVNET,
    );

    const payer = Keypair.generate();
    console.log(payer);
    const keypair = Keypair.generate();
    const space = 0;
    await connection.requestAirdrop(payer.publicKey, 5000 * LAMPORTS_PER_SOL);
    await connection.requestAirdrop(keypair.publicKey, 3000 * LAMPORTS_PER_SOL);
    const lamports = await connection.getMinimumBalanceForRentExemption(space);
    console.log(payer.publicKey);
    console.log(keypair.publicKey);

    const createAccountIx = SystemProgram.createAccount({
      fromPubkey: payer.publicKey,
      newAccountPubkey: keypair.publicKey,
      lamports,
      space,
      programId: SystemProgram.programId,
    });

    let recentBlockhash = await connection.getLatestBlockhash().then(res => res.blockhash);

    const message = new TransactionMessage({
      payerKey: payer.publicKey,
      recentBlockhash,
      instructions: [createAccountIx],
    }).compileToV0Message();

    const tx = new VersionedTransaction(message);

    tx.sign([payer, keypair]);

    console.log("tx after signing:", tx);
    const sig = await connection.sendTransaction(tx);
    console.log("Transaction completed.");
    // const sig = await connection.sendTransaction(tx);
  } catch (error) {
    console.log(error);
  }
};

generateKeyPair();

1 Answer 1

1

You are requesting 5000 Sol, which is far above the rate limit. You can ask for max 2 Sol a day. To be safer, request 1 Sol and check if it is working.

1
  • Thanks for the solution it is working now with little tweak for confirming the transaction. ``` await connection.confirmTransaction({ signature: airdropSignature }); ```
    – Viraj
    Mar 24 at 13:03

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.