Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.0]add capcity history, vote percentage #627

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions apis/candidates.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const logger = require('../helpers/logger')
const { check, validationResult, query } = require('express-validator/check')
const uuidv4 = require('uuid/v4')
const urljoin = require('url-join')
const BigNumber = require('bignumber.js')

const gas = config.get('blockchain.gas')

Expand Down Expand Up @@ -126,12 +127,35 @@ router.get('/masternodes', [
status: { $nin: ['RESIGNED', 'PROPOSED'] }
}).sort(sort).limit(limit).skip(skip).lean().exec()

const votedAmount = await db.Candidate.aggregate([
{
$match: {
status: { $ne: 'RESIGNED' }
}
},
{
$group: {
_id: null, sum: { $sum: '$capacityNumber' }
}
}
])

let map = candidates.map(async c => {
if (votedAmount) {
c.votePercentage = new BigNumber(c.capacityNumber)
.multipliedBy(100).div(votedAmount[0].sum).toString(10)
}
return c
})
let ret = await Promise.all(map)

return res.json({
items: candidates,
items: ret,
activeCandidates: await activeCandidates || 0,
totalSlashed: await totalSlashed,
totalResigned: await totalResigned,
totalProposed: await totalProposed
totalProposed: await totalProposed,
votedAmount
})
} catch (e) {
return next(e)
Expand Down
2 changes: 2 additions & 0 deletions crawl.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,8 @@ async function watchNewBlock (n) {
db.Rank.updateOne({ candidate: c.candidate, epoch: latestEpoch }, {
epoch: latestEpoch,
candidate: c.candidate,
capacity: c.capacity,
capacityNumber: c.capacityNumber,
rank: i + 1,
epochCreatedAt: moment.unix(block.timestamp).utc()
}, { upsert: true }).then(() => { return true })
Expand Down
2 changes: 2 additions & 0 deletions models/mongodb/rank.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ var Schema = mongoose.Schema
var Rank = new Schema({
candidate: Number,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lacks indexes (epoch, candidate)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of Rank table is in v1.4.0, so I will prepare another PR to update the table

rank: Number,
capacity: String,
capacityNumber: Number,
epoch: Number,
epochCreatedAt: Date
}, { timestamps: true })
Expand Down