11import locale
2+ import logging
23from dataclasses import dataclass
34
45from open_sea_v1 .helpers .ether_converter import EtherConverter , EtherUnit
56from open_sea_v1 .responses .abc import BaseResponse
67from open_sea_v1 .responses .asset import AssetResponse
78
9+ logger = logging .getLogger (__name__ )
10+
811
912@dataclass
1013class EventResponse (BaseResponse ):
@@ -14,14 +17,15 @@ def __str__(self) -> str:
1417 locale .setlocale (locale .LC_ALL , '' ) # big number str formater
1518
1619 name = self .asset .name [:20 ]
17- transaction_date = f"{ self .transaction ['timestamp' ][:10 ]} { self .transaction ['timestamp' ][11 :- 3 ]} "
20+ transaction_date = f"{ self .transaction ['timestamp' ][:10 ]} { self .transaction ['timestamp' ][11 :- 3 ]} " if self .transaction else ''
21+
1822 usd_price = round (self .usd_price / int (self .quantity ), 2 )
1923 usd_price = f"{ usd_price :,.2f} "
2024 usd_price = f"{ usd_price } USD"
2125 eth_price = self .eth_price / int (self .quantity )
2226 eth_price = f"{ eth_price :.4f} ETH" # trailing zeros
2327
24- str_representation = " " .join ([name , transaction_date , usd_price , eth_price ])
28+ str_representation = " " .join ([name , transaction_date , usd_price , eth_price ])
2529 return str_representation
2630
2731 def __post_init__ (self ):
@@ -47,12 +51,18 @@ def __post_init__(self):
4751 self .is_private = self ._json .get ('is_private' )
4852
4953 @property
50- def eth_price (self ):
54+ def eth_price (self ) -> float :
55+ if not self .total_price :
56+ logger .debug (f'Event { self .id } for asset { self .asset .name } ({ self .asset .id } ) has no ETH price. Returning 0.' )
57+ return 0.0
5158 eth_price = EtherConverter (quantity = self .total_price , unit = EtherUnit .WEI ).ether
5259 return eth_price
5360
5461 @property
5562 def usd_price (self ):
63+ if not self .payment_token :
64+ logger .debug (f'Event { self .id } for asset { self .asset .name } ({ self .asset .id } ) has no payment token. Returning 0.' )
65+ return 0.0
5666 eth_to_usd_price = float (self .payment_token ['usd_price' ]) # 'eth_price' key also available
5767 usd_price = round (self .eth_price * eth_to_usd_price , 2 )
5868 return usd_price
@@ -76,4 +86,3 @@ def transaction(self) -> dict:
7686 @property
7787 def winner_account (self ) -> dict :
7888 return self ._json ['winner_account' ]
79-
0 commit comments