Skip to content

Commit 50a599c

Browse files
update vote fetching logic, delegate only on global page (#1165)
* update vote fetching logic, delegate only on global page * Update index.ts * add helper message on proposal page Co-authored-by: Noah Zinsmeister <noahwz@gmail.com>
1 parent 9c47327 commit 50a599c

File tree

3 files changed

+50
-25
lines changed

3 files changed

+50
-25
lines changed

src/constants/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ export const PROPOSAL_LENGTH_IN_DAYS = 7
2525

2626
export const GOVERNANCE_ADDRESS = '0x5e4be8Bc9637f0EAA1A755019e06A68ce081D58F'
2727

28+
export const TIMELOCK_ADDRESS = '0x1a9C8182C09F50C8318d769245beA52c32BE35BC'
29+
2830
const UNI_ADDRESS = '0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984'
2931
export const UNI: { [chainId in ChainId]: Token } = {
3032
[ChainId.MAINNET]: new Token(ChainId.MAINNET, UNI_ADDRESS, 18, 'UNI', 'Uniswap'),
@@ -35,9 +37,9 @@ export const UNI: { [chainId in ChainId]: Token } = {
3537
}
3638

3739
export const COMMON_CONTRACT_NAMES: { [address: string]: string } = {
38-
'0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984': 'UNI',
39-
'0x5e4be8Bc9637f0EAA1A755019e06A68ce081D58F': 'Governance Alpha',
40-
'0x1a9C8182C09F50C8318d769245beA52c32BE35BC': 'Timelock'
40+
[UNI_ADDRESS]: 'UNI',
41+
[GOVERNANCE_ADDRESS]: 'Governance',
42+
[TIMELOCK_ADDRESS]: 'Timelock'
4143
}
4244

4345
// TODO: specify merkle distributor for mainnet

src/pages/Vote/VotePage.tsx

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,20 @@ import { CardSection, DataCard } from '../../components/earn/styled'
99
import { ArrowLeft } from 'react-feather'
1010
import { ButtonPrimary } from '../../components/Button'
1111
import { ProposalStatus } from './styled'
12-
import { useProposalData, useUserVotes, useUserDelegatee, ProposalData } from '../../state/governance/hooks'
12+
import { useProposalData, useUserVotesAsOfBlock, ProposalData, useUserDelegatee } from '../../state/governance/hooks'
1313
import { useTimestampFromBlock } from '../../hooks/useTimestampFromBlock'
1414
import { DateTime } from 'luxon'
1515
import ReactMarkdown from 'react-markdown'
1616
import VoteModal from '../../components/vote/VoteModal'
1717
import { TokenAmount, JSBI } from '@uniswap/sdk'
18-
import { useTokenBalance } from '../../state/wallet/hooks'
1918
import { useActiveWeb3React } from '../../hooks'
20-
import { UNI, ZERO_ADDRESS, PROPOSAL_LENGTH_IN_DAYS, COMMON_CONTRACT_NAMES } from '../../constants'
19+
import { PROPOSAL_LENGTH_IN_DAYS, COMMON_CONTRACT_NAMES, UNI, ZERO_ADDRESS } from '../../constants'
2120
import { isAddress, getEtherscanLink } from '../../utils'
2221
import { ApplicationModal } from '../../state/application/actions'
2322
import { useModalOpen, useToggleDelegateModal, useToggleVoteModal } from '../../state/application/hooks'
2423
import DelegateModal from '../../components/vote/DelegateModal'
24+
import { GreyCard } from '../../components/Card'
25+
import { useTokenBalance } from '../../state/wallet/hooks'
2526

2627
const PageWrapper = styled(AutoColumn)`
2728
width: 100%;
@@ -102,7 +103,7 @@ export default function VotePage({
102103
params: { id }
103104
}
104105
}: RouteComponentProps<{ id: string }>) {
105-
const { account, chainId } = useActiveWeb3React()
106+
const { chainId, account } = useActiveWeb3React()
106107

107108
// get data for this specific proposal
108109
const proposalData: ProposalData | undefined = useProposalData(id)
@@ -132,11 +133,21 @@ export default function VotePage({
132133
const againstPercentage: string =
133134
proposalData && totalVotes ? ((proposalData.againstCount * 100) / totalVotes).toFixed(0) + '%' : '0%'
134135

135-
// show delegation option if they have have a balance, have not delegated
136-
const availableVotes: TokenAmount | undefined = useUserVotes()
136+
// only count available votes as of the proposal start block
137+
const availableVotes: TokenAmount | undefined = useUserVotesAsOfBlock(proposalData?.startBlock ?? undefined)
138+
139+
// only show voting if user has > 0 votes at proposal start block and proposal is active,
140+
const showVotingButtons =
141+
availableVotes &&
142+
JSBI.greaterThan(availableVotes.raw, JSBI.BigInt(0)) &&
143+
proposalData &&
144+
proposalData.status === 'active'
145+
137146
const uniBalance: TokenAmount | undefined = useTokenBalance(account ?? undefined, chainId ? UNI[chainId] : undefined)
138147
const userDelegatee: string | undefined = useUserDelegatee()
139-
const showUnlockVoting = Boolean(
148+
149+
// in blurb link to home page if they are able to unlock
150+
const showLinkForUnlock = Boolean(
140151
uniBalance && JSBI.notEqual(uniBalance.raw, JSBI.BigInt(0)) && userDelegatee === ZERO_ADDRESS
141152
)
142153

@@ -171,23 +182,22 @@ export default function VotePage({
171182
? 'Voting ends approximately ' + (endDate && endDate.toLocaleString(DateTime.DATETIME_FULL))
172183
: ''}
173184
</TYPE.main>
174-
{showUnlockVoting && endDate && endDate > now && (
175-
<ButtonPrimary
176-
style={{ width: 'fit-content' }}
177-
padding="8px"
178-
borderRadius="8px"
179-
onClick={toggelDelegateModal}
180-
>
181-
Unlock Voting
182-
</ButtonPrimary>
183-
)}
184185
</RowBetween>
186+
{proposalData && proposalData.status === 'active' && !showVotingButtons && (
187+
<GreyCard>
188+
<TYPE.black>
189+
Only UNI votes that were self delegated or delegated to another address before block{' '}
190+
{proposalData.startBlock} are eligible for voting.{' '}
191+
{showLinkForUnlock && (
192+
<span>
193+
<StyledInternalLink to="/vote">Unlock voting</StyledInternalLink> to prepare for the next proposal.
194+
</span>
195+
)}
196+
</TYPE.black>
197+
</GreyCard>
198+
)}
185199
</AutoColumn>
186-
{!showUnlockVoting &&
187-
availableVotes &&
188-
JSBI.greaterThan(availableVotes?.raw, JSBI.BigInt(0)) &&
189-
endDate &&
190-
endDate > now ? (
200+
{showVotingButtons ? (
191201
<RowFixed style={{ width: '100%', gap: '12px' }}>
192202
<ButtonPrimary
193203
padding="8px"

src/state/governance/hooks.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ export function useUserDelegatee(): string {
153153
return result?.[0] ?? undefined
154154
}
155155

156+
// gets the users current votes
156157
export function useUserVotes(): TokenAmount | undefined {
157158
const { account, chainId } = useActiveWeb3React()
158159
const uniContract = useUniContract()
@@ -163,6 +164,18 @@ export function useUserVotes(): TokenAmount | undefined {
163164
return votes && uni ? new TokenAmount(uni, votes) : undefined
164165
}
165166

167+
// fetch available votes as of block (usually proposal start block)
168+
export function useUserVotesAsOfBlock(block: number | undefined): TokenAmount | undefined {
169+
const { account, chainId } = useActiveWeb3React()
170+
const uniContract = useUniContract()
171+
172+
// check for available votes
173+
const uni = chainId ? UNI[chainId] : undefined
174+
const votes = useSingleCallResult(uniContract, 'getPriorVotes', [account ?? undefined, block ?? undefined])
175+
?.result?.[0]
176+
return votes && uni ? new TokenAmount(uni, votes) : undefined
177+
}
178+
166179
export function useDelegateCallback(): (delegatee: string | undefined) => undefined | Promise<string> {
167180
const { account, chainId, library } = useActiveWeb3React()
168181
const addTransaction = useTransactionAdder()

0 commit comments

Comments
 (0)