This repository has been archived by the owner on Mar 22, 2023. It is now read-only.
forked from Uniswap/v2-subgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
coinmarketcapapi integrated and endpoints added
- Loading branch information
1 parent
bc607f7
commit ac2ba38
Showing
3 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |