Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A transaction class that can encode and decode from an RPC transaction dict #3534

Open
fselmo opened this issue Nov 20, 2024 · 0 comments
Open

Comments

@fselmo
Copy link
Collaborator

fselmo commented Nov 20, 2024

What feature should we add?

There should be an easy way to take a transaction directly as a JSON-RPC dict and put it into an object that can encode() and decode() the transaction. This would behave much like the TypedTransaction class does for typed transactions in eth-account - and in fact this class can probably carry a lot of the heavy load here - but it also needs to clean up certain fields based on the type in order for it to even be plugged into the TypedTransaction class.

I'm thinking something like:

>>> from web3.utils import Web3Transaction

>>> # from dict
>>> txn = Web3Transaction.from_dict(w3.eth.get_transaction("0x..."))
>>> txn.encode().hex()
02ed01808504a817c8008504a817c800830186a09400000000000000000000000000000000000000008080c0808080...

>>> # from bytes
>>> txn = Web3Transaction.from_bytes(b'\x02\xed\x01\x80\x85\x04\xa8\x17\xc8\x00\x85\x04\xa8\x17\xc8\x00\x83\x01\x86\xa0\x94\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x80\xc0\x80\x80\x80')
>>> txn.as_dict()
{
    "chainId": 1,
    "maxFeePerGas": 20000000000,
    "maxPriorityFeePerGas": 20000000000,
    "nonce": 0,
    ...
}

>>> # fields accessible as properties
>>> txn.max_fee_per_gas
20000000000

Or, maybe it's a HexBytes as the result of encode(). Either way, I think the utility here is fairly clear. Take a transaction straight from a result, put it into a utility class, and be able to manipulate and have useful methods available on the transaction.

Tasks

No tasks being tracked yet.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant