Skip to content

Commit

Permalink
use subgraph for delegationMetrics
Browse files Browse the repository at this point in the history
  • Loading branch information
tyler17 committed Jun 15, 2024
1 parent 23b6231 commit 0cdc3a1
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 8 deletions.
14 changes: 6 additions & 8 deletions modules/delegates/api/fetchDelegates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand All @@ -535,10 +536,7 @@ export async function fetchDelegatesPaginated({
query: delegatesQuery,
variables: delegatesQueryVariables
}),
gqlRequest<any>({
chainId,
query: delegationMetricsQuery
}),
fetchDelegationMetrics(network),
fetchAvcsTotalDelegated(avcAndCount, network)
]);

Expand All @@ -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(
Expand Down
36 changes: 36 additions & 0 deletions modules/delegates/api/fetchDelegationMetrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
SPDX-FileCopyrightText: © 2023 Dai Foundation <www.daifoundation.org>
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<DelegationMetrics> {
const res = await gqlRequest<any>({
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
};
}
19 changes: 19 additions & 0 deletions modules/gql/queries/subgraph/allDelegations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
SPDX-FileCopyrightText: © 2023 Dai Foundation <www.daifoundation.org>
SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { gql } from 'graphql-request';

export const allDelegations = gql`
{delegations {
delegator
delegate {
id
}
amount
}}
`;

0 comments on commit 0cdc3a1

Please sign in to comment.