Skip to content

Commit

Permalink
Release 3.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
leolower authored May 15, 2020
2 parents b5ef815 + a825836 commit f88fe08
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/components/PollDetails/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const getPollDataWithoutBalances = async poll => {

const hotCold = Array.from(new Set(voteRegistries.flatMap((el: any) => [el.coldAddress, el.hotAddress])))
const votersHotCold = Array.from(new Set([...votersAddresses, ...hotCold]))
const balances = getBalanceByAccount(await getVotersSnapshots(votersHotCold, msToSeconds(poll.endDate)))
const balances = getBalanceByAccount(await getVotersSnapshots(votersHotCold), msToSeconds(poll.endDate))

const stakedVotersAndBalances = votersHotCold.reduce((acc, key) => {
const staked = stakedVoters[key] || ZERO
Expand Down Expand Up @@ -90,9 +90,9 @@ export const getPollDataWithoutBalances = async poll => {
return ret
}

const getBalanceByAccount = balances => {
const getBalanceByAccount = (balances, endDate) => {
return balances.reduce((acc, accountSnapshots) => {
const lastSnapshot = accountSnapshots[0]
const lastSnapshot = accountSnapshots.find(snapshot => snapshot.timestamp <= endDate)

if (lastSnapshot) {
return {
Expand Down
8 changes: 6 additions & 2 deletions src/components/PollDetails/helpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ const getAllBalances = async poll => {
),
)

const allBalances = await getVotersSnapshots(allVoters, getUnixTime(end))
const allBalances = await getVotersSnapshots(allVoters)
return allBalances.flat().reduce((lookup, snapshot: any) => {
const account = snapshot.account.address
const balances = lookup[account] || []
Expand All @@ -225,6 +225,7 @@ export const getPollMakerHistogramData = async poll => {
const periods = getPollPeriods(poll)

const pollOptions = ['Abstein', ...poll.options]

const options = pollOptions.reduce((acc, el) => {
return {
...acc,
Expand Down Expand Up @@ -254,7 +255,9 @@ export const getPollMakerHistogramData = async poll => {
options[option] = new Set([...Array.from(options[option])]).add(el.sender)
voters[el.sender] = option

if (prevVote) options[prevVote] = options[prevVote].delete(el.sender)
if (prevVote) {
options[prevVote].delete(el.sender)
}
}
})
return {
Expand All @@ -266,6 +269,7 @@ export const getPollMakerHistogramData = async poll => {
return Promise.all(
votersPerPeriod.map(async period => {
const manualPoll = {
id: poll.id,
endDate: period.endDate,
votes: poll.options.flatMap(pop =>
Array.from(period[pop]).map(voter => ({ option: poll.options.indexOf(pop) + 1, voter })),
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useHomeData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import store from '../utils/cache'
import { addMinutes, isBefore } from 'date-fns'
import { mergeEventPages } from '../utils'

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

const getHomeVariables = data => {
const governance = data.governanceInfo
Expand Down
19 changes: 19 additions & 0 deletions src/utils/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,25 @@ myWindow.exportCache = async () => {
saveFile(file, fileName)
}

myWindow.exportCacheByKey = async key => {
let cache = await getCache(key)

const fileName = `maker-governace-${key}-${(Date.now() / 1000).toFixed(0)}.json`
const file = new File([JSON.stringify(cache)], fileName, { type: 'application/json' })
saveFile(file, fileName)
}

myWindow.exportCacheByMultipleKey = async (keys: Array<any>) => {
let cache = {}
await store.iterate(function(value, key) {
if (keys.includes(key)) cache[key] = value
})

const fileName = `maker-governace-multiple-${(Date.now() / 1000).toFixed(0)}.json`
const file = new File([JSON.stringify(cache)], fileName, { type: 'application/json' })
saveFile(file, fileName)
}

export const getCache = (key: string) => store.getItem<any>(key)

export const setCache = async (key: string, data: any) => {
Expand Down
10 changes: 5 additions & 5 deletions src/utils/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,15 @@ export const getIconContainer = (Component, cb, isModalOpen = false) => (
<IconContainer onClick={cb}>{isModalOpen ? <CloseIcon /> : <Component />}</IconContainer>
)

export const getVotersSnapshots = async (voters, endDate) => {
export const getVotersSnapshots = async voters => {
const endDate = getUnixTime(new Date())
const snapshotsCache = (await getCache('accounts-snapshots-cache')) || {}

const requiredVoters = voters.reduce((acc, voter) => {
const { lastUpdate, data } = snapshotsCache[voter] || {}
// Do not update for 1/2 hour
if (!lastUpdate || getUnixTime(addMinutes(fromUnixTime(lastUpdate), 30)) < endDate) {
return [...acc, { voter, fromDate: lastUpdate, endDate }]
return [...acc, { voter, endDate }]
}

return acc
Expand All @@ -195,12 +196,11 @@ export const getVotersSnapshots = async (voters, endDate) => {
requiredVoters.map(required => Promise.all([required.voter, getVoterBalancesFrom(required)])),
)
const newData = requiredBalances.reduce((acc: any, [voter, snapshots]: Array<any>) => {
const { data } = snapshotsCache[voter] || {}
return {
...acc,
[voter]: {
lastUpdate: endDate,
data: [...snapshots, ...(data ? data : [])], // this order matters since it's expected to ordered by timestamp desc
data: snapshots,
},
}
}, {})
Expand Down Expand Up @@ -504,7 +504,7 @@ export const getPollsBalances = async polls => {
new Set(polls.flatMap(poll => poll.votes.reduce((voters, v) => [...voters, v.voter], []))),
)

const allBalances = await getVotersSnapshots(allVoters, getUnixTime(now))
const allBalances = await getVotersSnapshots(allVoters)
return allBalances.flat().reduce((lookup, snapshot: any) => {
const account = snapshot.account.address
const balances = lookup[account] || []
Expand Down

0 comments on commit f88fe08

Please sign in to comment.