snt-voting/app/utils/polls.js

40 lines
1.4 KiB
JavaScript
Raw Normal View History

import web3 from "Embark/web3"
import MiniMeTokenInterface from 'Embark/contracts/MiniMeTokenInterface';
import PollManager from 'Embark/contracts/PollManager';
import DappToken from 'Embark/contracts/DappToken';
2018-07-16 09:20:42 -04:00
const excluded = {
2018-09-25 20:45:33 -04:00
// PROPER_LIGHT_CLIENT_SUPPORT : 3,
2018-07-16 09:20:42 -04:00
};
2018-07-13 10:15:27 -04:00
export const getBalance = async (startBlock) => {
const { fromWei } = web3.utils;
const { balanceOfAt } = DappToken.methods;
const balance = await balanceOfAt(web3.eth.defaultAccount, startBlock - 1).call();
// TODO: use decimals
return fromWei(balance);
}
export const getVote = async(idPoll) => {
const { fromWei } = web3.utils;
const votes = await PollManager.methods.getVote(idPoll, web3.eth.defaultAccount).call();
return votes.map(el => Math.floor(Math.sqrt(parseInt(fromWei(el)))));
}
const fetchPollData = async (index, pollMethod) => {
2018-10-30 16:53:53 -04:00
const poll = await pollMethod(index).call({from: web3.eth.defaultAccount});
const blockInfo = await web3.eth.getBlock(poll._startBlock);
2018-10-27 13:12:35 -04:00
return { ...poll, idPoll: index, blockInfo };
}
export const getPolls = (number, pollMethod) => {
const polls = [];
2018-11-06 13:49:25 -04:00
for (let i = number-1; i >= 0; i--) {
polls.push(fetchPollData(i, pollMethod));
}
return Promise.all(polls.reverse());
2018-07-13 10:15:27 -04:00
}
2018-07-16 09:20:42 -04:00
const excludedPolls = new Set(Object.values(excluded));
const exclusionFilter = (poll, idx) => !excludedPolls.has(idx);
export const omitPolls = polls => polls.filter(exclusionFilter);