Skip to content

Commit

Permalink
Merge pull request #424 from protofire/bug/issue-423
Browse files Browse the repository at this point in the history
  • Loading branch information
leolower authored Jan 6, 2021
2 parents 6d38eb5 + 458c6ea commit 4877e86
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 101 deletions.
6 changes: 0 additions & 6 deletions src/components/PollDetails/data.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import { request } from 'graphql-request'
import { BigNumber } from 'bignumber.js'
import { getPollVotersRegistries, getStakedByPoll, stakedByAddress, msToSeconds, getVotersSnapshots } from '../../utils'

const GOVERNANCE_API_URI = process.env.REACT_APP_GRAPH_HTTP
const MKR_API_URI = process.env.REACT_APP_MKR_GRAPH_HTTP

const fetchQuery = (url, query, variables) => request(url, query, variables)

export const getPollVotersPerOption = poll => {
return poll.votes.reduce((acum, el) => {
const option = acum[el.option] || []
Expand Down
25 changes: 1 addition & 24 deletions src/containers/Home/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { useState, useEffect } from 'react'
import HomeDetail from '../../components/Home/HomeDetail'

import { DEFAULT_FETCH_ROWS } from '../../constants'
import { FullLoading } from '../../components/common'
import { useQuery } from '@apollo/react-hooks'
import { GOVERNANCE_INFO_QUERY } from './queries'
Expand All @@ -11,28 +10,6 @@ import useAsyncMemo from '../../hooks/useAsyncMemo'
import useHomeData from '../../hooks/useHomeData'
import BigNumber from 'bignumber.js'

const getHomeVariables = data => {
const governance = data.governanceInfo
return {
voters: Number(governance.countProxies) + Number(governance.countAddresses) || DEFAULT_FETCH_ROWS,
}
}

const getPages = gData => {
if (!gData) {
return {
pollPages: 2,
executivesPages: 2,
}
}

const { governanceInfo } = gData
return {
pollPages: Math.ceil(Number(governanceInfo.countPolls) / 1000),
executivesPages: Math.ceil(Number(governanceInfo.countSpells) / 1000),
}
}

const Error = () => <div>ERROR: There was an error trying to fetch the data.</div>

function MakerGovernanceInfo() {
Expand Down Expand Up @@ -65,7 +42,7 @@ function MakerGovernanceInfo() {
return <FullLoading />
}

if (error || gResult.error) {
if (error || gResult.error || mkrError) {
return <Error />
}

Expand Down
2 changes: 1 addition & 1 deletion src/containers/VotingHistory/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useEffect } from 'react'
import React from 'react'
import { useQuery } from '@apollo/react-hooks'
import { PageTitle, FullLoading } from '../../components/common'
import { DEFAULT_FETCH_ROWS } from '../../constants'
Expand Down
42 changes: 17 additions & 25 deletions src/hooks/queries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,25 @@ export const POLLS_FIRST_QUERY = gql`
`
const getPollsData = (pageIndex, pageSize, offset, ordering) => {
return `
polls_${pageIndex}: polls(first: ${pageSize}, skip: ${offset}, ${ordering}, where: { id_not_in: [0,1,2,3,6,7,8,9,11] }) {
...pollsDetail
}
polls_${pageIndex}: polls(first: ${pageSize}, skip: ${offset}, ${ordering}, where: { id_not_in: [0,1,2,3,6,7,8,9,11] }) {
...pollsDetail
}
`
}

const getExecutivesData = (pageIndex, pageSize, offset, ordering) => {
return `
executives_${pageIndex}: spells(first: ${pageSize}, skip: ${offset}, ${ordering}) {
...executivesDetailHome
}
executives_${pageIndex}: spells(first: ${pageSize}, skip: ${offset}, ${ordering}) {
...executivesDetailHome
}
`
}

const getVotersData = (pageIndex, pageSize, offset, ordering) => {
return `
voters_${pageIndex}: actions(where: { type: VOTER }, first: ${pageSize}, skip: ${offset}, ${ordering}) {
...actionsDetail
}
`
}

Expand All @@ -143,27 +151,11 @@ const getHomeData = (pageIndex, pageSize, offset, ordering) => {
`
}

export const HOME_DATA_QUERY2 = gql`
query getHomeData2($voters: Int!) {
${getAllEvents(getPollsData, 'startDate')}
${getAllEvents(getExecutivesData)}
voters: actions(where: { type: VOTER }, first: $voters) {
...actionsDetail
}
${getAllEvents(getHomeData)}
}
${pollsDetailFragment}
${executivesDetailFragment}
${actionsDetailFragment}
`

export const HOME_DATA_QUERY = ({ pollPages, executivesPages }) => gql`
query getHomeData($voters: Int!) {
export const HOME_DATA_QUERY = ({ pollPages, executivesPages, votersPages }) => gql`
query getHomeData {
${getAllEvents(getExecutivesData, 'timestamp', executivesPages)}
${getAllEvents(getPollsData, 'startDate', pollPages)}
voters: actions(where: { type: VOTER }, first: $voters) {
...actionsDetail
}
${getAllEvents(getVotersData, 'timestamp', votersPages)}
${getAllEvents(getHomeData)}
}
${pollsDetailFragment}
Expand Down
10 changes: 2 additions & 8 deletions src/hooks/useHomeData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,20 @@ import { mergeEventPages } from '../utils'

const REACT_APP_HOME_DATA_TTL = Number(process.env.REACT_APP_HOME_DATA_TTL) || 5

const getHomeVariables = data => {
const governance = data.governanceInfo
return {
voters: Number(governance.countProxies) + Number(governance.countAddresses) || 1000,
}
}

const getPages = gData => {
if (!gData) {
return {
pollPages: 2,
executivesPages: 2,
votersPages: 1,
}
}

const { governanceInfo } = gData
return {
pollPages: Math.ceil(Number(governanceInfo.countPolls) / 1000),
executivesPages: Math.ceil(Number(governanceInfo.countSpells) / 1000),
votersPages: Math.ceil((Number(governanceInfo.countProxies) + Number(governanceInfo.countAddresses)) / 1000),
}
}

Expand All @@ -34,7 +29,6 @@ export function useHomeData(governanceInfo: Maybe<any>): any {
const [results, setResults] = useState<Maybe<null>>(null)
const [loading, setLoading] = useState(true)
const { data, error } = useQuery(HOME_DATA_QUERY(getPages(governanceInfo)), {
variables: governanceInfo && getHomeVariables(governanceInfo),
skip: skipQuery,
})

Expand Down
33 changes: 0 additions & 33 deletions src/types/generatedGQL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1181,39 +1181,6 @@ export interface getHistoryDataVariables {
/* eslint-disable */
// This file was automatically generated and should not be edited.

// ====================================================
// GraphQL query operation: getHomeData2
// ====================================================

export interface getHomeData2_voters {
__typename: 'Action'
id: string
/**
* Action timestamp as seconds (time)
*/
timestamp: any
/**
* Action value (arg)
*/
wad: any | null
/**
* Action name (act)
*/
type: ActionType
}

export interface getHomeData2 {
voters: getHomeData2_voters[]
}

export interface getHomeData2Variables {
voters: number
}

/* tslint:disable */
/* eslint-disable */
// This file was automatically generated and should not be edited.

// ====================================================
// GraphQL fragment: executivesDetailPage
// ====================================================
Expand Down
6 changes: 2 additions & 4 deletions src/utils/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@ import {
differenceInMinutes,
differenceInHours,
differenceInDays,
differenceInWeeks,
differenceInMonths,
differenceInYears,
addHours,
addMinutes,
isBefore,
} from 'date-fns'
import { LAST_YEAR, LAST_MONTH, LAST_WEEK, LAST_DAY } from '../constants'
import store, { setCache, getCache } from './cache'
import { setCache, getCache } from './cache'

export * from './mkr-registry'

Expand Down Expand Up @@ -181,7 +180,7 @@ export const getVotersSnapshots = async voters => {
const snapshotsCache = (await getCache('accounts-snapshots-cache')) || {}

const requiredVoters = voters.reduce((acc, voter) => {
const { lastUpdate, data } = snapshotsCache[voter] || {}
const { lastUpdate } = snapshotsCache[voter] || {}
// Do not update for 1/2 hour
if (!lastUpdate || getUnixTime(addMinutes(fromUnixTime(lastUpdate), 30)) < endDate) {
return [...acc, { voter, endDate }]
Expand Down Expand Up @@ -488,7 +487,6 @@ export const getPollData = async (poll, balancesLookup) => {

// TODO - improve function naming (snapshots of acctual voting addresses)
export const getPollsBalances = async polls => {
const now = new Date()
const allVoters = Array.from(
new Set(polls.flatMap(poll => poll.votes.reduce((voters, v) => [...voters, v.voter], []))),
)
Expand Down

0 comments on commit 4877e86

Please sign in to comment.