Skip to content

Commit f613163

Browse files
authored
Merge pull request #13 from getsafle/feature-multichain-nft-detection
Updated the SDK to enable multichain NFT discovery
2 parents cdfdbc9 + 70ee1ad commit f613163

File tree

6 files changed

+78
-42
lines changed

6 files changed

+78
-42
lines changed

CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@
77

88
* Added github URL's to package.json
99

10-
1110
### 1.0.2 (2021-05-05)
1211

1312
* Documentation Added
1413

1514
### 1.0.3 (2022-07-04)
1615

17-
* Updated error handling
16+
* Updated error handling
17+
18+
### 2.0.0 (2022-10-25)
19+
20+
* 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.
21+
* Supported Chains : `Ethereum`, `Polygon` and `BSC`.

README.md

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,47 @@
11
# **Safle NFT Controller**
22

3-
Safle NFT Controller SDK
3+
This library enables the developer to detect Non Fungible Tokens (NFT) for any public address across the supported chains.
44

5+
<br>
56

67
## **Installation and Usage**
78

89
> Installation
910
1011
Install the package by running the command,
1112

12-
`npm install @getsafle/nft-controller`
13+
```sh
14+
npm install @getsafle/nft-controller
15+
```
1316

1417
Import the package into your project using,
1518

16-
`const safleNftController = require('@getsafle/nft-controller');`
19+
```js
20+
const safleNftController = require('@getsafle/nft-controller');
21+
```
1722

18-
## **NFT Controller**
23+
<br>
1924

20-
> Initialising
25+
## Initialization
26+
27+
<br>
2128

2229
Initialise the class using,
2330

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

26-
> Methods
33+
<br>
34+
35+
## Methods
36+
37+
<br>
38+
39+
> Discover the NFTs and get their details
2740
28-
Get user nft details
41+
```js
42+
const nfts = await nftController.detectNFTs(publicAddress, chain);
43+
```
2944

30-
`const nftDetails = await nftController.getNftDetails(publicAddress, contractAddress);`
45+
* `publicAddress` - Public address to detect NFTs.
46+
* `chain` (optional) - Optional chain parameter to detect the NFTs. Supported chains : `Ethereum`, `Polygon` and `BSC`.
3147

32-
* `publicAddress` - user wallet public address,
33-
* `contractAddress` - NFT contract address

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@getsafle/nft-controller",
3-
"version": "1.0.3",
4-
"description": "",
3+
"version": "2.0.0",
4+
"description": "Library to enable detection of Non Fungible Tokens (NFT) for any public address across the supported chains.",
55
"main": "src/index.js",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1"

src/config/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
module.exports = {
2-
OPENSEA_BASE_URL: 'https://api.opensea.io/api/v1',
3-
OPENSEA_API_KEY: '',
2+
NFT_DETECTION_API: 'https://nft.metafi.codefi.network/accounts',
3+
CHAIN_ID: {
4+
ethereum: 1,
5+
polygon: 137,
6+
bsc: 56,
7+
}
48
};

src/index.js

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,42 @@
11

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');
44

55
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+
1013
if (error) {
11-
const { status, statusText } = error;
12-
return { error:{ status, statusText } };
14+
return { error };
1315
}
1416

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;
1922
} 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()]);
2224
}
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 };
2340
}
2441

2542
}

src/utils/helper.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
const axios = require('axios');
2-
const { OPENSEA_BASE_URL } = require('../config');
32

3+
const Config = require('../config');
44

5-
async function getRequest({ url, apiKey }) {
5+
async function getRequest(url) {
66
try {
7-
const response = await axios({
8-
url,
9-
method: 'GET',
10-
headers: {
11-
'X-API-KEY': apiKey,
12-
},
13-
});
7+
const response = await axios.get(url);
148

159
return { response: response.data };
1610
} catch (error) {
1711
return { error: error.response };
1812
}
1913
}
2014

21-
async function getUserNftDataApi({ publicAddress, contractAddress }){
22-
return `${OPENSEA_BASE_URL}/assets?owner=${publicAddress}&asset_contract_address=${contractAddress}`;
15+
function inputValidator(chain) {
16+
if (!Config.CHAIN_ID[chain.toLowerCase()] && chain !== 'all') {
17+
throw `${chain} chain not supported.`
18+
}
2319
}
2420

2521
module.exports = {
26-
getRequest, getUserNftDataApi,
22+
getRequest,
23+
inputValidator,
2724
};
2825

0 commit comments

Comments
 (0)