Skip to content

Commit

Permalink
Delegation and Error Handling (#662)
Browse files Browse the repository at this point in the history
* Change logic for delegation voting power and add validation for user balance

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

* Change voting power voting logic

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

* Rename vote weight hook and add check for delegation supported

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

---------

Signed-off-by: Manank Patni <[email protected]>
  • Loading branch information
Man-Jain authored Sep 19, 2023
1 parent 754801b commit d4ce63b
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Edit } from "@material-ui/icons"
import { DelegationDialog } from "./DelegationModal"
import { useDelegationStatus } from "services/contracts/token/hooks/useDelegationStatus"
import { useTezos } from "services/beacon/hooks/useTezos"
import { useDelegationVoteWeight } from "services/contracts/token/hooks/useDelegationVoteWeight"
import { useTokenVoteWeight } from "services/contracts/token/hooks/useTokenVoteWeight"
import BigNumber from "bignumber.js"
import { parseUnits } from "services/contracts/utils"
import { ProfileAvatar } from "modules/explorer/components/styled/ProfileAvatar"
Expand Down Expand Up @@ -65,7 +65,7 @@ export const Delegation: React.FC<{ daoId: string }> = ({ daoId }) => {
const { data: delegatedTo, isLoading, refetch } = useDelegationStatus(dao?.data.token.contract)
const [delegationStatus, setDelegationStatus] = useState<DelegationsType>(DelegationsType.NOT_DELEGATING)
const [openModal, setOpenModal] = useState(false)
const { data: voteWeight } = useDelegationVoteWeight(dao?.data.token.contract)
const { data: voteWeight } = useTokenVoteWeight(dao?.data.token.contract)
const [loadingRes, setLoadingRes] = useState(true)
const [shouldRefetch, setShouldRefetch] = useState(true)

Expand Down
7 changes: 6 additions & 1 deletion src/modules/explorer/pages/User/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { UserProfileName } from "../../components/UserProfileName"
import { DropButton } from "../Proposals"
import { usePolls } from "modules/lite/explorer/hooks/usePolls"
import { Delegation } from "./components/DelegationBanner"
import { useTokenDelegationSupported } from "services/contracts/token/hooks/useTokenDelegationSupported"

const ContentBlockItem = styled(Grid)({
padding: "35px 52px",
Expand Down Expand Up @@ -103,13 +104,16 @@ export const User: React.FC = () => {
const daoId = useDAOID()
const { data, cycleInfo } = useDAO(daoId)
const { data: proposals } = useProposals(daoId)

const history = useHistory()
const { data: executedProposals } = useProposals(daoId, ProposalStatus.EXECUTED)
const { data: droppedProposals } = useProposals(daoId, ProposalStatus.DROPPED)
const { mutate: unstakeFromAllProposals } = useUnstakeFromAllProposals()
const polls = usePolls(data?.liteDAOData?._id)
const pollsPosted = polls?.filter(p => p.author === account)

const { data: isTokenDelegationSupported } = useTokenDelegationSupported(data?.data.token.contract)

useEffect(() => {
if (!account) {
history.push(`../${daoId}`)
Expand Down Expand Up @@ -207,7 +211,8 @@ export const User: React.FC = () => {
</UserBalances>
</BalancesHeader>

<Delegation daoId={daoId} />
{isTokenDelegationSupported ? <Delegation daoId={daoId} /> : null}

<Grid item>
{proposalsCreated && cycleInfo && (
<ProposalsList
Expand Down
9 changes: 3 additions & 6 deletions src/modules/lite/explorer/components/Choices.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ 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 { useUserTokenBalance } from "services/contracts/token/hooks/useUserTokenBalance"
import BigNumber from "bignumber.js"
import { useTokenVoteWeight } from "services/contracts/token/hooks/useTokenVoteWeight"

const ChoicesContainer = styled(Grid)(({ theme }) => ({
paddingBottom: 19,
Expand Down Expand Up @@ -94,10 +93,8 @@ export const Choices: React.FC<any> = ({ choices, submitForm, isLoading, votingS
const { data } = useDAO(daoId)
const liteDAOId = data?.liteDAOData?._id ? data?.liteDAOData?._id : id
const tokenAddress = useToken(liteDAOId)
const { data: userBalance } = useUserTokenBalance(tokenAddress)
console.log("userBalance: ", userBalance)
const canCreateProposal = userBalance && new BigNumber(userBalance).gt(0) ? true : false
console.log("canCreateProposal: ", canCreateProposal)
const { data: userTokenVoteWeight } = useTokenVoteWeight(tokenAddress)
const canCreateProposal = userTokenVoteWeight && userTokenVoteWeight.gt(0) ? true : false

return (
<Grid container direction="column" style={{ gap: 30 }}>
Expand Down
15 changes: 1 addition & 14 deletions src/modules/lite/explorer/components/ProposalTableRow.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import React, { createContext, useContext, useEffect, useState } from "react"
import React from "react"
import { styled, Grid, Typography, useTheme, useMediaQuery, LinearProgress } from "@material-ui/core"
import { RowContainer } from "./tables/RowContainer"
import { ProposalStatus, TableStatusBadge } from "./ProposalTableRowStatusBadge"
import { useHistory } from "react-router"
import { Blockie } from "modules/common/Blockie"
import { toShortAddress } from "services/contracts/utils"
import { Choice } from "models/Choice"
import { Poll } from "models/Polls"
import { ChoiceDetails } from "./ChoiceDetails"
import { usePollChoices } from "../hooks/usePollChoices"

export interface ProposalTableRowData {
Expand Down Expand Up @@ -83,17 +81,6 @@ export const ProposalTableRow: React.FC<{ poll: Poll; daoId?: string }> = ({ pol
<DescriptionText color="textPrimary">{poll.description}</DescriptionText>
</Grid>
</Grid>

{choices && choices.length > 0
? choices.map((choice: Choice, index: number) => (
<ChoiceDetails
key={`'choice-'${choice.name}${index}`}
poll={poll}
choice={choice}
index={index}
></ChoiceDetails>
))
: null}
</RowContainer>
)
}
5 changes: 2 additions & 3 deletions src/modules/lite/explorer/pages/ProposalDetails/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ import { useSinglePoll } from "../../hooks/usePoll"
import { ProposalStatus } from "../../components/ProposalTableRowStatusBadge"
import { BackButton } from "modules/lite/components/BackButton"
import { voteOnLiteProposal } from "services/services/lite/lite-services"
import { useDelegationStatus } from "services/contracts/token/hooks/useDelegationStatus"
import { useDAO } from "services/services/dao/hooks/useDAO"
import { useDelegationVoteWeight } from "services/contracts/token/hooks/useDelegationVoteWeight"
import { useTokenVoteWeight } from "services/contracts/token/hooks/useTokenVoteWeight"
import BigNumber from "bignumber.js"

const PageContainer = styled("div")({
Expand Down Expand Up @@ -63,7 +62,7 @@ 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 } = useDelegationVoteWeight(dao?.data.token.contract)
const { data: voteWeight } = useTokenVoteWeight(dao?.data.token.contract)
const [selectedVotes, setSelectedVotes] = useState<Choice[]>([])

useEffect(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/services/bakingBad/delegations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const getTokenDelegation = async (tokenAddress: string, account: string,
return delegatedTo
}

export const getTokenDelegationVoteWeight = async (tokenAddress: string, account: string, network: Network) => {
export const getTokenVoteWeight = async (tokenAddress: string, account: string, network: Network) => {
const level = await getCurrentBlock(network)

const url = `${getEnv(
Expand Down
13 changes: 13 additions & 0 deletions src/services/bakingBad/tokenBalances/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,19 @@ export const getUserTokenBalance = async (accountAddress: string, network: Netwo
return userTokenBalance
}

export const isTokenDelegationSupported = async (tezos: TezosToolkit, address: string) => {
const token = await tezos.wallet.at(address)

const contractViews = Object.keys(token.contractViews)
const votingPowerView = contractViews.find(view => view === "voting_power")

if (votingPowerView) {
return true
}

return false
}

export const getVotingPowerAtReferenceBlock = async (
accountAddress: string,
network: Network = "mainnet",
Expand Down
26 changes: 26 additions & 0 deletions src/services/contracts/token/hooks/useTokenDelegationSupported.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useQuery } from "react-query"
import { isTokenDelegationSupported } from "services/bakingBad/tokenBalances"
import { useTezos } from "services/beacon/hooks/useTezos"

export const useTokenDelegationSupported = (tokenAddress: string | undefined) => {
const { tezos } = useTezos()

const { data, ...rest } = useQuery<boolean, Error>(
["delegationSupported", tokenAddress],
async () => {
let tokenDelegationSupported = false
if (tokenAddress) {
tokenDelegationSupported = await isTokenDelegationSupported(tezos, tokenAddress)
}
return tokenDelegationSupported
},
{
enabled: !!tokenAddress
}
)

return {
data,
...rest
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import BigNumber from "bignumber.js"
import { useQuery } from "react-query"
import { getTokenDelegationVoteWeight } from "services/bakingBad/delegations"
import { getTokenVoteWeight } from "services/bakingBad/delegations"
import { useTezos } from "services/beacon/hooks/useTezos"

export const useDelegationVoteWeight = (tokenAddress: string | undefined) => {
export const useTokenVoteWeight = (tokenAddress: string | undefined) => {
const { network, account } = useTezos()

const { data, ...rest } = useQuery<BigNumber | undefined, Error>(
["delegationVoteWeight", tokenAddress],
["userTokenVoteWeight", tokenAddress],
async () => {
if (tokenAddress) {
return await getTokenDelegationVoteWeight(tokenAddress, account, network)
return await getTokenVoteWeight(tokenAddress, account, network)
}
},
{
Expand Down
2 changes: 0 additions & 2 deletions src/services/contracts/token/hooks/useUserTokenBalance.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import BigNumber from "bignumber.js"
import { useQuery } from "react-query"
import { getTokenDelegationVoteWeight } from "services/bakingBad/delegations"
import { getUserTokenBalance } from "services/bakingBad/tokenBalances"
import { useTezos } from "services/beacon/hooks/useTezos"

Expand Down

0 comments on commit d4ce63b

Please sign in to comment.