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.0] View mode reward, stability, balance/ candidate list #638

Closed
wants to merge 2 commits into from
Closed
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
58 changes: 55 additions & 3 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 @@ -78,13 +79,15 @@ router.get('/', [
router.get('/masternodes', [
query('limit')
.isInt({ min: 0, max: 200 }).optional().withMessage('limit should greater than 0 and less than 200'),
query('page').isNumeric({ no_symbols: true }).optional().withMessage('page must be number')
query('page').isNumeric({ no_symbols: true }).optional().withMessage('page must be number'),
query('view').isIn(['default', 'reward', 'stability', 'balance'])
.optional().withMessage('view must be one of these: default, reward, stability, balance')
], async function (req, res, next) {
const errors = validationResult(req)
if (!errors.isEmpty()) {
return next(errors.array())
}

const view = req.query.view || 'default'
let limit = (req.query.limit) ? parseInt(req.query.limit) : 200
let skip
skip = (req.query.page) ? limit * (req.query.page - 1) : 0
Expand Down Expand Up @@ -126,8 +129,57 @@ router.get('/masternodes', [
status: { $nin: ['RESIGNED', 'PROPOSED'] }
}).sort(sort).limit(limit).skip(skip).lean().exec()

let map = null

switch (view) {
case 'reward':
break
case 'balance':
map = await Promise.all(candidates.map(async (c) => {
const masternodeCount = db.Status.count({
candidate: c.candidate,
status: { $ne: 'PROPOSED' }
}) || 1
const slashedCount = db.Status.count({
candidate: c.candidate,
status: 'SLASHED'
}) || 1
const candidateCount = await db.Status.count({
candidate: c.candidate
}) || 1

c.top150Percentage = ((await masternodeCount) * 100) / candidateCount
c.slashedPercentage = ((await slashedCount) * 100) / candidateCount
return c
}))
break
default:
let totalVoted = await db.Voter.aggregate([
{
$match: {
smartContractAddress: config.get('blockchain.validatorAddress')
}
},
{
$group: {
_id: null,
totalVoted: { $sum: '$capacityNumber' }
}
}
])

map = await Promise.all(candidates.map(async (c) => {
if (totalVoted && totalVoted.length > 0) {
const total = totalVoted[0].totalVoted
c.votePercentage = (c.capacityNumber * 100) / total
}
return c
}))
break
}

return res.json({
items: candidates,
items: map || candidates,
activeCandidates: await activeCandidates || 0,
totalSlashed: await totalSlashed,
totalResigned: await totalResigned,
Expand Down