Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tally-fetch): Added fetch from subgraph #437

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading