Skip to content
This repository has been archived by the owner on Aug 23, 2024. It is now read-only.

Commit

Permalink
Fixes bignumber issues in results calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
emmdim committed Mar 28, 2024
1 parent a03f0bb commit cf31c0b
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions components/blocks/question-results.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const QuestionResults = (props: QuestionsResultsProps) => {
const isMobile = useIsMobile();
// const [showResults, setSetShowResults] = useState(false);

const { votesWeight, liveResults, election } = useExtendedElection();
const { liveResults, election } = useExtendedElection();
const status = election.status;

let sortedChoices: ChoiceResult[];
Expand Down Expand Up @@ -77,11 +77,10 @@ export const QuestionResults = (props: QuestionsResultsProps) => {
const decimals = (election.meta as any)?.token?.decimals || 0;

Check warning on line 77 in components/blocks/question-results.tsx

View workflow job for this annotation

GitHub Actions / node-tests-and-build

Unexpected any. Specify a different type

Check warning on line 77 in components/blocks/question-results.tsx

View workflow job for this annotation

GitHub Actions / node-tests-and-build

Unexpected any. Specify a different type

//Calculate voting weight
let totalWeightCount = 0;
sortedChoices.map((choice: ChoiceResult, index: number) => {
totalWeightCount = BigInt(totalWeightCount) + BigInt(choice.votes);
})
totalWeightCount = BigNumber.from(totalWeightCount);
let totalWeightCount = BigNumber.from(0);
sortedChoices.map((choice: ChoiceResult) => {
totalWeightCount = totalWeightCount.add(choice.votes);
});

return (
<Card isMobile={isMobile}>
Expand Down Expand Up @@ -201,7 +200,7 @@ const getPercent = (votes: BigNumber, resultsWeight: BigNumber): number => {

// used to avoid losing decimal precision
const ratio = votes.mul(10000);
return ratio.div(resultsWeight)/100;
return ratio.div(resultsWeight).toNumber() / 100;
};
const getStringPercent = (percent: number): string => {
const decimal = percent % 1;
Expand Down

0 comments on commit cf31c0b

Please sign in to comment.