diff --git a/modules/delegates/api/fetchChainDelegates.ts b/modules/delegates/api/fetchChainDelegates.ts index bb3fb86f..9f5e9186 100644 --- a/modules/delegates/api/fetchChainDelegates.ts +++ b/modules/delegates/api/fetchChainDelegates.ts @@ -32,7 +32,6 @@ export async function fetchChainDelegates( address: d.ownerAddress, voteDelegateAddress: d.address, skyDelegated: formatEther(BigInt(totalDelegated)), - delegations: d.delegations || [], // Include current delegations from subgraph lastVoteDate: d.voter?.lastVotedTimestamp ? Number(d.voter.lastVotedTimestamp) : null }; }); diff --git a/modules/delegates/api/fetchDelegates.ts b/modules/delegates/api/fetchDelegates.ts index 19e4889a..7c950279 100644 --- a/modules/delegates/api/fetchDelegates.ts +++ b/modules/delegates/api/fetchDelegates.ts @@ -62,7 +62,6 @@ function mergeDelegateInfo({ skyDelegated: onChainDelegate.skyDelegated, proposalsSupported: onChainDelegate.proposalsSupported, execSupported: undefined, - delegations: onChainDelegate.delegations, // Include current delegations from subgraph blockTimestamp: onChainDelegate.blockTimestamp }; } @@ -352,11 +351,6 @@ export async function fetchDelegatesPaginated({ const lastVoteTimestamp = Math.max(lastVoteMainnet, lastVoteArbitrum); - const totalDelegated: bigint = delegate.delegations.reduce( - (acc, curr) => acc + BigInt(curr?.amount || 0n), - 0n - ); - return { name: githubDelegate?.name || 'Shadow Delegate', voteDelegateAddress: delegateId, @@ -366,7 +360,7 @@ export async function fetchDelegatesPaginated({ picture: githubDelegate?.picture, communication: githubDelegate?.communication, combinedParticipation: githubDelegate?.combinedParticipation, - skyDelegated: formatEther(totalDelegated), + skyDelegated: formatEther(BigInt(delegate.totalDelegated || '0')), delegatorCount: delegate.delegators, lastVoteDate: lastVoteTimestamp > 0 ? new Date(lastVoteTimestamp * 1000) : null, proposalsSupported: votedProposals?.length || 0, diff --git a/modules/delegates/api/fetchDelegationMetrics.ts b/modules/delegates/api/fetchDelegationMetrics.ts index 63abe7f1..38b541c0 100644 --- a/modules/delegates/api/fetchDelegationMetrics.ts +++ b/modules/delegates/api/fetchDelegationMetrics.ts @@ -7,7 +7,7 @@ SPDX-License-Identifier: AGPL-3.0-or-later */ import { gqlRequest } from 'modules/gql/gqlRequest'; -import { allDelegationsPaginated } from 'modules/gql/queries/subgraph/allDelegations'; +import { allDelegates } from 'modules/gql/queries/subgraph/allDelegates'; import { SupportedNetworks } from 'modules/web3/constants/networks'; import { networkNameToChainId } from 'modules/web3/helpers/chain'; import { formatEther } from 'viem'; @@ -17,40 +17,26 @@ interface DelegationMetrics { delegatorCount: number; } -interface Delegation { - delegator: string; - delegate: { - id: string; - version: string; - }; - amount: string; +interface Delegate { + totalDelegated: string; + delegators: number; } export async function fetchDelegationMetrics(network: SupportedNetworks): Promise { const chainId = networkNameToChainId(network); - const pageSize = 1000; - let skip = 0; - let hasMore = true; - const allDelegations: Delegation[] = []; - - // Fetch all delegations using pagination - while (hasMore) { - const res = await gqlRequest({ - chainId, - query: allDelegationsPaginated(chainId, pageSize, skip) - }); - - const delegations = res.delegations || []; - allDelegations.push(...delegations); - - // Check if there are more results to fetch - hasMore = delegations.length === pageSize; - skip += pageSize; - } - - // Calculate metrics from all delegations - const totalSkyDelegated = formatEther(allDelegations.reduce((acc, cur) => acc + BigInt(cur.amount), 0n)); - const delegatorCount = allDelegations.filter(d => BigInt(d.amount) > 0n).length; + + const res = await gqlRequest<{ delegates: Delegate[] }>({ + chainId, + query: allDelegates(chainId) + }); + + const delegates = res.delegates || []; + + // Sum totalDelegated and delegators across all delegates + const totalSkyDelegated = formatEther( + delegates.reduce((acc, cur) => acc + BigInt(cur.totalDelegated || '0'), 0n) + ); + const delegatorCount = delegates.reduce((acc, cur) => acc + (cur.delegators || 0), 0); return { totalSkyDelegated, diff --git a/modules/delegates/types/delegate.d.ts b/modules/delegates/types/delegate.d.ts index 7d2a6441..3e1bb63b 100644 --- a/modules/delegates/types/delegate.d.ts +++ b/modules/delegates/types/delegate.d.ts @@ -53,7 +53,6 @@ export type DelegateContractInformation = { blockTimestamp: string; skyDelegated: string; proposalsSupported: number; - delegations?: { delegator: string; amount: string }[]; // current delegations from subgraph lastVoteDate: number | null; }; @@ -72,7 +71,6 @@ export type Delegate = { skyDelegated: string; proposalsSupported: number; execSupported: CMSProposal | undefined; - delegations?: { delegator: string; amount: string }[]; // current delegations from subgraph blockTimestamp: string; }; diff --git a/modules/gql/queries/subgraph/allDelegates.ts b/modules/gql/queries/subgraph/allDelegates.ts index 972734c1..3cfce211 100644 --- a/modules/gql/queries/subgraph/allDelegates.ts +++ b/modules/gql/queries/subgraph/allDelegates.ts @@ -20,19 +20,10 @@ export const allDelegates = (chainId: number) => /* GraphQL */ ` id address totalDelegated + delegators voter { lastVotedTimestamp } - delegations( - limit: 1000 - where: { _and: [ - { _and: [{ delegator: { _nilike: "0xce01c90de7fd1bcfa39e237fe6d8d9f569e8a6a3" } }, { delegator: { _nilike: "0xb1fc11f03b084fff8dae95fa08e8d69ad2547ec1" } }] }, - { amount: { _gt: "0" } } - ] } - ) { - delegator - amount - } } } `; diff --git a/modules/gql/queries/subgraph/allDelegations.ts b/modules/gql/queries/subgraph/allDelegations.ts deleted file mode 100644 index c36b8403..00000000 --- a/modules/gql/queries/subgraph/allDelegations.ts +++ /dev/null @@ -1,25 +0,0 @@ -/* -SPDX-FileCopyrightText: © 2023 Dai Foundation -SPDX-License-Identifier: AGPL-3.0-or-later -*/ - -export const allDelegationsPaginated = (chainId: number, first: number, skip: number) => /* GraphQL */ ` -{ - delegations: Delegation( - limit: ${first} - offset: ${skip} - where: { _and: [ - { chainId: { _eq: ${chainId} } }, - { delegate: { version: { _eq: "3" } } }, - { _and: [{ delegator: { _nilike: "0xce01c90de7fd1bcfa39e237fe6d8d9f569e8a6a3" } }, { delegator: { _nilike: "0xb1fc11f03b084fff8dae95fa08e8d69ad2547ec1" } }] } - ] } - ) { - delegator - delegate { - id - version - } - amount - } -} -`; diff --git a/modules/gql/queries/subgraph/delegates.ts b/modules/gql/queries/subgraph/delegates.ts index 36d93c1a..5fbf474d 100644 --- a/modules/gql/queries/subgraph/delegates.ts +++ b/modules/gql/queries/subgraph/delegates.ts @@ -10,16 +10,7 @@ const delegateFields = /* GraphQL */ ` blockTimestamp blockNumber ownerAddress - delegations( - limit: 1000 - where: { _and: [ - { _and: [{ delegator: { _nilike: "0xce01c90de7fd1bcfa39e237fe6d8d9f569e8a6a3" } }, { delegator: { _nilike: "0xb1fc11f03b084fff8dae95fa08e8d69ad2547ec1" } }] }, - { amount: { _gt: "0" } } - ] } - ) { - delegator - amount - } + totalDelegated id address delegators