ethereum / web3.py

A python interface for interacting with the Ethereum blockchain and ecosystem.
http://web3py.readthedocs.io
MIT License
4.91k stars 1.68k forks source link

Incorrect documentation of `send_raw_transaction` and `wait_for_transaction_receipt` #3336

Closed barakman closed 3 months ago

barakman commented 3 months ago

What happened?

The value returned from function send_raw_transaction is a HexBytes, not a plain hexadecimal string.

The transactionHash attribute returned from function wait_for_transaction_receipt is a HexBytes, not a plain hexadecimal string. The input to this function, can apparently be any one of them with no issues.

The problem that I've personally encountered is that when you assert input_tx_hash == output['transactionHash'] it may fail only because the LHS is a plain hexadecimal string, and the RHS is a HexBytes.

But in either case, the documentation should just state the expected return-value type correctly, in order to remove all doubts.

In the case of function send_raw_transaction, the documentation is confusing, with one place stating the return-value type being a HexBytes (see here), and another place stating the return-value type being a plain hexadecimal string (scroll to the bottom of the example here).

In the case of function wait_for_transaction_receipt, the documentation is just wrong, stating that the returned transactionHash attribute is a plain string (see here).

Code that produced the error

No response

Full error output

No response

Fill this section in if you know how this could or should be fixed

Fix the docs

web3 Version

6.16.0

Python Version

3.9.13

Operating System

MacOS

Output from pip freeze

No response

tiendq commented 3 months ago

In the case of function send_raw_transaction, the documentation is confusing, with one place stating the return-value type being a HexBytes (see here), and another place stating the return-value type being a plain hexadecimal string (scroll to the bottom of the example here).

I see nothing wrong here, you saw plain text transaction hash in the last line because it's result of w3.to_hex call.

In the case of function wait_for_transaction_receipt, the documentation is just wrong, stating that the returned transactionHash attribute is a plain string (see here).

You're correct, transactionHash is actually HexBytes here.

barakman commented 3 months ago

If I might add, the whole idea of returning a response with AttributeDict and HexBytes (in opposed to plain dict and str) seems to be doing more harm than good, since these returned values cannot be used in order to perform the actual transaction, and one needs to json.loads(w3.to_json(...)) whatever you return in that response, in order to use it in the actual transaction.

For example:

response = w3.eth.create_access_list(tx)
if tx["gas"] > response["gasUsed"]:
    tx["gas"] = response["gasUsed"]
    tx["accessList"] = loads(w3.to_json(response["accessList"]))
fselmo commented 3 months ago

one needs to json.loads(w3.to_json(...)) whatever you return in that response, in order to use it in the actual transaction.

Please use the latest version of web3.py. The formatters should be handling this, there is no need to utilize the json library.

barakman commented 3 months ago

one needs to json.loads(w3.to_json(...)) whatever you return in that response, in order to use it in the actual transaction.

Please use the latest version of web3.py. The formatters should be handling this, there is no need to utilize the json library.

I am using 6.16.0, which the problem that I've describes above applies for.

Based on the release notes for the few more recent versions since that version (6.17.0, 6.17.1 and 6.17.2), no significant change which could potentially solve this problem has been introduced.

In other words, the response for w3.eth.create_access_list (and I'm assuming probably also for a bunch of other requests) cannot be used as is when sending the actual transaction.

When I mentioned that I needed to use loads(w3.to_json(response["accessList"])), it wasn't a speculation.

I did so following a previous attempt to use response["accessList"] as is, which had ended up throwing an exception before it was even sent to the node.

Please give it a try.

Thanks :)

fselmo commented 3 months ago

@barakman please use the latest version of web3.py. Based on the release notes you linked, version 6.17.0 includes this change:

Add formatters to ensure that the result of a eth_createAccessList response can be plugged directly into an accessList in a transaction. (#3329)

This change was based on an issue that you raised (thank you!) and you also commented on the PR that resolved it. If you have any other issues, please update your version of web3.py before posting them. And if you are still having issues after updating, please let us know.

Thanks!