Search code examples
pythonweb3py

Can't send transaction using web3.py (send_raw_transaction method)


I'm testing web3.py trying to send eth from one account to another using "send_raw_transaction" method. I'm using Ganache for testing purposes. Acutally, I'm just following the steps descipbed in the web3 doc. However, every time I get an error. It looks like the problem is in send_raw_transaction method.

Code:

from web3 import Web3
from web3.middleware import geth_poa_middleware
import json

#   GANACHE. TESTING TRANSACTIONS
ganache_url = "http://127.0.0.1:7545"
w3 = Web3(Web3.HTTPProvider(ganache_url))

#   accs and keys
account_1 = "0x72e94DaE4d186639B9Bb2baA89caa4Da5f931231"
account_2 = "0xaB2fF898c56a43f56845180081A0C467f2640332"
privatekey_1 = "0x5af8420db3d42b3fc14a13f64dd8dd490783746fdea406ac68afc9b3b9e51132"

#   getting eth balance
balance_gwei = w3.eth.get_balance(account_1)
k = 10 ** 18
balance_eth = balance_gwei/k

nonce = w3.eth.get_transaction_count(account_1)

tx = {
    "nonce": nonce,
    "to":   account_2,
    "value":    w3.to_wei(1, "ether"),
    "gas":  20000000000,
    "gasPrice": w3.to_wei("50", "gwei"),
}


print(balance_eth)
signed_tx = w3.eth.account.sign_transaction(tx, privatekey_1)
tx_hash = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
print(balance_eth)

So, here's the terminal:

(venv) PS C:\Users\Kirill\Desktop\PYTHON\CRYPTO_PAYMENTS_PYTHON> python main.py

100.0
Traceback (most recent call last):
  File "C:\Users\Kirill\Desktop\PYTHON\CRYPTO_PAYMENTS_PYTHON\main.py", line 34, in <module>
    tx_hash = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Kirill\Desktop\PYTHON\CRYPTO_PAYMENTS_PYTHON\venv\Lib\site-packages\web3\eth\eth.py", line 396, in send_raw_transaction
    return self._send_raw_transaction(transaction)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Kirill\Desktop\PYTHON\CRYPTO_PAYMENTS_PYTHON\venv\Lib\site-packages\web3\module.py", line 75, in caller
    result = w3.manager.request_blocking(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Kirill\Desktop\PYTHON\CRYPTO_PAYMENTS_PYTHON\venv\Lib\site-packages\web3\manager.py", line 330, in request_blocking
    return self.formatted_response(
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Kirill\Desktop\PYTHON\CRYPTO_PAYMENTS_PYTHON\venv\Lib\site-packages\web3\manager.py", line 293, in formatted_response
    raise ValueError(error)
ValueError: {'message': 'exceeds block gas limit', 'stack': 'Error: exceeds block gas limit\n    at TransactionPool.<anonymous> (C:\\Program Files\\WindowsApps\\GanacheUI_2.7.1.0_x64__rb4352f0jd4m2\\app\\resources\\static\\node\\node_modules\\ganache\\dist\\node\\1.js:2:120454)\n    at TransactionPool.prepareTransaction (C:\\Program Files\\WindowsApps\\GanacheUI_2.7.1.0_x64__rb4352f0jd4m2\\app\\resources\\static\\node\\node_modules\\ganache\\dist\\node\\1.js:2:120796)\n    at TransactionManager.add (C:\\Program Files\\WindowsApps\\GanacheUI_2.7.1.0_x64__rb4352f0jd4m2\\app\\resources\\static\\node\\node_modules\\ganache\\dist\\node\\1.js:2:138284)\n    at Blockchain.queueTransaction (C:\\Program Files\\WindowsApps\\GanacheUI_2.7.1.0_x64__rb4352f0jd4m2\\app\\resources\\static\\node\\node_modules\\ganache\\dist\\node\\1.js:2:70776)\n    at EthereumApi.eth_sendRawTransaction (C:\\Program Files\\WindowsApps\\GanacheUI_2.7.1.0_x64__rb4352f0jd4m2\\app\\resources\\static\\node\\node_modules\\ganache\\dist\\node\\1.js:2:88213)\n    at EthereumApi.n.value (C:\\Program Files\\WindowsApps\\GanacheUI_2.7.1.0_x64__rb4352f0jd4m2\\app\\resources\\static\\node\\node_modules\\ganache\\dist\\node\\1.js:2:238968)\n    at Object.execute (C:\\Program Files\\WindowsApps\\GanacheUI_2.7.1.0_x64__rb4352f0jd4m2\\app\\resources\\static\\node\\node_modules\\ganache\\dist\\node\\1.js:2:165607)\n    at RequestCoordinator.<anonymous> (C:\\Program Files\\WindowsApps\\GanacheUI_2.7.1.0_x64__rb4352f0jd4m2\\app\\resources\\static\\node\\node_modules\\ganache\\dist\\node\\1.js:2:165421)\n    at C:\\Program Files\\WindowsApps\\GanacheUI_2.7.1.0_x64__rb4352f0jd4m2\\app\\resources\\static\\node\\node_modules\\ganache\\dist\\node\\1.js:2:165687\n    at new Promise (<anonymous>)', 'code': -32000}

d in the web3 doc.


Solution

  • The error already stated what's wrong:

    Error: exceeds block gas limit

    The gas limit for a whole block is 30 million. It makes no sense to specify that your transaction alone would be allowed to consume 20 billion. Use a proper gas limit for your transaction.