|
| 1 | +/* eslint-disable @typescript-eslint/naming-convention */ |
| 2 | +/* eslint-disable no-restricted-globals */ |
| 3 | +import { assert } from '@metamask/superstruct'; |
| 4 | +import { Duration } from '@metamask/utils'; |
| 5 | + |
| 6 | +import type { ICache } from '../../caching/ICache'; |
| 7 | +import type { Serializable } from '../../serialization/types'; |
| 8 | +import type { ConfigProvider } from '../../services/config'; |
| 9 | +import { buildUrl } from '../../utils/buildUrl'; |
| 10 | +import type { ILogger } from '../../utils/logger'; |
| 11 | +import logger from '../../utils/logger'; |
| 12 | +import { UrlStruct } from '../../validation/structs'; |
| 13 | +import { MOCK_NFTS_LIST_RESPONSE_MAPPED } from './mocks/mockNftsListResponseMapped'; |
| 14 | +import { mapGetNftMetadataResponse } from './utils/mapGetNftMetadataResponse'; |
| 15 | + |
| 16 | +export class NftApiClient { |
| 17 | + readonly #fetch: typeof globalThis.fetch; |
| 18 | + |
| 19 | + readonly #logger: ILogger; |
| 20 | + |
| 21 | + readonly #baseUrl: string; |
| 22 | + |
| 23 | + readonly #cache: ICache<Serializable>; |
| 24 | + |
| 25 | + public static readonly cacheTtlsMilliseconds = { |
| 26 | + fiatExchangeRates: Duration.Minute, |
| 27 | + spotPrices: Duration.Minute, |
| 28 | + historicalPrices: Duration.Minute, |
| 29 | + }; |
| 30 | + |
| 31 | + constructor( |
| 32 | + configProvider: ConfigProvider, |
| 33 | + _cache: ICache<Serializable>, |
| 34 | + _fetch: typeof globalThis.fetch = globalThis.fetch, |
| 35 | + _logger: ILogger = logger, |
| 36 | + ) { |
| 37 | + const { baseUrl } = configProvider.get().nftApi; |
| 38 | + |
| 39 | + assert(baseUrl, UrlStruct); |
| 40 | + |
| 41 | + this.#fetch = _fetch; |
| 42 | + this.#logger = _logger; |
| 43 | + this.#baseUrl = baseUrl; |
| 44 | + |
| 45 | + this.#cache = _cache; |
| 46 | + } |
| 47 | + |
| 48 | + async listAddressSolanaNfts(address: string) { |
| 49 | + // let allItems: Balance[] = []; |
| 50 | + // let currentCursor: string | undefined; |
| 51 | + |
| 52 | + // do { |
| 53 | + // const url = buildUrl({ |
| 54 | + // baseUrl: this.#baseUrl, |
| 55 | + // path: `/users/${address}/solana-tokens`, |
| 56 | + // queryParams: currentCursor ? { cursor: currentCursor } : undefined, |
| 57 | + // }); |
| 58 | + // const response = await this.#fetch(url, { |
| 59 | + // headers: { |
| 60 | + // 'Content-Type': 'application/json', |
| 61 | + // accept: 'application/json', |
| 62 | + // version: '1', |
| 63 | + // }, |
| 64 | + // }); |
| 65 | + // const data = await response.json(); |
| 66 | + |
| 67 | + // const mappedData = mapListAddressSolanaNftsResponse(data); |
| 68 | + |
| 69 | + // console.log('FOUND THESE NFTS'); |
| 70 | + // console.log(mappedData.items.length); |
| 71 | + |
| 72 | + // allItems = [...allItems, ...mappedData.items]; |
| 73 | + // currentCursor = mappedData.cursor ?? undefined; |
| 74 | + // } while (currentCursor); |
| 75 | + |
| 76 | + // return allItems; |
| 77 | + |
| 78 | + return MOCK_NFTS_LIST_RESPONSE_MAPPED.items; |
| 79 | + } |
| 80 | + |
| 81 | + async getNftMetadata(tokenAddress: string) { |
| 82 | + try { |
| 83 | + const url = buildUrl({ |
| 84 | + baseUrl: this.#baseUrl, |
| 85 | + path: `/nfts/contracts/solana/${tokenAddress}/1`, |
| 86 | + }); |
| 87 | + const response = await this.#fetch(url); |
| 88 | + const data = await response.json(); |
| 89 | + |
| 90 | + const mappedData = mapGetNftMetadataResponse(data); |
| 91 | + |
| 92 | + return mappedData; |
| 93 | + } catch (error) { |
| 94 | + return null; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + async getNftsMetadata(tokenAddresses: string[]) { |
| 99 | + const nftsMetadata = await Promise.all( |
| 100 | + tokenAddresses.map(async (tokenAddress) => |
| 101 | + this.getNftMetadata(tokenAddress), |
| 102 | + ), |
| 103 | + ); |
| 104 | + |
| 105 | + return nftsMetadata; |
| 106 | + } |
| 107 | +} |
0 commit comments