Search code examples
pythontransactionsweb3pybsc

Web3 bsc token send with python , gas fee calculation. ValueError: {'code': -32000, 'message': 'insufficient funds for gas * price + value'}


I want to write small python programme that if my acc2 have balance program will detect a positive balance and send it to my other wallet which acc1. Using web3 bsc to create transactions I got error:

"ValueError: {'code': -32000, 'message': 'insufficient funds for gas * price + value'}"

I'm not sure but probably trying to do something wrong with transaction. My acc2 balance got tokens and bnb for gas fees.

from decimal import Decimal
from web3 import Web3
import time
import json


bsc = "https://bsc-dataseed.binance.org/"
web3 = Web3(Web3.HTTPProvider(bsc))
print(web3.isConnected())

# acc_collector_private_key = 'acc2_pkpkpkpk'
acc2_pk='pkpkpkpk'

token_contract = web3.toChecksumAddress('contract of token')

token_abi ='abi'

acc1 = '111111'
acc2    = '222222'
  


token = web3.eth.contract(address=token_contract, abi=token_abi) 
target_token_balance = token.functions.balanceOf(acc2).call() 
target_coin_name=token.functions.name().call()
target_coin_symbol=token.functions.symbol().call()

print(target_coin_name)
print(web3.fromWei(target_token_balance,'ether'))
print(target_coin_symbol)


nonce = web3.eth.getTransactionCount(acc2)

tx = {
    'nonce' : nonce,
    'to' : acc1,
    'value':web3.toWei(target_token_balance,'ether'),
    'gas' : 21000,
    'gasPrice': web3.toWei('50','gwei')

}

signed_tx =web3.eth.account.signTransaction(tx,acc2_pk)
tx_hash = web3.eth.sendRawTransaction(signed_tx.rawTransaction)
trans = web3.toHex(tx_hash)

time.sleep(5)
transaction = web3.eth.get_transaction(trans)
print(transaction)

target_balance  = token.functions.balanceOf(acc2).call() 
print(target_balance)

Solution

  • i think it is because you put the 50 gwei in high comma's.

    from decimal import Decimal
    from web3 import Web3
    import time
    import json
    
    
    bsc = "https://bsc-dataseed.binance.org/"
    web3 = Web3(Web3.HTTPProvider(bsc))
    print(web3.isConnected())
    
    # acc_collector_private_key = 'acc2_pkpkpkpk'
    acc2_pk='pkpkpkpk'
    
    token_contract = web3.toChecksumAddress('contract of token')
    
    token_abi ='abi'
    
    acc1 = '111111'
    acc2    = '222222'
      
    
    
    token = web3.eth.contract(address=token_contract, abi=token_abi) 
    target_token_balance = token.functions.balanceOf(acc2).call() 
    target_coin_name=token.functions.name().call()
    target_coin_symbol=token.functions.symbol().call()
    
    print(target_coin_name)
    print(web3.fromWei(target_token_balance,'ether'))
    print(target_coin_symbol)
    
    
    nonce = web3.eth.getTransactionCount(acc2)
    
    tx = {
        'nonce' : nonce,
        'to' : acc1,
        'value':web3.toWei(target_token_balance,'ether'),
        'gas' : 21000,
        'gasPrice': web3.toWei(50,'gwei') #<---- without the ""
    
    }
    
    signed_tx =web3.eth.account.signTransaction(tx,acc2_pk)
    tx_hash = web3.eth.sendRawTransaction(signed_tx.rawTransaction)
    trans = web3.toHex(tx_hash)
    
    time.sleep(5)
    transaction = web3.eth.get_transaction(trans)
    print(transaction)
    
    target_balance  = token.functions.balanceOf(acc2).call() 
    print(target_balance)
    

    Let me know if it worked.