From a6187433268836f24956a52681eb5ee9adae5f27 Mon Sep 17 00:00:00 2001 From: Vu Pham Date: Sat, 4 May 2019 14:55:51 +0700 Subject: [PATCH] add listing voting and unvoting amount of TOMO in 24 hours --- apis/candidates.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++ package-lock.json | 2 +- package.json | 2 +- 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/apis/candidates.js b/apis/candidates.js index bc794d1e..6faa8872 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') @@ -318,6 +319,61 @@ router.get('/crawlStatus', async function (req, res, next) { } }) +router.get('/votingDaily', [], async function (req, res, next) { + try { + let today = new Date() + let current = new Date() + let yesterday = new Date(current.setDate(current.getDate() - 1)) + // voting amount + const a = await db.Transaction.find({ + event: 'Vote', + createdAt: { + $gte: yesterday, + $lt: today + } + }) + // unvoting amount + const b = await db.Transaction.find({ + event: 'Unvote', + createdAt: { + $gte: yesterday, + $lt: today + } + }) + // couting gaining and lossing + const map1 = new Map() + a.map(async (m) => { + const amount = new BigNumber(m.capacity).div(10 ** 18).toNumber() + if (map1.has(m.candidate)) { + const storedAmount = map1.get(m.candidate) + map1.set(m.candidate, storedAmount + amount) + } else { + map1.set(m.candidate, amount) + } + }) + + b.map(async (m) => { + const amount = -(new BigNumber(m.capacity).div(10 ** 18).toNumber()) + if (map1.has(m.candidate)) { + const storedAmount = map1.get(m.candidate) + map1.set(m.candidate, storedAmount + amount) + } else { + map1.set(m.candidate, amount) + } + }) + + // sort by value + const mapSort1 = new Map([...map1.entries()].sort((a, b) => b[1] - a[1])) + const result = {} + for (let [key, val] of mapSort1.entries()) { + result[key] = val + } + return res.send(result) + } catch (error) { + return next(error) + } +}) + router.get('/:candidate', async function (req, res, next) { let address = (req.params.candidate || '').toLowerCase() let candidate = (await db.Candidate.findOne({ diff --git a/package-lock.json b/package-lock.json index d39d1288..afd0cb40 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16990,4 +16990,4 @@ } } } -} +} \ No newline at end of file diff --git a/package.json b/package.json index 0d2a3e8e..19c17a04 100644 --- a/package.json +++ b/package.json @@ -141,4 +141,4 @@ "category": "Network" } } -} +} \ No newline at end of file