Skip to content

Commit

Permalink
add listing voting and unvoting amount of TOMO in 24 hours
Browse files Browse the repository at this point in the history
  • Loading branch information
pqv199x committed May 6, 2019
1 parent 9796e35 commit 4cd9e2e
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 69 deletions.
56 changes: 56 additions & 0 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 @@ -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({
Expand Down
Loading

0 comments on commit 4cd9e2e

Please sign in to comment.