From 40405cc04227fd5a64cad26d40602f6ee49cc392 Mon Sep 17 00:00:00 2001 From: Vu Pham Date: Tue, 7 May 2019 17:48:08 +0700 Subject: [PATCH 1/2] view mode reward, voting percentage --- apis/candidates.js | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/apis/candidates.js b/apis/candidates.js index bc794d1e..7cd3f5eb 100644 --- a/apis/candidates.js +++ b/apis/candidates.js @@ -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') @@ -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 @@ -126,8 +129,38 @@ router.get('/masternodes', [ status: { $nin: ['RESIGNED', 'PROPOSED'] } }).sort(sort).limit(limit).skip(skip).lean().exec() + let map = null + + switch (view) { + case 'reward': + 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 + default: + break + } + return res.json({ - items: candidates, + items: map || candidates, activeCandidates: await activeCandidates || 0, totalSlashed: await totalSlashed, totalResigned: await totalResigned, From c5c621306f21883d94d4e3446661520f1ed796d6 Mon Sep 17 00:00:00 2001 From: Vu Pham Date: Wed, 8 May 2019 17:50:58 +0700 Subject: [PATCH 2/2] add balance view mode --- apis/candidates.js | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/apis/candidates.js b/apis/candidates.js index 7cd3f5eb..034579e0 100644 --- a/apis/candidates.js +++ b/apis/candidates.js @@ -133,6 +133,27 @@ router.get('/masternodes', [ 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: { @@ -155,8 +176,6 @@ router.get('/masternodes', [ return c })) break - default: - break } return res.json({