2

SOL token has been airdropped successfully on the generated payer address. Still I am getting the above error.

Here's the code:

import * as spl from "@solana/spl-token";
import * as web3 from "@solana/web3.js";

const connection = new web3.Connection(web3.clusterApiUrl("devnet"));

const lender = web3.Keypair.generate();
const lenderPk = lender.publicKey;
const payer = web3.Keypair.generate();

async function main() {
    console.log("request airdrop...");
    const tx = await connection.requestAirdrop(
        payer.publicKey,
        web3.LAMPORTS_PER_SOL
    );
    console.log(`Airdrop tx: ${tx}`);

    console.log("creating token mint...");
    const tokenMint = await spl.createMint(
        connection,
        payer,
        lenderPk,
        lenderPk,
        8
    );
    console.log(`Token mint: ${tokenMint}`);

    console.log("creating token address...");
    const lenderTokenAddress = await spl.createAccount(
        connection,
        payer,
        tokenMint,
        lenderPk
    );
    console.log(`Token address: ${tokenMint}`);

    console.log("minting tokens...");
    await spl.mintToChecked(
        connection,
        payer,
        tokenMint,
        lenderTokenAddress,
        lenderPk,
        100e8,
        8
    );

    const lenderTokenAccount = await spl.getAccount(
        connection,
        lenderTokenAddress
    );
    console.log(`Token account account: ${lenderTokenAccount.amount}`);
}

main();

1 Answer 1

3

Try waiting until the airdrop is "finalized", you can also try "confirmed"

  const tx = await connection.requestAirdrop(
    payer.publicKey,
    web3.LAMPORTS_PER_SOL
  )
  console.log(`Airdrop tx: ${tx}`)
  
  const { blockhash, lastValidBlockHeight } =
    await connection.getLatestBlockhash()

  await connection.confirmTransaction(
    {
      blockhash,
      lastValidBlockHeight,
      signature: tx,
    },
    "finalized"
  )
  const newBalance = await connection.getBalance(payer.publicKey)
  console.log("New balance is", newBalance / web3.LAMPORTS_PER_SOL)
1
  • 1
    Thanks. It's working now.
    – denimcodes
    Commented Apr 14, 2023 at 8:52

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.