Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use subgraph for delegation history by delegate #928

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion modules/delegates/api/fetchDelegatedTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export async function fetchDelegatedTo(
return {
delegateContractAddress: x.delegate.id,
lockAmount: x.amount,
blockTimestamp: x.timestamp,
blockTimestamp: new Date(parseInt(x.timestamp) * 1000).toISOString(),
hash: x.txnHash,
blockNumber: x.blockNumber,
immediateCaller: address
Expand Down
24 changes: 18 additions & 6 deletions modules/delegates/api/fetchDelegationEventsByAddresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ SPDX-License-Identifier: AGPL-3.0-or-later

import logger from 'lib/logger';
import { gqlRequest } from 'modules/gql/gqlRequest';
import { mkrLockedDelegateArrayTotalsV2 } from 'modules/gql/queries/mkrLockedDelegateArray';
import { delegateHistoryArray } from 'modules/gql/queries/subgraph/delegateHistoryArray';
import { SupportedNetworks } from 'modules/web3/constants/networks';
import { networkNameToChainId } from 'modules/web3/helpers/chain';
import { MKRLockedDelegateAPIResponse } from '../types';
import { utils } from 'ethers';

export async function fetchDelegationEventsByAddresses(
addresses: string[],
Expand All @@ -20,14 +21,25 @@ export async function fetchDelegationEventsByAddresses(
try {
const data = await gqlRequest({
chainId: networkNameToChainId(network),
query: mkrLockedDelegateArrayTotalsV2,
useSubgraph: true,
query: delegateHistoryArray,
variables: {
argAddress: addresses,
argUnixTimeStart: 0,
argUnixTimeEnd: Math.floor(Date.now() / 1000)
delegates: addresses
}
});
const addressData: MKRLockedDelegateAPIResponse[] = data.mkrLockedDelegateArrayTotalsV2.nodes;
const flattenedData = data.delegates.flatMap(delegate => delegate.delegationHistory);

const addressData: MKRLockedDelegateAPIResponse[] = flattenedData.map(x => {
return {
delegateContractAddress: x.delegate.id,
immediateCaller: x.delegator,
lockAmount: utils.formatEther(x.amount),
blockNumber: x.blockNumber,
blockTimestamp: new Date(parseInt(x.timestamp) * 1000).toISOString(),
hash: x.txnHash,
callerLockTotal: utils.formatEther(x.accumulatedAmount)
};
});
return addressData;
} catch (e) {
logger.error('fetchDelegationEventsByAddresses: Error fetching delegation events', e.message);
Expand Down
Loading