Skip to content

Commit

Permalink
Dataclass improvement from testing
Browse files Browse the repository at this point in the history
  • Loading branch information
gabedonnan committed Oct 31, 2023
1 parent e55afcb commit b68f804
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions pythereum/dclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,33 @@ def hex_list_decoder(hex_string_list: list[str] | None) -> list[HexStr] | None:
return None


def hex_list_encoder(hex_obj_list: list[HexStr]) -> list[str] | None:
def hex_list_encoder(hex_obj_list: list[HexStr] | None) -> list[str] | None:
if hex_obj_list is not None:
return [hex_encoder(hex_obj) for hex_obj in hex_obj_list]
else:
return None


def transaction_decoder(transaction_hex: dict | str) -> "TransactionFull | HexStr":
def transaction_decoder(
transaction_hex: dict | str | None,
) -> "TransactionFull | HexStr | None":
if isinstance(transaction_hex, dict):
return TransactionFull.from_dict(transaction_hex, infer_missing=True)
else:
elif transaction_hex is not None:
return hex_decoder(transaction_hex)
else:
return None


def transaction_encoder(transaction_obj: "HexStr | TransactionFull") -> str | dict:
def transaction_encoder(
transaction_obj: "HexStr | TransactionFull | None",
) -> str | dict | None:
if isinstance(transaction_obj, TransactionFull):
return transaction_obj.to_dict()
else:
elif transaction_obj is not None:
return hex_encoder(transaction_obj)
else:
return None


def transaction_list_decoder(
Expand Down Expand Up @@ -160,6 +168,7 @@ class Block:
"""
Full information about a block, either including full transactions or transaction hashes
"""

# Integer of the difficulty for the block
difficulty: int | None = field(
metadata=config(decoder=hex_int_decoder, encoder=hex_int_encoder)
Expand Down Expand Up @@ -292,6 +301,7 @@ class Receipt:
"""
A receipt generated by a transaction's execution
"""

# 32 Byte hash of transaction
transaction_hash: HexStr | None = field(
metadata=config(decoder=hex_decoder, encoder=hex_encoder)
Expand Down Expand Up @@ -374,6 +384,7 @@ class Log:
"""
A log generated by smart contract event triggers
"""

address: HexStr | None = field(
metadata=config(decoder=hex_decoder, encoder=hex_encoder)
)
Expand All @@ -398,7 +409,7 @@ class Log:
transaction_index: int | None = field(
metadata=config(decoder=hex_int_decoder, encoder=hex_int_encoder)
)
removed: bool
removed: bool | None


@dataclass_json(letter_case=LetterCase.CAMEL)
Expand All @@ -407,6 +418,7 @@ class TransactionFull:
"""
The full information on a transaction to be executed, including metadata with reference to its inclusion on chain
"""

block_hash: HexStr | None = field(
metadata=config(decoder=hex_decoder, encoder=hex_encoder)
)
Expand Down Expand Up @@ -572,6 +584,7 @@ class Bundle(dict):
"""
A bundle of transactions to submit to a block builder
"""

def __init__(
self,
txs: list[str] | list[HexStr],
Expand Down

0 comments on commit b68f804

Please sign in to comment.