From 87b75ec9d498064a66ef4a15700e4220f176aaae Mon Sep 17 00:00:00 2001 From: "LAPTOP-HBL4M4SL\\rauld" Date: Tue, 3 Jan 2023 15:20:14 -0300 Subject: [PATCH] CU-864dmfjy9 - Upgrade Ghost Market API to v2 --- app/actions/nftGalleryActions.js | 63 +++++++++---------- .../TransferNftModal/TransferNftModal.jsx | 2 +- app/containers/NftGallery/NftGallery.jsx | 26 ++++---- 3 files changed, 44 insertions(+), 47 deletions(-) diff --git a/app/actions/nftGalleryActions.js b/app/actions/nftGalleryActions.js index e5c5d7360..04525f81f 100644 --- a/app/actions/nftGalleryActions.js +++ b/app/actions/nftGalleryActions.js @@ -9,19 +9,11 @@ const NFT_PROVIDER = GHOST_MARKET export type NftGalleryItem = { metadata: { - attributes: any, description: string, - image: string, - media_type: string, + mediaUri: string, + mediaType: string, name: string, }, - series: { - chain: string, - contrract: string, - creator: string, - current_supply: number, - description: string, - }, tokenId: string, contract: string, collection: { @@ -35,11 +27,15 @@ export type NftGalleryResults = { page: number, } -const DEFAULT_NFT_GALLERY_RESULTS = { results: [], page: 0, count: 0 } +const DEFAULT_NFT_GALLERY_RESULTS = (previousResults?: NftGalleryItem[]) => ({ + results: previousResults ?? [], + page: 0, + count: 0, +}) export async function parseGhostMarketResults({ address, - page = 0, + page = 1, previousResults = [], }: { address: string, @@ -47,34 +43,37 @@ export async function parseGhostMarketResults({ previousResults: NftGalleryItem[], }): Promise { try { - const LIMIT = 8 - const OFFSET = LIMIT * page + const SIZE = 8 const response = await axios.get( - `https://api.ghostmarket.io/api/v1/assets?chain=n3&owner=${address}&limit=${LIMIT}&offset=${OFFSET}&with_total=1`, + `https://api.ghostmarket.io/api/v2/assets?chain=n3&owners[]=${address}&size=${SIZE}&page=${page}&getTotal=true`, ) - const items = response?.data?.assets ?? [] - const count = response?.data?.total_results ?? 0 - if (items.length) { - const results = items.map(asset => { - const parsed = { - metadata: asset.nft.nft_metadata, - series: asset.nft.series, - tokenId: asset.nft.token_id, - contract: asset.nft.contract, - collection: asset.nft.collection, - } - return parsed - }) + const { assets, total: count } = response?.data - return { results: previousResults.concat(results), page, count } - } + if (!assets || !assets.length || !count) + return DEFAULT_NFT_GALLERY_RESULTS(previousResults) + + const results = assets.map( + ({ metadata, collection, contract, tokenId }) => ({ + metadata: { + description: metadata.description, + mediaUri: metadata.mediaUri, + mediaType: metadata.mediaType, + name: metadata.name, + }, + tokenId, + contract: contract.hash, + collection: { + name: collection.name, + }, + }), + ) - return { results: previousResults, page: 0, count } + return { results: previousResults.concat(results), page, count } } catch (e) { console.error('An error occurred fetching data for NFT gallery', { e }) - return DEFAULT_NFT_GALLERY_RESULTS + return DEFAULT_NFT_GALLERY_RESULTS(previousResults) } } diff --git a/app/components/Modals/TransferNftModal/TransferNftModal.jsx b/app/components/Modals/TransferNftModal/TransferNftModal.jsx index 10907f5a7..1cb24b5f0 100644 --- a/app/components/Modals/TransferNftModal/TransferNftModal.jsx +++ b/app/components/Modals/TransferNftModal/TransferNftModal.jsx @@ -17,7 +17,7 @@ import { addPendingTransaction } from '../../../actions/pendingTransactionAction type Props = { hideModal: () => any, - imageHref: string, + mediaUri: string, url: string, mediaType: string, isWatchOnly: boolean, diff --git a/app/containers/NftGallery/NftGallery.jsx b/app/containers/NftGallery/NftGallery.jsx index 39b80322e..d52f93736 100644 --- a/app/containers/NftGallery/NftGallery.jsx +++ b/app/containers/NftGallery/NftGallery.jsx @@ -31,11 +31,11 @@ type Props = { } export function NFT({ - imageHref, + mediaUri, url = '', mediaType = '', }: { - imageHref: string, + mediaUri: string, url: string, mediaType: string, }) { @@ -52,7 +52,7 @@ export function NFT({ () => { async function determineMediaType() { try { - const mediaUrl = imageHref.replace('ipfs://', 'https://ipfs.io/ipfs/') + const mediaUrl = mediaUri.replace('ipfs://', 'https://ipfs.io/ipfs/') const results = await axios.head(mediaUrl) setParsedMediaType(results?.headers?.['content-type'] ?? '') } catch (e) { @@ -66,7 +66,7 @@ export function NFT({ determineMediaType() } }, - [mediaType], + [mediaType, mediaUri], ) return ( @@ -91,14 +91,14 @@ export function NFT({ style={{ display: isLoading ? 'none' : 'block' }} > ) : ( // eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions {results.map(data => { const { - // eslint-disable-next-line camelcase - metadata: { image, name, description, media_type }, + metadata: { mediaUri, name, description, mediaType }, tokenId, collection, contract, @@ -156,9 +155,8 @@ export default function NFTGallery({ return (
@@ -185,9 +183,9 @@ export default function NFTGallery({ renderIcon={() => } onClick={() => showModal(TRANSFER_NFT, { - imageHref: image, + mediaUri, url: `https://ghostmarket.io/asset/n3/${contract}/${tokenId}/`, - mediaType: media_type, + mediaType, isWatchOnly: true, showModal, contract, @@ -195,7 +193,7 @@ export default function NFTGallery({ }) } > - Send + SendButton
)}