diff --git a/server/Crypto-Server.postman_collection.json b/server/Crypto-Server.postman_collection.json index 8eceefa..a02ecd0 100644 --- a/server/Crypto-Server.postman_collection.json +++ b/server/Crypto-Server.postman_collection.json @@ -86,6 +86,214 @@ } }, "response": [] + }, + { + "name": "volume_24", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "http://localhost:3000/crypto/SOL/volume_24h", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "3000", + "path": [ + "crypto", + "SOL", + "volume_24h" + ] + } + }, + "response": [] + }, + { + "name": "volume_change_24h", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "http://localhost:3000/crypto/ETH/volume_change_24h", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "3000", + "path": [ + "crypto", + "ETH", + "volume_change_24h" + ] + } + }, + "response": [] + }, + { + "name": "percent_change_1h", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "http://localhost:3000/crypto/SOL/percent_change_1h", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "3000", + "path": [ + "crypto", + "SOL", + "percent_change_1h" + ] + } + }, + "response": [] + }, + { + "name": "percent_change_24h", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "http://localhost:3000/crypto/ETH/percent_change_24h", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "3000", + "path": [ + "crypto", + "ETH", + "percent_change_24h" + ] + } + }, + "response": [] + }, + { + "name": "percent_change_7d", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "http://localhost:3000/crypto/LINK/percent_change_7d", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "3000", + "path": [ + "crypto", + "LINK", + "percent_change_7d" + ] + } + }, + "response": [] + }, + { + "name": "percent_change_30d", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "http://localhost:3000/crypto/ADA/percent_change_30d", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "3000", + "path": [ + "crypto", + "ADA", + "percent_change_30d" + ] + } + }, + "response": [] + }, + { + "name": "market_cap", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "http://localhost:3000/crypto/ETH/market_cap", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "3000", + "path": [ + "crypto", + "ETH", + "market_cap" + ] + } + }, + "response": [] + }, + { + "name": "market_cap_dominance", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "http://localhost:3000/crypto/ADA/market_cap_dominance", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "3000", + "path": [ + "crypto", + "ADA", + "market_cap_dominance" + ] + } + }, + "response": [] + }, + { + "name": "convert", + "request": { + "method": "GET", + "header": [ + { + "key": "X-CMC_PRO_API_KEY", + "value": "e3cbef6e-746b-4f11-8c34-3ebe787cb615", + "type": "text" + } + ], + "url": { + "raw": "http://localhost:3000/Crypto/price-convert?amount=1&symbol=LINK&convert=INR", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "3000", + "path": [ + "Crypto", + "price-convert" + ], + "query": [ + { + "key": "amount", + "value": "1" + }, + { + "key": "symbol", + "value": "LINK" + }, + { + "key": "convert", + "value": "INR" + } + ] + } + }, + "response": [] } ] } \ No newline at end of file diff --git a/server/controller/Notes.md b/server/controller/Notes.md new file mode 100644 index 0000000..c3a2db2 --- /dev/null +++ b/server/controller/Notes.md @@ -0,0 +1 @@ +Controllers for api diff --git a/server/middleware/Notes.md b/server/middleware/Notes.md new file mode 100644 index 0000000..2a2986d --- /dev/null +++ b/server/middleware/Notes.md @@ -0,0 +1 @@ +Middlewars for api diff --git a/server/routes/CryptoCurrency.ts b/server/routes/CryptoCurrency.ts index d0dd775..c2405e5 100644 --- a/server/routes/CryptoCurrency.ts +++ b/server/routes/CryptoCurrency.ts @@ -1,4 +1,4 @@ -import * as redis from 'redis'; +import * as redis from "redis"; import express from "express"; import axios from "axios"; const router = express.Router(); @@ -28,6 +28,48 @@ const api = axios.create({ }, }); +// Create an Axios instance for API requests +const apipc = axios.create({ + baseURL: "https://pro-api.coinmarketcap.com/v2/tools", + headers: { + "X-CMC_PRO_API_KEY": API_KEY, + Accept: "application/json", + }, +}); + +// Define a new route to convert cryptocurrency price +router.get("/price-convert", async (req, res) => { + try { + // Extract query parameters from the request + const { amount, symbol, convert } = req.query; + + // Make an API request to CoinMarketCap for price conversion + const response = await apipc.get("/price-conversion", { + params: { + amount, + symbol, + convert, + }, + }); + + // Check if the response is successful + if (response.status === 200) { + // Extract the converted price from the response + //@ts-ignore + const convertedPrice = response.data.data[0].quote[convert].price; + + // Send the converted price as a JSON response + res.json({ convertedPrice }); + } else { + // Handle API error response + res.status(response.status).json({ error: response.data.error.message }); + } + } catch (err) { + console.error(err); + res.status(500).json({ error: "An error occurred" }); + } +}); + router.get("/top20", async (req, res) => { try { const catchData = await redisClient.get("top20"); diff --git a/server/routes/cryptoData.ts b/server/routes/cryptoData.ts deleted file mode 100644 index 0b55b5f..0000000 --- a/server/routes/cryptoData.ts +++ /dev/null @@ -1,117 +0,0 @@ -// import dotenv from "dotenv"; -import redis from "redis"; -import axios from "axios"; -// const router = express.Router(); -// dotenv.config(); - -const API_KEY = "e3cbef6e-746b-4f11-8c34-3ebe787cb615"; -const BASE_URL = "https://pro-api.coinmarketcap.com/v1/cryptocurrency"; - -let redisClient; -// Redis connection -(async () => { - redisClient = redis.createClient(); - redisClient.on("error", (err) => console.log("redis error", err)); - await redisClient.connect(); -})(); - -const api = axios.create({ - baseURL: BASE_URL, - headers: { - "X-CMC_PRO_API_KEY": API_KEY, - Accept: "application/json", - }, -}); - -const getData = async (req, res) => { - try { - const catchData = await redisClient.get("topTen"); - if (catchData) { - res.json(JSON.parse(catchData)); - return; - } - - const response = await api.get("/quotes/latest"); - const Data = response.data.data; - await redisClient.set("topTen", JSON.stringify(Data)); - res.json(response.data.data); - } catch (err) { - console.error(err); - res.status(500).json({ error: "An error occurred" }); - } -}; - -export default getData; - -// const getSymbol = async (req, res) => { -// try { -// // Fetch cryptocurrency data by symbol -// const response = await api.get(`/quotes/latest?symbol=${symbol}`); - -// if (response.data && response.data.data && response.data.data[symbol]) { -// const cryptoData = response.data.data[symbol]; -// res.json(cryptoData); -// } else { -// res.status(404).json({ error: "Cryptocurrency not found" }); -// } -// } catch (err) { -// console.error(err); -// res.status(500).json({ error: "An error occurred" }); -// } -// }; - -// // Define a route for a specific cryptocurrency by symbol -// router.get("/:symbol", async (req, res) => { -// const symbol = req.params.symbol; - -// try { -// // Fetch cryptocurrency data by symbol -// const response = await api.get(`/quotes/latest?symbol=${symbol}`); - -// if (response.data && response.data.data && response.data.data[symbol]) { -// const cryptoData = response.data.data[symbol]; -// res.json(cryptoData); -// } else { -// res.status(404).json({ error: "Cryptocurrency not found" }); -// } -// } catch (err) { -// console.error(err); -// res.status(500).json({ error: "An error occurred" }); -// } -// }); - -// router.get("/:symbol/price", async (req, res) => { -// const symbol = req.params.symbol; - -// try { -// // Fetch cryptocurrency data by symbol and get price in USD -// const response = await api.get(`/quotes/latest?symbol=${symbol}`); - -// if (response.data && response.data.data && response.data.data[symbol]) { -// const cryptoData = response.data.data[symbol]; -// if ( -// cryptoData.quote && -// cryptoData.quote.USD && -// cryptoData.quote.USD.price -// ) { -// const priceInUSD = cryptoData.quote.USD.price; -// res.json({ priceUSD: priceInUSD }); -// } else { -// res -// .status(404) -// .json({ error: "Price not found for the cryptocurrency" }); -// } -// } else { -// res.status(404).json({ error: "Cryptocurrency not found" }); -// } -// } catch (err) { -// console.error(err); -// res.status(500).json({ error: "An error occurred" }); -// } -// }); - -// module.exports = router; -// export default { -// getTopTen, -// getSymbol, -// }; diff --git a/server/src/index.ts b/server/src/index.ts index f37aeb6..c79aace 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -16,13 +16,14 @@ const app = express(); // app.use(cookieParser()); // app.use(bodyParser.json()); -// app.use("/crypto", CryptoCurrency); + const PORT = 3000; app.use("/crypto", CryptoCurrency); + app.listen(PORT, () => { console.log("Server Listening on PORT:", PORT); });