Skip to content

Commit

Permalink
Merge pull request #288 from EveripediaNetwork/fix/broken-hiiq-count
Browse files Browse the repository at this point in the history
chore: created server action to fix hiiq count issue
  • Loading branch information
invisiblemask authored Dec 20, 2024
2 parents c8f7c9e + 76fed4d commit 2998245
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 5 deletions.
43 changes: 43 additions & 0 deletions src/app/dashboard/_actions.ts
Original file line number Diff line number Diff line change
@@ -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,
},
}
}
}
2 changes: 0 additions & 2 deletions src/hooks/useGetTokenHolders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ const useTokenHolders = () => {
},
}

console.log('Token Holders Data:', holdersData)

return holdersData
} catch (err) {
console.error('Error fetching token holders:', err)
Expand Down
20 changes: 20 additions & 0 deletions src/lib/graphql-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { GraphQLClient } from 'graphql-request'
import config from '@/config'

const client = new GraphQLClient(config.graphqlUrl)

export async function makeGraphQLRequest<T>(
document: string,
variables?: Record<string, any>,
): Promise<T> {
try {
return await client.request<T>(document, variables)
} catch (error) {
console.error('GraphQL request error:', error)
throw error
}
}

export function getGraphQLClient(): GraphQLClient {
return client
}
1 change: 1 addition & 0 deletions src/pages/api/token-holder-count.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions src/services/holder.ts
Original file line number Diff line number Diff line change
@@ -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
}
}
7 changes: 6 additions & 1 deletion src/utils/stats-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions src/utils/use-stats-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down Expand Up @@ -71,7 +71,6 @@ export function useStatsData() {
const [data, setData] = useState<Dict>({})
const [totals, setTotals] = useState<Dict>({})
const isFetched = useRef(false)
const { getTokenHolders } = useTokenHolders()

useEffect(() => {
const fetchData = async () => {
Expand Down

0 comments on commit 2998245

Please sign in to comment.