-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #180 from privacy-scaling-explorations/feat/fetch-…
…poll-subgraph feat: fetch poll data from subgraph
- Loading branch information
Showing
3 changed files
with
101 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { IGetPollData } from "maci-cli/sdk"; | ||
|
||
import { config } from "~/config"; | ||
|
||
import { createCachedFetch } from "./fetch"; | ||
|
||
const cachedFetch = createCachedFetch({ ttl: 1000 * 60 * 10 }); | ||
|
||
interface Poll { | ||
pollId: string; | ||
createdAt: string; | ||
duration: string; | ||
stateRoot: string; | ||
messageRoot: string; | ||
numSignups: string; | ||
id: string; | ||
} | ||
|
||
export interface GraphQLResponse { | ||
data?: { | ||
polls: Poll[]; | ||
}; | ||
} | ||
|
||
const PollQuery = ` | ||
query Poll { | ||
polls(orderBy: createdAt, orderDirection: desc, first: 1) { | ||
pollId | ||
duration | ||
createdAt | ||
stateRoot | ||
messageRoot | ||
numSignups | ||
id | ||
} | ||
} | ||
`; | ||
|
||
export async function fetchPoll(): Promise<IGetPollData> { | ||
const poll = ( | ||
await cachedFetch<{ polls: Poll[] }>(config.maciSubgraphUrl, { | ||
method: "POST", | ||
body: JSON.stringify({ | ||
query: PollQuery, | ||
}), | ||
}).then((response: GraphQLResponse) => response.data?.polls) | ||
)?.at(0); | ||
|
||
// cast this to a IGetPollData object so that we can deal with one object only in MACIContext | ||
return { | ||
isStateAqMerged: !!poll?.messageRoot, | ||
id: poll?.pollId ?? 0, | ||
duration: poll?.duration ?? 0, | ||
deployTime: poll?.createdAt ?? 0, | ||
numSignups: poll?.numSignups ?? 0, | ||
address: poll?.id ?? "", | ||
}; | ||
} |