Skip to content

Commit

Permalink
Merge pull request #405 from protofire/fix/poll-detail-mkr-vpo
Browse files Browse the repository at this point in the history
Filter out accounts balance snapshots taken after the poll ended
  • Loading branch information
Lisandro authored May 11, 2020
2 parents 708b6ea + b4cb66e commit a825836
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 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
2 changes: 1 addition & 1 deletion 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 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 a825836

Please sign in to comment.