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

Change logic for delegation voting power and add validation for user balance #654

Merged
merged 2 commits into from
Sep 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
1 change: 1 addition & 0 deletions src/models/Community.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ export interface CommunityToken {
symbol: string
tokenAddress: string
decimals: string
message?: string
}
16 changes: 3 additions & 13 deletions src/modules/explorer/pages/User/components/DelegationBanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,12 @@ export const matchTextToStatus = (value: DelegationsType | undefined) => {

export const Delegation: React.FC<{ daoId: string }> = ({ daoId }) => {
const { data: dao } = useDAO(daoId)
const { network, account, connect } = useTezos()
const { account } = useTezos()

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: delegateVoteBalances } = useDelegationVoteWeight(dao?.data.token.contract, dao?.data.address)
const [voteWeight, setVoteWeight] = useState(new BigNumber(0))
const { data: voteWeight } = useDelegationVoteWeight(dao?.data.token.contract)
const [loadingRes, setLoadingRes] = useState(true)
const [shouldRefetch, setShouldRefetch] = useState(true)

Expand Down Expand Up @@ -97,15 +96,6 @@ export const Delegation: React.FC<{ daoId: string }> = ({ daoId }) => {
setShouldRefetch(false)
}, [delegatedTo])

useEffect(() => {
let totalVoteWeight = new BigNumber(0)
delegateVoteBalances?.forEach(delegatedVote => {
const balance = new BigNumber(delegatedVote.balance)
totalVoteWeight = totalVoteWeight.plus(balance)
})
setVoteWeight(totalVoteWeight)
}, [delegateVoteBalances])

return (
<DelegationBox container direction="column">
<Grid container style={{ gap: 12 }} direction="column">
Expand All @@ -114,7 +104,7 @@ export const Delegation: React.FC<{ daoId: string }> = ({ daoId }) => {
</Typography>
<Subtitle variant="body1">These settings only affect your participation in off-chain polls</Subtitle>
</Grid>
{dao && (
{dao && voteWeight && (
<Grid container style={{ gap: 12 }} direction="column">
<Typography color="textPrimary">Voting Weight</Typography>
<Balance color="secondary">
Expand Down
26 changes: 23 additions & 3 deletions src/modules/lite/explorer/components/Choices.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ import { theme } from "theme"
import { AddCircleOutline, RemoveCircleOutline } from "@material-ui/icons"
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 { useUserTokenBalance } from "services/contracts/token/hooks/useUserTokenBalance"
import BigNumber from "bignumber.js"

const ChoicesContainer = styled(Grid)(({ theme }) => ({
paddingBottom: 19,
Expand Down Expand Up @@ -76,9 +81,24 @@ const CustomFormikTextField = withStyles({
disabled: {}
})(FormikTextField)

export const Choices: React.FC<any> = ({ choices, submitForm, isLoading, votingStrategy, setFieldValue }) => {
const MainButton = styled(Button)(({ theme }) => ({
"&$disabled": {
boxShadow: "none"
}
}))

export const Choices: React.FC<any> = ({ choices, submitForm, isLoading, votingStrategy, setFieldValue, id }) => {
const isMobileExtraSmall = useMediaQuery(theme.breakpoints.down("sm"))

const daoId = useDAOID()
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)

return (
<Grid container direction="column" style={{ gap: 30 }}>
<ChoicesContainer container direction="column">
Expand Down Expand Up @@ -175,9 +195,9 @@ export const Choices: React.FC<any> = ({ choices, submitForm, isLoading, votingS
</ChoicesContainer>
<Grid container style={{ gap: 10, marginTop: 31 }}>
{!isLoading ? (
<Button variant="contained" color="secondary" onClick={submitForm}>
<MainButton disabled={!canCreateProposal} variant="contained" color="secondary" onClick={submitForm}>
Create Proposal
</Button>
</MainButton>
) : (
<CircularProgress color="secondary" />
)}
Expand Down
3 changes: 2 additions & 1 deletion src/modules/lite/explorer/components/ProposalList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ export const ProposalList: React.FC<{ polls: Poll[]; id: string | undefined; dao
polls.forEach(async poll => {
await fetch(`${getEnv(EnvKey.REACT_APP_LITE_API_URL)}/token/${communityId}`).then(async response => {
if (!response.ok) {
const data = await response.json()
openNotification({
message: "An error has occurred",
message: data.message,
autoHideDuration: 2000,
variant: "error"
})
Expand Down
3 changes: 2 additions & 1 deletion src/modules/lite/explorer/hooks/useCommunity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ export const useCommunity = (daoId: string, isUpdated?: number) => {
try {
await fetch(`${getEnv(EnvKey.REACT_APP_LITE_API_URL)}/daos/${daoId.toString()}`).then(async response => {
if (!response.ok) {
const data = await response.json()
openNotification({
message: "An error has occurred",
message: data.message,
autoHideDuration: 2000,
variant: "error"
})
Expand Down
5 changes: 3 additions & 2 deletions src/modules/lite/explorer/hooks/useCommunityToken.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ export const useCommunityToken = (communityId: any) => {
if (communityId !== undefined) {
await fetch(`${getEnv(EnvKey.REACT_APP_LITE_API_URL)}/token/${String(communityId)}`).then(async response => {
if (!response.ok) {
const message = `An error has occurred: ${response.statusText}`
return
const data = await response.json()
const message = data.message
return message
}

const record: CommunityToken = await response.json()
Expand Down
3 changes: 2 additions & 1 deletion src/modules/lite/explorer/hooks/useHasVoted.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ export const useHasVoted = (refresh?: number) => {
await fetch(`${getEnv(EnvKey.REACT_APP_LITE_API_URL)}/choices/${String(account)}/user`).then(
async response => {
if (!response.ok) {
const data = await response.json()
openNotification({
message: "An error has occurred",
message: data.message,
autoHideDuration: 2000,
variant: "error"
})
Expand Down
3 changes: 2 additions & 1 deletion src/modules/lite/explorer/hooks/usePoll.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ export const useSinglePoll = (pollId: string | undefined, id?: any, community?:
try {
await fetch(`${getEnv(EnvKey.REACT_APP_LITE_API_URL)}/polls/${pollId}/polls`).then(async response => {
if (!response.ok) {
const data = await response.json()
openNotification({
message: "An error has occurred",
message: data.message,
autoHideDuration: 2000,
variant: "error"
})
Expand Down
3 changes: 2 additions & 1 deletion src/modules/lite/explorer/hooks/usePollChoices.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ export const usePollChoices = (poll: Poll | undefined, refresh?: number) => {
if (poll) {
await fetch(`${getEnv(EnvKey.REACT_APP_LITE_API_URL)}/choices/${poll._id}/find`).then(async response => {
if (!response.ok) {
const data = await response.json()
openNotification({
message: "An error has occurred",
message: data.message,
autoHideDuration: 2000,
variant: "error"
})
Expand Down
4 changes: 2 additions & 2 deletions src/modules/lite/explorer/hooks/usePolls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ export const usePolls = (id: any) => {
async function fetchPoll() {
await fetch(`${getEnv(EnvKey.REACT_APP_LITE_API_URL)}/polls/${id}/list`).then(async response => {
if (!response.ok) {
const data = await response.json()
openNotification({
message: "An error has occurred",
message: data.message,
autoHideDuration: 2000,
variant: "error"
})
Expand All @@ -36,7 +37,6 @@ export const usePolls = (id: any) => {
if (poll) {
await fetch(`${getEnv(EnvKey.REACT_APP_LITE_API_URL)}/choices/${poll._id}/votes`).then(async response => {
if (!response.ok) {
console.log("error in query")
return
}
const records: number = await response.json()
Expand Down
3 changes: 2 additions & 1 deletion src/modules/lite/explorer/hooks/useToken.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ export const useToken = (daoId: string | undefined) => {
const communityId = daoId
await fetch(`${getEnv(EnvKey.REACT_APP_LITE_API_URL)}/token/${communityId}`).then(async response => {
if (!response.ok) {
const data = await response.json()
openNotification({
message: "An error has occurred",
message: data.message,
autoHideDuration: 2000,
variant: "error"
})
Expand Down
5 changes: 4 additions & 1 deletion src/modules/lite/explorer/pages/CreateProposal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { useToken } from "../../hooks/useToken"
import { isWebUri } from "valid-url"
import { useDAO } from "services/services/dao/hooks/useDAO"
import { useDAOID } from "modules/explorer/pages/DAO/router"
import { useUserTokenBalance } from "services/contracts/token/hooks/useUserTokenBalance"
dayjs.extend(duration)

const ProposalContainer = styled(Grid)(({ theme }) => ({
Expand Down Expand Up @@ -430,6 +431,7 @@ export const ProposalForm = ({
) : null}
<ProposalChoices>
<Choices
id={id}
choices={getIn(values, "choices")}
isLoading={isSubmitting}
submitForm={submitForm}
Expand Down Expand Up @@ -596,6 +598,7 @@ export const ProposalCreator: React.FC<{ id?: string; onClose?: any }> = props =
}

const res = await saveLiteProposal(signature, publicKey, payloadBytes)
const respData = await res.json()
if (res.ok) {
openNotification({
message: "Proposal created!",
Expand All @@ -609,7 +612,7 @@ export const ProposalCreator: React.FC<{ id?: string; onClose?: any }> = props =
: navigate.push(`/explorer/lite/dao/${id}/community`)
} else {
openNotification({
message: "Proposal could not be created",
message: respData.message,
autoHideDuration: 3000,
variant: "error"
})
Expand Down
10 changes: 7 additions & 3 deletions src/modules/lite/explorer/pages/ProposalDetails/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ 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 BigNumber from "bignumber.js"

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

useEffect(() => {
Expand Down Expand Up @@ -97,6 +99,7 @@ export const ProposalDetails: React.FC<{ id: string }> = ({ id }) => {
return
}
const resp = await voteOnLiteProposal(signature, publicKey, payloadBytes)
const response = await resp.json()
if (resp.ok) {
openNotification({
message: "Your vote has been submitted",
Expand All @@ -107,13 +110,14 @@ export const ProposalDetails: React.FC<{ id: string }> = ({ id }) => {
setSelectedVotes([])
} else {
openNotification({
message: `Something went wrong!!`,
message: response.message,
autoHideDuration: 3000,
variant: "error"
})
return
}
} catch (error) {
console.log("error: ", error)
openNotification({
message: `Something went wrong!!`,
autoHideDuration: 3000,
Expand Down Expand Up @@ -156,7 +160,7 @@ export const ProposalDetails: React.FC<{ id: string }> = ({ id }) => {
</Grid>
{poll?.isActive === ProposalStatus.ACTIVE ? (
<Button
disabled={selectedVotes.length === 0 || !!delegatedTo}
disabled={selectedVotes.length === 0 || voteWeight?.eq(new BigNumber(0))}
variant="contained"
color="secondary"
onClick={() => saveVote()}
Expand Down
Loading
Loading