Skip to content

Commit

Permalink
use correct voter token record when relinquishing veto (#1725)
Browse files Browse the repository at this point in the history
  • Loading branch information
asktree authored Jul 25, 2023
1 parent 5629549 commit 5f17b75
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 47 deletions.
48 changes: 7 additions & 41 deletions components/VotePanel/VetoButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,18 @@ import VoteCommentModal from '@components/VoteCommentModal'
import { BanIcon } from '@heroicons/react/solid'
import useRealm from '@hooks/useRealm'
import useWalletOnePointOh from '@hooks/useWalletOnePointOh'
import { VoteThresholdType, VoteKind } from '@solana/spl-governance'
import { useMemo, useState } from 'react'
import { useIsInCoolOffTime, useIsVoting, useVotingPop } from './hooks'
import { VoteKind } from '@solana/spl-governance'
import { useState } from 'react'
import {
useUserCommunityTokenOwnerRecord,
useUserCouncilTokenOwnerRecord,
} from '@hooks/queries/tokenOwnerRecord'
import { useRealmQuery } from '@hooks/queries/realm'
import { useProposalGovernanceQuery } from '@hooks/useProposal'
useIsInCoolOffTime,
useIsVoting,
useUserVetoTokenRecord,
useVetoingPop,
} from './hooks'
import { useProposalVoteRecordQuery } from '@hooks/queries/voteRecord'
import { useSubmitVote } from '@hooks/useSubmitVote'
import { useSelectedRealmInfo } from '@hooks/selectedRealm/useSelectedRealmRegistryEntry'

/*
returns: undefined if loading, false if nobody can veto, 'council' if council can veto, 'community' if community can veto
*/
export const useVetoingPop = () => {
const tokenRole = useVotingPop()
const governance = useProposalGovernanceQuery().data?.result
const realm = useRealmQuery().data?.result
const vetoingPop = useMemo(() => {
if (governance === undefined) return undefined

return tokenRole === 'community'
? governance?.account.config.councilVetoVoteThreshold.type !==
VoteThresholdType.Disabled &&
// if there is no council then there's not actually a vetoing population, in my opinion
realm?.account.config.councilMint !== undefined &&
'council'
: governance?.account.config.communityVetoVoteThreshold.type !==
VoteThresholdType.Disabled && 'community'
}, [governance, tokenRole, realm?.account.config.councilMint])

return vetoingPop
}

const useIsVetoable = (): undefined | boolean => {
const vetoingPop = useVetoingPop()
const isVoting = useIsVoting()
Expand All @@ -49,16 +25,6 @@ const useIsVetoable = (): undefined | boolean => {
return !!vetoingPop
}

const useUserVetoTokenRecord = () => {
const ownTokenRecord = useUserCommunityTokenOwnerRecord().data?.result
const ownCouncilTokenRecord = useUserCouncilTokenOwnerRecord().data?.result

const vetoingPop = useVetoingPop()
const voterTokenRecord =
vetoingPop === 'community' ? ownTokenRecord : ownCouncilTokenRecord
return voterTokenRecord
}

const useCanVeto = ():
| undefined
| { canVeto: true }
Expand Down
12 changes: 10 additions & 2 deletions components/VotePanel/YouVoted.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ import { SecondaryButton } from '../Button'
import { getProgramVersionForRealm } from '@models/registry/api'
import useVotePluginsClientStore from 'stores/useVotePluginsClientStore'
import Tooltip from '@components/Tooltip'
import { useVoterTokenRecord, useIsVoting, useIsInCoolOffTime } from './hooks'
import {
useVoterTokenRecord,
useIsVoting,
useIsInCoolOffTime,
useUserVetoTokenRecord,
} from './hooks'
import assertUnreachable from '@utils/typescript/assertUnreachable'
import { useHasVoteTimeExpired } from '@hooks/useHasVoteTimeExpired'
import { useMaxVoteRecord } from '@hooks/useMaxVoteRecord'
Expand Down Expand Up @@ -50,7 +55,10 @@ export const YouVoted = ({ quorum }: { quorum: 'electoral' | 'veto' }) => {

const { data } = useProposalVoteRecordQuery(quorum)
const ownVoteRecord = data?.result
const voterTokenRecord = useVoterTokenRecord()
const electoralVoterTokenRecord = useVoterTokenRecord()
const vetoVotertokenRecord = useUserVetoTokenRecord()
const voterTokenRecord =
quorum === 'electoral' ? electoralVoterTokenRecord : vetoVotertokenRecord

const isWithdrawEnabled =
connected &&
Expand Down
42 changes: 41 additions & 1 deletion components/VotePanel/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { useRouteProposalQuery } from '@hooks/queries/proposal'
import { useRealmQuery } from '@hooks/queries/realm'
import {
useUserCommunityTokenOwnerRecord,
useUserCouncilTokenOwnerRecord,
} from '@hooks/queries/tokenOwnerRecord'
import useRoleOfGovToken from '@hooks/selectedRealm/useRoleOfToken'
import { useHasVoteTimeExpired } from '@hooks/useHasVoteTimeExpired'
import { useProposalGovernanceQuery } from '@hooks/useProposal'
import { ProposalState, Proposal, Governance } from '@solana/spl-governance'
import {
ProposalState,
Proposal,
Governance,
VoteThresholdType,
} from '@solana/spl-governance'
import dayjs from 'dayjs'
import { useMemo } from 'react'

export const useIsVoting = () => {
const proposal = useRouteProposalQuery().data?.result
Expand Down Expand Up @@ -68,3 +75,36 @@ export const useVoterTokenRecord = () => {
votingPop === 'community' ? ownTokenRecord : ownCouncilTokenRecord
return voterTokenRecord
}

/*
returns: undefined if loading, false if nobody can veto, 'council' if council can veto, 'community' if community can veto
*/
export const useVetoingPop = () => {
const tokenRole = useVotingPop()
const governance = useProposalGovernanceQuery().data?.result
const realm = useRealmQuery().data?.result
const vetoingPop = useMemo(() => {
if (governance === undefined) return undefined

return tokenRole === 'community'
? governance?.account.config.councilVetoVoteThreshold.type !==
VoteThresholdType.Disabled &&
// if there is no council then there's not actually a vetoing population, in my opinion
realm?.account.config.councilMint !== undefined &&
'council'
: governance?.account.config.communityVetoVoteThreshold.type !==
VoteThresholdType.Disabled && 'community'
}, [governance, tokenRole, realm?.account.config.councilMint])

return vetoingPop
}

export const useUserVetoTokenRecord = () => {
const ownTokenRecord = useUserCommunityTokenOwnerRecord().data?.result
const ownCouncilTokenRecord = useUserCouncilTokenOwnerRecord().data?.result

const vetoingPop = useVetoingPop()
const voterTokenRecord =
vetoingPop === 'community' ? ownTokenRecord : ownCouncilTokenRecord
return voterTokenRecord
}
2 changes: 1 addition & 1 deletion components/VoteResultStatus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { XCircleIcon, CheckCircleIcon } from '@heroicons/react/outline'
import { BanIcon } from '@heroicons/react/solid'
import useProposalVotes from '@hooks/useProposalVotes'
import { ProposalState } from '@solana/spl-governance'
import { useVetoingPop } from './VotePanel/VetoButtons'
import { useRouteProposalQuery } from '@hooks/queries/proposal'
import { useVetoingPop } from './VotePanel/hooks'

const VetoResult = () => {
const vetoingPop = useVetoingPop()
Expand Down
3 changes: 1 addition & 2 deletions components/VotingRules.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useState } from 'react'
import { useVotingPop } from './VotePanel/hooks'
import { useVetoingPop, useVotingPop } from './VotePanel/hooks'
import {
ChevronRight,
ChevronUp,
Expand All @@ -17,7 +17,6 @@ import clsx from 'clsx'
import { TimerBar } from './ProposalTimer'
import { formatShortAddress } from '@cardinal/namespaces-components'
import { useAsync } from 'react-async-hook'
import { useVetoingPop } from './VotePanel/VetoButtons'
import useRealm from '@hooks/useRealm'
import { ACCOUNT_NAMES } from './instructions/tools'
import { ExploreButton } from './treasuryV2/Details/ExploreLink'
Expand Down

1 comment on commit 5f17b75

@vercel
Copy link

@vercel vercel bot commented on 5f17b75 Jul 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.