Skip to content

Commit

Permalink
add correct logic to xtz votes dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiolalombardim committed Feb 13, 2024
1 parent 0692e81 commit ca36444
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
13 changes: 4 additions & 9 deletions src/modules/lite/explorer/components/VoteDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
calculateChoiceTotal,
calculateProposalTotal,
calculateWeight,
calculateWeightXTZ,
calculateXTZTotal,
getTotalVoters,
getTreasuryPercentage,
Expand Down Expand Up @@ -74,13 +75,6 @@ export const VoteDetails: React.FC<{
setOpen(true)
}

const calculateWeightXTZ = (choice: Choice) => {
if (choice && choice.walletAddresses.length > 0) {
return (choice.walletAddresses.length / totalVoters) * 100
}
return 0
}

const handleClose = () => {
setOpen(false)
}
Expand Down Expand Up @@ -152,7 +146,7 @@ export const VoteDetails: React.FC<{
)
.dp(2, 1)
.toNumber()
: calculateWeightXTZ(choice)
: calculateWeightXTZ(choice, totalVoters)
}
variant="determinate"
/>
Expand All @@ -167,7 +161,7 @@ export const VoteDetails: React.FC<{
)
.dp(2, 1)
.toString()
: numbro(calculateWeightXTZ(choice)).format(formatConfig)}
: numbro(calculateWeightXTZ(choice, totalVoters)).format(formatConfig)}
%
</Typography>
</Grid>
Expand Down Expand Up @@ -227,6 +221,7 @@ export const VoteDetails: React.FC<{
symbol={isXTZ ? "XTZ" : tokenData?.symbol ? tokenData?.symbol : ""}
choices={votes}
open={open}
isXTZ={isXTZ}
handleClose={handleClose}
/>
</GraphicsContainer>
Expand Down
21 changes: 14 additions & 7 deletions src/modules/lite/explorer/components/VotesDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import React, { useEffect, useState } from "react"
import React from "react"
import {
Dialog,
DialogActions,
DialogContent,
DialogContentText,
DialogTitle,
styled,
Typography,
Button,
Expand All @@ -15,9 +12,10 @@ import {
import { toShortAddress } from "services/contracts/utils"
import { FileCopyOutlined } from "@material-ui/icons"
import { Choice } from "models/Choice"
import { formatByDecimals, getTotalVoters } from "services/lite/utils"
import { calculateWeightXTZ, formatByDecimals, getTotalVoters } from "services/lite/utils"
import { useNotification } from "modules/common/hooks/useNotification"
import { ResponsiveDialog } from "modules/explorer/components/ResponsiveDialog"
import numbro from "numbro"

const CustomContent = styled(DialogContent)(({ theme }) => ({
padding: 0,
Expand Down Expand Up @@ -60,13 +58,19 @@ const Row = styled(Grid)(({ theme }) => ({
}
}))

const formatConfig = {
mantissa: 4,
trimMantissa: true
}

export const VotesDialog: React.FC<{
open: boolean
handleClose: any
choices: Choice[]
symbol: string
decimals: string
}> = ({ open, handleClose, choices, symbol, decimals }) => {
isXTZ: boolean
}> = ({ open, handleClose, choices, symbol, decimals, isXTZ }) => {
const descriptionElementRef = React.useRef<HTMLElement>(null)
const openNotification = useNotification()

Expand Down Expand Up @@ -143,7 +147,10 @@ export const VotesDialog: React.FC<{
>
<Typography color="textPrimary" variant="body1">
{" "}
{formatByDecimals(choice.balanceAtReferenceBlock, decimals)} {symbol}{" "}
{!isXTZ
? formatByDecimals(choice.balanceAtReferenceBlock, decimals)
: numbro(formatByDecimals(choice.balanceAtReferenceBlock, "6")).format(formatConfig)}{" "}
{symbol}{" "}
</Typography>
</Grid>
</Row>
Expand Down
7 changes: 7 additions & 0 deletions src/services/lite/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ export const calculateChoiceTotal = (choice_voters: any[], decimals: any) => {
return result
}

export const calculateWeightXTZ = (choice: Choice, totalVoters: number) => {
if (choice && choice.walletAddresses.length > 0) {
return (choice.walletAddresses.length / totalVoters) * 100
}
return 0
}

export const calculateXTZTotal = (choices: Choice[] | undefined) => {
let totalVoters = 0
if (choices) {
Expand Down

0 comments on commit ca36444

Please sign in to comment.