From e9e18c4da2528435fca9624097929bf0d9b6ccbd Mon Sep 17 00:00:00 2001 From: evandrosaturnino Date: Tue, 2 Jul 2024 05:24:39 -0300 Subject: [PATCH 1/2] update subgraph service querying implementation --- .github/workflows/ci.yml | 1 + src/config/subgraph.ts | 8 ++++++-- src/hooks/tBTC/useTotalMints.ts | 7 +++---- src/hooks/useQuery.ts | 8 +++----- src/templates/earn-page/btc/index.tsx | 4 ++-- .../staker/NetworkDistribution/index.tsx | 20 +++++++++---------- .../ThresholdDaoDataSection.tsx | 4 ++-- src/templates/home-page/Hero/index.tsx | 20 +++++++++---------- 8 files changed, 37 insertions(+), 35 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4c709a28..6b769bf1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -46,6 +46,7 @@ jobs: POSTHOG_HOSTNAME_HTTP: ${{ secrets.MAINNET_POSTHOG_HOSTNAME_HTTP }} GATSBY_GTM_SUPPORT: true GATSBY_GTM_ID: ${{ secrets.GTM_ID }} + SUBGRAPH_API_KEY: ${{ secrets.SUBGRAPH_API_KEY }} - uses: actions/upload-artifact@v3 with: diff --git a/src/config/subgraph.ts b/src/config/subgraph.ts index cde5cbef..75bef53e 100644 --- a/src/config/subgraph.ts +++ b/src/config/subgraph.ts @@ -1,5 +1,9 @@ export const T_NETWORK_SUBGRAPH_URL = "https://api.studio.thegraph.com/query/24143/threshold/0.0.4" -export const TBTC_SUBGRAPH_URL = - "https://api.thegraph.com/subgraphs/name/suntzu93/threshold-tbtc" +export const T_NETWORK_SUBGRAPH_ID = + "5TJAMbsRwm1avUTV4CofaLT4apfQoAiNcysEit9BWr6R" + +export const TBTC_SUBGRAPH_ID = "DETCX5Xm6tJfctRcZAxhQB9q3aK8P4BXLbujHmzEBXYV" + +export const SUBGRAPH_GATEWAY_URL = `https://gateway-arbitrum.network.thegraph.com/api/${process.env.SUBGRAPH_API_KEY}/subgraphs/id/` diff --git a/src/hooks/tBTC/useTotalMints.ts b/src/hooks/tBTC/useTotalMints.ts index 9fca7035..434b5cf4 100644 --- a/src/hooks/tBTC/useTotalMints.ts +++ b/src/hooks/tBTC/useTotalMints.ts @@ -1,5 +1,5 @@ import { gql, request } from "graphql-request" -import { TBTC_SUBGRAPH_URL } from "../../config/subgraph" +import { TBTC_SUBGRAPH_ID, SUBGRAPH_GATEWAY_URL } from "../../config/subgraph" import { useEffect, useState } from "react" export const useTotalMints = () => { @@ -22,11 +22,10 @@ export const useTotalMints = () => { ) { id } - } - ` + }` const result: { transactions: { id: string }[] } = await request( - TBTC_SUBGRAPH_URL, + SUBGRAPH_GATEWAY_URL + TBTC_SUBGRAPH_ID, query ) diff --git a/src/hooks/useQuery.ts b/src/hooks/useQuery.ts index 78a809b7..f9e92ce6 100644 --- a/src/hooks/useQuery.ts +++ b/src/hooks/useQuery.ts @@ -1,3 +1,4 @@ +import { SUBGRAPH_GATEWAY_URL } from "../config/subgraph" import { useEffect, useReducer, useRef, Reducer } from "react" import { request } from "graphql-request" @@ -34,10 +35,7 @@ const fetchReducer = (state: State, action: Action): State => { } } -function useQuery( - graphEndpoint: string, - query?: string -): State { +function useQuery(subgraphId: string, query?: string): State { const shouldUpdateState = useRef(true) const initialState: State = { error: undefined, @@ -59,7 +57,7 @@ function useQuery( dispatch({ type: Actions.Start }) try { - const response = await request(graphEndpoint, query) + const response = await request(SUBGRAPH_GATEWAY_URL + subgraphId, query) if (!shouldUpdateState.current) return dispatch({ type: Actions.Success, payload: response }) diff --git a/src/templates/earn-page/btc/index.tsx b/src/templates/earn-page/btc/index.tsx index 14b88c9b..718dda96 100644 --- a/src/templates/earn-page/btc/index.tsx +++ b/src/templates/earn-page/btc/index.tsx @@ -7,7 +7,7 @@ import { BodyLg, H4, H5 } from "../../../components" import { LPCardGroup } from "../../../components/LPCard" import { TBTCStats } from "./TBTCStats" import { gql } from "graphql-request" -import { TBTC_SUBGRAPH_URL } from "../../../config/subgraph" +import { TBTC_SUBGRAPH_ID } from "../../../config/subgraph" import useQuery from "../../../hooks/useQuery" import { LatestMint, LatestMints } from "./LatestMints" import ExternalButtonLink from "../../../components/Buttons/ExternalButtonLink" @@ -32,7 +32,7 @@ const BTCPageTemplate: FC = ({ data }) => { tbtctoken: { currentTokenHolders: string; totalSupply: string } transactions: LatestMint[] }>( - TBTC_SUBGRAPH_URL, + TBTC_SUBGRAPH_ID, gql` query { tbtctoken(id: "TBTCToken") { diff --git a/src/templates/earn-page/staker/NetworkDistribution/index.tsx b/src/templates/earn-page/staker/NetworkDistribution/index.tsx index 11285a3e..27369f49 100644 --- a/src/templates/earn-page/staker/NetworkDistribution/index.tsx +++ b/src/templates/earn-page/staker/NetworkDistribution/index.tsx @@ -4,7 +4,7 @@ import Card from "../../../../components/Card" import TStakedChart from "./TStakedChart" import { BodyLg, H2, H3, H5, LabelMd } from "../../../../components" import useQuery from "../../../../hooks/useQuery" -import { T_NETWORK_SUBGRAPH_URL } from "../../../../config/subgraph" +import { T_NETWORK_SUBGRAPH_ID } from "../../../../config/subgraph" import { exchangeAPI, formatFiatCurrencyAmount, @@ -20,14 +20,14 @@ import { function NetworkDistribution() { const { isFetching, data, error } = useQuery<{ - epoches: { totalStaked: string }[] + daometric: { stakedTotal: string } minStakeAmounts: { amount: string }[] }>( - T_NETWORK_SUBGRAPH_URL, + T_NETWORK_SUBGRAPH_ID, gql` query { - epoches(orderBy: startTime, orderDirection: desc, first: 1) { - totalStaked + daometric(id: "dao-metrics") { + stakedTotal } minStakeAmounts(first: 1, orderBy: updatedAt, orderDirection: desc) { amount @@ -35,18 +35,18 @@ function NetworkDistribution() { } ` ) - const { epoches, minStakeAmounts } = data || { - epoches: [{ totalStaked: "0" }], + const { daometric, minStakeAmounts } = data || { + daometric: { stakedTotal: "0" }, minStakeAmounts: [{ amount: "0" }], } - const totalStaked = !error ? epoches[0].totalStaked : "0" - const forrmattedTotalStaked = formatTokenAmount(totalStaked) + const stakedTotal = !error ? daometric.stakedTotal : "0" + const forrmattedTotalStaked = formatTokenAmount(stakedTotal) const minStakeAmount = formatTokenAmount( !error ? minStakeAmounts[0].amount : "0" ) const tPrice = useTTokenPrice() const totalValueStakedInUSD = formatFiatCurrencyAmount( - exchangeAPI.toUsdBalance(formatUnits(totalStaked), tPrice).toString() + exchangeAPI.toUsdBalance(formatUnits(stakedTotal), tPrice).toString() ) return ( diff --git a/src/templates/governance-page/ThresholdDaoDataSection.tsx b/src/templates/governance-page/ThresholdDaoDataSection.tsx index 4666f69b..e9c64815 100644 --- a/src/templates/governance-page/ThresholdDaoDataSection.tsx +++ b/src/templates/governance-page/ThresholdDaoDataSection.tsx @@ -8,7 +8,7 @@ import { PageSection } from "../../components/PageSection" import { H2, H3, LabelMd } from "../../components" import { ExternalLinkHref } from "../../components/Navbar/types" import { StatBoxGroup } from "../../components/StatBox" -import { T_NETWORK_SUBGRAPH_URL } from "../../config/subgraph" +import { T_NETWORK_SUBGRAPH_ID } from "../../config/subgraph" import useQuery from "../../hooks/useQuery" import { formatFiatCurrencyAmount, formatTokenAmount } from "../../utils" import { useBalanceOfDAOTreasury } from "../../hooks/useBalanceOfDAOTreasury" @@ -21,7 +21,7 @@ const ThresholdDaoDataSection = () => { tokenholderDelegations: { id: string }[] stakeDelegations: { id: string }[] }>( - T_NETWORK_SUBGRAPH_URL, + T_NETWORK_SUBGRAPH_ID, gql` query { daometric(id: "dao-metrics") { diff --git a/src/templates/home-page/Hero/index.tsx b/src/templates/home-page/Hero/index.tsx index 7c80715f..4896a216 100644 --- a/src/templates/home-page/Hero/index.tsx +++ b/src/templates/home-page/Hero/index.tsx @@ -8,8 +8,8 @@ import { TrackComponent } from "../../../components/Posthog/TrackComponent" import { Analytics } from "./Analytics" import { gql } from "graphql-request" import { - TBTC_SUBGRAPH_URL, - T_NETWORK_SUBGRAPH_URL, + TBTC_SUBGRAPH_ID, + T_NETWORK_SUBGRAPH_ID, } from "../../../config/subgraph" import useQuery from "../../../hooks/useQuery" import { exchangeAPI, formatUnits } from "../../../utils" @@ -33,14 +33,14 @@ const Hero: FC<{ data: totalStakedData, error: totalStakedError, } = useQuery<{ - epoches: { totalStaked: string }[] + daometric: { stakedTotal: string } minStakeAmounts: { amount: string }[] }>( - T_NETWORK_SUBGRAPH_URL, + T_NETWORK_SUBGRAPH_ID, gql` query { - epoches(orderBy: startTime, orderDirection: desc, first: 1) { - totalStaked + daometric(id: "dao-metrics") { + stakedTotal } } ` @@ -49,7 +49,7 @@ const Hero: FC<{ const { isFetching, data, error } = useQuery<{ tbtctoken: { currentTokenHolders: string; totalSupply: string } }>( - TBTC_SUBGRAPH_URL, + TBTC_SUBGRAPH_ID, gql` query { tbtctoken(id: "TBTCToken") { @@ -60,10 +60,10 @@ const Hero: FC<{ ` ) - const { epoches } = totalStakedData || { - epoches: [{ totalStaked: "0" }], + const { daometric } = totalStakedData || { + daometric: { stakedTotal: "0" }, } - const totalStaked = !error ? epoches[0].totalStaked : "0" + const totalStaked = !error ? daometric.stakedTotal : "0" const tPrice = useTTokenPrice() const totalValueStakedInUSD = exchangeAPI .toUsdBalance(formatUnits(totalStaked), tPrice) From abdaee93e588216abb14cf0860df7cd57c570c65 Mon Sep 17 00:00:00 2001 From: michalsmiarowski Date: Fri, 5 Jul 2024 14:35:31 +0200 Subject: [PATCH 2/2] Fix staking tvl calculation + revert some changes Only tbtc graph was not working so we don't need to replace the graph for t network. Replacing it the staking tvl section to show a wrong value. --- src/config/subgraph.ts | 4 +-- src/hooks/tBTC/useTotalMints.ts | 4 +-- src/hooks/useQuery.ts | 8 ++++-- src/templates/earn-page/btc/index.tsx | 5 ++-- .../staker/NetworkDistribution/index.tsx | 28 ++++++++----------- .../ThresholdDaoDataSection.tsx | 4 +-- src/templates/home-page/Hero/index.tsx | 20 ++++++------- 7 files changed, 34 insertions(+), 39 deletions(-) diff --git a/src/config/subgraph.ts b/src/config/subgraph.ts index 75bef53e..cca1849b 100644 --- a/src/config/subgraph.ts +++ b/src/config/subgraph.ts @@ -4,6 +4,4 @@ export const T_NETWORK_SUBGRAPH_URL = export const T_NETWORK_SUBGRAPH_ID = "5TJAMbsRwm1avUTV4CofaLT4apfQoAiNcysEit9BWr6R" -export const TBTC_SUBGRAPH_ID = "DETCX5Xm6tJfctRcZAxhQB9q3aK8P4BXLbujHmzEBXYV" - -export const SUBGRAPH_GATEWAY_URL = `https://gateway-arbitrum.network.thegraph.com/api/${process.env.SUBGRAPH_API_KEY}/subgraphs/id/` +export const TBTC_SUBGRAPH_URL = `https://gateway-arbitrum.network.thegraph.com/api/${process.env.SUBGRAPH_API_KEY}/subgraphs/id/DETCX5Xm6tJfctRcZAxhQB9q3aK8P4BXLbujHmzEBXYV` diff --git a/src/hooks/tBTC/useTotalMints.ts b/src/hooks/tBTC/useTotalMints.ts index 434b5cf4..094fc103 100644 --- a/src/hooks/tBTC/useTotalMints.ts +++ b/src/hooks/tBTC/useTotalMints.ts @@ -1,6 +1,6 @@ import { gql, request } from "graphql-request" -import { TBTC_SUBGRAPH_ID, SUBGRAPH_GATEWAY_URL } from "../../config/subgraph" import { useEffect, useState } from "react" +import { TBTC_SUBGRAPH_URL } from "../../config/subgraph" export const useTotalMints = () => { const [totalMints, setTotalMints] = useState(undefined) @@ -25,7 +25,7 @@ export const useTotalMints = () => { }` const result: { transactions: { id: string }[] } = await request( - SUBGRAPH_GATEWAY_URL + TBTC_SUBGRAPH_ID, + TBTC_SUBGRAPH_URL, query ) diff --git a/src/hooks/useQuery.ts b/src/hooks/useQuery.ts index f9e92ce6..78a809b7 100644 --- a/src/hooks/useQuery.ts +++ b/src/hooks/useQuery.ts @@ -1,4 +1,3 @@ -import { SUBGRAPH_GATEWAY_URL } from "../config/subgraph" import { useEffect, useReducer, useRef, Reducer } from "react" import { request } from "graphql-request" @@ -35,7 +34,10 @@ const fetchReducer = (state: State, action: Action): State => { } } -function useQuery(subgraphId: string, query?: string): State { +function useQuery( + graphEndpoint: string, + query?: string +): State { const shouldUpdateState = useRef(true) const initialState: State = { error: undefined, @@ -57,7 +59,7 @@ function useQuery(subgraphId: string, query?: string): State { dispatch({ type: Actions.Start }) try { - const response = await request(SUBGRAPH_GATEWAY_URL + subgraphId, query) + const response = await request(graphEndpoint, query) if (!shouldUpdateState.current) return dispatch({ type: Actions.Success, payload: response }) diff --git a/src/templates/earn-page/btc/index.tsx b/src/templates/earn-page/btc/index.tsx index 718dda96..8bbd5bc7 100644 --- a/src/templates/earn-page/btc/index.tsx +++ b/src/templates/earn-page/btc/index.tsx @@ -4,10 +4,9 @@ import { Box, Stack, VStack } from "@chakra-ui/react" import RolePageTemplate from "../RolePageTemplate" import SectionTemplate from "../../home-page/SectionTemplate" import { BodyLg, H4, H5 } from "../../../components" -import { LPCardGroup } from "../../../components/LPCard" import { TBTCStats } from "./TBTCStats" import { gql } from "graphql-request" -import { TBTC_SUBGRAPH_ID } from "../../../config/subgraph" +import { TBTC_SUBGRAPH_URL } from "../../../config/subgraph" import useQuery from "../../../hooks/useQuery" import { LatestMint, LatestMints } from "./LatestMints" import ExternalButtonLink from "../../../components/Buttons/ExternalButtonLink" @@ -32,7 +31,7 @@ const BTCPageTemplate: FC = ({ data }) => { tbtctoken: { currentTokenHolders: string; totalSupply: string } transactions: LatestMint[] }>( - TBTC_SUBGRAPH_ID, + TBTC_SUBGRAPH_URL, gql` query { tbtctoken(id: "TBTCToken") { diff --git a/src/templates/earn-page/staker/NetworkDistribution/index.tsx b/src/templates/earn-page/staker/NetworkDistribution/index.tsx index 27369f49..8dba53a0 100644 --- a/src/templates/earn-page/staker/NetworkDistribution/index.tsx +++ b/src/templates/earn-page/staker/NetworkDistribution/index.tsx @@ -1,33 +1,29 @@ -import { Box, HStack, Icon, SimpleGrid, Stack } from "@chakra-ui/react" +import { Box, HStack, Icon, Stack } from "@chakra-ui/react" import { gql } from "graphql-request" import Card from "../../../../components/Card" import TStakedChart from "./TStakedChart" import { BodyLg, H2, H3, H5, LabelMd } from "../../../../components" import useQuery from "../../../../hooks/useQuery" -import { T_NETWORK_SUBGRAPH_ID } from "../../../../config/subgraph" +import { T_NETWORK_SUBGRAPH_URL } from "../../../../config/subgraph" import { exchangeAPI, formatFiatCurrencyAmount, formatTokenAmount, formatUnits, } from "../../../../utils" -import StatBox from "../../../../components/StatBox" import { useTTokenPrice } from "../../../../contexts/TokenPriceContext" -import { - BsFillInfoCircleFill, - IoInformationCircleOutline, -} from "react-icons/all" +import { BsFillInfoCircleFill } from "react-icons/all" function NetworkDistribution() { const { isFetching, data, error } = useQuery<{ - daometric: { stakedTotal: string } + epoches: { totalStaked: string }[] minStakeAmounts: { amount: string }[] }>( - T_NETWORK_SUBGRAPH_ID, + T_NETWORK_SUBGRAPH_URL, gql` query { - daometric(id: "dao-metrics") { - stakedTotal + epoches(orderBy: startTime, orderDirection: desc, first: 1) { + totalStaked } minStakeAmounts(first: 1, orderBy: updatedAt, orderDirection: desc) { amount @@ -35,18 +31,18 @@ function NetworkDistribution() { } ` ) - const { daometric, minStakeAmounts } = data || { - daometric: { stakedTotal: "0" }, + const { epoches, minStakeAmounts } = data || { + epoches: [{ totalStaked: "0" }], minStakeAmounts: [{ amount: "0" }], } - const stakedTotal = !error ? daometric.stakedTotal : "0" - const forrmattedTotalStaked = formatTokenAmount(stakedTotal) + const totalStaked = !error ? epoches[0].totalStaked : "0" + const forrmattedTotalStaked = formatTokenAmount(totalStaked) const minStakeAmount = formatTokenAmount( !error ? minStakeAmounts[0].amount : "0" ) const tPrice = useTTokenPrice() const totalValueStakedInUSD = formatFiatCurrencyAmount( - exchangeAPI.toUsdBalance(formatUnits(stakedTotal), tPrice).toString() + exchangeAPI.toUsdBalance(formatUnits(totalStaked), tPrice).toString() ) return ( diff --git a/src/templates/governance-page/ThresholdDaoDataSection.tsx b/src/templates/governance-page/ThresholdDaoDataSection.tsx index e9c64815..4666f69b 100644 --- a/src/templates/governance-page/ThresholdDaoDataSection.tsx +++ b/src/templates/governance-page/ThresholdDaoDataSection.tsx @@ -8,7 +8,7 @@ import { PageSection } from "../../components/PageSection" import { H2, H3, LabelMd } from "../../components" import { ExternalLinkHref } from "../../components/Navbar/types" import { StatBoxGroup } from "../../components/StatBox" -import { T_NETWORK_SUBGRAPH_ID } from "../../config/subgraph" +import { T_NETWORK_SUBGRAPH_URL } from "../../config/subgraph" import useQuery from "../../hooks/useQuery" import { formatFiatCurrencyAmount, formatTokenAmount } from "../../utils" import { useBalanceOfDAOTreasury } from "../../hooks/useBalanceOfDAOTreasury" @@ -21,7 +21,7 @@ const ThresholdDaoDataSection = () => { tokenholderDelegations: { id: string }[] stakeDelegations: { id: string }[] }>( - T_NETWORK_SUBGRAPH_ID, + T_NETWORK_SUBGRAPH_URL, gql` query { daometric(id: "dao-metrics") { diff --git a/src/templates/home-page/Hero/index.tsx b/src/templates/home-page/Hero/index.tsx index 4896a216..7db4d727 100644 --- a/src/templates/home-page/Hero/index.tsx +++ b/src/templates/home-page/Hero/index.tsx @@ -8,8 +8,8 @@ import { TrackComponent } from "../../../components/Posthog/TrackComponent" import { Analytics } from "./Analytics" import { gql } from "graphql-request" import { - TBTC_SUBGRAPH_ID, - T_NETWORK_SUBGRAPH_ID, + TBTC_SUBGRAPH_URL, + T_NETWORK_SUBGRAPH_URL, } from "../../../config/subgraph" import useQuery from "../../../hooks/useQuery" import { exchangeAPI, formatUnits } from "../../../utils" @@ -33,14 +33,14 @@ const Hero: FC<{ data: totalStakedData, error: totalStakedError, } = useQuery<{ - daometric: { stakedTotal: string } + epoches: { totalStaked: string }[] minStakeAmounts: { amount: string }[] }>( - T_NETWORK_SUBGRAPH_ID, + T_NETWORK_SUBGRAPH_URL, gql` query { - daometric(id: "dao-metrics") { - stakedTotal + epoches(orderBy: startTime, orderDirection: desc, first: 1) { + totalStaked } } ` @@ -49,7 +49,7 @@ const Hero: FC<{ const { isFetching, data, error } = useQuery<{ tbtctoken: { currentTokenHolders: string; totalSupply: string } }>( - TBTC_SUBGRAPH_ID, + TBTC_SUBGRAPH_URL, gql` query { tbtctoken(id: "TBTCToken") { @@ -60,10 +60,10 @@ const Hero: FC<{ ` ) - const { daometric } = totalStakedData || { - daometric: { stakedTotal: "0" }, + const { epoches } = totalStakedData || { + epoches: [{ totalStaked: "0" }], } - const totalStaked = !error ? daometric.stakedTotal : "0" + const totalStaked = !totalStakedError ? epoches[0].totalStaked : "0" const tPrice = useTTokenPrice() const totalValueStakedInUSD = exchangeAPI .toUsdBalance(formatUnits(totalStaked), tPrice)