diff --git a/packages/interface/src/server/api/routers/maci.ts b/packages/interface/src/server/api/routers/maci.ts index 0ad59e0d..b692c833 100644 --- a/packages/interface/src/server/api/routers/maci.ts +++ b/packages/interface/src/server/api/routers/maci.ts @@ -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"; @@ -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) => @@ -52,7 +54,6 @@ export const maciRouter = createTRPCRouter({ registrationEndsAt: new Date(data.registrationEndsAt), votingStartsAt, votingEndsAt, - tallyFile: data.tallyFile, } as IRoundData; }), ), diff --git a/packages/interface/src/utils/fetchTally.ts b/packages/interface/src/utils/fetchTally.ts new file mode 100644 index 00000000..66b1225f --- /dev/null +++ b/packages/interface/src/utils/fetchTally.ts @@ -0,0 +1,39 @@ +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 + } + } + } +`; + +/** + * Fetches the tally data from the subgraph + * + * @returns The tally data + */ +export async function fetchTally(id: string): Promise { + return cachedFetch<{ tally: Tally }>(config.maciSubgraphUrl, { + method: "POST", + body: JSON.stringify({ + query: tallyQuery.replace("id: $id", `id: "${id}"`), + }), + }).then((response: GraphQLResponse) => response.data?.tally); +} diff --git a/packages/interface/src/utils/types.ts b/packages/interface/src/utils/types.ts index 55ebbfdb..0791d1d4 100644 --- a/packages/interface/src/utils/types.ts +++ b/packages/interface/src/utils/types.ts @@ -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) { @@ -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 */