@@ -49,11 +49,9 @@ class UnisatApi(BlockchainApi, INftParser, INftProvider):
4949
5050 supported_requests = {
5151 'get_nfts' : 'v1/indexer/address/{address}/inscription-data' ,
52- 'get_collection' : 'v1/collection-indexer/collection/{collectionId}/info' ,
53- 'get_collection_items' : 'v1/collection-indexer/collection/{collectionId}/items' ,
54- 'get_collection_stats' : 'v3/market/collection/auction/collection_statistic' ,
5552 'get_listings' : 'v3/market/collection/auction/list' ,
5653 'get_offers' : 'v3/market/collection/auction/actions' ,
54+ 'get_collection_stats' : 'market-v4/collection/auction/collection_statistic' ,
5755 }
5856
5957 def __init__ (self , api_key : str , sleep_provider : Optional [ISleepProvider ] = None ):
@@ -192,27 +190,19 @@ def _yield_parsed_nfts(self, data: Dict) -> Generator[NftToken, None, None]:
192190
193191 def fetch_collection (self , collection : str ) -> FetchResult :
194192 """Fetch collection data from Unisat API."""
195- info_response = self .get_data (
196- 'get_collection' ,
197- headers = self .headers ,
198- collectionId = collection ,
199- )
200- items_response = self .get_data (
201- 'get_collection_items' ,
202- headers = self .headers ,
203- collectionId = collection ,
204- )
205-
206- stats_data = self .post (
207- 'get_collection_stats' ,
208- json = {'collectionId' : collection },
209- headers = self .headers ,
210- )
211- stats_response = FetchResult (data = stats_data )
212-
213- return FetchResult .from_fetch_results (
214- info = info_response , items = items_response , stats = stats_response
215- )
193+ try :
194+ stats_data = self .post (
195+ 'get_collection_stats' ,
196+ json = {'collectionId' : collection },
197+ headers = self .headers ,
198+ )
199+ return FetchResult (data = stats_data )
200+ except (HTTPError , ValueError , TypeError ) as e :
201+ logger .error (f"Error fetching collection { collection } : { str (e )} " )
202+ return FetchResult (errors = [str (e )])
203+ except Exception as e :
204+ logger .error (f"Unexpected error fetching collection { collection } : { str (e )} " )
205+ return FetchResult (errors = [str (e )])
216206
217207 def parse_collection (self , fetch_result : FetchResult ) -> ParseResult :
218208 """
@@ -227,33 +217,45 @@ def parse_collection(self, fetch_result: FetchResult) -> ParseResult:
227217 if not fetch_result or not fetch_result .data :
228218 return ParseResult (errors = fetch_result .errors if fetch_result else None )
229219
230- info = fetch_result .data . get ( "info" , {}) .get ("data" , {})
231- items = fetch_result . data . get ( "items" , {}). get ( "data" , {})
232- stats = fetch_result . data . get ( "stats" , {}). get ( " data" , {} )
220+ stats = fetch_result .data .get ("data" , {})
221+ if not stats :
222+ return ParseResult ( errors = [ "No collection data found in response" ] )
233223
234224 collection_id = stats .get ("collectionId" )
235225 if not collection_id :
236226 return ParseResult (errors = ["No collection ID found in response" ])
237227
228+ # Format the icon URL
229+ icon = stats .get ("icon" )
230+ icon_url = None
231+ if icon :
232+ icon_url = f"https://static.unisat.io/content/{ icon } "
233+
234+ # Create NftCollectionTotalStats
235+ floor_price = stats .get ("floorPrice" , 0 )
236+ total_nfts = stats .get ("total" , 0 )
237+ # Calculate market cap as floor price × total supply
238+ market_cap = floor_price * total_nfts if floor_price and total_nfts else 0
239+
238240 total_stats = NftCollectionTotalStats .from_api (
239- volume = "" ,
240- sales_count = "" ,
241- owners_count = str (info . get ( "holders" , "" ) ),
242- market_cap = "" ,
243- floor_price = str (stats . get ( "floorPrice" , "" ) ),
244- average_price = "" ,
241+ volume = str ( stats . get ( "btcValue" , 0 )) ,
242+ sales_count = str ( stats . get ( "listed" , 0 )) ,
243+ owners_count = str (total_nfts ),
244+ market_cap = str ( market_cap ) ,
245+ floor_price = str (floor_price ),
246+ average_price = "0 " ,
245247 coin = self .coin ,
246248 )
247249
248250 collection = NftCollection .from_api (
249- ident = collection_id , # Matches test_collection_id
251+ ident = collection_id ,
250252 name = stats .get ("name" , f"Collection { collection_id } " ),
251253 contracts = [
252254 ContractInfo .from_api (
253255 blockchain = Blockchain .BITCOIN , address = collection_id
254256 )
255257 ],
256- image = stats . get ( "icon" ) ,
258+ image = icon_url ,
257259 is_disabled = False ,
258260 is_nsfw = False ,
259261 blockchain = Blockchain .BITCOIN ,
@@ -574,8 +576,6 @@ def _yield_parsed_offers(
574576 formatted_time = None
575577 if timestamp :
576578 try :
577- from datetime import datetime
578-
579579 timestamp_seconds = timestamp / 1000
580580 formatted_time = datetime .fromtimestamp (
581581 timestamp_seconds
0 commit comments