Skip to content

Commit

Permalink
fix: use the graph for augmented data (#317)
Browse files Browse the repository at this point in the history
* fix: use the graph for augmented data

* rework how we query graphs to be more specific

Signed-off-by: david <[email protected]>

* improve hadling v2 queries

* require vote subgraph, and remove kv dep

* fix compile issues, comparison types

---------

Signed-off-by: david <[email protected]>
  • Loading branch information
daywiss authored Jul 26, 2024
1 parent 9592a9e commit 2ff981a
Show file tree
Hide file tree
Showing 11 changed files with 724 additions and 68 deletions.
10 changes: 8 additions & 2 deletions components/Panel/VotePanel/Details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from "helpers";
import { config } from "helpers/config";
import { useOptimisticGovernorData } from "hooks/queries/votes/useOptimisticGovernorData";
import { useAssertionClaim } from "hooks";
import { useAssertionClaim, useAugmentedVoteData } from "hooks";

import AncillaryData from "public/assets/icons/ancillary-data.svg";
import Chat from "public/assets/icons/chat.svg";
Expand Down Expand Up @@ -43,7 +43,6 @@ export function Details({
discordLink,
decodedAdminTransactions,
umipOrUppLink,
augmentedData,
assertionChildChainId,
assertionAsserter,
assertionId,
Expand All @@ -69,6 +68,13 @@ export function Details({
setShowRawClaimData(!showRawClaimData);
}

const augmentedDataResponse = useAugmentedVoteData({
time,
identifier: decodedIdentifier,
ancillaryData: ancillaryData,
});
const augmentedData = augmentedDataResponse.data;

function makeOoRequestLink() {
if (!augmentedData?.ooRequestUrl) return;

Expand Down
3 changes: 0 additions & 3 deletions contexts/VotesContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
useAccountDetails,
useActiveVoteResults,
useActiveVotes,
useAugmentedVoteData,
useCommittedVotes,
useCommittedVotesByCaller,
useCommittedVotesForDelegator,
Expand Down Expand Up @@ -146,7 +145,6 @@ export function VotesProvider({ children }: { children: ReactNode }) {
userOrDelegatorAddress
);
const { data: decodedAdminTransactions } = useDecodedAdminTransactions();
const { data: augmentedData } = useAugmentedVoteData();
const { voteHistoryByKey } = votingAndStakingDetails || {};

// This function tells you if your current logged in account can reveal this vote, ie the commit was cast by your current account.
Expand Down Expand Up @@ -218,7 +216,6 @@ export function VotesProvider({ children }: { children: ReactNode }) {
? { price: pastVoteRevealed, salt: "" }
: decryptedVotes?.[uniqueKey],
contentfulData: contentfulData?.[uniqueKey],
augmentedData: augmentedData?.[uniqueKey],
voteHistory: voteHistoryByKey?.[uniqueKey] ?? {
uniqueKey,
voted: false,
Expand Down
41 changes: 23 additions & 18 deletions hooks/queries/votes/useAugmentedVoteData.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
import assert from "assert";
import { useQuery } from "@tanstack/react-query";
import { augmentedVoteDataKey } from "constant";
import { getAugmentedVoteData } from "web3";
import { useActiveVotes } from "./useActiveVotes";
import { usePastVotes } from "./usePastVotes";
import { useUpcomingVotes } from "./useUpcomingVotes";

export function useAugmentedVoteData() {
const { data: activeVotes } = useActiveVotes();
const { data: upcomingVotes } = useUpcomingVotes();
const { data: pastVotes } = usePastVotes();

const allVotes = { ...activeVotes, ...upcomingVotes, ...pastVotes };

const queryResult = useQuery({
export function useAugmentedVoteData(
params: Partial<{
ancillaryData: string;
time: number;
identifier: string;
}>
) {
return useQuery({
queryKey: [
augmentedVoteDataKey,
activeVotes,
upcomingVotes,
pastVotes,
allVotes,
params.identifier ?? "",
(params.time ?? 0).toString(),
params.ancillaryData ?? "",
],
queryFn: () => getAugmentedVoteData(allVotes),
queryFn: () => {
assert(params.identifier, "identifier missing");
assert(params.time, "time missing");
assert(params.ancillaryData, "ancillaryData missing");
return getAugmentedVoteData({
identifier: params.identifier,
time: params.time,
ancillaryData: params.ancillaryData,
});
},
enabled: !!params.identifier && !!params.time && !!params.identifier,
});

return queryResult;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
"style-loader": "^3.3.1",
"tailwindcss": "^3.3.2",
"ts-node": "^10.9.1",
"typescript": "4.8.3",
"typescript": "^5.5.3",
"url": "^0.11.0",
"webpack": "^5.74.0"
},
Expand Down
193 changes: 193 additions & 0 deletions pages/api/_common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import { getAbi, getAddress } from "@uma/contracts-node";
import { Contract, ethers } from "ethers";
import { NodeUrls, SupportedChainIds } from "types";
import { supportedChains } from "constant";
import * as ss from "superstruct";

export const VoteSubgraphURL: string = ss.create(
process.env.NEXT_PUBLIC_GRAPH_ENDPOINT,
ss.string()
);

type GetAddressParams = Parameters<typeof getAddress>;

Expand Down Expand Up @@ -65,6 +71,10 @@ export const fromBlocks: Record<string, Record<number | string, number>> = {
11155111: 5427310,
},
};
export const ChainId = ss.enums([
1, 5, 10, 100, 137, 288, 416, 8453, 11155111, 1116, 42161, 43114, 80001,
80002, 81457,
]);

export function getFromBlock(
oracleType: string,
Expand All @@ -73,3 +83,186 @@ export function getFromBlock(
): number {
return fromBlocks?.[oracleType]?.[chainId] || fallbackFromBlock;
}

const SubgraphConfig = ss.object({
source: ss.literal("gql"),
url: ss.string(),
type: ss.enums([
"Optimistic Oracle V1",
"Optimistic Oracle V2",
"Optimistic Oracle V3",
"Skinny Optimistic Oracle",
]),
chainId: ChainId,
});

export type SubgraphConfig = ss.Infer<typeof SubgraphConfig>;
const SubgraphConfigs = ss.array(SubgraphConfig);
export type SubgraphConfigs = ss.Infer<typeof SubgraphConfigs>;
const Env = ss.object({
SUBGRAPH_V1_1: ss.optional(ss.string()),
SUBGRAPH_V2_1: ss.optional(ss.string()),
SUBGRAPH_V3_1: ss.optional(ss.string()),
SUBGRAPH_SKINNY_1: ss.optional(ss.string()),

SUBGRAPH_V1_10: ss.optional(ss.string()),
SUBGRAPH_V2_10: ss.optional(ss.string()),
SUBGRAPH_V3_10: ss.optional(ss.string()),
SUBGRAPH_SKINNY_10: ss.optional(ss.string()),

SUBGRAPH_V1_137: ss.optional(ss.string()),
SUBGRAPH_V2_137: ss.optional(ss.string()),
SUBGRAPH_V3_137: ss.optional(ss.string()),
SUBGRAPH_SKINNY_137: ss.optional(ss.string()),

SUBGRAPH_V1_5: ss.optional(ss.string()),
SUBGRAPH_SKINNY_5: ss.optional(ss.string()),
SUBGRAPH_V3_5: ss.optional(ss.string()),
SUBGRAPH_V2_5: ss.optional(ss.string()),

SUBGRAPH_V1_1116: ss.optional(ss.string()),
SUBGRAPH_V2_1116: ss.optional(ss.string()),
SUBGRAPH_V3_1116: ss.optional(ss.string()),
SUBGRAPH_SKINNY_1116: ss.optional(ss.string()),

SUBGRAPH_V1_80002: ss.optional(ss.string()),
SUBGRAPH_V2_80002: ss.optional(ss.string()),
SUBGRAPH_V3_80002: ss.optional(ss.string()),
SUBGRAPH_SKINNY_80002: ss.optional(ss.string()),

SUBGRAPH_V1_81457: ss.optional(ss.string()),
SUBGRAPH_V2_81457: ss.optional(ss.string()),
SUBGRAPH_V3_81457: ss.optional(ss.string()),
SUBGRAPH_SKINNY_81457: ss.optional(ss.string()),

SUBGRAPH_V2_11155111: ss.optional(ss.string()),
SUBGRAPH_V3_11155111: ss.optional(ss.string()),
SUBGRAPH_SKINNY_11155111: ss.optional(ss.string()),
SUBGRAPH_V1_11155111: ss.optional(ss.string()),

SUBGRAPH_V1_288: ss.optional(ss.string()),
SUBGRAPH_V2_288: ss.optional(ss.string()),
SUBGRAPH_V3_288: ss.optional(ss.string()),
SUBGRAPH_SKINNY_288: ss.optional(ss.string()),

SUBGRAPH_V1_42161: ss.optional(ss.string()),
SUBGRAPH_V2_42161: ss.optional(ss.string()),
SUBGRAPH_V3_42161: ss.optional(ss.string()),
SUBGRAPH_SKINNY_42161: ss.optional(ss.string()),

SUBGRAPH_V2_80001: ss.optional(ss.string()),
SUBGRAPH_V1_80001: ss.optional(ss.string()),
SUBGRAPH_V3_80001: ss.optional(ss.string()),
SUBGRAPH_SKINNY_80001: ss.optional(ss.string()),

SUBGRAPH_V1_8453: ss.optional(ss.string()),
SUBGRAPH_V2_8453: ss.optional(ss.string()),
SUBGRAPH_V3_8453: ss.optional(ss.string()),
SUBGRAPH_SKINNY_8453: ss.optional(ss.string()),
});
export type Env = ss.Infer<typeof Env>;

// every prop of next envs needs to be explicitly pulled in
const env = ss.create(
{
SUBGRAPH_V1_1: process.env.SUBGRAPH_V1_1,
SUBGRAPH_V1_10: process.env.SUBGRAPH_V1_10,
SUBGRAPH_V1_137: process.env.SUBGRAPH_V1_137,
SUBGRAPH_V1_288: process.env.SUBGRAPH_V1_288,
SUBGRAPH_V1_1116: process.env.SUBGRAPH_V1_1116,
SUBGRAPH_V1_42161: process.env.SUBGRAPH_V1_42161,
SUBGRAPH_V1_5: process.env.SUBGRAPH_V1_5,
SUBGRAPH_V1_80001: process.env.SUBGRAPH_V1_80001,
SUBGRAPH_V1_80002: process.env.SUBGRAPH_V1_80002,
SUBGRAPH_V1_81457: process.env.SUBGRAPH_V1_81457,
SUBGRAPH_V1_11155111: process.env.SUBGRAPH_V1_11155111,

SUBGRAPH_V2_1: process.env.SUBGRAPH_V2_1,
SUBGRAPH_V2_10: process.env.SUBGRAPH_V2_10,
SUBGRAPH_V2_137: process.env.SUBGRAPH_V2_137,
SUBGRAPH_V2_288: process.env.SUBGRAPH_V2_288,
SUBGRAPH_V2_1116: process.env.SUBGRAPH_V2_1116,
SUBGRAPH_V2_42161: process.env.SUBGRAPH_V2_42161,
SUBGRAPH_V2_5: process.env.SUBGRAPH_V2_5,
SUBGRAPH_V2_80001: process.env.SUBGRAPH_V2_80001,
SUBGRAPH_V2_80002: process.env.SUBGRAPH_V2_80002,
SUBGRAPH_V2_81457: process.env.SUBGRAPH_V2_81457,
SUBGRAPH_V2_11155111: process.env.SUBGRAPH_V2_11155111,

SUBGRAPH_V3_1: process.env.SUBGRAPH_V3_1,
SUBGRAPH_V3_10: process.env.SUBGRAPH_V3_10,
SUBGRAPH_V3_137: process.env.SUBGRAPH_V3_137,
SUBGRAPH_V3_288: process.env.SUBGRAPH_V3_288,
SUBGRAPH_V3_1116: process.env.SUBGRAPH_V3_1116,
SUBGRAPH_V3_42161: process.env.SUBGRAPH_V3_42161,
SUBGRAPH_V3_5: process.env.SUBGRAPH_V3_5,
SUBGRAPH_V3_80001: process.env.SUBGRAPH_V3_80001,
SUBGRAPH_V3_80002: process.env.SUBGRAPH_V3_80002,
SUBGRAPH_V3_81457: process.env.SUBGRAPH_V3_81457,
SUBGRAPH_V3_11155111: process.env.SUBGRAPH_V3_11155111,

SUBGRAPH_SKINNY_1: process.env.SUBGRAPH_SKINNY_1,
SUBGRAPH_SKINNY_10: process.env.SUBGRAPH_SKINNY_10,
SUBGRAPH_SKINNY_137: process.env.SUBGRAPH_SKINNY_137,
SUBGRAPH_SKINNY_288: process.env.SUBGRAPH_SKINNY_288,
SUBGRAPH_SKINNY_1116: process.env.SUBGRAPH_SKINNY_1116,
SUBGRAPH_SKINNY_42161: process.env.SUBGRAPH_SKINNY_42161,
SUBGRAPH_SKINNY_5: process.env.SUBGRAPH_SKINNY_5,
SUBGRAPH_SKINNY_80001: process.env.SUBGRAPH_SKINNY_80001,
SUBGRAPH_SKINNY_80002: process.env.SUBGRAPH_SKINNY_80002,
SUBGRAPH_SKINNY_81457: process.env.SUBGRAPH_SKINNY_81457,
SUBGRAPH_SKINNY_11155111: process.env.SUBGRAPH_SKINNY_11155111,

SUBGRAPH_V1_8453: process.env.SUBGRAPH_V1_8453,
SUBGRAPH_V2_8453: process.env.SUBGRAPH_V2_8453,
SUBGRAPH_V3_8453: process.env.SUBGRAPH_V3_8453,
SUBGRAPH_SKINNY_8453: process.env.SUBGRAPH_SKINNY_8453,
},
Env
);

export function parseSubgraphEnv(env: Env): SubgraphConfigs {
const subgraphs: SubgraphConfigs = [];

for (const [key, value] of Object.entries(env)) {
if (!value) continue;
const [type, version, chainId] = key.split("_");
if (type === "SUBGRAPH") {
if (version === "SKINNY") {
const subgraph = {
source: "gql",
url: value,
type: "Skinny Optimistic Oracle",
chainId: parseInt(chainId),
};
if (ss.is(subgraph, SubgraphConfig)) {
subgraphs.push(subgraph);
}
} else {
const subgraph = {
source: "gql",
url: value,
type: `Optimistic Oracle ${version}`,
chainId: parseInt(chainId),
};
if (ss.is(subgraph, SubgraphConfig)) {
subgraphs.push(subgraph);
}
}
}
}
return subgraphs;
}

export const subgraphConfigs = parseSubgraphEnv(env);

export function getSubgraphConfig(
type: string,
chainId: number
): SubgraphConfig {
const found = subgraphConfigs.find(
(config) => config.chainId === chainId && config.type === type
);
if (found !== undefined) return found;
throw new Error(`No subgraph information found for ${type} on ${chainId}`);
}
Loading

0 comments on commit 2ff981a

Please sign in to comment.