import { CustomResponse , NftType } from 'interfaces/index' ; import { MARKETPLACE_ID , NODE_API_URL } from 'utils/constant' ; import { encryptCookie } from 'utils/cookie' ; import { DEFAULT_LIMIT_PAGINATION } from "../utils/constant" ; export const filterNFTs = ( data ) => data . filter ( ( item ) => item . properties ?. preview . ipfs ) ; export const getOwnedNFTS = async ( id , onlyFromMpId , listed ? , page = "1" , limit = DEFAULT_LIMIT_PAGINATION , nftIdsFilter = [ ] ) => { const paginationOptions = { page , limit } ; const filterOptions = { owner : id } ; if ( listed !== undefined ) filterOptions . listed = listed ; if ( onlyFromMpId ) filterOptions . marketplaceId = MARKETPLACE_ID ; if ( nftIdsFilter . length > 0 ) filterOptions . ids = nftIdsFilter ; const sortOptions = "created_at:desc" ; const res = await fetch ( `${ NODE_API_URL }/api/NFTs/?pagination=${ JSON . stringify ( paginationOptions ) }&filter=${ JSON . stringify ( filterOptions ) }&sort=${ sortOptions }` ) ; if ( ! res . ok ) throw new Error ( 'error fetching owned NFTs' ) ; let result = await res . json ( ) ; result . data = filterNFTs ( result . data ) ; return result ; } ; export const getCreatorNFTS = async ( id , page = "1" , limit = DEFAULT_LIMIT_PAGINATION ) => { const paginationOptions = { page , limit } ; const filterOptions = { creator : id } ; const sortOptions = "created_at:desc" ; const res = await fetch ( `${ NODE_API_URL }/api/NFTs/?pagination=${ JSON . stringify ( paginationOptions ) }&filter=${ JSON . stringify ( filterOptions ) }&sort=${ sortOptions }` ) ; if ( ! res . ok ) throw new Error ( 'error fetching created NFTs' ) ; let result = await res . json ( ) ; result . data = filterNFTs ( result . data ) ; return result ; } ; export const getNFTs = async ( codes ? , page = "1" , limit = DEFAULT_LIMIT_PAGINATION , listed ? , useCache = false ) => { const paginationOptions = { page , limit } ; const filterOptions = { marketplaceId : MARKETPLACE_ID } ; const sortOptions = "created_at:desc" ; if ( codes ) filterOptions . categories = codes ; if ( listed !== undefined ) filterOptions . listed = listed ; const res = await fetch ( `${ NODE_API_URL }/api/NFTs/?pagination=${ JSON . stringify ( paginationOptions ) }&filter=${ JSON . stringify ( filterOptions ) }&sort=${ sortOptions }&useCache300=${ useCache }` ) ; if ( ! res . ok ) throw new Error ( 'error fetching NFTs by categories' ) ; let result = await res . json ( ) ; result . data = filterNFTs ( result . data ) ; return result ; } ; export const getNFT = async ( id , incViews = false , viewerWalletId = null , ip ? , marketplaceId ? , useCache = false ) => { const filterOptions = { marketplaceId } ; const res = await fetch ( `${ NODE_API_URL }/api/NFTs/${ id }?filter=${ JSON . stringify ( filterOptions ) }&incViews=${ incViews }&viewerWalletId=${ viewerWalletId }&viewerIp=${ ip }&useCache60=${ useCache }` ) ; if ( ! res . ok ) throw new Error ( 'error fetching NFT' ) ; let data = await res . json ( ) ; if ( ! data . properties ?. preview . ipfs ) throw new Error ( ) ; return data ; } ; export const getLikedNFTs = async ( walletId , page = "1" , limit = DEFAULT_LIMIT_PAGINATION ) => { const paginationOptions = { page , limit } ; const filterOptions = { liked : walletId } ; const sortOptions = "created_at:desc" ; const res = await fetch ( `${ NODE_API_URL }/api/NFTs/?pagination=${ JSON . stringify ( paginationOptions ) }&filter=${ JSON . stringify ( filterOptions ) }&sort=${ sortOptions }` ) ; if ( ! res . ok ) throw new Error ( ) ; let result = await res . json ( ) ; result . data = filterNFTs ( result . data ) ; return result ; } ; export const getByTheSameArtistNFTs = async ( walletId , page = "1" , limit = DEFAULT_LIMIT_PAGINATION ) => { const paginationOptions = { page , limit } ; const filterOptions = { creator : walletId } ; const sortOptions = "created_at:desc" ; const res = await fetch ( `${ NODE_API_URL }/api/NFTs/?pagination=${ JSON . stringify ( paginationOptions ) }&filter=${ JSON . stringify ( filterOptions ) }&sort=${ sortOptions }&useCache300=true` ) ; if ( ! res . ok ) throw new Error ( ) ; let result = await res . json ( ) ; result . data = filterNFTs ( result . data ) ; return result ; } ; export const getUserNFTsStat = async ( id , onlyFromMpId ) : Promise < { countOwned : number ; countOwnedListed : number ; countOwnedUnlisted : number ; countCreated : number ; countFollowers : number ; countFollowed : number ; } > => { const filterOptions = { } ; if ( onlyFromMpId ) filterOptions . marketplaceId = MARKETPLACE_ID ; const res = await fetch ( `${ NODE_API_URL }/api/NFTs/stat/${ id }?filter=${ JSON . stringify ( filterOptions ) }` ) ; if ( ! res . ok ) throw new Error ( 'error fetching user NFTs stat' ) ; let result = await res . json ( ) ; return result ; } ; export const getHistory = async ( nftId , seriesId , grouped = false ) => { const sortOptions = "timestamp:desc" ; const filterOptions = { seriesId , nftId , grouped } ; const res = await fetch ( `${ NODE_API_URL }/api/nfts/history/?sort=${ sortOptions }&filter=${ JSON . stringify ( filterOptions ) }&useCache300=true` ) ; if ( ! res . ok ) throw new Error ( 'error fetching NFT history' ) ; let result = await res . json ( ) ; return result ; } ; export const canAddToSeries = async ( seriesId , walletId ) => { const res = await fetch ( `${ NODE_API_URL }/api/nfts/series/can-add?seriesId=${ seriesId }&walletId=${ walletId }` ) ; if ( ! res . ok ) throw new Error ( 'error getting information about this series for this user' ) ; let result = await res . json ( ) ; return result as boolean ; } ; export const addNFTsToCategories = async ( creator , chainIds , categories ) => { const nftsAuthToken = encryptCookie ( `${ creator },${ chainIds . join ( '-' ) },${ categories . join ( '-' ) }` ) ; const res = await fetch ( `${ NODE_API_URL }/api/nfts/add-nfts-categories` , { method : 'POST' , body : JSON . stringify ( { creator , chainIds , categories , nftsAuthToken } ) , } ) ; if ( ! res . ok ) throw new Error ( ) ; let success = await res . json ( ) ; return success ; } ; export const getSeriesData = async ( seriesId , marketplaceId = MARKETPLACE_ID , owner ? , page ? , limit ? ) => { const paginationOptions = { page , limit } ; const filterOptions = { owner , marketplaceId } ; const res = await fetch ( `${ NODE_API_URL }/api/nfts/series/data/?seriesIds=${ seriesId }&pagination=${ JSON . stringify ( paginationOptions ) }&filter=${ JSON . stringify ( filterOptions ) }&useCache60=true` ) ; if ( ! res . ok ) throw new Error ( 'error fetching NFT series data' ) ; let result = await res . json ( ) ; return result ; } ; export const getTotalOnSaleOnMarketplace = async ( marketplaceId = MARKETPLACE_ID ) => { const res = await fetch ( `${ NODE_API_URL }/api/nfts/total-on-sale/?marketplaceId=${ marketplaceId }` ) ; if ( ! res . ok ) throw new Error ( 'error fetching NFT total' ) ; let result = await res . json ( ) ; return result ; } ;