I have some wallets that have some erc20 tokens that I want to move, but these wallets currently do not have etherum for the gas, so I started writing a program to handle this. The idea is to load the private key and then estimate the amount of etherum that I need to transfer into in order to transfer the erc20 tokens. The issue that I am having is that I seem to be having difficulty figuring out how much etherum to send. It works sometimes, and then other times the transaction fails with this error:

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

It doesn't happen all of the time, rather probably half the time. I posted a full log of what is going on here: https://termbin.com/hxn1

It does not make sense to me because there is always ETH leftover after I send the tokens, so why is it claiming that there is not enough eth for gas?

My program is written in python and I am using web3. I am sending erc20 like:

unicorns = self.web3.eth.contract(address=contact_address, abi=EIP20_ABI)

raw_txn = {
        "from": '0xDb2733F1143D59E2bF096F7Ee6d9cF2f61352F82',
        "gas": 200000,  # 200000
        'maxPriorityFeePerGas': max_priority_fee_per_gas,
        'maxFeePerGas': max_fee_per_gas,
        "to": contact_address,
        "value": "0x0",
        "data": unicorns.encodeABI('transfer', args=(to_address, raw_amount)),
        "nonce": self.web3.eth.getTransactionCount(from_address),
        "chainId": self.web3.eth.chain_id
    signed_txn = self.web3.eth.account.signTransaction(raw_txn, private_key)
    tx = self.web3.eth.sendRawTransaction(signed_txn.rawTransaction)

And of course right now I am testing on testnet till I get this right. [1]: https://

1 Answer 1


Turns out this solves the issue:

unicorns = self.web3.eth.contract(address=contact_address, abi=EIP20_ABI)
est = unicorns.functions.transfer(to_address, raw_amount).estimateGas({'from': '0xDb2733F1143D59E2bF096F7Ee6d9cF2f61352F82'})
est = self.web3.eth.estimate_gas(raw_txn)

signed_txn = self.web3.eth.account.signTransaction(raw_txn, private_key)
ret = self.web3.eth.sendRawTransaction(signed_txn.rawTransaction)

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.