Skip to content

Commit

Permalink
Remove turnout logic from the proposal details from daos that don't s…
Browse files Browse the repository at this point in the history
…upport delegation (#668)

* Remove turnout logic from the proposal

Signed-off-by: Manank Patni <[email protected]>

* Fix share button background

Signed-off-by: Manank Patni <[email protected]>

* Fix DAO share proposal link issue

Signed-off-by: Manank Patni <[email protected]>

* Fix network switch on lite proposal

Signed-off-by: Manank Patni <[email protected]>

---------

Signed-off-by: Manank Patni <[email protected]>
  • Loading branch information
Man-Jain authored Sep 20, 2023
1 parent 388afbf commit d170fc9
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 91 deletions.
1 change: 1 addition & 0 deletions src/models/Community.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface Community {
decimals?: string
network: string
votingAddressesCount: number
daoContract?: string
}

export interface CommunityToken {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const CustomPopover = withStyles({
"marginTop": 10,
"padding": 8,
"cursor": "pointer",
"background": "#1c1f23 !important",
"&:hover": {
background: "#81feb76b !important"
}
Expand Down
32 changes: 22 additions & 10 deletions src/modules/lite/explorer/components/VoteDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import {
calculateWeight,
getTotalVoters,
getTreasuryPercentage,
getTurnoutValue,
nFormatter
} from "services/lite/utils"
import { useTezos } from "services/beacon/hooks/useTezos"
import { useCommunityToken } from "../hooks/useCommunityToken"
import { getTurnoutValue } from "services/utils/utils"
import { useTokenDelegationSupported } from "services/contracts/token/hooks/useTokenDelegationSupported"

const Container = styled(Grid)(({ theme }) => ({
background: theme.palette.primary.main,
Expand Down Expand Up @@ -59,9 +60,10 @@ export const VoteDetails: React.FC<{ poll: Poll | undefined; choices: Choice[];
const isMobile = useMediaQuery(theme.breakpoints.down("sm"))
const [open, setOpen] = React.useState(false)
const { network } = useTezos()
const [turnout, setTurnout] = useState(0)
const [turnout, setTurnout] = useState<number | null>()
const [votes, setVotes] = useState<Choice[]>([])
const tokenData = useCommunityToken(communityId)
const { data: isTokenDelegationSupported } = useTokenDelegationSupported(tokenData?.tokenAddress)

const handleClickOpen = () => {
setVotes(choices.filter(elem => elem.walletAddresses.length > 0))
Expand All @@ -76,11 +78,19 @@ export const VoteDetails: React.FC<{ poll: Poll | undefined; choices: Choice[];
}

useMemo(async () => {
if (token && token !== undefined) {
const value = await getTurnoutValue(network, token, Number(poll?.referenceBlock), getTotalVoters(choices))
setTurnout(value)
if (token && tokenData) {
const value = await getTurnoutValue(
network,
tokenData?.tokenAddress,
tokenData.tokenID,
Number(poll?.referenceBlock),
getTotalVoters(choices)
)
if (value) {
setTurnout(value)
}
}
}, [poll, choices, network, token])
}, [poll, choices, network, token, tokenData])

return (
<Container container direction="column">
Expand Down Expand Up @@ -148,9 +158,11 @@ export const VoteDetails: React.FC<{ poll: Poll | undefined; choices: Choice[];
<Typography color="textPrimary" variant="body1">
Votes
</Typography>
<Typography color="textPrimary" variant="body2">
({turnout.toFixed(2)} % Turnout)
</Typography>
{isTokenDelegationSupported && turnout ? (
<Typography color="textPrimary" variant="body1">
({turnout.toFixed(2)} % Turnout)
</Typography>
) : null}
</Grid>

<Grid
Expand All @@ -170,7 +182,7 @@ export const VoteDetails: React.FC<{ poll: Poll | undefined; choices: Choice[];
<Typography color="textPrimary" variant="body1">
{poll?.tokenSymbol}
</Typography>
<Typography color="textPrimary" variant="body2">
<Typography color="textPrimary" variant="body1">
(
{getTreasuryPercentage(
calculateProposalTotal(choices, tokenData?.decimals),
Expand Down
13 changes: 12 additions & 1 deletion src/modules/lite/explorer/pages/CommunityDetails/router.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import React from "react"
import React, { useEffect } from "react"
import { Switch, Route, Redirect, useRouteMatch } from "react-router"
import { CommunityDetails } from "./index"
import { ProposalCreator } from "../CreateProposal"
import { ProposalDetails } from "../ProposalDetails"
import { useCommunity } from "../../hooks/useCommunity"
import { useTezos } from "services/beacon/hooks/useTezos"
import { Network } from "services/beacon"

export const CommunityDetailsRouter: React.FC<{ id: any }> = ({ id }): JSX.Element => {
const match = useRouteMatch()
const community = useCommunity(id)
const { network, changeNetwork } = useTezos()

useEffect(() => {
if (community && community.network.toLowerCase() !== network.toLowerCase()) {
changeNetwork(community.network.toLowerCase() as Network)
}
}, [community, changeNetwork, network])

return (
<Switch>
Expand Down
2 changes: 1 addition & 1 deletion src/modules/lite/explorer/pages/ProposalDetails/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const ProposalDetails: React.FC<{ id: string }> = ({ id }) => {
const isMobileSmall = useMediaQuery(theme.breakpoints.down("sm"))
const { state } = useLocation<{ poll: Poll; choices: Choice[]; daoId: string }>()

const { data: dao } = useDAO(state.daoId)
const { data: dao } = useDAO(state?.daoId)
const { account, wallet } = useTezos()
const openNotification = useNotification()
const [refresh, setRefresh] = useState<number>()
Expand Down
53 changes: 0 additions & 53 deletions src/services/lite/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,43 +36,6 @@ export const getTotalSupplyAtReferenceBlock = async (network: Network, address:
return result[0].value
}

export const getTotalHolders = async (network: Network, address: string): Promise<number> => {
const url = `https://api.${networkNameMap[network]}.tzkt.io/v1/tokens/balances/count?token.contract=${address}`
const response = await axios.get(url)

if (!response) {
throw new Error("Failed to fetch contract current block")
}

const result = await response.data

return result
}

export const getUserTotalSupplyAtReferenceBlock = async (
network: Network,
address: string,
level: number,
userAddress: string
) => {
const url = `https://api.${networkNameMap[network]}.tzkt.io/v1/contracts/${address}/bigmaps/ledger/historical_keys/${level}`
const response = await fetch(url)

if (!response.ok) {
throw new Error("Failed to fetch contract current block")
}

const result = await response.json()

let userBalance

if (result && result.length > 0) {
userBalance = result.find((elem: any) => elem.key.address === userAddress)
return userBalance.value
}
return 0
}

export const hasTokenBalance = async (network: Network, account: string, contract: any) => {
const url = `https://api.${networkNameMap[network]}.tzkt.io/v1/tokens/balances?account=${account}&token.contract=${contract}`
const response = await fetch(url)
Expand All @@ -98,22 +61,6 @@ export const hasTokenBalance = async (network: Network, account: string, contrac
return hasBalance
}

export const getTurnoutValue = async (network: Network, address: string, level: number, voters: number) => {
const url = `https://api.${networkNameMap[network]}.tzkt.io/v1/contracts/${address}/bigmaps/ledger/historical_keys/${level}`
const response = await fetch(url)

if (!response.ok) {
throw new Error("Failed to fetch contract current block")
}
const result = await response.json()

if (result) {
return (voters * 100) / result.length
}

return 0
}

export const isProposalActive = (date: number) => {
const config = {
rounding: Math.floor
Expand Down
40 changes: 14 additions & 26 deletions src/services/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,9 @@ export const getTotalSupplyAtReferenceBlock = async (network: Network, address:
return result[0].value
}

export const getUserTotalSupplyAtReferenceBlock = async (
network: Network,
address: string,
level: number,
userAddress: string
) => {
const url = `https://api.${networkNameMap[network]}.tzkt.io/v1/contracts/${address}/bigmaps/ledger/historical_keys/${level}`
export const getTokenHoldersCount = async (network: Network, address: string, tokenID: number) => {
const url = `https://api.${networkNameMap[network]}.tzkt.io/v1/tokens?tokenId=${tokenID}&contract=${address}`

const response = await fetch(url)

if (!response.ok) {
Expand All @@ -50,13 +46,7 @@ export const getUserTotalSupplyAtReferenceBlock = async (

const result = await response.json()

let userBalance

if (result && result.length > 0) {
userBalance = result.find((elem: any) => elem.key.address === userAddress)
return userBalance.value
}
return 0
return result[0].holdersCount
}

export const hasTokenBalance = async (network: Network, account: string, contract: any) => {
Expand Down Expand Up @@ -84,20 +74,18 @@ export const hasTokenBalance = async (network: Network, account: string, contrac
return hasBalance
}

export const getTurnoutValue = async (network: Network, address: string, level: number, voters: number) => {
const url = `https://api.${networkNameMap[network]}.tzkt.io/v1/contracts/${address}/bigmaps/ledger/historical_keys/${level}`
const response = await fetch(url)

if (!response.ok) {
throw new Error("Failed to fetch contract current block")
}
const result = await response.json()
export const getTurnoutValue = async (
network: Network,
address: string,
tokenID: number,
level: number,
voters: number
) => {
const tokenHolders = await getTokenHoldersCount(network, address, tokenID)

if (result) {
return (voters * 100) / result.length
if (tokenHolders) {
return (voters * 100) / Number(tokenHolders)
}

return 0
}

export const isProposalActive = (date: number) => {
Expand Down

0 comments on commit d170fc9

Please sign in to comment.