diff --git a/CHANGELOG.md b/CHANGELOG.md index 6af1f62..62cdb1f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,11 +7,15 @@ * Added github URL's to package.json - ### 1.0.2 (2021-05-05) * Documentation Added ### 1.0.3 (2022-07-04) -* Updated error handling \ No newline at end of file +* Updated error handling + +### 2.0.0 (2022-10-25) + +* SDK v2.0.0 - This SDK can now detect and return the details of all the NFTs for a particular address across any supported chains. +* Supported Chains : `Ethereum`, `Polygon` and `BSC`. diff --git a/README.md b/README.md index 6e607c7..86e924e 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,8 @@ # **Safle NFT Controller** -Safle NFT Controller SDK +This library enables the developer to detect Non Fungible Tokens (NFT) for any public address across the supported chains. +
## **Installation and Usage** @@ -9,25 +10,38 @@ Safle NFT Controller SDK Install the package by running the command, -`npm install @getsafle/nft-controller` +```sh +npm install @getsafle/nft-controller +``` Import the package into your project using, -`const safleNftController = require('@getsafle/nft-controller');` +```js +const safleNftController = require('@getsafle/nft-controller'); +``` -## **NFT Controller** +
-> Initialising +## Initialization + +
Initialise the class using, `const nftController = new safleNftController.NftController();`  -> Methods +
+ +## Methods + +
+ +> Discover the NFTs and get their details -Get user nft details +```js +const nfts = await nftController.detectNFTs(publicAddress, chain); +``` -`const nftDetails = await nftController.getNftDetails(publicAddress, contractAddress);` +* `publicAddress` - Public address to detect NFTs. +* `chain` (optional) - Optional chain parameter to detect the NFTs. Supported chains : `Ethereum`, `Polygon` and `BSC`. -* `publicAddress` - user wallet public address, -* `contractAddress` - NFT contract address diff --git a/package.json b/package.json index aacab59..7324f6f 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@getsafle/nft-controller", - "version": "1.0.3", - "description": "", + "version": "2.0.0", + "description": "Library to enable detection of Non Fungible Tokens (NFT) for any public address across the supported chains.", "main": "src/index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" diff --git a/src/config/index.js b/src/config/index.js index 5ecb0c0..faecc97 100644 --- a/src/config/index.js +++ b/src/config/index.js @@ -1,4 +1,8 @@ module.exports = { - OPENSEA_BASE_URL: 'https://api.opensea.io/api/v1', - OPENSEA_API_KEY: '', + NFT_DETECTION_API: 'https://nft.metafi.codefi.network/accounts', + CHAIN_ID: { + ethereum: 1, + polygon: 137, + bsc: 56, + } }; \ No newline at end of file diff --git a/src/index.js b/src/index.js index ae384e4..37a45ff 100644 --- a/src/index.js +++ b/src/index.js @@ -1,25 +1,42 @@ -const HELPER = require('./utils/helper'); -const { OPENSEA_API_KEY } = require('./config/index'); +const Config = require('./config'); +const Helper = require('./utils/helper'); class NftController { - async getNftDetails(publicAddress, contractAddress) { - const url = await HELPER.getUserNftDataApi({ publicAddress, contractAddress }); - const apiKey = OPENSEA_API_KEY; - const { response, error } = await HELPER.getRequest({ url, apiKey }); + async detectNFTs(publicAddress, chain = 'all') { + Helper.inputValidator(chain); + + const url = `${Config.NFT_DETECTION_API}/${publicAddress}/nfts`; + + const { response, error } = await Helper.getRequest(url); + if (error) { - const { status, statusText } = error; - return { error:{ status, statusText } }; + return { error }; } - const { assets } = response; - if(assets.length === 0) { - return { error: { status: 400, statusText: 'No details Found' } }; - + const assetDetails = []; + let filteredData; + + if (chain === 'all') { + filteredData = response.data; } else { - const nftDetails = JSON.parse(JSON.stringify(assets[0])); - return { response: nftDetails }; + filteredData = response.data.filter((asset) => asset.chainId === Config.CHAIN_ID[chain.toLowerCase()]); } + + filteredData.forEach((asset) => { + const obj = {}; + + obj.name = asset.name; + obj.symbol = asset.symbol; + obj.tokenId = asset.tokenId; + obj.tokenUrl = asset.tokenUrl; + obj.contractAddress = asset.tokenAddress; + obj.metadata = asset.metadata; + + assetDetails.push(obj); + }); + + return { response: assetDetails }; } } diff --git a/src/utils/helper.js b/src/utils/helper.js index ef1c7fa..a71be36 100644 --- a/src/utils/helper.js +++ b/src/utils/helper.js @@ -1,16 +1,10 @@ const axios = require('axios'); -const { OPENSEA_BASE_URL } = require('../config'); +const Config = require('../config'); -async function getRequest({ url, apiKey }) { +async function getRequest(url) { try { - const response = await axios({ - url, - method: 'GET', - headers: { - 'X-API-KEY': apiKey, - }, - }); + const response = await axios.get(url); return { response: response.data }; } catch (error) { @@ -18,11 +12,14 @@ async function getRequest({ url, apiKey }) { } } -async function getUserNftDataApi({ publicAddress, contractAddress }){ - return `${OPENSEA_BASE_URL}/assets?owner=${publicAddress}&asset_contract_address=${contractAddress}`; +function inputValidator(chain) { + if (!Config.CHAIN_ID[chain.toLowerCase()] && chain !== 'all') { + throw `${chain} chain not supported.` + } } module.exports = { - getRequest, getUserNftDataApi, + getRequest, + inputValidator, };