Skip to content

Commit

Permalink
Merge pull request #29 from AbdulAhadArain/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
AbdulAhadArain authored Aug 26, 2024
2 parents b8458f3 + c270ccd commit d55f7b5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 37 deletions.
64 changes: 28 additions & 36 deletions src/controllers/info.controller.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,39 @@
import { Request, Response, NextFunction } from 'express';
import { transactionsService } from '../services';
import axios from 'axios';
import { COINPAPRIKA_API } from '../utils/constants';
import { COINGECKO_API } from '../utils/constants';

export const getInfoData = async (
req: Request,
res: Response,
next: NextFunction,
): Promise<void> => {
try {
// => To get FRM data
const response = await axios.get(COINPAPRIKA_API);
const price = response?.data.quotes.USD.price;
const marketCapUsd = response?.data.quotes.USD.market_cap;
const marketCapChange = response?.data.quotes.USD.market_cap_change_24h;
const volume24h = response?.data?.quotes?.USD?.volume_24h;
// Fetch FRM data from CoinGecko API
const { data } = await axios.get(COINGECKO_API);

// => to get garph data
const today = new Date();
const frmPrice = data?.market_data.current_price.usd;
const frmPriceChangePercentage =
data?.market_data.price_change_percentage_24h;
const frmMarketCap = data?.market_data.market_cap.usd;
const frmMarketCapChangePercentage =
data?.market_data.market_cap_change_percentage_24h;
const frmVolume24h = data?.market_data.total_volume.usd;

const timestampsForLast7Days: any = [];
const requestedDays = Number(req.query.days);
// Fetch graph data for the last 24 hours
const now = new Date();
const oneDayAgo = new Date(now);
oneDayAgo.setDate(now.getDate() - 1);

for (let i = 0; i < 7; i++) {
const date = new Date(today);
date.setDate(today.getDate() - i);
date.toISOString();
const timestamp = getTimestampForDate(date);
timestampsForLast7Days.push(timestamp);
}

var endDate = new Date(); // Current date
var startDate = new Date(endDate);
startDate.setDate(endDate.getDate() - requestedDays);

const startTimeStamp = getTimestampForDate(startDate);
const endTimeStamp = getTimestampForDate(endDate);
const startTimeStamp = convertDateToTimestamp(oneDayAgo);
const endTimeStamp = convertDateToTimestamp(now);

const transactions = await transactionsService.getDataForChart(
startTimeStamp,
endTimeStamp,
);

// => to get total Transactions
// Fetch total transactions
const totalTransactions = await transactionsService.totalTransactions();
const generateLast24HourData = () => {
const data = [];
Expand All @@ -52,7 +43,7 @@ export const getInfoData = async (
date.setHours(date.getHours() - i);
const volume = Math.floor(Math.random() * (5000 - 1000 + 1)) + 1000;
data.push({
date: date.toISOString(),
// date: date.toISOString(),
volume: volume,
});
}
Expand All @@ -63,23 +54,24 @@ export const getInfoData = async (
// Example usage for graph data
const graphData =
transactions.length > 0 ? transactions : generateLast24HourData();

// Send data to the frontend
res.send({
frmData: {
price,
marketCapUsd,
marketCapChange,
volume24h,
price: frmPrice,
priceChangePercentage: frmPriceChangePercentage,
marketCap: frmMarketCap,
marketCapChangePercentage: frmMarketCapChangePercentage,
volume24h: frmVolume24h,
},
totalTransactions: totalTransactions,
graphData: graphData,
totalTransactions,
graphData,
});
} catch (error) {
next(error);
}
};

// => To Convert date into seconds
export const getTimestampForDate = (date: Date) => {
// Utility function to convert a date to a Unix timestamp (in seconds)
export const convertDateToTimestamp = (date: Date): number => {
return Math.floor(date.getTime() / 1000);
};
3 changes: 2 additions & 1 deletion src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export const isTransactionOrBlockHash = (addr: string) => {
return /^0x([A-Fa-f0-9]{64})$/.test(addr);
};

export const COINPAPRIKA_API = `https://api.coinpaprika.com/v1/tickers/frm-ferrum-network`;
// export const COINPAPRIKA_API = `https://api.coinpaprika.com/v1/tickers/frm-ferrum-network`;
export const COINGECKO_API = `https://api.coingecko.com/api/v3/coins/ferrum-network`;

export const ContractAddresses = {
QuantumPortalGateway: '0x42344220775e9A3A89534503d8eE4414e853476A',
Expand Down

0 comments on commit d55f7b5

Please sign in to comment.