Skip to content
This repository has been archived by the owner on Mar 22, 2023. It is now read-only.

Commit

Permalink
coinmarketcapapi integrated and endpoints added
Browse files Browse the repository at this point in the history
  • Loading branch information
Hammad-Mubeen committed Dec 23, 2021
1 parent bc607f7 commit ac2ba38
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 0 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
NODE_MODE=deployed
DATABASE_URL_LOCAL=mongodb://localhost:27017/V2-graphQL
DATABASE_URL_ONLINE=mongodb+srv://admin:[email protected]/V2-graphQL-backend-demo?retryWrites=true&w=majority
COIN_MARKET_CAP_API_KEY=35a7ebf7-894d-4b67-8d57-36a2b050cbc8

FACTORY_CONTRACT=Feb336a5487f160DA388d878296C9043Ab29b50daE9756675FD332408275bBeB
FACTORY_CONTRACT_PACKAGE=315A4d4d50132831DFD82bc3eB4f49Ec4E7CE6985b0dD57C43F075FFA4323853
Expand Down
2 changes: 2 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const { graphqlHTTP } = require("express-graphql");
const schema = require("./graphql/schema");
var listenerRouter = require('./routes/listenerroutes');
var tokensListRouter = require('./routes/tokenslist');
var coinsmarketcapapiRouter = require('./routes/coinsmarketcapapi');

// view engine setup
app.set('views', path.join(__dirname, 'views'));
Expand Down Expand Up @@ -67,6 +68,7 @@ app.get("/", (req, res) => {
});
app.use('/', listenerRouter);
app.use('/', tokensListRouter);
app.use('/', coinsmarketcapapiRouter);

app.use('/graphql', graphqlHTTP({
schema: schema,
Expand Down
190 changes: 190 additions & 0 deletions routes/coinsmarketcapapi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
require('dotenv').config()
var express = require('express');
var router = express.Router();
const axios = require('axios').default;

const requestOptions = {
method: 'GET',
url: "https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest",
qs: {
'start': '1',
'limit': '5000',
'convert': 'USD'
},
headers: {
'X-CMC_PRO_API_KEY': process.env.COIN_MARKET_CAP_API_KEY
},
json: true,
gzip: true
};

router.route("/getworthinUSD").post(async function (req, res, next) {
try {

if(!req.body.symbol)
{
return res.status(400).json({
success: false,
message: "symbol not found in the request body."
});
}
if(!req.body.amount)
{
return res.status(400).json({
success: false,
message: "amount not found in the request body."
});
}

axios(requestOptions)
.then(response => {

for(var i=0; i<(response.data).data.length;i++)
{
console.log("symbol: ",response.data.data[i].symbol);
if(response.data.data[i].symbol == req.body.symbol)
{
return res.status(200).json({
success: true,
message: req.body.symbol+" worth in USD is = "+response.data.data[i].quote.USD.price,
worth:response.data.data[i].quote.USD.price,
worthforamountpassed:req.body.amount*(response.data.data[i].quote.USD.price)
});
}

if(i==(response.data.data.length)-1)
{
console.log(req.body.symbol+" not found in the coinmarketcap Api cryptocurrencies");
return res.status(400).json({
success: true,
message: req.body.symbol+" not found in the coinmarketcap Api cryptocurrencies",
});
}
}

}).catch((err) => {

console.log('API call error:', err.message);
return res.status(400).json({
success: false,
message: err.message
});
});


} catch (error) {
console.log("error (try-catch) : " + error);
return res.status(500).json({
success: false,
err: error,
});
}
});

router.route("/tokensworthconversion").post(async function (req, res, next) {
try {

if(!req.body.symbolforconversion)
{
return res.status(400).json({
success: false,
message: "symbolforconversion not found in the request body."
});
}
if(!req.body.symboltoconvertto)
{
return res.status(400).json({
success: false,
message: "symboltoconvertto not found in the request body."
});
}
if(!req.body.amounttoconvertto)
{
return res.status(400).json({
success: false,
message: "amount not found in the request body."
});
}
let symboltoconverttoworthinUSD;
let symbolforconvertionworthinUSD;
let count=0;
let flag=0;

axios(requestOptions)
.then(response => {

//console.log('API call response: ', response.data);
for(var i=0; i<(response.data).data.length;i++)
{
console.log("symbol: ",response.data.data[i].symbol);
if(response.data.data[i].symbol == req.body.symbolforconversion)
{
symbolforconvertionworthinUSD=response.data.data[i].quote.USD.price;
console.log("symbolforconverion worth in USD: ",symbolforconvertionworthinUSD);
count++;
flag=1;
}
if(response.data.data[i].symbol == req.body.symboltoconvertto)
{
symboltoconverttoworthinUSD=response.data.data[i].quote.USD.price;
console.log("symboltoconverttoworthinUSD worth in USD: ",symboltoconverttoworthinUSD);
count++;
flag=2;
}
if(count==2)
{

return res.status(200).json({
success: true,
symbolforconversionWorthInUSD:req.body.symbolforconversion+" worth in USD = "+symbolforconvertionworthinUSD,
symboltoconverttoWorthInUSD:req.body.symboltoconvertto+" worth in USD = "+symboltoconverttoworthinUSD,
ratio: "1 "+ req.body.symboltoconvertto+ " = "+ symboltoconverttoworthinUSD/symbolforconvertionworthinUSD+" "+req.body.symbolforconversion +" ($"+symboltoconverttoworthinUSD+")",
worth: symbolforconvertionworthinUSD/symboltoconverttoworthinUSD,
worthforamountpassed:(symbolforconvertionworthinUSD/symboltoconverttoworthinUSD)*req.body.amounttoconvertto

});
}
if(i==(response.data.data.length)-1)
{
if(flag==1)
{
console.log(req.body.symboltoconvertto+" not found in the coinmarketcap Api cryptocurrencies");
return res.status(400).json({
success: true,
message: req.body.symboltoconvertto+" not found in the coinmarketcap Api cryptocurrencies",
});
}
else if(flag==2)
{
console.log(req.body.symbolforconversion+" not found in the coinmarketcap Api cryptocurrencies");
return res.status(400).json({
success: true,
message: req.body.symbolforconversion+ " not found in the coinmarketcap Api cryptocurrencies",
});

}

}
}

}).catch((err) => {

console.log('API call error:', err.message);
return res.status(400).json({
success: false,
message: err.message
});
});


} catch (error) {
console.log("error (try-catch) : " + error);
return res.status(500).json({
success: false,
err: error,
});
}
});


module.exports = router;

0 comments on commit ac2ba38

Please sign in to comment.