Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

248
Views
Properly Convert Lamports to SOL

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();
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

In all libraries, I think that there are auxiliary operations to perform these conversions, in the library Solnet for .Net we do this operation:

decimal ConvertToSol(ulong lamports)
{
  return decimal.Round((decimal)lamports / 1000000000m, 9);
}

If you translate it to JS it will give you the same result and it works for you

about 4 years ago · Juan Pablo Isaza Report

0

There is not much that I could find for my particular situation since Solana development is so new, but I was able to resolve it when stumbling upon the parseFloat() method.

I first had to remove the quotient from the const fromAirDropSignature and prepend the parseFloat() method to walletBalanceinside the console.log section.

Intial code:

    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
    );

Output:

    Wallet balance is 0

Working Code:

const getWalletBalance = async () => {
  try {
    const connection = new Connection(clusterApiUrl("devnet"), "confirmed");
    const walletBalance = await connection.getBalance(publicKey);
    console.log(
      `Wallet balance is ${parseFloat(walletBalance) / LAMPORTS_PER_SOL} SOL`
    );
  } catch (err) {
    console.error(err);
  }
};
const airDropSol = async () => {
  try {
    const connection = new Connection(clusterApiUrl("devnet"), "confirmed");

    const fromAirDropSignature = await connection.requestAirdrop(
      publicKey,
      1.0024 * LAMPORTS_PER_SOL
    );

Output:

Wallet balance is 1.0024 SOL

One thing I notice is that when using 'devnet', it will not allow a denomination higher than 2. Any integer below that will work.

about 4 years ago · Juan Pablo Isaza Report

0

So you can convert the walletBalance to Javascript's BigInt, so that you can easily divide by BigInt(LAMPORTS_PER_URL)

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");
          let walletBalance = await connection.getBalance(publicKey);
          **walletBalance = BigInt(walletBalance) / BigInt(LAMPORTS_PER_SOL)**
          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
          );
      
          await connection.confirmTransaction(fromAirDropSignature);
        } catch (err) {
          console.log(err);
        }
      };
      const main = async () => {
        await getWalletBalance();
        await airDropSol();
        await getWalletBalance();
      };
      main();
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!