Skip to content

Commit

Permalink
fix tally for running proposals (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
faboweb authored and colw committed Oct 30, 2019
1 parent 5832099 commit 502c485
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 20 deletions.
17 changes: 12 additions & 5 deletions lib/cosmosV0-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,20 +202,27 @@ class CosmosV0API extends RESTDataSource {
return this.reducers.proposalReducer(
this.networkId,
proposal,
{}, //TODO also add tally to overview when we need it
totalBondedTokens
)
})
return _.orderBy(proposals, 'id', 'desc')
}

async getProposalById({ proposalId }) {
const response = await this.query(`gov/proposals/${proposalId}`)
const { bonded_tokens: totalBondedTokens } = await this.query(
'/staking/pool'
)
const [
proposal,
{ bonded_tokens: totalBondedTokens },
tally
] = await Promise.all([
this.query(`/gov/proposals/${proposalId}`),
this.query('/staking/pool'),
this.query(`/gov/proposals/${proposalId}/tally`)
])
return this.reducers.proposalReducer(
this.networkId,
response,
proposal,
tally,
totalBondedTokens
)
}
Expand Down
37 changes: 24 additions & 13 deletions lib/reducers/cosmosV0-reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ function proposalEndTime(proposal) {
}
}

function proposalFinalized(proposal) {
return ['Passed', 'Rejected'].indexOf(proposal.proposal_status) !== -1
}

function atoms(nanoAtoms) {
return BigNumber(nanoAtoms)
.div(1000000)
Expand Down Expand Up @@ -82,25 +86,32 @@ function getDeposit(proposal, bondDenom) {

function getTotalVotePercentage(proposal, totalBondedTokens, totalVoted) {
// for passed proposals we can't calculate the total voted percentage, as we don't know the totalBondedTokens in the past
if (['Passed', 'Rejected'].indexOf(proposal.proposal_status) !== -1) return -1
if (totalVoted.eq(0)) return 0
if (proposalFinalized(proposal)) return -1
if (BigNumber(totalVoted).eq(0)) return 0
if (!totalBondedTokens) return -1
return BigNumber(totalBondedTokens)
.div(atoms(totalVoted))
.toNumber()
}

function tallyReducer(proposal, totalBondedTokens) {
const totalVoted = BigNumber(proposal.final_tally_result.yes)
.plus(proposal.final_tally_result.no)
.plus(proposal.final_tally_result.abstain)
.plus(proposal.final_tally_result.no_with_veto)
function tallyReducer(proposal, tally, totalBondedTokens) {
// if the proposal is out of voting, use the final result for the tally
if (proposalFinalized(proposal)) {
tally = proposal.final_tally_result
}

const totalVoted = atoms(
BigNumber(tally.yes)
.plus(tally.no)
.plus(tally.abstain)
.plus(tally.no_with_veto)
)

return {
yes: atoms(proposal.final_tally_result.yes),
no: atoms(proposal.final_tally_result.no),
abstain: atoms(proposal.final_tally_result.abstain),
veto: atoms(proposal.final_tally_result.no_with_veto),
yes: atoms(tally.yes),
no: atoms(tally.no),
abstain: atoms(tally.abstain),
veto: atoms(tally.no_with_veto),
total: totalVoted,
totalVotedPercentage: getTotalVotePercentage(
proposal,
Expand All @@ -110,7 +121,7 @@ function tallyReducer(proposal, totalBondedTokens) {
}
}

function proposalReducer(networkId, proposal, totalBondedTokens) {
function proposalReducer(networkId, proposal, tally, totalBondedTokens) {
return {
networkId,
id: Number(proposal.proposal_id),
Expand All @@ -121,7 +132,7 @@ function proposalReducer(networkId, proposal, totalBondedTokens) {
status: proposal.proposal_status,
statusBeginTime: proposalBeginTime(proposal),
statusEndTime: proposalEndTime(proposal),
tally: tallyReducer(proposal, totalBondedTokens),
tally: tallyReducer(proposal, tally, totalBondedTokens),
deposit: getDeposit(proposal, 'uatom') // TODO use denom lookup
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/reducers/cosmosV2-reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const {
expectedRewardsPerToken
} = cosmosV0Reducers

function proposalReducer(networkId, proposal, totalBondedTokens) {
function proposalReducer(networkId, proposal, tally, totalBondedTokens) {
return {
networkId,
id: Number(proposal.id),
Expand All @@ -20,7 +20,7 @@ function proposalReducer(networkId, proposal, totalBondedTokens) {
status: proposal.proposal_status,
statusBeginTime: proposalBeginTime(proposal),
statusEndTime: proposalEndTime(proposal),
tally: tallyReducer(proposal, totalBondedTokens),
tally: tallyReducer(proposal, tally, totalBondedTokens),
deposit: getDeposit(proposal, 'stake') // TODO use denom lookup + use network config
}
}
Expand Down

0 comments on commit 502c485

Please sign in to comment.