Skip to content

Commit

Permalink
Feature/relinquish votes (#616)
Browse files Browse the repository at this point in the history
* Adding numActiveVotes to gov provider

* adding logic

* relinquish
  • Loading branch information
bryzettler authored Jan 20, 2024
1 parent 7594664 commit 44b7a80
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 157 deletions.
40 changes: 20 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,33 @@
"@coral-xyz/anchor": "^0.28.0",
"@gorhom/bottom-sheet": "4.6.0",
"@gorhom/portal": "1.0.14",
"@helium/account-fetch-cache": "^0.6.21",
"@helium/account-fetch-cache-hooks": "^0.6.21",
"@helium/account-fetch-cache": "^0.6.22",
"@helium/account-fetch-cache-hooks": "^0.6.22",
"@helium/address": "4.10.2",
"@helium/circuit-breaker-sdk": "^0.6.21",
"@helium/circuit-breaker-sdk": "^0.6.22",
"@helium/crypto-react-native": "4.8.0",
"@helium/currency-utils": "^0.6.21",
"@helium/data-credits-sdk": "^0.6.21",
"@helium/distributor-oracle": "^0.6.21",
"@helium/fanout-sdk": "^0.6.21",
"@helium/helium-entity-manager-sdk": "^0.6.21",
"@helium/helium-react-hooks": "^0.6.21",
"@helium/helium-sub-daos-sdk": "^0.6.21",
"@helium/currency-utils": "^0.6.22",
"@helium/data-credits-sdk": "^0.6.22",
"@helium/distributor-oracle": "^0.6.22",
"@helium/fanout-sdk": "^0.6.22",
"@helium/helium-entity-manager-sdk": "^0.6.22",
"@helium/helium-react-hooks": "^0.6.22",
"@helium/helium-sub-daos-sdk": "^0.6.22",
"@helium/http": "4.7.5",
"@helium/idls": "^0.6.21",
"@helium/lazy-distributor-sdk": "^0.6.21",
"@helium/idls": "^0.6.22",
"@helium/lazy-distributor-sdk": "^0.6.22",
"@helium/modular-governance-hooks": "^0.0.8",
"@helium/modular-governance-idls": "^0.0.8-next",
"@helium/onboarding": "4.11.0",
"@helium/organization-sdk": "^0.0.8",
"@helium/proto-ble": "4.0.0",
"@helium/react-native-sdk": "^3.0.4",
"@helium/spl-utils": "^0.6.21",
"@helium/spl-utils": "^0.6.22",
"@helium/state-controller-sdk": "^0.0.8",
"@helium/transactions": "4.8.1",
"@helium/treasury-management-sdk": "^0.6.21",
"@helium/voter-stake-registry-hooks": "^0.6.21",
"@helium/voter-stake-registry-sdk": "^0.6.21",
"@helium/treasury-management-sdk": "^0.6.22",
"@helium/voter-stake-registry-hooks": "^0.6.22",
"@helium/voter-stake-registry-sdk": "^0.6.22",
"@helium/wallet-link": "4.11.0",
"@jup-ag/api": "^6.0.6",
"@ledgerhq/hw-app-solana": "7.0.13",
Expand Down Expand Up @@ -256,10 +256,10 @@
},
"resolutions": {
"@types/react": "17",
"@helium/account-fetch-cache": "^0.6.21",
"@helium/account-fetch-cache-hooks": "^0.6.21",
"@helium/helium-react-hooks": "^0.6.21",
"@helium/voter-stake-registry-hooks": "^0.6.21",
"@helium/account-fetch-cache": "^0.6.22",
"@helium/account-fetch-cache-hooks": "^0.6.22",
"@helium/helium-react-hooks": "^0.6.22",
"@helium/voter-stake-registry-hooks": "^0.6.22",
"@helium/modular-governance-hooks": "^0.0.8",
"@helium/onboarding": "4.11.0"
},
Expand Down
29 changes: 29 additions & 0 deletions src/features/governance/GovernanceScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,35 @@ export const GovernanceScreen = () => {
})
}
/>
{/* {numActiveVotes > 0 && (
<TouchableOpacityBox onPress={() => {}}>
<Box
flexDirection="row"
backgroundColor="secondaryBackground"
justifyContent="space-between"
alignItems="center"
borderRadius="l"
padding="m"
marginTop="m"
>
<Box flexDirection="row">
<Text
variant="body2"
color="secondaryText"
marginRight="xs"
>
Relinquish Active Votes:
</Text>
<Text variant="body2" color="secondaryText">
{numActiveVotes}
</Text>
</Box>
<Text variant="body2" color="white" marginRight="xs">
&gt;
</Text>
</Box>
</TouchableOpacityBox>
)} */}
<ProposalsList />
</Box>
)}
Expand Down
48 changes: 45 additions & 3 deletions src/features/governance/PositionCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ import {
useSplitPosition,
useTransferPosition,
useUndelegatePosition,
useRelinquishPositionVotes,
} from '@helium/voter-stake-registry-hooks'
import { organizationKey } from '@helium/organization-sdk'
import useAlert from '@hooks/useAlert'
import { useMetaplexMetadata } from '@hooks/useMetaplexMetadata'
import { BoxProps } from '@shopify/restyle'
Expand Down Expand Up @@ -72,8 +74,9 @@ export const PositionCard = ({
const [isExtendModalOpen, setIsExtendModalOpen] = useState(false)
const [isSplitModalOpen, setIsSplitModalOpen] = useState(false)
const [isDelegateModalOpen, setIsDelegateModalOpen] = useState(false)
const { positions, mint, refetch: refetchState } = useGovernance()
const { positions, mint, network, refetch: refetchState } = useGovernance()
const { backgroundStyle } = useCreateOpacity()
const organization = useMemo(() => organizationKey(network)[0], [network])
const transferablePositions: PositionWithMeta[] = useMemo(() => {
if (!unixNow || !positions || !positions.length) {
return []
Expand Down Expand Up @@ -244,6 +247,12 @@ export const PositionCard = ({
undelegatePosition,
} = useUndelegatePosition()

const {
loading: isRelinquishing,
error: relinquishingError,
relinquishPositionVotes,
} = useRelinquishPositionVotes()

const transactionError = useMemo(() => {
if (extendingError) {
return extendingError.message || t('gov.errors.extendLockup')
Expand Down Expand Up @@ -277,6 +286,10 @@ export const PositionCard = ({
if (undelegatingError) {
return undelegatingError.message || t('gov.errors.undelegatePosition')
}

if (relinquishingError) {
return relinquishingError.message || t('gov.errors.relinquishVotes')
}
}, [
t,
isConstant,
Expand All @@ -287,6 +300,7 @@ export const PositionCard = ({
closingError,
delegatingError,
undelegatingError,
relinquishingError,
])

const showError = useMemo(() => {
Expand Down Expand Up @@ -393,6 +407,19 @@ export const PositionCard = ({
})
}

const handleRelinquishVotes = async () => {
await relinquishPositionVotes({
position,
organization,
onInstructions: async (ixs) => {
await decideAndExecute(t('gov.transactions.relinquishPosition'), ixs)
if (!relinquishingError) {
refetchState()
}
},
})
}

const actions = () => {
return (
<>
Expand Down Expand Up @@ -500,14 +527,26 @@ export const PositionCard = ({
<ListItem
key="delegate"
title={t('gov.positions.delegate')}
onPress={async () => {
onPress={() => {
setIsDelegateModalOpen(true)
setActionsOpen(false)
}}
selected={false}
hasPressedState={false}
/>
)}
{hasActiveVotes && (
<ListItem
key="relinquish"
title={t('gov.positions.relinquish')}
onPress={async () => {
setActionsOpen(false)
await handleRelinquishVotes()
}}
selected={false}
hasPressedState={false}
/>
)}
</>
)}
</>
Expand All @@ -531,7 +570,8 @@ export const PositionCard = ({
isTransfering ||
isFlipping ||
isDelegating ||
isUndelegating,
isUndelegating ||
isRelinquishing,
[
loadingMetadata,
isExtending,
Expand All @@ -541,6 +581,7 @@ export const PositionCard = ({
isFlipping,
isDelegating,
isUndelegating,
isRelinquishing,
],
)

Expand All @@ -564,6 +605,7 @@ export const PositionCard = ({
{isFlipping && !isConstant && t('gov.positions.pausing')}
{isDelegating && t('gov.positions.delegating')}
{isUndelegating && t('gov.positions.undelegating')}
{isRelinquishing && t('gov.positions.relinquishing')}
</Text>
<Box flex={1} marginTop="ms" width="100%">
<IndeterminateProgressBar height={6} />
Expand Down
42 changes: 30 additions & 12 deletions src/features/governance/ProposalScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Box from '@components/Box'
import { DelayedFadeIn } from '@components/FadeInOut'
import SafeAreaBox from '@components/SafeAreaBox'
import Text from '@components/Text'
import TouchableOpacityBox from '@components/TouchableOpacityBox'
import { useMint } from '@helium/helium-react-hooks'
import {
useProposal,
Expand All @@ -19,7 +20,7 @@ import {
useRelinquishVote,
useVote,
} from '@helium/voter-stake-registry-hooks'
import { RouteProp, useRoute } from '@react-navigation/native'
import { RouteProp, useNavigation, useRoute } from '@react-navigation/native'
import { useTheme } from '@shopify/restyle'
import { PublicKey, TransactionInstruction } from '@solana/web3.js'
import { useAccountStorage } from '@storage/AccountStorageProvider'
Expand All @@ -43,6 +44,7 @@ import { WalletStandardMessageTypes } from '../../solana/walletSignBottomSheetTy
import { VoteOption } from './VoteOption'
import {
GovernanceStackParamList,
GovernanceNavigationProp,
ProposalV0,
VoteChoiceWithMeta,
VotingResultColors,
Expand All @@ -52,6 +54,7 @@ type Route = RouteProp<GovernanceStackParamList, 'ProposalScreen'>
export const ProposalScreen = () => {
const { t } = useTranslation()
const route = useRoute<Route>()
const navigation = useNavigation<GovernanceNavigationProp>()
const theme = useTheme<Theme>()
const { upsertAccount, currentAccount } = useAccountStorage()
const [currVote, setCurrVote] = useState(0)
Expand Down Expand Up @@ -388,18 +391,33 @@ export const ProposalScreen = () => {
</Box>
</Box>
{noVotingPower && (
<Box
flexGrow={1}
justifyContent="center"
backgroundColor="secondaryBackground"
borderRadius="l"
padding="m"
marginTop="m"
<TouchableOpacityBox
onPress={() =>
navigation.push('VotingPowerScreen', {
mint: mint.toBase58(),
})
}
>
<Text variant="body2" color="primaryText">
{t('gov.votingPower.noPower')}
</Text>
</Box>
<Box
flexDirection="row"
justifyContent="space-between"
alignItems="center"
flexGrow={1}
backgroundColor="secondaryBackground"
borderRadius="l"
padding="m"
marginTop="m"
>
<Text variant="body2" color="flamenco">
{`${t('gov.votingPower.noPower')}, ${t(
'gov.votingPower.increase',
)}`}
</Text>
<Text variant="subtitle1" color="primaryText">
&gt;
</Text>
</Box>
</TouchableOpacityBox>
)}
{derivedState === 'active' && !noVotingPower && (
<Box
Expand Down
4 changes: 4 additions & 0 deletions src/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,7 @@ export default {
increase: 'Increase your voting power by locking tokens.',
},
positions: {
relinquish: 'Relinquish Votes',
lockedAmount: 'Locked Amount {{amount}} {{symbol}}',
constant: 'Constant',
decaying: 'Decaying',
Expand All @@ -1215,6 +1216,7 @@ export default {
pausing: 'Pausing...',
delegating: 'Delegating...',
undelegating: 'Undelegating...',
relinquishing: 'Relinquishing...',
landrush: 'Landrush',
lockupType: 'Lockup Type',
amountToLock: 'Amount to lock',
Expand Down Expand Up @@ -1265,6 +1267,7 @@ export default {
transferPosition: 'Transfer Tokens',
delegatePosition: 'Delegate Position',
undelegatePosition: 'Undelegate Tokens',
relinquishPosition: 'Relinquish Votes',
},
errors: {
lockTokens: 'Lock failed, please try again.',
Expand All @@ -1280,6 +1283,7 @@ export default {
markdown: 'Failed to retrive proposal markdown.',
castVote: 'Vote failed, please try again.',
relinquishVote: 'Relinquish vote failed, please try again.',
relinquishVotes: 'Relinquish votes failed, please try again.',
},
},
}
2 changes: 1 addition & 1 deletion src/navigation/NavigationHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ export const navToImportAccount = (
params: HomeStackParamList['ReImportAccountNavigator'],
) => {
if (!navigationRef.isReady()) return
navigationRef.navigate('ReImportAccountNavigator' as never, params as never)
navigationRef.navigate('ReImportAccountNavigator' as any, params as any)
}
10 changes: 9 additions & 1 deletion src/storage/GovernanceProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,13 @@ const useGovernance = () => {
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { mint, ...heliumVsrState } = useHeliumVsrState()
const { mint, positions, ...heliumVsrState } = useHeliumVsrState()

const numActiveVotes = useMemo(
() => positions?.reduce((acc, p) => acc + p.numActiveVotes, 0) || 0,
[positions],
)

const loading = useMemo(
() => context.loading || heliumVsrState.loading,
[context.loading, heliumVsrState.loading],
Expand All @@ -166,6 +172,8 @@ const useGovernance = () => {
return {
...context,
...heliumVsrState,
numActiveVotes,
positions,
loading,
}
}
Expand Down
Loading

0 comments on commit 44b7a80

Please sign in to comment.