Skip to content

Commit

Permalink
Merge pull request #408 from protofire/staked-voting-mkr
Browse files Browse the repository at this point in the history
Dashboard - Staked MKR histogram - Fix staked mkr calc
  • Loading branch information
cmalfesi authored May 19, 2020
2 parents a825836 + eb54422 commit 1c32269
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ REACT_APP_GRAPH_HTTP='https://api.thegraph.com/subgraphs/name/protofire/makerdao
REACT_APP_MKR_GRAPH_HTTP='https://api.thegraph.com/subgraphs/name/protofire/mkr-registry'
REACT_APP_ETHERSCAN_API_KEY='XQ2QTEM7H4KX7AQTE9JWXD3HWTTZ46TTU9'
REACT_APP_LAST_CACHE_UPDATE='1587733935'
REACT_APP_HOME_DATA_TTL='5' # IN MINUTES
REACT_APP_HOME_DATA_TTL='5'
6 changes: 3 additions & 3 deletions netlify.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@
REACT_APP_MKR_GRAPH_HTTP='https://api.thegraph.com/subgraphs/name/protofire/mkr-registry'
REACT_APP_ETHERSCAN_API_KEY='XQ2QTEM7H4KX7AQTE9JWXD3HWTTZ46TTU9'
REACT_APP_LAST_CACHE_UPDATE='1587733935'
REACT_APP_HOME_DATA_TTL='5' # IN MINUTES
REACT_APP_HOME_DATA_TTL='5'

[context.branch-deploy.environment]
REACT_APP_GRAPH_HTTP = "https://api.thegraph.com/subgraphs/name/protofire/makerdao-governance"
REACT_APP_GRAPH_WS = "wss://api.thegraph.com/subgraphs/name/protofire/makerdao-governance"
REACT_APP_MKR_GRAPH_HTTP='https://api.thegraph.com/subgraphs/name/protofire/mkr-registry'
REACT_APP_ETHERSCAN_API_KEY='XQ2QTEM7H4KX7AQTE9JWXD3HWTTZ46TTU9'
REACT_APP_LAST_CACHE_UPDATE='1587733935'
REACT_APP_HOME_DATA_TTL='5' # IN MINUTES
REACT_APP_HOME_DATA_TTL='5'

[context.deploy-preview.environment]
REACT_APP_GRAPH_HTTP = "https://api.thegraph.com/subgraphs/name/protofire/makerdao-governance"
REACT_APP_GRAPH_WS = "wss://api.thegraph.com/subgraphs/name/protofire/makerdao-governance"
REACT_APP_MKR_GRAPH_HTTP='https://api.thegraph.com/subgraphs/name/protofire/mkr-registry'
REACT_APP_ETHERSCAN_API_KEY='XQ2QTEM7H4KX7AQTE9JWXD3HWTTZ46TTU9'
REACT_APP_LAST_CACHE_UPDATE='1587733935'
REACT_APP_HOME_DATA_TTL='5' # IN MINUTES
REACT_APP_HOME_DATA_TTL='5'
98 changes: 66 additions & 32 deletions src/components/Home/helpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,23 @@ export const getRecentPolls = polls => polls.sort((a, b) => Number(b.startDate)
export const getStakedMkrData = (data: any, time: string) => {
const periods = periodsMap[time]()
const mkrEvents = [data.mint, data.burn]
const votingEvents = [data.lock, data.vote, data.free]

// Some LOCK/FREE events occurs in the same block/timestamp that VOTE, when need LOCK/FREE before VOTE
const votingEvents = [data.lock, data.free, data.vote].flat().sort((a, b) => {
if (a.timestamp > b.timestamp) {
return 1
}

if (a.timestamp < b.timestamp) {
return -1
}

if (a.type === 'LOCK' || (a.type === 'FREE' && b.type)) {
return -1
}

return 1
})

// Calculate total MKR supply before first period
let totalSupply = calculateMkrSupply(
Expand All @@ -83,7 +99,7 @@ export const getStakedMkrData = (data: any, time: string) => {

// Calculate total MKR staked/voting before first period
let { totalStaked, totalVoting, voters } = calculateVotingMkr(
votingEvents.map(events => events.filter(({ timestamp }) => timestamp < periods[0].from)),
votingEvents.filter(({ timestamp }) => timestamp < periods[0].from),
)

return periods.map(period => {
Expand All @@ -95,7 +111,7 @@ export const getStakedMkrData = (data: any, time: string) => {

// Calculate total MKR staked/voting in the period
;({ totalStaked, totalVoting, voters } = calculateVotingMkr(
votingEvents.map(events => events.filter(({ timestamp }) => timestamp > period.from && timestamp <= period.to)),
votingEvents.filter(({ timestamp }) => timestamp > period.from && timestamp <= period.to),
{ totalStaked, totalVoting, voters },
))

Expand Down Expand Up @@ -124,41 +140,59 @@ function calculateVotingMkr(
votingEvents: any[],
previousValue = { totalStaked: new BigNumber(0), totalVoting: new BigNumber(0), voters: {} },
): { totalStaked: BigNumber; totalVoting: BigNumber; voters: { [addr: string]: BigNumber } } {
return votingEvents
.reduce((all, events) => [...all, ...events], [])
.sort((a, b) => (a.timestamp > b.timestamp ? 1 : -1))
.reduce(({ totalStaked, totalVoting, voters }, { type, wad, sender, yays }) => {
const stake = voters[sender] || new BigNumber(0)
return votingEvents.reduce(({ totalStaked, totalVoting, voters }, { type, wad, sender, yays }) => {
const stake = voters[sender] || new BigNumber(0)

if (type === 'LOCK') {
return {
totalStaked: totalStaked.plus(wad),
totalVoting: stake.isZero() ? totalVoting : totalVoting.plus(wad),
voters: stake.isZero() ? voters : { ...voters, [sender]: stake.plus(wad) },
}
} else if (type === 'VOTE') {
return {
totalStaked,
totalVoting: stake.isZero()
? yays.length === 0
? totalVoting.minus(wad)
: totalVoting.plus(wad)
: totalVoting,
voters: { ...voters, [sender]: yays.length ? BigNumber.max(new BigNumber(0), stake, wad) : new BigNumber(0) },
if (type === 'LOCK') {
const tS = totalStaked.plus(wad)
const tV = stake.isZero() ? totalVoting : totalVoting.plus(wad)
const vs = { ...voters, [sender]: !stake.isZero() ? stake.plus(wad) : new BigNumber(0) }

return {
totalStaked: totalStaked.plus(wad),
totalVoting: stake.isZero() ? totalVoting : totalVoting.plus(wad),
voters: stake.isZero() ? voters : { ...voters, [sender]: stake.plus(wad) },
}
} else if (type === 'VOTE') {
let newTotalVoting = totalVoting

// first time voting or voting for something again after removing its vote
if (stake.isZero()) {
// but it does has to be voting something
if (yays.length !== 0) {
newTotalVoting = totalVoting.plus(wad)
}
} else if (type === 'FREE') {
return {
totalStaked: totalStaked.minus(wad),
totalVoting: stake.isZero() ? totalVoting : totalVoting.minus(wad),
voters: stake.isZero() ? voters : { ...voters, [sender]: stake.minus(wad) },
}

// already voting
if (!stake.isZero()) {
// voting for somthing else
if (yays.length !== 0) {
newTotalVoting = totalVoting
} else {
// removing its vote
newTotalVoting = totalVoting.minus(stake)
}
}

return {
totalStaked: 0,
totalVoting: 0,
voters: 0,
totalStaked: totalStaked,
totalVoting: newTotalVoting,
voters: { ...voters, [sender]: yays.length ? new BigNumber(wad) : new BigNumber(0) },
}
}, previousValue)
} else if (type === 'FREE') {
return {
totalStaked: totalStaked.minus(wad),
totalVoting: stake.isZero() ? totalVoting : totalVoting.minus(wad),
voters: stake.isZero() ? voters : { ...voters, [sender]: stake.minus(wad) },
}
}
return {
totalStaked,
totalVoting,
voters,
}
}, previousValue)
}

export const getVotersVsMkrData = (data: Array<any>, mkrLockFree: Array<any>, time: string): Array<any> => {
Expand Down

0 comments on commit 1c32269

Please sign in to comment.