Skip to content

Commit 046ca42

Browse files
authored
Merge pull request #15 from getsafle/feature-return-price-data
2 parents f613163 + b9799fe commit 046ca42

File tree

6 files changed

+44
-6
lines changed

6 files changed

+44
-6
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ test.js
77
.terraform
88
*.tfstate*
99
.DS_Store
10-
.vscode
10+
.vscode
11+
test.js

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@
1919

2020
* 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.
2121
* Supported Chains : `Ethereum`, `Polygon` and `BSC`.
22+
23+
### 2.1.0 (2022-11-24)
24+
25+
* This version will return the price data for every NFT. If the price data in not available, then it will return `No price data found`.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@getsafle/nft-controller",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"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": {

src/config/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@ module.exports = {
44
ethereum: 1,
55
polygon: 137,
66
bsc: 56,
7-
}
7+
},
8+
OPENSEA_API(contractAddress, tokenId) {
9+
return `https://api.opensea.io/api/v1/asset/${contractAddress}/${tokenId}/?include_orders=false`
10+
},
811
};

src/index.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class NftController {
2323
filteredData = response.data.filter((asset) => asset.chainId === Config.CHAIN_ID[chain.toLowerCase()]);
2424
}
2525

26-
filteredData.forEach((asset) => {
26+
for (const asset of filteredData) {
2727
const obj = {};
2828

2929
obj.name = asset.name;
@@ -32,9 +32,20 @@ class NftController {
3232
obj.tokenUrl = asset.tokenUrl;
3333
obj.contractAddress = asset.tokenAddress;
3434
obj.metadata = asset.metadata;
35+
obj.chainId = asset.chainId;
36+
37+
let priceData;
38+
39+
if (obj.chainId === 1) {
40+
priceData = await Helper.getPriceData(obj.contractAddress, obj.tokenId);
41+
} else {
42+
priceData = 'No price data found';
43+
}
44+
45+
obj.priceData = priceData;
3546

3647
assetDetails.push(obj);
37-
});
48+
};
3849

3950
return { response: assetDetails };
4051
}

src/utils/helper.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ async function getRequest(url) {
88

99
return { response: response.data };
1010
} catch (error) {
11-
return { error: error.response };
11+
return { error: error };
1212
}
1313
}
1414

@@ -18,8 +18,27 @@ function inputValidator(chain) {
1818
}
1919
}
2020

21+
async function getPriceData(contractAddress, tokenId) {
22+
const { error, response } = await this.getRequest(Config.OPENSEA_API(contractAddress, tokenId));
23+
24+
if (error) {
25+
return 'No price data found';
26+
}
27+
28+
const { last_sale: { total_price, payment_token: { symbol, decimals, usd_price } } } = response;
29+
30+
const priceData = {
31+
symbol,
32+
valueInCrypto: total_price/decimals,
33+
valueInUSD: (total_price/decimals) * usd_price,
34+
}
35+
36+
return priceData;
37+
}
38+
2139
module.exports = {
2240
getRequest,
2341
inputValidator,
42+
getPriceData,
2443
};
2544

0 commit comments

Comments
 (0)