Skip to content

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

@fselmo

Description

@fselmo

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions