tx

Documentation for eth_defi.tx Python module.

Transaction parsing and building utilities.

Functions

decode_signed_transaction(raw_bytes)

Decode already signed transaction.

get_tx_broadcast_data(signed_tx)

Get raw transaction bytes with compatibility for attribute name changes.

Classes

AssetDelta

Spend/incoming asset information.

Exceptions

DecodeFailure

We could not decode transaction for a reason or another.

exception DecodeFailure

Bases: Exception

We could not decode transaction for a reason or another.

__init__(*args, **kwargs)
__new__(**kwargs)
add_note()

Exception.add_note(note) – add a note to the exception

with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

decode_signed_transaction(raw_bytes)

Decode already signed transaction.

Reverse raw transaction bytes back to dictionary form, so you can access its data field and other parameters.

The function supports:

Example:

signed_tx = hot_wallet.sign_transaction_with_new_nonce(raw_tx)
signed_tx_bytes = get_tx_broadcast_data(signed_tx)
d = decode_signed_transaction(signed_tx_bytes)
assert d["chainId"] == 1337
assert d["nonce"] == 0
assert d["data"].hex().startswith("0xa9059cbb0")  # transfer() function selector
Parameters

raw_bytes (Union[bytes, str, hexbytes.main.HexBytes]) – A bunch of bytes in your favorite format.

Raises

DecodeFailure – If the tx bytes is something we do not know how to handle.

Returns

Dictionary like object containing data, v, r, s, nonce, value, gas, gasPrice. Some fields like chainId, accessList, maxPriorityFeePerGas depend on the transaction type.

Return type

Optional[dict]

class AssetDelta

Bases: object

Spend/incoming asset information.

Some transaction builders, like Enzyme vaults, need to have the incoming/outgoing asset information associated with the transaction. This is because internally Enzyme needs to move assets to the adapter contract from the vault contract to perform the transaction.

We use this data structure to describe what assets the transaction touches.

See eth_defi.enzyme.vault_transaction_builder for more information.

asset: Union[eth_typing.evm.HexAddress, str]

The ERC-20 token for this delta.

raw_amount: int

Changed amount.

Negative for tokens that are going to be used for purchases in this tx, positive for incoming.

Much include any slippage tolerance for trades.

is_incoming()

This delta describes incoming assets.

Return type

bool

is_spending()

This delta describes assets that we spend in the transaction.

Return type

bool

as_json_friendly_dict()

Get the asset delta representation as JSON’nable dict.

We need to convert large Python ints to strings.

__init__(asset, raw_amount)
Parameters
Return type

None

get_tx_broadcast_data(signed_tx)

Get raw transaction bytes with compatibility for attribute name changes.

eth_account changed rawTransaction to raw_transaction in newer versions. This function handles both attribute names.

Parameters

signed_tx (SignedTransaction | SignedTransactionWithNonce) – Signed transaction object from SignedTransaction | SignedTransactionWithNonce

Returns

Raw transaction bytes ready for broadcasting to the network

Return type

HexBytes

Raises

AttributeError – If the signed transaction object has neither ‘raw_transaction’ nor ‘rawTransaction’ attribute

Example:

from eth_defi.compat import get_tx_broadcast_data

# Works with both old and new eth_account versions
raw_bytes = get_tx_broadcast_data(signed_tx)
tx_hash = web3.eth.send_raw_transaction(raw_bytes)