Search code examples
pythonpython-3.xethereumweb3py

Sending raw transaction from web3py: TypeError: <lambda>() missing 4 required positional arguments: 'hash', 'r', 's', and 'v'


I am trying to send raw transaction by web3py using this code:

t = w3.eth.account.sign_transaction(test_contract.functions.edit("test").buildTransaction(
    {
        "nonce": w3.eth.get_transaction_count(w3.eth.default_account)
    }
), pkey)
w3.eth.send_raw_transaction(t)

But, where python comes to the last line, I have this error in console:

Traceback (most recent call last):
  File "***/main.py", line 64, in <module>
    w3.eth.send_raw_transaction(t)
  File "***/venv/lib/python3.9/site-packages/web3/module.py", line 53, in caller
    (method_str, params), response_formatters = method.process_params(module, *args, **kwargs)  # noqa: E501
  File "***/venv/lib/python3.9/site-packages/web3/method.py", line 194, in process_params
    _apply_request_formatters(params, self.request_formatters(method)))
  File "***/venv/lib/python3.9/site-packages/eth_utils/functional.py", line 45, in inner
    return callback(fn(*args, **kwargs))
  File "***/venv/lib/python3.9/site-packages/web3/method.py", line 50, in _apply_request_formatters
    formatted_params = pipe(params, request_formatters)
  File "cytoolz/functoolz.pyx", line 667, in cytoolz.functoolz.pipe
  File "cytoolz/functoolz.pyx", line 642, in cytoolz.functoolz.c_pipe
  File "cytoolz/functoolz.pyx", line 254, in cytoolz.functoolz.curry.__call__
  File "cytoolz/functoolz.pyx", line 250, in cytoolz.functoolz.curry.__call__
  File "***/venv/lib/python3.9/site-packages/web3/_utils/abi.py", line 799, in map_abi_data
    return pipe(data, *pipeline)
  File "cytoolz/functoolz.pyx", line 667, in cytoolz.functoolz.pipe
  File "cytoolz/functoolz.pyx", line 642, in cytoolz.functoolz.c_pipe
  File "cytoolz/functoolz.pyx", line 254, in cytoolz.functoolz.curry.__call__
  File "cytoolz/functoolz.pyx", line 250, in cytoolz.functoolz.curry.__call__
  File "***/venv/lib/python3.9/site-packages/web3/_utils/abi.py", line 833, in data_tree_map
    return recursive_map(map_to_typed_data, data_tree)
  File "***/venv/lib/python3.9/site-packages/web3/_utils/decorators.py", line 30, in wrapped
    wrapped_val = to_wrap(*args)
  File "***/venv/lib/python3.9/site-packages/web3/_utils/formatters.py", line 89, in recursive_map
    items_mapped = map_collection(recurse, data)
  File "***/venv/lib/python3.9/site-packages/web3/_utils/formatters.py", line 76, in map_collection
    return datatype(map(func, collection))
  File "***/venv/lib/python3.9/site-packages/web3/_utils/formatters.py", line 88, in recurse
    return recursive_map(func, item)
  File "***/venv/lib/python3.9/site-packages/web3/_utils/decorators.py", line 30, in wrapped
    wrapped_val = to_wrap(*args)
  File "***/venv/lib/python3.9/site-packages/web3/_utils/formatters.py", line 89, in recursive_map
    items_mapped = map_collection(recurse, data)
  File "***/venv/lib/python3.9/site-packages/web3/_utils/formatters.py", line 76, in map_collection
    return datatype(map(func, collection))
  File "***/venv/lib/python3.9/site-packages/web3/_utils/abi.py", line 855, in __new__
    return super().__new__(cls, *iterable)
  File "***/venv/lib/python3.9/site-packages/web3/_utils/formatters.py", line 88, in recurse
    return recursive_map(func, item)
  File "***/venv/lib/python3.9/site-packages/web3/_utils/decorators.py", line 30, in wrapped
    wrapped_val = to_wrap(*args)
  File "***/venv/lib/python3.9/site-packages/web3/_utils/formatters.py", line 89, in recursive_map
    items_mapped = map_collection(recurse, data)
  File "***/venv/lib/python3.9/site-packages/web3/_utils/formatters.py", line 76, in map_collection
    return datatype(map(func, collection))
TypeError: <lambda>() missing 4 required positional arguments: 'hash', 'r', 's', and 'v'

I am using infura custom node, that's why I cant send transaction by contract.functions.method.transact(). Don't know to do with this error, spent a lot of time reading docs and got nothing.

How can I fix that?


Solution

  • You need to sign the transaction before sending with your account that has ETH balance.

    You need to use Signing middleware.

    >>> from web3 import Web3, EthereumTesterProvider
    >>> w3 = Web3(EthereumTesterProvider)
    >>> from web3.middleware import construct_sign_and_send_raw_middleware
    >>> from eth_account import Account
    >>> acct = Account.create('KEYSMASH FJAFJKLDSKF7JKFDJ 1530')
    >>> w3.middleware_onion.add(construct_sign_and_send_raw_middleware(acct))
    >>> w3.eth.default_account = acct.address
    # Now you can send a tx from acct.address without having to build and sign each raw transaction