|
1 | 1 |
|
2 | | -const HELPER = require('./utils/helper'); |
3 | | -const { OPENSEA_API_KEY } = require('./config/index'); |
| 2 | +const Config = require('./config'); |
| 3 | +const Helper = require('./utils/helper'); |
4 | 4 |
|
5 | 5 | class NftController { |
6 | | - async getNftDetails(publicAddress, contractAddress) { |
7 | | - const url = await HELPER.getUserNftDataApi({ publicAddress, contractAddress }); |
8 | | - const apiKey = OPENSEA_API_KEY; |
9 | | - const { response, error } = await HELPER.getRequest({ url, apiKey }); |
| 6 | + async detectNFTs(publicAddress, chain = 'all') { |
| 7 | + Helper.inputValidator(chain); |
| 8 | + |
| 9 | + const url = `${Config.NFT_DETECTION_API}/${publicAddress}/nfts`; |
| 10 | + |
| 11 | + const { response, error } = await Helper.getRequest(url); |
| 12 | + |
10 | 13 | if (error) { |
11 | | - const { status, statusText } = error; |
12 | | - return { error:{ status, statusText } }; |
| 14 | + return { error }; |
13 | 15 | } |
14 | 16 |
|
15 | | - const { assets } = response; |
16 | | - if(assets.length === 0) { |
17 | | - return { error: { status: 400, statusText: 'No details Found' } }; |
18 | | - |
| 17 | + const assetDetails = []; |
| 18 | + let filteredData; |
| 19 | + |
| 20 | + if (chain === 'all') { |
| 21 | + filteredData = response.data; |
19 | 22 | } else { |
20 | | - const nftDetails = JSON.parse(JSON.stringify(assets[0])); |
21 | | - return { response: nftDetails }; |
| 23 | + filteredData = response.data.filter((asset) => asset.chainId === Config.CHAIN_ID[chain.toLowerCase()]); |
22 | 24 | } |
| 25 | + |
| 26 | + filteredData.forEach((asset) => { |
| 27 | + const obj = {}; |
| 28 | + |
| 29 | + obj.name = asset.name; |
| 30 | + obj.symbol = asset.symbol; |
| 31 | + obj.tokenId = asset.tokenId; |
| 32 | + obj.tokenUrl = asset.tokenUrl; |
| 33 | + obj.contractAddress = asset.tokenAddress; |
| 34 | + obj.metadata = asset.metadata; |
| 35 | + |
| 36 | + assetDetails.push(obj); |
| 37 | + }); |
| 38 | + |
| 39 | + return { response: assetDetails }; |
23 | 40 | } |
24 | 41 |
|
25 | 42 | } |
|
0 commit comments