This commit is contained in:
Nouman Tahir 2023-05-29 16:33:23 +05:00
parent a20f3da34c
commit 82a123d05b
3 changed files with 11 additions and 13 deletions

View File

@ -4,7 +4,7 @@ const BASE_URL = 'https://api.coingecko.com';
const PATH_API = 'api';
const API_VERSION = 'v3';
//documentation reference: https://www.coingecko.com/en/api/documentation
// documentation reference: https://www.coingecko.com/en/api/documentation
const coingeckoApi = axios.create({
baseURL: `${BASE_URL}/${PATH_API}/${API_VERSION}`,

View File

@ -17,7 +17,6 @@ export const fetchMarketChart = async (
days: number,
interval?: ChartInterval,
): Promise<MarketData> => {
try {
const params = {
vs_currency,
@ -25,14 +24,13 @@ export const fetchMarketChart = async (
interval,
};
//failsafe for accidental invalid parameters
//ref: https://www.coingecko.com/en/api/documentation
//endpoint: /coins/{id}/market_chart
if(days > 90 && interval === ChartInterval.HOURLY){
interval = ChartInterval.DAILY
// failsafe for accidental invalid parameters
// ref: https://www.coingecko.com/en/api/documentation
// endpoint: /coins/{id}/market_chart
if (days > 90 && interval === ChartInterval.HOURLY) {
interval = ChartInterval.DAILY;
}
const res = await coingeckoApi.get(`/${PATH_COINS}/${coingeckoId}/${PATH_MARKET_CHART}`, {
params,
});

View File

@ -21,7 +21,6 @@ export const CoinChart = ({ coinId, isEngine }: CoinChartProps) => {
const [chartData, setChartData] = useState(priceHistory?.data);
const _fetchMarketData = async (days: number) => {
if (isEngine) {
const marketData = await fetchEngineMarketData(
coinId,
@ -32,10 +31,11 @@ export const CoinChart = ({ coinId, isEngine }: CoinChartProps) => {
setChartData(marketData.map((item) => item.close));
} else {
const marketData = await fetchMarketChart(
coinId,
currency.currency,
days,
days > 30 ? ChartInterval.DAILY : ChartInterval.HOURLY);
coinId,
currency.currency,
days,
days > 30 ? ChartInterval.DAILY : ChartInterval.HOURLY,
);
setChartData(marketData.prices.map((item) => item.yValue));
}
};