Skip to content

Commit

Permalink
chore: refactor InternalTransfer.from_trace (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobTheBuidler authored Dec 17, 2024
1 parent 548da7c commit 5a0d1b3
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions eth_portfolio/structs/structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
The classes are designed to provide a consistent and flexible interface for working with blockchain data. Instance attributes can be fetched with either dot notation or key lookup. Classes are compatible with the standard dictionary interface.
"""

import logging
from functools import cached_property
from typing import (
Any,
Expand Down Expand Up @@ -38,9 +37,6 @@
from eth_portfolio.structs.modified import ModifiedTrace, _modified_trace_type_map


logger = logging.getLogger(__name__)


class _LedgerEntryBase(DictStruct, kw_only=True, frozen=True, omit_defaults=True, repr_omit_defaults=True): # type: ignore [call-arg]
"""
The :class:`~structs._LedgerEntryBase` class is a base class for ledger entries representing on-chain actions in a blockchain.
Expand Down Expand Up @@ -347,6 +343,7 @@ class InternalTransfer(
@staticmethod
@stuck_coro_debugger
async def from_trace(trace: evmspec.FilterTrace, load_prices: bool) -> "InternalTransfer":
# sourcery skip: simplify-boolean-comparison
"""
Asynchronously processes a raw internal transfer dictionary into an InternalTransfer object.
Expand Down Expand Up @@ -386,12 +383,14 @@ async def from_trace(trace: evmspec.FilterTrace, load_prices: bool) -> "Internal
"""

modified_cls = _modified_trace_type_map[type(trace)]
args = {"trace": modified_cls(**_get_init_kwargs(trace))}
if load_prices:
price = await _get_price(EEE_ADDRESS, trace.block)
args["price"] = price
args["value_usd"] = round(trace.action.value.scaled * price, 18)
return InternalTransfer(**args)
modified_trace = modified_cls(**_get_init_kwargs(trace))

if load_prices is False:
return InternalTransfer(trace=modified_trace)

price = await _get_price(EEE_ADDRESS, trace.block)
value_usd = round(trace.action.value.scaled * price, 18)
return InternalTransfer(trace=modified_trace, price=price, value_usd=value_usd)

entry_type: ClassVar[Literal["internal_transfer"]] = "internal_transfer"
"""
Expand Down

0 comments on commit 5a0d1b3

Please sign in to comment.