From 0cdc3a16e40d807b40b920ff98b42eb320f0675c Mon Sep 17 00:00:00 2001 From: tyler17 Date: Fri, 14 Jun 2024 19:31:30 -0700 Subject: [PATCH] use subgraph for delegationMetrics --- modules/delegates/api/fetchDelegates.ts | 14 ++++---- .../delegates/api/fetchDelegationMetrics.ts | 36 +++++++++++++++++++ .../gql/queries/subgraph/allDelegations.ts | 19 ++++++++++ 3 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 modules/delegates/api/fetchDelegationMetrics.ts create mode 100644 modules/gql/queries/subgraph/allDelegations.ts diff --git a/modules/delegates/api/fetchDelegates.ts b/modules/delegates/api/fetchDelegates.ts index 810c0ef76..fdf2aea00 100644 --- a/modules/delegates/api/fetchDelegates.ts +++ b/modules/delegates/api/fetchDelegates.ts @@ -43,9 +43,10 @@ import { fetchDelegatesExecSupport } from './fetchDelegatesExecSupport'; import { fetchDelegateAddresses } from './fetchDelegateAddresses'; import getDelegatesCounts from '../helpers/getDelegatesCounts'; import { filterDelegates } from '../helpers/filterDelegates'; -import { delegationMetricsQuery } from 'modules/gql/queries/delegationMetrics'; +import { allDelegations } from 'modules/gql/queries/subgraph/allDelegations'; import { AvcWithCountAndDelegates } from '../types/avc'; import { fetchAvcsTotalDelegated } from './fetchAvcsTotalDelegated'; +import { fetchDelegationMetrics } from './fetchDelegationMetrics'; function mergeDelegateInfo({ onChainDelegate, @@ -526,7 +527,7 @@ export async function fetchDelegatesPaginated({ delegatesQueryVariables['seed'] = seed; } - const [githubExecutives, delegatesExecSupport, delegatesQueryRes, delegationMetricsRes, avcStats] = + const [githubExecutives, delegatesExecSupport, delegatesQueryRes, delegationMetrics, avcStats] = await Promise.all([ getGithubExecutives(network), fetchDelegatesExecSupport(network), @@ -535,10 +536,7 @@ export async function fetchDelegatesPaginated({ query: delegatesQuery, variables: delegatesQueryVariables }), - gqlRequest({ - chainId, - query: delegationMetricsQuery - }), + fetchDelegationMetrics(network), fetchAvcsTotalDelegated(avcAndCount, network) ]); @@ -553,8 +551,8 @@ export async function fetchDelegatesPaginated({ total: totalDelegatesCount, shadow: shadowDelegatesCount, aligned: alignedDelegatesCount, - totalMKRDelegated: delegationMetricsRes.delegationMetrics.totalMkrDelegated || 0, - totalDelegators: +delegationMetricsRes.delegationMetrics.delegatorCount || 0 + totalMKRDelegated: delegationMetrics.totalMkrDelegated || 0, + totalDelegators: delegationMetrics.delegatorCount || 0 }, delegates: delegatesQueryRes.delegates.nodes.map(delegate => { const allDelegatesEntry = allDelegatesWithNamesAndLinks.find( diff --git a/modules/delegates/api/fetchDelegationMetrics.ts b/modules/delegates/api/fetchDelegationMetrics.ts new file mode 100644 index 000000000..813ce33ab --- /dev/null +++ b/modules/delegates/api/fetchDelegationMetrics.ts @@ -0,0 +1,36 @@ +/* + +SPDX-FileCopyrightText: © 2023 Dai Foundation + +SPDX-License-Identifier: AGPL-3.0-or-later + +*/ + +import { gqlRequest } from 'modules/gql/gqlRequest'; +import { allDelegations } from 'modules/gql/queries/subgraph/allDelegations'; +import { SupportedNetworks } from 'modules/web3/constants/networks'; +import { networkNameToChainId } from 'modules/web3/helpers/chain'; + +interface DelegationMetrics { + totalMkrDelegated: number; + delegatorCount: number; + } + +export async function fetchDelegationMetrics( + network: SupportedNetworks +): Promise { + const res = await gqlRequest({ + chainId: networkNameToChainId(network), + useSubgraph: true, + query: allDelegations + }); + const delegations = res.delegations; + + const totalMkrDelegated = delegations.reduce((acc, cur) => acc + Number(cur.amount), 0); + const delegatorCount = delegations.filter(d => Number(d.amount) > 0).length; + + return { + totalMkrDelegated, + delegatorCount + }; +} diff --git a/modules/gql/queries/subgraph/allDelegations.ts b/modules/gql/queries/subgraph/allDelegations.ts new file mode 100644 index 000000000..dfc582374 --- /dev/null +++ b/modules/gql/queries/subgraph/allDelegations.ts @@ -0,0 +1,19 @@ +/* + +SPDX-FileCopyrightText: © 2023 Dai Foundation + +SPDX-License-Identifier: AGPL-3.0-or-later + +*/ + +import { gql } from 'graphql-request'; + +export const allDelegations = gql` +{delegations { + delegator + delegate { + id + } + amount + }} +`;