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]listing voting and unvoting amount of TOMO in 24 hours #634

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
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
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,4 @@
"category": "Network"
}
}
}
}