@@ -5,14 +5,14 @@ import { useCurrentAccount, useSignAndExecuteTransaction, useSuiClient } from '@
55import { Transaction } from '@mysten/sui/transactions' ;
66import { useQuery } from '@tanstack/react-query' ;
77import { marketplaceConfig , MIST_PER_SUI } from '@/config/marketplace' ;
8- import {
9- CreateListingInput ,
10- CreateListingResult ,
11- DatasetListing ,
12- PurchaseResult
8+ import {
9+ CreateListingInput ,
10+ CreateListingResult ,
11+ DatasetListing ,
12+ PurchaseResult
1313} from '@/types/marketplace' ;
14- import {
15- getMarketplaceTarget ,
14+ import {
15+ getMarketplaceTarget ,
1616 parseDatasetListing
1717} from '@/lib/marketplace' ;
1818
@@ -120,7 +120,7 @@ export function useListDataset() {
120120
121121 // Build PTB with optimized structure
122122 const tx = new Transaction ( ) ;
123-
123+
124124 // Set gas budget based on operation complexity
125125 tx . setGasBudget ( 10_000_000 ) ; // 0.01 SUI - sufficient for single listing
126126
@@ -195,7 +195,7 @@ export function useListDataset() {
195195 }
196196
197197 const tx = new Transaction ( ) ;
198-
198+
199199 // Dynamic gas budget based on number of listings
200200 // Base: 10M MIST + 5M per additional listing
201201 const gasBudget = 10_000_000 + ( inputs . length - 1 ) * 5_000_000 ;
@@ -239,12 +239,12 @@ export function useListDataset() {
239239 onSuccess : ( result ) => {
240240 const effects = result . effects as { created ?: Array < { reference : { objectId : string } } > } | undefined ;
241241 const createdIds = effects ?. created ?. map ( ( obj ) => obj . reference . objectId ) || [ ] ;
242-
242+
243243 const results : CreateListingResult [ ] = createdIds . map ( ( id ) => ( {
244244 listingId : id ,
245245 digest : result . digest ,
246246 } ) ) ;
247-
247+
248248 onSuccess ( results ) ;
249249 } ,
250250 }
@@ -369,10 +369,10 @@ export function useOwnedListings(address?: string) {
369369 queryKey : [ 'owned-listings' , address , marketplaceConfig . packageId ] ,
370370 queryFn : async ( ) => {
371371 if ( ! address ) return [ ] ;
372-
372+
373373 // Build type string dynamically to ensure env vars are loaded
374374 const listingType = `${ marketplaceConfig . packageId } ::${ marketplaceConfig . moduleName } ::DatasetListing` ;
375-
375+
376376 const { data } = await suiClient . getOwnedObjects ( {
377377 owner : address ,
378378 filter : { StructType : listingType } ,
@@ -414,7 +414,7 @@ export function useListing(listingId: string | undefined) {
414414 queryKey : [ 'listing' , listingId ] ,
415415 queryFn : async ( ) => {
416416 if ( ! listingId ) return null ;
417-
417+
418418 const object = await suiClient . getObject ( {
419419 id : listingId ,
420420 options : { showContent : true } ,
@@ -441,12 +441,12 @@ export function useAccountBalance() {
441441 queryKey : [ 'account-balance' , account ?. address ] ,
442442 queryFn : async ( ) => {
443443 if ( ! account ?. address ) return null ;
444-
444+
445445 const balance = await suiClient . getBalance ( {
446446 owner : account . address ,
447447 coinType : '0x2::sui::SUI' ,
448448 } ) ;
449-
449+
450450 return {
451451 mist : BigInt ( balance . totalBalance ) ,
452452 sui : Number ( balance . totalBalance ) / Number ( MIST_PER_SUI ) ,
@@ -464,29 +464,68 @@ export function usePurchasedDatasets(address?: string) {
464464 queryKey : [ 'purchased-datasets' , address ] ,
465465 queryFn : async ( ) => {
466466 if ( ! address ) return [ ] ;
467-
467+
468468 const RECEIPT_TYPE = `${ marketplaceConfig . packageId } ::${ marketplaceConfig . moduleName } ::PurchaseReceipt` ;
469-
469+
470+ // Step 1: Get all purchase receipts owned by the user
470471 const { data } = await suiClient . getOwnedObjects ( {
471472 owner : address ,
472473 filter : { StructType : RECEIPT_TYPE } ,
473474 options : { showContent : true , showType : true } ,
474475 } ) ;
475476
476- return data
477+ const receipts = data
477478 . map ( ( obj ) => {
478479 if ( ! obj . data ?. content || obj . data . content . dataType !== 'moveObject' ) return null ;
479480 const fields = obj . data . content . fields as Record < string , unknown > ;
480481 return {
481482 id : obj . data . objectId || '' ,
482- datasetId : ( fields . dataset_id as { id : string } ) ?. id || '' ,
483+ datasetId : fields . dataset_id as string ,
483484 buyer : fields . buyer as string ,
484485 seller : fields . seller as string ,
485486 price : BigInt ( fields . price as string ) ,
486487 timestamp : Number ( fields . timestamp ) ,
487488 } ;
488489 } )
489490 . filter ( ( receipt ) : receipt is { id : string ; datasetId : string ; buyer : string ; seller : string ; price : bigint ; timestamp : number } => receipt !== null ) ;
491+
492+ if ( receipts . length === 0 ) return [ ] ;
493+
494+ // Step 2: Fetch the dataset details for each purchase receipt
495+ const datasetIds = receipts . map ( r => r . datasetId ) . filter ( id => id && id !== '' ) ;
496+
497+ if ( datasetIds . length === 0 ) return receipts . map ( r => ( { ...r , dataset : null } ) ) ;
498+
499+ const datasetObjects = await suiClient . multiGetObjects ( {
500+ ids : datasetIds ,
501+ options : { showContent : true } ,
502+ } ) ;
503+
504+ // Create a map of dataset ID -> dataset details
505+ const datasetMap = new Map < string , DatasetListing | null > ( ) ;
506+ datasetObjects . forEach ( ( obj ) => {
507+ if ( obj . data ?. content ?. dataType === 'moveObject' ) {
508+ const fields = obj . data . content . fields as Record < string , unknown > ;
509+ const dataset : DatasetListing = {
510+ id : obj . data . objectId || '' ,
511+ seller : fields . seller as string ,
512+ price : BigInt ( fields . price as string ) ,
513+ blobId : fields . blob_id as string ,
514+ encryptedObject : fields . encrypted_object as string ,
515+ name : fields . name as string ,
516+ description : fields . description as string ,
517+ previewSize : BigInt ( fields . preview_size as string ) ,
518+ totalSize : BigInt ( fields . total_size as string ) ,
519+ } ;
520+ datasetMap . set ( obj . data . objectId || '' , dataset ) ;
521+ }
522+ } ) ;
523+
524+ // Step 3: Combine receipts with dataset details
525+ return receipts . map ( receipt => ( {
526+ ...receipt ,
527+ dataset : datasetMap . get ( receipt . datasetId ) || null ,
528+ } ) ) ;
490529 } ,
491530 enabled : ! ! address ,
492531 refetchInterval : 10000 ,
@@ -583,7 +622,7 @@ export function useDownloadDataset() {
583622 }
584623
585624 const result = await response . json ( ) ;
586-
625+
587626 if ( result . error ) {
588627 throw new Error ( result . error ) ;
589628 }
0 commit comments