Skip to content

Commit

Permalink
Merge branch 'new-delegate-factory' into paginated-delegates-with-sub…
Browse files Browse the repository at this point in the history
…graph
  • Loading branch information
tyler17 committed Jul 12, 2024
2 parents a32236a + 2861219 commit ce33bdd
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 58 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ The following configuration values can be added to the `.env` file:

- Set `GASLESS_WEBHOOK_URL` for sending gasless vote requests to discord

- Set `USE_PRIVATE_SUBGRAPH` to `true` to connect to the subgraph privately

**Optional** Set `DEFENDER_API_KEY_MAINNET` and/or `DEFENDER_API_KEY_TESTNET` to a valid OpenZeppelin Defender Relay key (used for gasless poll voting)
**Optional** Set `DEFENDER_API_SECRET_MAINNET` and/or`DEFENDER_API_SECRET_TESTNET` to a valid OpenZeppelin Defender Relay secret
**Optional** Set `ALCHEMY_ARBITRUM_KEY` to a valid Alchemy API key for the arbitrum network
Expand Down
5 changes: 3 additions & 2 deletions modules/delegates/api/fetchDelegationMetrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ 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';
import { BigNumberWAD } from 'modules/web3/constants/numbers';
import { BigNumberJS } from 'lib/bigNumberJs';

interface DelegationMetrics {
totalMkrDelegated: number;
Expand All @@ -25,8 +27,7 @@ export async function fetchDelegationMetrics(
query: allDelegations
});
const delegations = res.delegations;

const totalMkrDelegated = delegations.reduce((acc, cur) => acc + Number(cur.amount), 0);
const totalMkrDelegated = delegations.reduce((acc, cur) => acc.plus(new BigNumberJS(cur.amount).div(BigNumberWAD)), new BigNumberJS(0)).toNumber();
const delegatorCount = delegations.filter(d => Number(d.amount) > 0).length;

return {
Expand Down
2 changes: 1 addition & 1 deletion modules/delegates/helpers/formatDelegationHistoryChart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const formatDelegationHistoryChart = (
): MKRWeightHisory[] => {
// We need to fill all the data for the interval
// If we get last month, we need to add all the missing days
const start = formatIsoDateConversion(lockEvents[0].blockTimestamp);
const start = formatIsoDateConversion(new Date(Number(lockEvents[0].blockTimestamp) * 1000).toISOString());

const years = differenceInCalendarYears(Date.now(), new Date(lockEvents[0].blockTimestamp));

Expand Down
7 changes: 5 additions & 2 deletions modules/gql/gql.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ export const MAINNET_SPOCK_URL = 'https://pollingdb2-mainnet-prod.makerdux.com/a
export const TENDERLY_SPOCK_URL = 'https://pollingdb2-tenderly-staging.makerdux.com/api/v1';

/* Subgraph URLs */
export const TENDERLY_SUBGRAPH_URL = 'https://query-subgraph-testnet.sky.money/subgraphs/name/jetstreamgg/subgraph-testnet';
export const MAINNET_SUBGRAPH_URL = 'https://query-subgraph-testnet.sky.money/subgraphs/name/jetstreamgg/subgraph-testnet';

const usePrivateSubgraph = process.env.USE_PRIVATE_SUBGRAPH === 'true';
const permission = usePrivateSubgraph ? 'private' : 'public';
export const TENDERLY_SUBGRAPH_URL = `https://query-subgraph-staging.sky.money/${permission}/subgraphs/name/jetstreamgg/subgraph-testnet`;
export const MAINNET_SUBGRAPH_URL = `https://query-subgraph-staging.sky.money/${permission}/private/subgraphs/name/jetstreamgg/subgraph-testnet`;

export enum QueryFilterNames {
Active = 'active',
Expand Down
14 changes: 6 additions & 8 deletions modules/gql/gqlRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { request, Variables, RequestDocument, GraphQLClient } from 'graphql-request';
import { request, Variables, RequestDocument } from 'graphql-request';
import logger from 'lib/logger';
import { backoffRetry } from 'lib/utils';
import { ApiError } from 'modules/app/api/ApiError';
Expand All @@ -30,20 +30,18 @@ export const gqlRequest = async <TQuery = any>({
try {
const id = chainId ?? SupportedChainId.MAINNET;
let url;
if (useSubgraph && chainId === SupportedChainId.TENDERLY) { //TODO: update to use subgraph on mainnet too
if (useSubgraph) {
url = CHAIN_INFO[id].subgraphUrl;
} else {
url = CHAIN_INFO[id].spockUrl;
}
if (!url) {
return Promise.reject(new ApiError(`Missing spock url in configuration for chainId: ${id}`));
return Promise.reject(new ApiError(`Missing ${useSubgraph ? 'subgraph' : 'spock'} url in configuration for chainId: ${id}`));
}
const client = new GraphQLClient(url);
// client.setHeader('Origin', 'http://localhost:3000');
// client.setHeader('Referer', 'http://localhost:3000/');

const resp = await backoffRetry(
3,
() => client.request(query, variables),
() => request(url, query, variables),
500,
(message: string) => {
logger.debug(`GQL Request: ${message}. --- ${query}`);
Expand All @@ -52,7 +50,7 @@ export const gqlRequest = async <TQuery = any>({
return resp;
} catch (e) {
const status = e.response ? e.response.status : 500;
const errorMessage = status === 403 ? e.message : e.message; //'Rate limited on gov polling' : e.message;
const errorMessage = e.message;
const message = `Error on GraphQL query, Chain ID: ${chainId}, query: ${query}, message: ${errorMessage}`;
throw new ApiError(message, status, 'Error fetching gov polling data');
}
Expand Down
6 changes: 4 additions & 2 deletions modules/gql/queries/subgraph/allDelegations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ SPDX-License-Identifier: AGPL-3.0-or-later
import { gql } from 'graphql-request';

export const allDelegations = gql`
{delegations {
{
delegations {
delegator
delegate {
id
}
amount
}}
}
}
`;
43 changes: 0 additions & 43 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ import { useVotedProposals } from 'modules/executive/hooks/useVotedProposals';
import { fetchLandingPageData } from 'modules/home/api/fetchLandingPageData';
import { LandingPageData } from 'modules/home/api/fetchLandingPageData';
import { useLandingPageDelegates } from 'modules/gql/hooks/useLandingPageDelegates';
// import { fetchDelegationMetrics } from 'modules/delegates/api/fetchDelegationMetrics';
// import { fetchChainDelegates } from 'modules/delegates/api/fetchChainDelegates';
import { fetchDelegatesPaginated } from 'modules/delegates/api/fetchDelegates';
import { DelegateTypeEnum } from 'modules/delegates/delegates.constants';
import { DelegateOrderByEnum } from 'modules/delegates/delegates.constants';

const LandingPage = ({
proposals,
Expand Down Expand Up @@ -137,44 +132,6 @@ const LandingPage = ({
}
}, []);


// useEffect(() => {
// const fetchMetrics = async () => {
// const metrics = await fetchDelegationMetrics(SupportedNetworks.TENDERLY);
// console.log('metrics', metrics);
// };

// fetchMetrics();
// }, []);

// useEffect(() => {
// const fetchDelegates = async () => {
// const chainDelegates = await fetchChainDelegates(SupportedNetworks.TENDERLY);
// console.log('chainDelegates', chainDelegates);
// };

// fetchDelegates();
// }, []);

useEffect(() => {
const getDelegates = async () => {
const paginatedDelegates = await fetchDelegatesPaginated({
network: SupportedNetworks.TENDERLY,
pageSize: 10,
page: 1,
orderBy: DelegateOrderByEnum.MKR,
orderDirection: 'desc',
includeExpired: false,
seed: null,
delegateType: DelegateTypeEnum.ALIGNED,
searchTerm: null,
});
console.log('paginatedDelegates', paginatedDelegates);
};

getDelegates();
}, []);

return (
<div>
{delegates.length === 0 && delegatesInfo.length === 0 && polls.length === 0 && (
Expand Down

0 comments on commit ce33bdd

Please sign in to comment.