Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Vote weight bug for lite communities and explorer sorting #692

Merged
merged 7 commits into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/modules/explorer/pages/DAOList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export const DAOList: React.FC = () => {
},
allowPublicAccess: dao.dao_type.name === "lite" ? dao.allowPublicAccess : true
}))
.sort((a, b) => b.votingAddresses.length - a.votingAddresses.length)
.sort((a, b) => b.votingAddressesCount - a.votingAddressesCount)

if (searchText) {
return formattedDAOs.filter(
Expand Down
8 changes: 4 additions & 4 deletions src/modules/lite/explorer/components/Choices.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import { FieldArray, Field } from "formik"
import { TextField as FormikTextField } from "formik-material-ui"
import { useDAOID } from "modules/explorer/pages/DAO/router"
import { useDAO } from "services/services/dao/hooks/useDAO"
import { useToken } from "../hooks/useToken"
import { useTokenVoteWeight } from "services/contracts/token/hooks/useTokenVoteWeight"
import { useCommunity } from "../hooks/useCommunity"

const ChoicesContainer = styled(Grid)(({ theme }) => ({
paddingBottom: 19,
Expand Down Expand Up @@ -91,9 +91,9 @@ export const Choices: React.FC<any> = ({ choices, submitForm, isLoading, votingS

const daoId = useDAOID()
const { data } = useDAO(daoId)
const liteDAOId = data?.liteDAOData?._id ? data?.liteDAOData?._id : id
const tokenAddress = useToken(liteDAOId)
const { data: userTokenVoteWeight } = useTokenVoteWeight(tokenAddress)
const community = useCommunity(id)

const { data: userTokenVoteWeight } = useTokenVoteWeight(data?.data?.token?.contract || community?.tokenAddress)
const canCreateProposal = userTokenVoteWeight && userTokenVoteWeight.gt(0) ? true : false

return (
Expand Down
42 changes: 19 additions & 23 deletions src/modules/lite/explorer/components/DaoCardDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import React, { useCallback, useContext, useEffect, useState } from "react"
import { useHistory } from "react-router"
import { useTezos } from "services/beacon/hooks/useTezos"
import { DashboardContext } from "../context/ActionSheets/explorer"
import { useHoldersTotalCount } from "../hooks/useHolderTotalCount"
import { updateCount } from "services/services/lite/lite-services"
import { useIsMember } from "../hooks/useIsMember"
import { useHoldersTotalCount } from "../hooks/useHolderTotalCount"

const StyledAvatar = styled(Avatar)({
height: 159,
Expand Down Expand Up @@ -58,34 +58,31 @@ export const DaoCardDetail: React.FC<DaoCardDetailProps> = ({ community, setIsUp
const { network, account } = useTezos()
const theme = useTheme()
const { isConnected } = useContext(DashboardContext)
const count = useHoldersTotalCount(network, community?.tokenAddress || "")
const isMember = useIsMember(network, community?.tokenAddress || "", account)
const count = useHoldersTotalCount(
network,
community?.tokenAddress || "",
community?.tokenID ? Number(community?.tokenID) : 0
)

const updateCommunityCount = useCallback(
async (count: number) => {
if (community) {
try {
const resp = await updateCount(community._id, count)
const respData = await resp.json()
const updateCommunityCount = useCallback(async () => {
if (community) {
try {
const resp = await updateCount(community._id)
const respData = await resp.json()

if (!resp.ok) {
console.log(respData.message)
}
} catch (error) {
console.log("Error: ", error)
if (!resp.ok) {
console.log(respData.message)
}
} catch (error) {
console.log("Error: ", error)
}
},
[community]
)
}
}, [community])

useEffect(() => {
updateCommunityCount(count)
}, [count, updateCommunityCount])

const shouldBeDisabled = () => {
return community?.requiredTokenOwnership && isMember ? false : true
}
updateCommunityCount()
}, [updateCommunityCount])

return (
<DaoCardContainer container style={{ gap: 10 }} direction="column">
Expand All @@ -110,7 +107,6 @@ export const DaoCardDetail: React.FC<DaoCardDetailProps> = ({ community, setIsUp
{isConnected ? (
<Grid item>
<ProposalButton
disabled={shouldBeDisabled()}
variant="contained"
color="secondary"
size="small"
Expand Down
28 changes: 4 additions & 24 deletions src/modules/lite/explorer/hooks/useHolderTotalCount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,25 @@ import React, { useEffect, useState } from "react"
import { useNotification } from "modules/common/hooks/useNotification"
import { Network } from "services/beacon"
import { networkNameMap } from "services/bakingBad"
import { getTokenHoldersCount } from "services/utils/utils"

export const useHoldersTotalCount = (network: Network, tokenAddress: string) => {
export const useHoldersTotalCount = (network: Network, tokenAddress: string, tokenID: number) => {
const [count, setCount] = useState<number>(0)
const openNotification = useNotification()

useEffect(() => {
async function fetchTotalCount() {
try {
if (tokenAddress !== "") {
const url = `https://api.${networkNameMap[network]}.tzkt.io/v1/tokens/balances/?token.contract=${tokenAddress}`
await fetch(url).then(async response => {
if (!response.ok) {
openNotification({
message: "An error has occurred",
autoHideDuration: 2000,
variant: "error"
})
return
}

const record: any[] = await response.json()

const nonZeroBalance = record.filter((item: any) => item.balance !== "0")
if (!record) {
return
}

setCount(nonZeroBalance.length)
return
})
const holdersCount = await getTokenHoldersCount(network, tokenAddress, tokenID)
setCount(holdersCount)
}
return
} catch (error) {
openNotification({
message: "An error has occurred",
autoHideDuration: 2000,
variant: "error"
})
return
}
}
fetchTotalCount()
Expand Down
5 changes: 4 additions & 1 deletion src/modules/lite/explorer/pages/ProposalDetails/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ export const ProposalDetails: React.FC<{ id: string }> = ({ id }) => {
const community = useCommunity(id)
const poll = useSinglePoll(proposalId, id, community)
const choices = usePollChoices(poll, refresh)
const { data: voteWeight } = useTokenVoteWeight(dao?.data.token.contract, poll?.referenceBlock)
const { data: voteWeight } = useTokenVoteWeight(
dao?.data.token.contract || community?.tokenAddress,
poll?.referenceBlock
)
const [selectedVotes, setSelectedVotes] = useState<Choice[]>([])

useEffect(() => {
Expand Down
20 changes: 1 addition & 19 deletions src/services/services/lite/lite-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,21 +131,6 @@ export const saveLiteCommunity = async (signature: string, publicKey: string | u
return resp
}

export const joinLiteCommunity = async (signature: string, publicKey: string | undefined, payloadBytes: string) => {
const resp = await fetch(`${getEnv(EnvKey.REACT_APP_LITE_API_URL)}/daos/join`, {
method: "POST",
body: JSON.stringify({
signature,
publicKey,
payloadBytes
}),
headers: {
"Content-Type": "application/json"
}
})
return resp
}

export const saveLiteProposal = async (signature: string, publicKey: string | undefined, payloadBytes: string) => {
const resp = await fetch(`${getEnv(EnvKey.REACT_APP_LITE_API_URL)}/poll/add`, {
method: "POST",
Expand Down Expand Up @@ -193,12 +178,9 @@ export const fetchLiteData = async (daoContract: string, network: Network) => {
}
}

export const updateCount = async (id: string, count: number) => {
export const updateCount = async (id: string) => {
const resp = await fetch(`${getEnv(EnvKey.REACT_APP_LITE_API_URL)}/daos/count/${id}`, {
method: "POST",
body: JSON.stringify({
count
}),
headers: {
"Content-Type": "application/json"
}
Expand Down