From ac2ba38732a2dfdf405bc015b355dfb6e15678e1 Mon Sep 17 00:00:00 2001 From: Hammad-Mubeen Date: Fri, 24 Dec 2021 02:18:51 +0500 Subject: [PATCH] coinmarketcapapi integrated and endpoints added --- .env | 1 + app.js | 2 + routes/coinsmarketcapapi.js | 190 ++++++++++++++++++++++++++++++++++++ 3 files changed, 193 insertions(+) create mode 100644 routes/coinsmarketcapapi.js diff --git a/.env b/.env index 651a0f06..4c2f1ad6 100644 --- a/.env +++ b/.env @@ -1,6 +1,7 @@ NODE_MODE=deployed DATABASE_URL_LOCAL=mongodb://localhost:27017/V2-graphQL DATABASE_URL_ONLINE=mongodb+srv://admin:scytalelabs@cluster0-9yop5.mongodb.net/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 diff --git a/app.js b/app.js index abb922c7..1d86163c 100644 --- a/app.js +++ b/app.js @@ -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')); @@ -67,6 +68,7 @@ app.get("/", (req, res) => { }); app.use('/', listenerRouter); app.use('/', tokensListRouter); +app.use('/', coinsmarketcapapiRouter); app.use('/graphql', graphqlHTTP({ schema: schema, diff --git a/routes/coinsmarketcapapi.js b/routes/coinsmarketcapapi.js new file mode 100644 index 00000000..a6f593d9 --- /dev/null +++ b/routes/coinsmarketcapapi.js @@ -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;