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 6, 2024
1 parent 7cf6997 commit 74ee056
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
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
39 changes: 39 additions & 0 deletions packages/interface/src/utils/fetchTally.ts
Original file line number Diff line number Diff line change
@@ -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<Tally | undefined> {
return cachedFetch<{ tally: Tally }>(config.maciSubgraphUrl, {
method: "POST",
body: JSON.stringify({
query: tallyQuery.replace("id: $id", `id: "${id}"`),
}),
}).then((response: GraphQLResponse) => response.data?.tally);
}
19 changes: 19 additions & 0 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

0 comments on commit 74ee056

Please sign in to comment.