-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tally-fetch): Added fetch from subgraph
- Loading branch information
Crisgarner
committed
Nov 5, 2024
1 parent
7cf6997
commit 6dd0c93
Showing
4 changed files
with
91 additions
and
16 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,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; | ||
} |
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