Skip to content

Commit

Permalink
feat(tally-fetch): Added fetch from subgraph
Browse files Browse the repository at this point in the history
  • Loading branch information
Crisgarner committed Nov 5, 2024
1 parent 7cf6997 commit 6dd0c93
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 16 deletions.
26 changes: 15 additions & 11 deletions packages/interface/src/pages/rounds/[pollId]/stats/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { useRound } from "~/contexts/Round";
import { useProjectCount } from "~/features/projects/hooks/useProjects";
import { useProjectsResults, useResults } from "~/hooks/useResults";

Check failure on line 13 in packages/interface/src/pages/rounds/[pollId]/stats/index.tsx

View workflow job for this annotation

GitHub Actions / check (lint:ts)

'useResults' is defined but never used

Check failure on line 13 in packages/interface/src/pages/rounds/[pollId]/stats/index.tsx

View workflow job for this annotation

GitHub Actions / check (lint:ts)

'useResults' is defined but never used
import { Layout } from "~/layouts/DefaultLayout";
import { api } from "~/utils/api";
import { formatNumber } from "~/utils/formatNumber";
import { useRoundState } from "~/utils/state";
import { ERoundState } from "~/utils/types";
Expand All @@ -35,31 +36,34 @@ interface IStatsProps {
}

const Stats = ({ pollId }: IStatsProps) => {
const tally = api.maci.tally.useQuery({ id: pollId });

const { isLoading } = useMaci();
const { chain, isConnected } = useAccount();
const { getRoundByPollId } = useRound();

const round = useMemo(() => getRoundByPollId(pollId), [pollId, getRoundByPollId]);
const results = useResults(pollId, (round?.registryAddress ?? zeroAddress) as Hex, round?.tallyFile);
const results = tally.data ?? {};

Check failure on line 46 in packages/interface/src/pages/rounds/[pollId]/stats/index.tsx

View workflow job for this annotation

GitHub Actions / check (lint:ts)

'results' is assigned a value but never used
const count = useProjectCount({
chain: chain!,
registryAddress: (round?.registryAddress ?? zeroAddress) as Hex,
});

const { data: projectsResults } = useProjectsResults((round?.registryAddress ?? zeroAddress) as Hex);

Check failure on line 52 in packages/interface/src/pages/rounds/[pollId]/stats/index.tsx

View workflow job for this annotation

GitHub Actions / check (lint:ts)

'projectsResults' is assigned a value but never used

const { averageVotes, projects = {} } = results.data ?? {};
// TODO: this needs to be updated to use the new results API

Check failure on line 54 in packages/interface/src/pages/rounds/[pollId]/stats/index.tsx

View workflow job for this annotation

GitHub Actions / check (lint:ts)

Insert `··`
// const { averageVotes, projects = {} } = results.data ?? {};

Check failure on line 55 in packages/interface/src/pages/rounds/[pollId]/stats/index.tsx

View workflow job for this annotation

GitHub Actions / check (lint:ts)

Insert `··`

const chartData = useMemo(() => {
const data = (projectsResults?.pages[0] ?? [])
.map((project) => ({
x: project.index,
y: projects[project.id]?.votes,
}))
.slice(0, 15);
// const chartData = useMemo(() => {

Check failure on line 57 in packages/interface/src/pages/rounds/[pollId]/stats/index.tsx

View workflow job for this annotation

GitHub Actions / check (lint:ts)

Insert `··`
// const data = (projectsResults?.pages[0] ?? [])

Check failure on line 58 in packages/interface/src/pages/rounds/[pollId]/stats/index.tsx

View workflow job for this annotation

GitHub Actions / check (lint:ts)

Insert `··`
// .map((project) => ({
// x: project.index,
// y: projects[project.id]?.votes,
// }))
// .slice(0, 15);

return [{ id: "awarded", data }];
}, [projects, projectsResults]);
// return [{ id: "awarded", data }];
// }, [projects, projectsResults]);

if (isLoading) {
return <div>Loading...</div>;
Expand Down
3 changes: 2 additions & 1 deletion packages/interface/src/server/api/routers/maci.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { z, ZodType } from "zod";
import { createTRPCRouter, publicProcedure } from "~/server/api/trpc";
import { fetchMetadata } from "~/utils/fetchMetadata";
import { fetchPolls } from "~/utils/fetchPoll";
import { fetchTally } from "~/utils/fetchTally";
import { fetchUser } from "~/utils/fetchUser";

import type { IPollData, IRoundMetadata, IRoundData } from "~/utils/types";
Expand All @@ -26,6 +27,7 @@ export const maciRouter = createTRPCRouter({
.input(z.object({ publicKey: z.string() }))
.query(async ({ input }) => fetchUser(PubKey.deserialize(input.publicKey).rawPubKey)),
poll: publicProcedure.query(async () => fetchPolls()),
tally: publicProcedure.input(z.object({ id: z.string() })).query(async ({ input }) => fetchTally(input.id)),
round: publicProcedure.input(z.object({ polls: z.array(PollSchema) })).query(async ({ input }) =>
Promise.all(
input.polls.map((poll) =>
Expand All @@ -52,7 +54,6 @@ export const maciRouter = createTRPCRouter({
registrationEndsAt: new Date(data.registrationEndsAt),
votingStartsAt,
votingEndsAt,
tallyFile: data.tallyFile,
} as IRoundData;
}),
),
Expand Down
55 changes: 55 additions & 0 deletions packages/interface/src/utils/fetchTally.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { config } from "~/config";

import type { Tally } from "./types";

import { createCachedFetch } from "./fetch";

const cachedFetch = createCachedFetch({ ttl: 1000 * 60 * 10 });

export interface GraphQLResponse {
data?: {
tally: Tally;
};
}

const tallyQuery = `
query Tally {
tally(id: $id) {
id
results{
id
result
}
}
}
`;

/**
* Maps the tally data to the ITallyData interface
*
* @param tally - The tally data
* @returns The mapped tally data
*/
function mappedTallyData(tally: Tally): Tally {
return {
id: tally.id,
results: tally.results,
};
}

/**
* Fetches the tally data from the subgraph
*
* @returns The tally data
*/
export async function fetchTally(id: string): Promise<Tally | undefined> {
const tally = await cachedFetch<{ tally: Tally }>(config.maciSubgraphUrl, {
method: "POST",
body: JSON.stringify({
query: tallyQuery.replace("id: $id", `id: "${id}"`),
}),
}).then((response: GraphQLResponse) => response.data?.tally);

// cast this to a TallyData array so that we can deal with one object only in MACIContext
return tally ? mappedTallyData(tally) : undefined;
}
23 changes: 19 additions & 4 deletions packages/interface/src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ export interface Metadata {
type: string;
}

export interface TallyResult {
id: string;
result: string;
}

export const AttestationsQuery = `
query Attestations($where: AttestationWhereInput, $orderBy: [AttestationOrderByWithRelationInput!], $take: Int, $skip: Int) {
attestations(take: $take, skip: $skip, orderBy: $orderBy, where: $where) {
Expand Down Expand Up @@ -146,6 +151,20 @@ export interface Poll {
};
}

/**
* The tally data
*/
export interface Tally {
/**
* The tally address
*/
id: string;
/**
* an array with the results
*/
results: TallyResult[];
}

/**
* The request type
*/
Expand Down Expand Up @@ -386,8 +405,4 @@ export interface IRoundData {
* The round voting ends at
*/
votingEndsAt: Date;
/**
* The round tally file
*/
tallyFile: string;
}

0 comments on commit 6dd0c93

Please sign in to comment.