From e157e05506e19a26a570f24548f41646a031e613 Mon Sep 17 00:00:00 2001 From: Olaleye Blessing Date: Thu, 9 Oct 2025 23:41:26 +0100 Subject: [PATCH] Get votes from subgraph --- src/app/governance/useCurrentVotes.ts | 73 +++++++++++++++------------ 1 file changed, 42 insertions(+), 31 deletions(-) diff --git a/src/app/governance/useCurrentVotes.ts b/src/app/governance/useCurrentVotes.ts index d2d9e598..8f2f0d3d 100644 --- a/src/app/governance/useCurrentVotes.ts +++ b/src/app/governance/useCurrentVotes.ts @@ -1,38 +1,49 @@ -import { getPublicClient } from "@wagmi/core"; -import { getConfig } from "@/app/core/hooks/WagmiProvider/config/getConfig"; -import { useActiveChain } from "@/app/core/hooks/useActiveChain"; import { useQuery } from "@tanstack/react-query"; -import { Hex } from "viem"; -import { DISTRIBUTOR_ABI } from "@/abi"; - -type VoteLogData = { - blockTimestamp: Hex; - args: { - account: Hex; - points: Array; - projects: Array; - }; -}; +import { getAddress, Hex } from "viem"; +import { GraphQLClient } from "graphql-request"; +import { SUBGRAPH_QUERY_URL } from "@/constants"; + +interface BreadHolderVoted { + id: string; + account: Hex; + points: Array; + projects: Array; + timestamp: string; + blockNumber: string; +} + +interface QueryResponse { + breadHolderVoteds: BreadHolderVoted[]; +} export function useCurrentVotes(lastClaimedBlockNumber: bigint | null) { - const chainConfig = useActiveChain(); - const distributorAddress = chainConfig.DISBURSER.address; - const publicClient = getPublicClient(getConfig().config); + const client = new GraphQLClient(SUBGRAPH_QUERY_URL, { + headers: { + Authorization: `Bearer ${process.env.NEXT_PUBLIC_SUBGRAPH_API_KEY}`, + }, + }); return useQuery({ queryKey: ["getVotesForCurrentRound"], refetchInterval: 500, enabled: !!lastClaimedBlockNumber, queryFn: async () => { - // TODO: Look into this and see if and how chain Id can be passed explicitly - const logs = await publicClient.getContractEvents({ - address: distributorAddress, - abi: DISTRIBUTOR_ABI, - eventName: "BreadHolderVoted", - fromBlock: lastClaimedBlockNumber || BigInt(0), - toBlock: "latest", - }); - const parsed = (logs as unknown as Array).map(parseVoteLog); + const data = await client.request(` + query { + breadHolderVoteds( + where: { blockNumber_gte: "${lastClaimedBlockNumber}" } + ) { + id + account + points + projects + timestamp + blockNumber + } + } + `); + + const parsed = data.breadHolderVoteds.map(parseVoteFromSubgraph); return parsed; }, }); @@ -45,12 +56,12 @@ export type ParsedVote = { projects: Hex[]; }; -function parseVoteLog(log: VoteLogData): ParsedVote { +function parseVoteFromSubgraph(vote: BreadHolderVoted): ParsedVote { return { - account: log.args.account, - blockTimestamp: 1000, - points: log.args.points.map((bigPoints) => Number(bigPoints)), - projects: log.args.projects, + account: getAddress(vote.account), + blockTimestamp: Number(vote.timestamp) * 1000, + points: vote.points.map(Number), + projects: vote.projects.map(p => getAddress(p)), }; }