Testing out devnet and airdrops. Below is my code for a basic wallet with a 2 sol airdrop. I am trying to convert Lamports back into SOL by dividing LAMPARTS_PER_SOL constant. The expected output should be Wallet balance is 2 when dividing by the constant mentioned above, instead it's Wallet balance is 0.
How can I resolve this?
My code:
const {
Connection,
PublicKey,
clusterApiUrl,
Keypair,
LAMPORTS_PER_SOL,
} = require("@solana/web3.js");
const wallet = new Keypair();
const publicKey = new PublicKey(wallet._keypair.publicKey);
const secretKey = wallet._keypair.secretKey;
const getWalletBalance = async () => {
try {
const connection = new Connection(clusterApiUrl("devnet"), "confirmed");
const walletBalance = await connection.getBalance(publicKey);
console.log(`Wallet balance is ${walletBalance}`);
} catch (err) {
console.error(err);
}
};
const airDropSol = async () => {
try {
const connection = new Connection(clusterApiUrl("devnet"), "confirmed");
const fromAirDropSignature = await connection.requestAirdrop(
publicKey,
(2 * LAMPORTS_PER_SOL) /= LAMPORTS_PER_SOL
);
await connection.confirmTransaction(fromAirDropSignature);
} catch (err) {
console.log(err);
}
};
const main = async () => {
await getWalletBalance();
await airDropSol();
await getWalletBalance();
};
main();