Skip to content

Commit

Permalink
Merge pull request #13 from getsafle/feature-multichain-nft-detection
Browse files Browse the repository at this point in the history
Updated the SDK to enable multichain NFT discovery
  • Loading branch information
apoorvq authored Oct 28, 2022
2 parents cdfdbc9 + 70ee1ad commit f613163
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 42 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
* 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`.
34 changes: 24 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,47 @@
# **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.

<br>

## **Installation and Usage**

> Installation
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**
<br>

> Initialising
## Initialization

<br>

Initialise the class using,

`const nftController = new safleNftController.NftController();` 

> Methods
<br>

## Methods

<br>

> 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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
8 changes: 6 additions & 2 deletions src/config/index.js
Original file line number Diff line number Diff line change
@@ -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,
}
};
45 changes: 31 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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 };
}

}
Expand Down
21 changes: 9 additions & 12 deletions src/utils/helper.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
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) {
return { error: error.response };
}
}

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,
};

0 comments on commit f613163

Please sign in to comment.