From 76fed4d71d20c6bf5b681cef5a1e924f51435766 Mon Sep 17 00:00:00 2001 From: Kelechi Date: Fri, 20 Dec 2024 12:14:09 +0100 Subject: [PATCH] chore: created server action to fix hiiq count issue --- src/app/dashboard/_actions.ts | 43 +++++++++++++++++++++++++++++ src/hooks/useGetTokenHolders.ts | 2 -- src/lib/graphql-client.ts | 20 ++++++++++++++ src/pages/api/token-holder-count.ts | 1 + src/services/holder.ts | 14 ++++++++++ src/utils/stats-data.ts | 7 ++++- src/utils/use-stats-data.ts | 3 +- 7 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 src/app/dashboard/_actions.ts create mode 100644 src/lib/graphql-client.ts create mode 100644 src/services/holder.ts diff --git a/src/app/dashboard/_actions.ts b/src/app/dashboard/_actions.ts new file mode 100644 index 00000000..177efed1 --- /dev/null +++ b/src/app/dashboard/_actions.ts @@ -0,0 +1,43 @@ +import { bscHolders, maticHolders } from '@/data/StatsData' +import { getHIIQHoldersCount } from '@/services/holder' + +export async function getTokenHolders() { + try { + const hiiqCount = await getHIIQHoldersCount() + + const eosDataResponse = await fetch( + 'https://www.api.bloks.io/tokens?type=tokenHoldersCount&chain=eos&contract=everipediaiq&symbol=IQ', + { cache: 'no-store' }, + ) + const eosData = await eosDataResponse.text() + + const ethDataResponse = await fetch( + '/api/token-holder-count?address=0x579cea1889991f68acc35ff5c3dd0621ff29b0c9', + { cache: 'no-store' }, + ) + const ethData = await ethDataResponse.json() + + const holdersData = { + holders: { + eos: eosData, + eth: ethData?.response ?? 0, + matic: maticHolders, + bsc: bscHolders, + hiiq: hiiqCount ?? 0, + }, + } + + return holdersData + } catch (err) { + console.error('Error fetching token holders:', err) + return { + holders: { + eos: 0, + eth: 0, + matic: 0, + bsc: 0, + hiiq: 0, + }, + } + } +} diff --git a/src/hooks/useGetTokenHolders.ts b/src/hooks/useGetTokenHolders.ts index 5534610c..3ccfec04 100644 --- a/src/hooks/useGetTokenHolders.ts +++ b/src/hooks/useGetTokenHolders.ts @@ -26,8 +26,6 @@ const useTokenHolders = () => { }, } - console.log('Token Holders Data:', holdersData) - return holdersData } catch (err) { console.error('Error fetching token holders:', err) diff --git a/src/lib/graphql-client.ts b/src/lib/graphql-client.ts new file mode 100644 index 00000000..c2d0bad6 --- /dev/null +++ b/src/lib/graphql-client.ts @@ -0,0 +1,20 @@ +import { GraphQLClient } from 'graphql-request' +import config from '@/config' + +const client = new GraphQLClient(config.graphqlUrl) + +export async function makeGraphQLRequest( + document: string, + variables?: Record, +): Promise { + try { + return await client.request(document, variables) + } catch (error) { + console.error('GraphQL request error:', error) + throw error + } +} + +export function getGraphQLClient(): GraphQLClient { + return client +} diff --git a/src/pages/api/token-holder-count.ts b/src/pages/api/token-holder-count.ts index 2589f009..36f6748a 100644 --- a/src/pages/api/token-holder-count.ts +++ b/src/pages/api/token-holder-count.ts @@ -18,6 +18,7 @@ export default async function handler( `https://api.ethplorer.io/getTokenInfo/${address}?apiKey=${config.ethplorerApiKey}`, ) const holderCount = response.data?.holdersCount + res.setHeader('Cache-Control', 's-maxage=120') return res.status(200).json({ response: holderCount, diff --git a/src/services/holder.ts b/src/services/holder.ts new file mode 100644 index 00000000..71ab3ff4 --- /dev/null +++ b/src/services/holder.ts @@ -0,0 +1,14 @@ +import { makeGraphQLRequest } from '@/lib/graphql-client' +import { HIIQ_HOLDERS_COUNT } from './holders/queries' + +export async function getHIIQHoldersCount() { + try { + const response = await makeGraphQLRequest<{ + hiIQHoldersCount: { amount: number }[] + }>(HIIQ_HOLDERS_COUNT) + return response.hiIQHoldersCount[0].amount + } catch (error) { + console.error('Error fetching HIIQ holders count:', error) + return 0 + } +} diff --git a/src/utils/stats-data.ts b/src/utils/stats-data.ts index 5329d48e..58ac4cc0 100644 --- a/src/utils/stats-data.ts +++ b/src/utils/stats-data.ts @@ -103,13 +103,18 @@ const getTokenHolders = async () => { ) const ethData = await ethDataResponse.json() + const hiiqDataResponse = await fetch( + '/api/token-holder-count?address=0x1bF5457eCAa14Ff63CC89EFd560E251e814E16Ba', + ) + const hiiqData = await hiiqDataResponse.json() + console.log(hiiqData) return { holders: { eos: eosData, eth: ethData?.response ?? 0, matic: maticHolders, bsc: bscHolders, - hiiq: count ?? 0, + hiiq: hiiqData ?? 0, }, } } catch (err) { diff --git a/src/utils/use-stats-data.ts b/src/utils/use-stats-data.ts index 1b4dfd0e..4a4217ad 100644 --- a/src/utils/use-stats-data.ts +++ b/src/utils/use-stats-data.ts @@ -7,7 +7,7 @@ import { getSocialData, getVolume, } from '@/utils/stats-data' -import useTokenHolders from '@/hooks/useGetTokenHolders' +import { getTokenHolders } from '@/app/dashboard/_actions' const initialDataForTokenHolders = { holders: { eos: 0, eth: 0, matic: 0, bsc: 0, hiiq: 0 }, @@ -71,7 +71,6 @@ export function useStatsData() { const [data, setData] = useState({}) const [totals, setTotals] = useState({}) const isFetched = useRef(false) - const { getTokenHolders } = useTokenHolders() useEffect(() => { const fetchData = async () => {