Skip to content

Commit

Permalink
Merge pull request #217 from DAOmasons/trailingDecimals
Browse files Browse the repository at this point in the history
Trailing decimals
  • Loading branch information
jordanlesich authored Jun 14, 2024
2 parents dd8fea3 + 67e8e4b commit ad102a7
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
7 changes: 4 additions & 3 deletions src/components/voting/VoteCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import { CondensedChoiceData } from '../../pages/Vote';
import { GsVoter } from '../../queries/getVoters';
import { useMemo } from 'react';
import { AddressAvatar } from '../AddressAvatar';
import { Address, formatEther } from 'viem';
import { Address } from 'viem';
import { formatBigIntPercentage } from '../../utils/helpers';
import { IconChevronDown, IconChevronUp } from '@tabler/icons-react';
import classes from '../feed/FeedStyles.module.css';
import { formatBalance } from '../../types/common';

export const VoteCard = ({
voter,
Expand Down Expand Up @@ -91,6 +92,7 @@ const ShipChoiceVoteBar = ({
tokenSymbol?: string;
didVote?: boolean;
}) => {
const voteAmountDisplay = formatBalance(voteAmount);
const votePercentage = formatBigIntPercentage(voteAmount, totalVotes);
return (
<Box mb="md">
Expand All @@ -104,8 +106,7 @@ const ShipChoiceVoteBar = ({
mb={2}
/>
<Text fz="xs">
{votePercentage}% Voted ({formatEther(voteAmount)}){' '}
{tokenSymbol || ''}
{votePercentage}% Voted ({voteAmountDisplay}) {tokenSymbol || ''}
</Text>
</Box>
</Group>
Expand Down
12 changes: 7 additions & 5 deletions src/components/voting/VoteResultsPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import { ShipsCardUI } from '../../types/ui';
import { useMemo } from 'react';
import { MainSection, PageTitle } from '../../layout/Sections';
import { formatBigIntPercentage } from '../../utils/helpers';
import { formatEther } from 'viem';
import { CondensedChoiceData } from '../../pages/Vote';
import { getContestVoters } from '../../queries/getVoters';
import { VoteCard } from './VoteCard';
import { useQuery } from '@tanstack/react-query';
import { formatBalance } from '../../types/common';

export const VoteResultsPanel = ({ ships }: { ships: ShipsCardUI[] }) => {
const { contest, userVotes, tokenData } = useVoting();
Expand Down Expand Up @@ -94,7 +94,8 @@ export const VoteResultsPanel = ({ ships }: { ships: ShipsCardUI[] }) => {
totals?.totalUserVotes
)
: '0';
const tokenAmount = formatEther(BigInt(ship.vote?.amount || 0));

const tokenAmount = formatBalance(BigInt(ship.vote?.amount || 0));

return (
<Box key={`total_v_${ship.id}`}>
Expand All @@ -116,7 +117,7 @@ export const VoteResultsPanel = ({ ships }: { ships: ShipsCardUI[] }) => {
<Text fz="sm" component="span" fw={600}>
Total:{' '}
</Text>
{formatEther(totals?.totalUserVotes || 0n)}{' '}
{formatBalance(totals?.totalUserVotes || 0n)}{' '}
{tokenData.tokenSymbol}
</Text>
</Stack>
Expand All @@ -132,7 +133,8 @@ export const VoteResultsPanel = ({ ships }: { ships: ShipsCardUI[] }) => {
totals?.totalVotes
)
: '0';
const tokenAmount = formatEther(BigInt(ship.choice?.voteTally));
const tokenAmount = formatBalance(BigInt(ship.choice?.voteTally));

return (
<Box key={`total_v_${ship.id}`}>
<Group gap="xs" mb="sm">
Expand All @@ -152,7 +154,7 @@ export const VoteResultsPanel = ({ ships }: { ships: ShipsCardUI[] }) => {
<Text fz="sm" component="span" fw={600}>
Total:{' '}
</Text>
{formatEther(totals?.totalVotes || 0n)} {tokenData.tokenSymbol}
{formatBalance(totals?.totalVotes || 0n)} {tokenData.tokenSymbol}
</Text>
</Stack>
</Flex>
Expand Down
2 changes: 1 addition & 1 deletion src/constants/addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const ADDR_TESTNET: Record<string, Address> = {
GM_FACTORY: '0x14e32E7893D6A1fA5f852d8B2fE8c57A2aB670ba',
GS_FACTORY: '0x8D994BEef251e30C858e44eCE3670feb998CA77a',
HATS_POSTER: '0x4F0dc1C7d91d914d921F3C9C188F4454AE260317',
VOTE_CONTEST: '0x7E1E70ef71A7EA8a3328822bA4410A82A73f575d',
VOTE_CONTEST: '0x3d06D95026696826B507D25cfBb86E4457F34eC5',
} as const;

export const ADDR_PROD: Record<string, Address> = {
Expand Down
7 changes: 7 additions & 0 deletions src/types/common.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Arbitrary JSON type to ensure that non-JSON serializable types are not used

import { formatEther } from 'viem';

export type Json =
| string
| number
Expand Down Expand Up @@ -93,3 +95,8 @@ export enum VotingStage {
Finalized,
Unknown,
}

export const formatBalance = (balance?: bigint) => {
if (!balance || typeof balance !== 'bigint') return 0;
return Number(Number(formatEther(balance)).toFixed(2));
};

0 comments on commit ad102a7

Please sign in to comment.