[Ethereum] Getting this error `ValueError: {‘code’: -32000, ‘message’: ‘insufficient funds for gas * price + value’}` on raw transaction send

erroretherweb3.py

I know for sure I have enough eth on account. Some prelims:

I am signing a raw transaction with web3.py, like so:

transaction = {
    'to': '0xSENDHEREFINALLYPLEASE',
    'value': 1000,
    'gas': 2000000,
    'gasPrice': 20000000000,
    'nonce': 0,
    'chainId': 1,
}

I am using INFURA project id env variable in conjunction with this import:

from web3.auto.infura import w3

I sign successfully and attempt to send like so:

signed_tx = w3.eth.account.sign_transaction(transaction, 0xMYPRIVKEY)
w3.eth.sendRawTransaction(signed_tx.rawTransaction)

web3.py version is: 5.10.0

And then bam this error. Any idea what might be causing it?

Running get balance on the sender address I get its balance:

w3.eth.getBalance("0xd93800B7290B37a3ac36e4cDd3F881a929acD4A3")
>> 11000000000000000

Suppose I am writing incorrect units for gas, gasPrice or value ?

2000000 * 20000000000 + 1000 < 11000000000000000

is False, thus the error?

But when I use these params, it says the transaction is underpriced, what the… how does this work

value = 1000
gas = int(2000000 / 4)
gasPrice = 20000000000

Best Answer

The fact that you are not setting the data field implies that you are interested only in sending ether (rather than in executing a contract function).

If the destination address is of an externally-owned account, then you only need 21000.

If the destination address is of a smart-contract account, then you need slightly more than 21000.

However, even when the transaction requires much less gas for executing, configuring it with 'gas': 2000000 requires that you actually have the corresponding amount of ether (gas * gas price + value) in the account whose private key you are using in order to sign the transaction.

And although this account will not be charged that extra amount of gas, you still need to have the corresponding amount of ether (gas * gas price + value) in it.

Related Topic