Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

249
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda