From 52a70f53650c33721bcecc30b4ff2f3b5d740463 Mon Sep 17 00:00:00 2001 From: Barry Gitarts Date: Tue, 17 Jul 2018 16:02:32 -0400 Subject: [PATCH] fix NaN value after setting to zero localize state --- app/components/simple-voting/PollsList.js | 13 ------------- app/utils/polls.js | 20 ++++++++++++++++---- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/app/components/simple-voting/PollsList.js b/app/components/simple-voting/PollsList.js index a26c4cc..8f35f04 100644 --- a/app/components/simple-voting/PollsList.js +++ b/app/components/simple-voting/PollsList.js @@ -11,7 +11,6 @@ import Slide from '@material-ui/core/Slide'; import Typography from '@material-ui/core/Typography'; import Slider from '@material-ui/lab/Slider'; import PollManager from 'Embark/contracts/PollManager'; -import MiniMeTokenInterface from 'Embark/contracts/MiniMeTokenInterface'; import IconButton from '@material-ui/core/IconButton'; import CloseIcon from '@material-ui/icons/Close'; import web3 from "Embark/web3" @@ -104,21 +103,9 @@ class Poll extends PureComponent { } componentDidMount() { - this.getBalance(); this.getVote(); } - getBalance() { - const { fromWei } = web3.utils; - const { appendToPoll, idPoll } = this.props; - MiniMeTokenInterface.options.address = this.props._token; - MiniMeTokenInterface.methods.balanceOfAt(web3.eth.defaultAccount, this.props._startBlock - 1) - .call() - .then(balance => { - appendToPoll(idPoll, {balance: fromWei(balance)}) - }); - } - getVote() { const { fromWei } = web3.utils; const { idPoll } = this.props; diff --git a/app/utils/polls.js b/app/utils/polls.js index 875bbe5..0e39a0b 100644 --- a/app/utils/polls.js +++ b/app/utils/polls.js @@ -1,16 +1,28 @@ +import web3 from "Embark/web3" +import MiniMeTokenInterface from 'Embark/contracts/MiniMeTokenInterface'; + const excluded = { PROPER_LIGHT_CLIENT_SUPPORT : 3, IMPLEMENT_SECURITY_PRACTICES : 14, SHIP_1_0 : 16 }; -export const getPolls = (number, pollMethod) => { +export const getBalance = async (idPoll, token, startBlock) => { + const { fromWei } = web3.utils; + const { balanceOfAt } = MiniMeTokenInterface.methods; + MiniMeTokenInterface.options.address = token; + const balance = await balanceOfAt(web3.eth.defaultAccount, startBlock - 1).call(); + return fromWei(balance); +} + +export const getPolls = async (number, pollMethod) => { const polls = []; for (let i = number-1; i >= 0; i--) { - const poll = pollMethod(i).call(); - polls.push(poll); + const poll = await pollMethod(i).call(); + const balance = await getBalance(i, poll._token, poll._startBlock); + polls.push({ ...poll, balance }); } - return Promise.all(polls.reverse()); + return polls.reverse(); } const excludedPolls = new Set(Object.values(excluded));