From b4cb66ec713b92efc7d4c8c7f5af4b3b448db076 Mon Sep 17 00:00:00 2001 From: Lisandro Corbalan Date: Mon, 11 May 2020 12:35:32 -0300 Subject: [PATCH] Filter account snapshots after poll ended --- src/components/PollDetails/data.ts | 6 +++--- src/components/PollDetails/helpers.tsx | 2 +- src/utils/cache.ts | 19 +++++++++++++++++++ src/utils/index.tsx | 10 +++++----- 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/components/PollDetails/data.ts b/src/components/PollDetails/data.ts index 2c42962..189c8e6 100644 --- a/src/components/PollDetails/data.ts +++ b/src/components/PollDetails/data.ts @@ -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 @@ -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 { diff --git a/src/components/PollDetails/helpers.tsx b/src/components/PollDetails/helpers.tsx index 0d0d573..0238ab9 100644 --- a/src/components/PollDetails/helpers.tsx +++ b/src/components/PollDetails/helpers.tsx @@ -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] || [] diff --git a/src/utils/cache.ts b/src/utils/cache.ts index 3e9b5da..1630265 100644 --- a/src/utils/cache.ts +++ b/src/utils/cache.ts @@ -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) => { + 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(key) export const setCache = async (key: string, data: any) => { diff --git a/src/utils/index.tsx b/src/utils/index.tsx index f5c3808..8b9f5dd 100644 --- a/src/utils/index.tsx +++ b/src/utils/index.tsx @@ -178,14 +178,15 @@ export const getIconContainer = (Component, cb, isModalOpen = false) => ( {isModalOpen ? : } ) -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 @@ -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) => { - 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, }, } }, {}) @@ -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] || []