3

I'm creating a transaction to send funds to two different addresses with @solana/web3.js.

Let's say that my balance is around 1 SOL I want this transaction to send ALMOST all my funds, so

const transaction = new Transaction().add(
    SystemProgram.transfer({
      fromPubkey: new PublicKey(publicKey),
      toPubkey: new PublicKey("address1"),
      lamports: 1 SOL - 100.000 lamports
    })
  ).add(
    SystemProgram.transfer({
      fromPubkey: new PublicKey(publicKey),
      toPubkey: new PublicKey("address2"),
      lamports: 10.000 lamports
    })
  )

Doing this I should have enough funds to pay the fees and my balance should be around 90.000 lamports after the transaction. Why does this transaction fails?

2 Answers 2

4

Solana accounts require a minimum amount of SOL in order to exists on the blockchain, this is called rent-exempt account.

Currently, on mainnet, this amount is around 1250000 lamports, which your 90.000 lamports do not cover.

3
  • 1
    How can one get exaclty that amount needed to exist? Is there also a simple way to implement a mechanism that sends like ALL the funds in the address?
    – IlSasso
    Oct 5, 2023 at 21:51
  • You can you this call, docs.solana.com/api/http#getminimumbalanceforrentexemption
    – Serban
    Oct 6, 2023 at 6:01
  • Ok, now can i calculate the size in byte of my account?
    – IlSasso
    Oct 6, 2023 at 8:12
1

It this situation when you're trying to send a specific amount of lamports to two different addresses while retaining a certain balance. However, the issue here is how you're calculating the lamports to send. You should calculate and set the lamports based on the difference you want to retain, like this:

const balance = 1 SOL; // Assuming 1 SOL = 10^9 lamports const amountToSend1 = balance - 100000; // Calculate the amount to send to address1 const amountToSend2 = 10000; // Amount to send to address2

const transaction = new Transaction().add( SystemProgram.transfer({ fromPubkey: new PublicKey(publicKey), toPubkey: new PublicKey("address1"), lamports: amountToSend1 }) ).add( SystemProgram.transfer({ fromPubkey: new PublicKey(publicKey), toPubkey: new PublicKey("address2"), lamports: amountToSend2 }) )

1
  • Just started learning web3.js at Christex Foundation Sierra Leone. Thanks to C.oogo Oct 6, 2023 at 21:53

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.