Skip to content

Commit

Permalink
feat: introduce feeling of subgraph<>chain id relation
Browse files Browse the repository at this point in the history
  • Loading branch information
petrovska-petro committed Aug 23, 2023
1 parent 030cdf7 commit 9249a30
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
13 changes: 13 additions & 0 deletions src/helpers/chainDetails.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// in future this `type` may contain more data pieces
interface IChainDetails {
[key: number]: {
subgraphGauges: string;
};
}

export const chainDetails: IChainDetails = {
10: {
subgraphGauges:
"https://api.studio.thegraph.com/proxy/50162/smartgarden-optimism-gauges/version/latest",
},
};
21 changes: 14 additions & 7 deletions src/queries/useGetUserGaugePositions.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import { useQuery } from "@tanstack/react-query";
import { useAccount } from "wagmi";
import { useAccount, useNetwork } from "wagmi";

import { request, gql } from "graphql-request";

const SUBGRAPH_URL =
"https://api.studio.thegraph.com/proxy/50162/smartgarden-optimism-gauges/version/latest";
import { chainDetails } from "../helpers/chainDetails";

export interface IGaugePositions {
id: string;
pool_name: string;
}

async function getUserGaugePositions(safeAddress: string | undefined) {
async function getUserGaugePositions(
safeAddress: string | undefined,
chainId: number | undefined,
) {
try {
if (!safeAddress) throw new Error("No Safe Account");
if (!chainId) throw new Error("Missing Chain ID");
if (!chainDetails[chainId].subgraphGauges)
throw new Error("No Gauge subgraph url available");
const querySafeAddr = gql`
query UserGaugePositions {
gaugePositions(
Expand All @@ -31,8 +36,9 @@ async function getUserGaugePositions(safeAddress: string | undefined) {
}
}
`;
const positions = (await request(SUBGRAPH_URL, querySafeAddr))
.gaugePositions;
const positions = (
await request(chainDetails[chainId].subgraphGauges, querySafeAddr)
).gaugePositions;
const gaugeResults: IGaugePositions[] = [];
positions.forEach((position: any) => {
gaugeResults.push({
Expand All @@ -49,7 +55,8 @@ async function getUserGaugePositions(safeAddress: string | undefined) {

export default function useGetUserGaugePositions() {
const { address } = useAccount();
const { chain } = useNetwork();
return useQuery(["userGaugePositions", address?.toLowerCase()], () =>
getUserGaugePositions(address?.toLowerCase()),
getUserGaugePositions(address?.toLowerCase(), chain?.id),
);
}

0 comments on commit 9249a30

Please sign in to comment.