Skip to main content
Bumped by Community user
deleted 7 characters in body
Source Link
Janio
  • 43
  • 3

I'm trying to send all SOL from one wallet to another. My main problem is that I have many wallets with a small amount of SOL so I want to save as much SOL as I can.

async function transferToMain(keyFile){
  const fromKeypair = Keypair.generate();
  const toKeypair = Keypair.generate();
  const gasFee = 0; // Is there a safe amount?
  const lamports =  await connection.getBalance(fromKeypair.publicKey) - gasFee; 
  console.log(`FROM ${fromKeypair} TO ${toKeypair} AMOUNT ${lamports} BALANCE ${lamports + gasFee}`)
  const transaction = new Transaction().add(SystemProgram.transfer({
    fromPubkey: fromKeypair.publicKey,
    toPubkey: toKeypair.publicKey,
    lamports: lamports
  }));
  const signature = await sendAndConfirmTransaction(
    connection,
    transaction,
    [fromKeypair]
  )
  console.log('SIGNATURE: '+ signature)
  
}

Trying to send SOL exactly equal to balance will yield

SendTransactionError: failed to send transaction: Transaction simulation failed: Transaction leaves an account with a lower balance than rent-exempt minimum

I've tried to add a small amount for transfer fee, but it is prone to failure because gas fee fluctuates. The source code shows that it just takes BigInt/ number type for the lamport argument - and doesn't handle special input : transfer, transferParams.

On the CLI, we can do something like

$solana transfer --from <FROM_ADDRESS> <RECIPIENT_ADDRESS> ALL

I'm wondering if there is a similar way to do it on the JS API?

I'm trying to send all SOL from one wallet to another. My main problem is that I have many wallets with a small amount of SOL so I want to save as much SOL as I can.

async function transferToMain(keyFile){
  const fromKeypair = Keypair.generate();
  const toKeypair = Keypair.generate();
  const gasFee = 0; // Is there a safe amount?
  const lamports =  await connection.getBalance(fromKeypair.publicKey) - gasFee; 
  console.log(`FROM ${fromKeypair} TO ${toKeypair} AMOUNT ${lamports} BALANCE ${lamports + gasFee}`)
  const transaction = new Transaction().add(SystemProgram.transfer({
    fromPubkey: fromKeypair.publicKey,
    toPubkey: toKeypair.publicKey,
    lamports: lamports
  }));
  const signature = await sendAndConfirmTransaction(
    connection,
    transaction,
    [fromKeypair]
  )
  console.log('SIGNATURE: '+ signature)
  
}

Trying to send SOL exactly equal to balance will yield

SendTransactionError: failed to send transaction: Transaction simulation failed: Transaction leaves an account with a lower balance than rent-exempt minimum

I've tried to add a small amount for transfer fee, but it is prone to failure because gas fee fluctuates. The source code shows that it just takes BigInt/ number type for the lamport argument - and doesn't handle special input : transfer, transferParams.

On the CLI, we can do something like

$solana transfer --from <FROM_ADDRESS> <RECIPIENT_ADDRESS> ALL

I'm wondering if there is a similar way to do it on the JS API?

I'm trying to send all SOL from one wallet to another. My main problem is that I have many wallets with a small amount of SOL so I want to save as much SOL as I can.

async function transferToMain(){
  const fromKeypair = Keypair.generate();
  const toKeypair = Keypair.generate();
  const gasFee = 0; // Is there a safe amount?
  const lamports =  await connection.getBalance(fromKeypair.publicKey) - gasFee; 
  console.log(`FROM ${fromKeypair} TO ${toKeypair} AMOUNT ${lamports} BALANCE ${lamports + gasFee}`)
  const transaction = new Transaction().add(SystemProgram.transfer({
    fromPubkey: fromKeypair.publicKey,
    toPubkey: toKeypair.publicKey,
    lamports: lamports
  }));
  const signature = await sendAndConfirmTransaction(
    connection,
    transaction,
    [fromKeypair]
  )
  console.log('SIGNATURE: '+ signature)
  
}

Trying to send SOL exactly equal to balance will yield

SendTransactionError: failed to send transaction: Transaction simulation failed: Transaction leaves an account with a lower balance than rent-exempt minimum

I've tried to add a small amount for transfer fee, but it is prone to failure because gas fee fluctuates. The source code shows that it just takes BigInt/ number type for the lamport argument - and doesn't handle special input : transfer, transferParams.

On the CLI, we can do something like

$solana transfer --from <FROM_ADDRESS> <RECIPIENT_ADDRESS> ALL

I'm wondering if there is a similar way to do it on the JS API?

Source Link
Janio
  • 43
  • 3

How to transfer all SOL from one wallet to another using JS API

I'm trying to send all SOL from one wallet to another. My main problem is that I have many wallets with a small amount of SOL so I want to save as much SOL as I can.

async function transferToMain(keyFile){
  const fromKeypair = Keypair.generate();
  const toKeypair = Keypair.generate();
  const gasFee = 0; // Is there a safe amount?
  const lamports =  await connection.getBalance(fromKeypair.publicKey) - gasFee; 
  console.log(`FROM ${fromKeypair} TO ${toKeypair} AMOUNT ${lamports} BALANCE ${lamports + gasFee}`)
  const transaction = new Transaction().add(SystemProgram.transfer({
    fromPubkey: fromKeypair.publicKey,
    toPubkey: toKeypair.publicKey,
    lamports: lamports
  }));
  const signature = await sendAndConfirmTransaction(
    connection,
    transaction,
    [fromKeypair]
  )
  console.log('SIGNATURE: '+ signature)
  
}

Trying to send SOL exactly equal to balance will yield

SendTransactionError: failed to send transaction: Transaction simulation failed: Transaction leaves an account with a lower balance than rent-exempt minimum

I've tried to add a small amount for transfer fee, but it is prone to failure because gas fee fluctuates. The source code shows that it just takes BigInt/ number type for the lamport argument - and doesn't handle special input : transfer, transferParams.

On the CLI, we can do something like

$solana transfer --from <FROM_ADDRESS> <RECIPIENT_ADDRESS> ALL

I'm wondering if there is a similar way to do it on the JS API?