From 33225a612f74ea419bb57471e87597c9a9a78708 Mon Sep 17 00:00:00 2001 From: fabiolalombardim Date: Sun, 30 Jul 2023 22:07:15 +0200 Subject: [PATCH 01/18] delegation options & status updates --- .../pages/DAOList/components/DAOItem.tsx | 3 +- .../User/components/DelegationBanner.tsx | 51 ++++++++----- .../pages/User/components/DelegationModal.tsx | 72 +++++++++---------- 3 files changed, 67 insertions(+), 59 deletions(-) diff --git a/src/modules/explorer/pages/DAOList/components/DAOItem.tsx b/src/modules/explorer/pages/DAOList/components/DAOItem.tsx index 5cdc0d9b..6de4ccbc 100644 --- a/src/modules/explorer/pages/DAOList/components/DAOItem.tsx +++ b/src/modules/explorer/pages/DAOList/components/DAOItem.tsx @@ -32,7 +32,8 @@ const Container = styled(Grid)(({ theme }: { theme: Theme }) => ({ }, ["@media (max-width:1155px)"]: { - minHeight: 123 + minHeight: 123, + maxWidth: 480 }, ["@media (max-width:1030px)"]: {}, diff --git a/src/modules/explorer/pages/User/components/DelegationBanner.tsx b/src/modules/explorer/pages/User/components/DelegationBanner.tsx index dcc3775a..ac782db0 100644 --- a/src/modules/explorer/pages/User/components/DelegationBanner.tsx +++ b/src/modules/explorer/pages/User/components/DelegationBanner.tsx @@ -1,5 +1,6 @@ +/* eslint-disable react-hooks/exhaustive-deps */ import React, { Fragment, useEffect, useState } from "react" -import { Grid, Theme, Typography, styled } from "@material-ui/core" +import { CircularProgress, Grid, Theme, Typography, styled } from "@material-ui/core" import { useDAO } from "services/services/dao/hooks/useDAO" import { Edit } from "@material-ui/icons" import { DelegationDialog } from "./DelegationModal" @@ -10,8 +11,7 @@ import BigNumber from "bignumber.js" import { parseUnits } from "services/contracts/utils" export enum DelegationsType { - ACCEPTING_DELEGATION = "ACCEPTING_DELEGATION", - NOT_ACCEPTING_DELEGATION = "NOT_ACCEPTING_DELEGATION", + NOT_DELEGATING = "NOT_DELEGATING", DELEGATING = "DELEGATING" } @@ -38,9 +38,7 @@ const Balance = styled(Typography)({ export const matchTextToStatus = (value: DelegationsType | undefined) => { switch (value) { - case DelegationsType.ACCEPTING_DELEGATION: - return "Accepting delegations" - case DelegationsType.NOT_ACCEPTING_DELEGATION: + case DelegationsType.NOT_DELEGATING: return "Not currently accepting delegations or delegating" case DelegationsType.DELEGATING: return "Delegating to " @@ -53,26 +51,35 @@ export const Delegation: React.FC<{ daoId: string }> = ({ daoId }) => { const { data: dao } = useDAO(daoId) const { network, tezos, account, connect } = useTezos() - const { data: delegatedTo } = useDelegationStatus(dao?.data.token.contract) - const [delegationStatus, setDelegationStatus] = useState(DelegationsType.NOT_ACCEPTING_DELEGATION) + const { data: delegatedTo, isLoading, refetch } = useDelegationStatus(dao?.data.token.contract) + const [delegationStatus, setDelegationStatus] = useState(DelegationsType.NOT_DELEGATING) const [openModal, setOpenModal] = useState(false) const { data: delegateVoteBalances } = useDelegationVoteWeight(dao?.data.token.contract) const [voteWeight, setVoteWeight] = useState(new BigNumber(0)) - console.log("voteWeight: ", voteWeight.toString()) + // console.log("voteWeight: ", voteWeight.toString()) + + useEffect(() => { + refetch() + + setTimeout(() => { + if (delegatedTo === account) { + setDelegationStatus(DelegationsType.DELEGATING) + } else if (delegatedTo && delegatedTo !== account) { + setDelegationStatus(DelegationsType.DELEGATING) + } else { + setDelegationStatus(DelegationsType.NOT_DELEGATING) + } + }, 2000) + }, []) const onCloseAction = () => { setOpenModal(false) } useEffect(() => { - if (delegatedTo === account) { - setDelegationStatus(DelegationsType.ACCEPTING_DELEGATION) - } else if (delegatedTo && delegatedTo !== account) { - setDelegationStatus(DelegationsType.DELEGATING) - } else { - setDelegationStatus(DelegationsType.NOT_ACCEPTING_DELEGATION) - } - }, [delegatedTo, account]) + refetch() + console.log(delegatedTo) + }, [delegationStatus, setDelegationStatus]) useEffect(() => { let totalVoteWeight = new BigNumber(0) @@ -124,8 +131,14 @@ export const Delegation: React.FC<{ daoId: string }> = ({ daoId }) => { - {matchTextToStatus(delegationStatus)} - {delegationStatus === DelegationsType.DELEGATING ? delegatedTo : null} + {isLoading ? ( + + ) : ( + <> + {matchTextToStatus(delegationStatus)} + {delegationStatus === DelegationsType.DELEGATING ? delegatedTo : null} + + )} { switch (value) { - case ActionTypes.ACCEPT_DELEGATIONS: - return "Accept Delegations" - case ActionTypes.DELEGATE: - return "Delegate" - case ActionTypes.CHANGE_DELEGATE: - return "Change Delegate" - case ActionTypes.STOP_ACCEPTING_DELEGATIONS: - return "Stop Accepting Delegations" - case ActionTypes.STOP_DELEGATING: - return "Stop Delegating" + case ActionTypes.SET_DELEGATE: + return "Enter delegate address" + case ActionTypes.NOT_DELEGATING: + return "I want to own my voting power" default: return } @@ -58,9 +49,9 @@ export const DelegationDialog: React.FC<{ delegationStatus: DelegationsType delegatedTo: string | null | undefined }> = ({ status, onClose, open, setDelegationStatus, delegationStatus, delegatedTo }) => { - const [options, setOptions] = useState([]) + const [options, setOptions] = useState([ActionTypes.NOT_DELEGATING, ActionTypes.SET_DELEGATE]) const [selectedOption, setSelectedOption] = useState() - const { mutate: delegateToken } = useTokenDelegate() + const { mutate: delegateToken, data: tokenData } = useTokenDelegate() const daoId = useDAOID() const { data, cycleInfo } = useDAO(daoId) const { tezos, connect, network, account } = useTezos() @@ -81,37 +72,39 @@ export const DelegationDialog: React.FC<{ } const updateStatus = () => { - if (selectedOption === ActionTypes.DELEGATE || selectedOption === ActionTypes.CHANGE_DELEGATE) { + if (selectedOption === ActionTypes.SET_DELEGATE) { if (newDelegate && data?.data.token.contract) { - delegateToken({ tokenAddress: data?.data.token.contract, delegateAddress: newDelegate }) + delegateToken( + { tokenAddress: data?.data.token.contract, delegateAddress: newDelegate }, + { onSuccess: () => setDelegationStatus(DelegationsType.DELEGATING) } + ) + return } - } else if ( - selectedOption === ActionTypes.STOP_ACCEPTING_DELEGATIONS || - selectedOption === ActionTypes.STOP_DELEGATING - ) { + } else if (selectedOption === ActionTypes.NOT_DELEGATING) { if (data?.data.token.contract) { - delegateToken({ tokenAddress: data?.data.token.contract, delegateAddress: null }) - } - } else if (selectedOption === ActionTypes.ACCEPT_DELEGATIONS) { - if (data?.data.token.contract && account) { - delegateToken({ tokenAddress: data?.data.token.contract, delegateAddress: account }) + delegateToken( + { tokenAddress: data?.data.token.contract, delegateAddress: null }, + { + onSuccess: () => { + setDelegationStatus(DelegationsType.NOT_DELEGATING) + return + } + } + ) } + return } } const getOptionsByStatus = (status: DelegationsType | undefined) => { switch (status) { - case DelegationsType.NOT_ACCEPTING_DELEGATION: - const optionsOne = [ActionTypes.ACCEPT_DELEGATIONS, ActionTypes.DELEGATE] + case DelegationsType.NOT_DELEGATING: + const optionsOne = [ActionTypes.SET_DELEGATE] setOptions(optionsOne) break - case DelegationsType.ACCEPTING_DELEGATION: - const optionsTwo = [ActionTypes.STOP_ACCEPTING_DELEGATIONS] - setOptions(optionsTwo) - break case DelegationsType.DELEGATING: - const optionsThree = [ActionTypes.CHANGE_DELEGATE, ActionTypes.STOP_DELEGATING, ActionTypes.ACCEPT_DELEGATIONS] - setOptions(optionsThree) + const optionsTwo = [ActionTypes.NOT_DELEGATING] + setOptions(optionsTwo) break } } @@ -146,8 +139,7 @@ export const DelegationDialog: React.FC<{ name="radio-buttons" inputProps={{ "aria-label": "A" }} /> - {item === selectedOption && - (selectedOption === ActionTypes.DELEGATE || selectedOption === ActionTypes.CHANGE_DELEGATE) ? ( + {item === selectedOption && selectedOption === ActionTypes.SET_DELEGATE ? ( { setNewDelegate(e.target.value) @@ -163,7 +155,9 @@ export const DelegationDialog: React.FC<{ })} - Submit + + Submit + From b365e15d96bce8e277a15595dd8bcb70b596ec9a Mon Sep 17 00:00:00 2001 From: fabiolalombardim <37227394+fabiolalombardim@users.noreply.github.com> Date: Wed, 9 Aug 2023 20:15:21 +0200 Subject: [PATCH 02/18] Delegation options & status updates (#628) * new options & refetch * Fix Voting Weight Logic Signed-off-by: Manank Patni --------- Signed-off-by: Manank Patni Co-authored-by: Manank Patni --- .../User/components/DelegationBanner.tsx | 2 +- src/services/bakingBad/delegations/index.ts | 109 +++++++++++++----- .../token/hooks/useDelegationVoteWeight.ts | 4 +- 3 files changed, 86 insertions(+), 29 deletions(-) diff --git a/src/modules/explorer/pages/User/components/DelegationBanner.tsx b/src/modules/explorer/pages/User/components/DelegationBanner.tsx index ac782db0..e8a50c75 100644 --- a/src/modules/explorer/pages/User/components/DelegationBanner.tsx +++ b/src/modules/explorer/pages/User/components/DelegationBanner.tsx @@ -54,7 +54,7 @@ export const Delegation: React.FC<{ daoId: string }> = ({ daoId }) => { const { data: delegatedTo, isLoading, refetch } = useDelegationStatus(dao?.data.token.contract) const [delegationStatus, setDelegationStatus] = useState(DelegationsType.NOT_DELEGATING) const [openModal, setOpenModal] = useState(false) - const { data: delegateVoteBalances } = useDelegationVoteWeight(dao?.data.token.contract) + const { data: delegateVoteBalances } = useDelegationVoteWeight(dao?.data.token.contract, dao?.data.address) const [voteWeight, setVoteWeight] = useState(new BigNumber(0)) // console.log("voteWeight: ", voteWeight.toString()) diff --git a/src/services/bakingBad/delegations/index.ts b/src/services/bakingBad/delegations/index.ts index a2a4a7eb..b6262ebb 100644 --- a/src/services/bakingBad/delegations/index.ts +++ b/src/services/bakingBad/delegations/index.ts @@ -2,6 +2,7 @@ import { Network } from "services/beacon" import { networkNameMap } from ".." import { DelegationDTO, TokenDelegationDTO, UserDelegateBalance } from "./types" import { getUserTokenBalance } from "../tokenBalances" +import BigNumber from "bignumber.js" export const getLatestDelegation = async (daoAddress: string, network: Network) => { const url = `https://api.${networkNameMap[network]}.tzkt.io/v1/operations/delegations?sender=${daoAddress}&status=applied` @@ -21,6 +22,7 @@ export const getLatestDelegation = async (daoAddress: string, network: Network) export const getTokenDelegation = async (tokenAddress: string, account: string, network: Network) => { const url = `https://api.${networkNameMap[network]}.tzkt.io/v1/contracts/${tokenAddress}/bigmaps/delegates/keys?key.eq=${account}&active=true` + console.log("urlssdasd: ", url) const response = await fetch(url) if (!response.ok) { @@ -38,44 +40,99 @@ export const getTokenDelegation = async (tokenAddress: string, account: string, return delegatedTo } -export const getTokenDelegationVoteWeight = async (tokenAddress: string, account: string, network: Network) => { - const selfBalance = await getUserTokenBalance(account, network, tokenAddress) +export const getUserDAODepositBalance = async (account: string, network: Network, daoAddress: string) => { + const url = `https://api.${network}.tzkt.io/v1/contracts/${daoAddress}/bigmaps/freeze_history/keys?key.eq=${account}` - if (!selfBalance) { - throw new Error("Could not fetch delegate token balance from the TZKT API") - } - - const url = `https://api.${networkNameMap[network]}.tzkt.io/v1/contracts/${tokenAddress}/bigmaps/delegates/keys?value.eq=${account}&active=true` const response = await fetch(url) if (!response.ok) { throw new Error("Failed to fetch token delegations from TZKT API") } - const resultingDelegations: TokenDelegationDTO[] = await response.json() + const userStakedBalances = await response.json() - const delegateBalance: UserDelegateBalance = { - address: account, - balance: selfBalance - } + let userDAODepositBalance = new BigNumber(0) - if (resultingDelegations.length === 0) { - return [delegateBalance] + if (userStakedBalances && userStakedBalances[0]) { + const userStakedBalance = new BigNumber(userStakedBalances[0].value.staked) + const userCurrentUnstakedBalance = new BigNumber(userStakedBalances[0].value.current_unstaked) + const userPastUnstakedBalance = new BigNumber(userStakedBalances[0].value.past_unstaked) + + userDAODepositBalance = userStakedBalance.plus(userCurrentUnstakedBalance).plus(userPastUnstakedBalance) } - const delegatedAddressBalances: UserDelegateBalance[] = [] + return userDAODepositBalance.toString() +} - await Promise.all( - resultingDelegations.map(async del => { - const balance = await getUserTokenBalance(del.key, network, tokenAddress) - if (balance) { - delegatedAddressBalances.push({ - address: del.key, - balance: balance - }) +export const getTokenDelegationVoteWeight = async ( + tokenAddress: string, + account: string, + network: Network, + daoContract?: string +) => { + const tokenDelegationStatus = await getTokenDelegation(tokenAddress, account, network) + console.log("tokenDelegationStatus: ", tokenDelegationStatus) + if (tokenDelegationStatus && tokenDelegationStatus !== account) { + return [] + } else { + const selfBalance = await getUserTokenBalance(account, network, tokenAddress) + if (!selfBalance) { + throw new Error("Could not fetch delegate token balance from the TZKT API") + } + console.log("selfBalance: ", selfBalance) + + let selfDAOBalance + + console.log("daoContract: ", daoContract) + if (daoContract) { + selfDAOBalance = await getUserDAODepositBalance(account, network, daoContract) + console.log("selfDAOBalance: ", selfDAOBalance) + if (!selfDAOBalance) { + throw new Error("Could not fetch delegate dao balance from the TZKT API") + } + + const url = `https://api.${networkNameMap[network]}.tzkt.io/v1/contracts/${tokenAddress}/bigmaps/delegates/keys?value.eq=${account}&active=true` + console.log("ursssssl: ", url) + const response = await fetch(url) + + if (!response.ok) { + throw new Error("Failed to fetch token delegations from TZKT API") + } + + const resultingDelegations: TokenDelegationDTO[] = await response.json() + + const delegateBalance: UserDelegateBalance = { + address: account, + balance: selfDAOBalance + ? new BigNumber(selfBalance).plus(new BigNumber(selfDAOBalance)).toString() + : selfBalance + } + + if (resultingDelegations.length === 0) { + return [delegateBalance] } - }) - ) - return delegatedAddressBalances + const delegatedAddressBalances: UserDelegateBalance[] = [delegateBalance] + + await Promise.all( + resultingDelegations.map(async del => { + if (del.key === del.value) { + return + } + const balance = await getUserTokenBalance(del.key, network, tokenAddress) + const userDAOBalance = await getUserDAODepositBalance(del.key, network, daoContract) + if (balance) { + delegatedAddressBalances.push({ + address: del.key, + balance: userDAOBalance ? new BigNumber(userDAOBalance).plus(new BigNumber(balance)).toString() : balance + }) + } + }) + ) + + console.log("delegatedAddressBalances: ", delegatedAddressBalances) + + return delegatedAddressBalances + } + } } diff --git a/src/services/contracts/token/hooks/useDelegationVoteWeight.ts b/src/services/contracts/token/hooks/useDelegationVoteWeight.ts index 93a26dff..b5048842 100644 --- a/src/services/contracts/token/hooks/useDelegationVoteWeight.ts +++ b/src/services/contracts/token/hooks/useDelegationVoteWeight.ts @@ -3,14 +3,14 @@ import { getTokenDelegationVoteWeight } from "services/bakingBad/delegations" import { UserDelegateBalance } from "services/bakingBad/delegations/types" import { useTezos } from "services/beacon/hooks/useTezos" -export const useDelegationVoteWeight = (tokenAddress: string | undefined) => { +export const useDelegationVoteWeight = (tokenAddress: string | undefined, daoContract?: string | undefined) => { const { network, account } = useTezos() const { data, ...rest } = useQuery( ["delegationVoteWeight", tokenAddress], async () => { if (tokenAddress) { - return await getTokenDelegationVoteWeight(tokenAddress, account, network) + return await getTokenDelegationVoteWeight(tokenAddress, account, network, daoContract) } }, { From 6487e269f5d45ebb9be454c5749ea918cc0f174c Mon Sep 17 00:00:00 2001 From: fabiolalombardim <37227394+fabiolalombardim@users.noreply.github.com> Date: Wed, 9 Aug 2023 20:15:59 +0200 Subject: [PATCH 03/18] user who has delegate cant vote (#630) --- src/modules/explorer/pages/Proposals/index.tsx | 2 +- .../lite/explorer/components/ProposalList.tsx | 8 ++++++-- .../explorer/components/ProposalTableRow.tsx | 6 ++++-- .../explorer/pages/ProposalDetails/index.tsx | 17 ++++++++++------- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/modules/explorer/pages/Proposals/index.tsx b/src/modules/explorer/pages/Proposals/index.tsx index 606590cd..e05c8402 100644 --- a/src/modules/explorer/pages/Proposals/index.tsx +++ b/src/modules/explorer/pages/Proposals/index.tsx @@ -338,7 +338,7 @@ export const Proposals: React.FC = () => { )} - {polls.length > 0 ? : null} + {polls.length > 0 ? : null} {proposals?.length === 0 && polls?.length === 0 ? ( diff --git a/src/modules/lite/explorer/components/ProposalList.tsx b/src/modules/lite/explorer/components/ProposalList.tsx index 1388c1d1..1bc59033 100644 --- a/src/modules/lite/explorer/components/ProposalList.tsx +++ b/src/modules/lite/explorer/components/ProposalList.tsx @@ -43,7 +43,11 @@ const Title = styled(Typography)(({ theme }) => ({ marginLeft: -2 } })) -export const ProposalList: React.FC<{ polls: Poll[]; id: string | undefined }> = ({ polls, id }) => { +export const ProposalList: React.FC<{ polls: Poll[]; id: string | undefined; daoId?: string }> = ({ + polls, + id, + daoId +}) => { const communityId = id?.toString() const openNotification = useNotification() const [communityPolls, setCommunityPolls] = useState() @@ -195,7 +199,7 @@ export const ProposalList: React.FC<{ polls: Poll[]; id: string | undefined }> = communityPolls.map((poll, i) => { return (
- + {communityPolls.length - 1 !== i ? : null}
) diff --git a/src/modules/lite/explorer/components/ProposalTableRow.tsx b/src/modules/lite/explorer/components/ProposalTableRow.tsx index 96300b54..d982cf8e 100644 --- a/src/modules/lite/explorer/components/ProposalTableRow.tsx +++ b/src/modules/lite/explorer/components/ProposalTableRow.tsx @@ -41,7 +41,7 @@ const DescriptionText = styled(Typography)(({ theme }) => ({ } })) -export const ProposalTableRow: React.FC<{ poll: Poll }> = ({ poll }) => { +export const ProposalTableRow: React.FC<{ poll: Poll; daoId?: string }> = ({ poll, daoId }) => { const navigate = useHistory() const theme = useTheme() const isMobile = useMediaQuery(theme.breakpoints.down("xs")) @@ -53,7 +53,9 @@ export const ProposalTableRow: React.FC<{ poll: Poll }> = ({ poll }) => { item container alignItems="center" - onClick={() => navigate.push(`/explorer/lite/dao/${poll.daoID}/community/proposal/${poll._id}`, { poll: poll })} + onClick={() => + navigate.push(`/explorer/lite/dao/${poll.daoID}/community/proposal/${poll._id}`, { poll: poll, daoId: daoId }) + } > diff --git a/src/modules/lite/explorer/pages/ProposalDetails/index.tsx b/src/modules/lite/explorer/pages/ProposalDetails/index.tsx index 94cba4aa..99819c83 100644 --- a/src/modules/lite/explorer/pages/ProposalDetails/index.tsx +++ b/src/modules/lite/explorer/pages/ProposalDetails/index.tsx @@ -1,22 +1,23 @@ -import React, { useContext, useEffect, useState } from "react" +import React, { useEffect, useState } from "react" import { Button, Grid, styled, useMediaQuery, useTheme } from "@material-ui/core" import { ProposalDetailCard } from "../../components/ProposalDetailCard" import { GridContainer } from "modules/common/GridContainer" import { ChoiceItemSelected } from "../../components/ChoiceItemSelected" import { VoteDetails } from "../../components/VoteDetails" -import { useHistory, useLocation, useParams } from "react-router-dom" +import { useLocation, useParams } from "react-router-dom" import { Poll } from "models/Polls" import { Choice } from "models/Choice" import { useTezos } from "services/beacon/hooks/useTezos" import { getSignature } from "services/lite/utils" import { useNotification } from "modules/common/hooks/useNotification" -import { useHasVoted } from "../../hooks/useHasVoted" import { usePollChoices } from "../../hooks/usePollChoices" import { useCommunity } from "../../hooks/useCommunity" 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" const PageContainer = styled("div")({ marginBottom: 50, @@ -51,18 +52,20 @@ export const ProposalDetails: React.FC<{ id: string }> = ({ id }) => { const theme = useTheme() const isMobileSmall = useMediaQuery(theme.breakpoints.down("sm")) - const navigate = useHistory() - const { state } = useLocation<{ poll: Poll; choices: Choice[] }>() + const { state } = useLocation<{ poll: Poll; choices: Choice[]; daoId: string }>() + + const { data: dao } = useDAO(state.daoId) const { account, wallet } = useTezos() const openNotification = useNotification() const [refresh, setRefresh] = useState() const community = useCommunity(id) const poll = useSinglePoll(proposalId, id, community) const choices = usePollChoices(poll, refresh) - + const { data: delegatedTo } = useDelegationStatus(dao?.data.token.contract) const [selectedVotes, setSelectedVotes] = useState([]) useEffect(() => { + // refetch() choices.map(elem => { return (elem.selected = false) }) @@ -153,7 +156,7 @@ export const ProposalDetails: React.FC<{ id: string }> = ({ id }) => { {poll?.isActive === ProposalStatus.ACTIVE ? ( + ) : ( )} diff --git a/src/modules/lite/explorer/components/ProposalList.tsx b/src/modules/lite/explorer/components/ProposalList.tsx index 1bc59033..d5c2bc28 100644 --- a/src/modules/lite/explorer/components/ProposalList.tsx +++ b/src/modules/lite/explorer/components/ProposalList.tsx @@ -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" }) diff --git a/src/modules/lite/explorer/hooks/useCommunity.tsx b/src/modules/lite/explorer/hooks/useCommunity.tsx index d8b85245..a424b886 100644 --- a/src/modules/lite/explorer/hooks/useCommunity.tsx +++ b/src/modules/lite/explorer/hooks/useCommunity.tsx @@ -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" }) diff --git a/src/modules/lite/explorer/hooks/useCommunityToken.tsx b/src/modules/lite/explorer/hooks/useCommunityToken.tsx index 4c0e76ab..551e064c 100644 --- a/src/modules/lite/explorer/hooks/useCommunityToken.tsx +++ b/src/modules/lite/explorer/hooks/useCommunityToken.tsx @@ -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() diff --git a/src/modules/lite/explorer/hooks/useHasVoted.tsx b/src/modules/lite/explorer/hooks/useHasVoted.tsx index 7f35c636..8766505a 100644 --- a/src/modules/lite/explorer/hooks/useHasVoted.tsx +++ b/src/modules/lite/explorer/hooks/useHasVoted.tsx @@ -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" }) diff --git a/src/modules/lite/explorer/hooks/usePoll.tsx b/src/modules/lite/explorer/hooks/usePoll.tsx index 31e11307..cdf5c8fb 100644 --- a/src/modules/lite/explorer/hooks/usePoll.tsx +++ b/src/modules/lite/explorer/hooks/usePoll.tsx @@ -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" }) diff --git a/src/modules/lite/explorer/hooks/usePollChoices.tsx b/src/modules/lite/explorer/hooks/usePollChoices.tsx index 87fcc996..a4ddfe40 100644 --- a/src/modules/lite/explorer/hooks/usePollChoices.tsx +++ b/src/modules/lite/explorer/hooks/usePollChoices.tsx @@ -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" }) diff --git a/src/modules/lite/explorer/hooks/usePolls.tsx b/src/modules/lite/explorer/hooks/usePolls.tsx index edfe2d5a..deabe3b3 100644 --- a/src/modules/lite/explorer/hooks/usePolls.tsx +++ b/src/modules/lite/explorer/hooks/usePolls.tsx @@ -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" }) @@ -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() diff --git a/src/modules/lite/explorer/hooks/useToken.tsx b/src/modules/lite/explorer/hooks/useToken.tsx index af8cad5b..a277f118 100644 --- a/src/modules/lite/explorer/hooks/useToken.tsx +++ b/src/modules/lite/explorer/hooks/useToken.tsx @@ -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" }) diff --git a/src/modules/lite/explorer/pages/CreateProposal/index.tsx b/src/modules/lite/explorer/pages/CreateProposal/index.tsx index 5c237ceb..50c74117 100644 --- a/src/modules/lite/explorer/pages/CreateProposal/index.tsx +++ b/src/modules/lite/explorer/pages/CreateProposal/index.tsx @@ -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 }) => ({ @@ -435,6 +436,7 @@ export const ProposalForm = ({ ) : null} = props = } const res = await saveLiteProposal(signature, publicKey, payloadBytes) + const respData = await res.json() if (res.ok) { openNotification({ message: "Proposal created!", @@ -627,7 +630,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" }) diff --git a/src/modules/lite/explorer/pages/ProposalDetails/index.tsx b/src/modules/lite/explorer/pages/ProposalDetails/index.tsx index 99819c83..1e91affc 100644 --- a/src/modules/lite/explorer/pages/ProposalDetails/index.tsx +++ b/src/modules/lite/explorer/pages/ProposalDetails/index.tsx @@ -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, @@ -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([]) useEffect(() => { @@ -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", @@ -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, @@ -156,7 +160,7 @@ export const ProposalDetails: React.FC<{ id: string }> = ({ id }) => { {poll?.isActive === ProposalStatus.ACTIVE ? (