michaelhly / solana-py

Solana Python SDK
https://michaelhly.github.io/solana-py
MIT License
979 stars 252 forks source link

send_transaction insufficient funds for rent #401

Closed richoarbianto closed 6 months ago

richoarbianto commented 6 months ago

I work on Ubuntu 20.04 and use Python 3.8.10. I'm using Solana version 0.32.0 and Solder version 0.20.

When I run the send_transaction command, it shows an error of insufficient balance, even though I've checked before the transaction that my balance is sufficient. Please assist me.

>>> from solders.keypair import Keypair
>>> from solders.pubkey import Pubkey
>>> from solana.rpc.api import Client
>>> from solders.system_program import TransferParams, transfer
>>> from solana.transaction import Transaction
>>> sol_provider = 'https://api.mainnet-beta.solana.com'
>>> solana_client = Client(sol_provider)
>>> sol_key = "my key"
>>> sender = Keypair.from_seed(bytes(sol_key[:32]))
>>> txn = Transaction(fee_payer = sender.pubkey()).add(transfer(TransferParams(from_pubkey=sender.pubkey(), to_pubkey=Pubkey.from_string('to address'), lamports=10000)))
>>> txn.sign_partial(sender)
>>> txn.verify_signatures()
True
>>> solana_client.get_balance(sender.pubkey())
GetBalanceResp {
    context: RpcResponseContext {
        slot: 254485364,
        api_version: Some(
            "1.17.21",
        ),
    },
    value: 343539498,
}
>>> k = solana_client.send_transaction(txn, sender)
solana.rpc.core.RPCException: SendTransactionPreflightFailureMessage { message: "Transaction simulation failed: Transaction results in an account (1) with insufficient funds for rent", data: RpcSimulateTransactionResult(RpcSimulateTransactionResult { err: Some(InsufficientFundsForRent { account_index: 1 }), logs: Some(["Program 11111111111111111111111111111111 invoke [1]", "Program 11111111111111111111111111111111 success"]), accounts: None, units_consumed: Some(150), return_data: None, inner_instructions: None }) }
richoarbianto commented 6 months ago

I have successfully resolved the issue on my own.

It turns out that the amount of lamports sent must be sufficiently large, perhaps a minimum of 0.002 SOL or in lamports (2000000), and the data type for lamports must be an integer (int).

For those facing similar issues like mine, try the tips above. Good luck.