-
Notifications
You must be signed in to change notification settings - Fork 103
Governance - delegate to Drep #4443
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
2fcb90e
add card state from provider
SorinC6 de1cf8f
fix cards hover state
SorinC6 90bf307
add delegate impl on status page
SorinC6 506ec5f
Merge branch 'develop' into sorin/YOEXT-2371/gov-devegate
SorinC6 28af868
fix card UI
SorinC6 bf61105
add all options delegation
SorinC6 72498da
Merge branch 'develop' into sorin/YOEXT-2371/gov-devegate
SorinC6 8786f66
fix format
SorinC6 b4cc5ac
fix flow
SorinC6 731d72f
fix warnings
SorinC6 1a1351e
Merge branch 'develop' into sorin/YOEXT-2371/gov-devegate
SorinC6 7d9c598
ui fixes
SorinC6 5e21be8
update link
SorinC6 190e2ed
add preprod link, fix label
SorinC6 350f751
fix condition
SorinC6 eb147c5
fix condition
SorinC6 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| export function truncateFormatter(addr: string, cutoff: number): string { | ||
| const shortener = '...'; | ||
| if (addr.length - shortener.length <= cutoff) { | ||
| return addr; | ||
| } | ||
| return addr.substring(0, cutoff / 2) + shortener + addr.substring(addr.length - cutoff / 2, addr.length); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
190 changes: 190 additions & 0 deletions
190
...i-extension/app/UI/features/governace/common/hooks/useGovernanceDelegationToYoroiDrep.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,190 @@ | ||
| import * as React from 'react'; | ||
| import { NotEnoughMoneyToSendError } from '../../../../../api/common/errors'; | ||
| import { dRepToMaybeCredentialHex } from '../../../../../api/ada/lib/cardanoCrypto/utils'; | ||
| import { TransactionResult } from '../../../transaction-review/common/types'; | ||
| import { useGovernance } from '../../module/GovernanceContextProvider'; | ||
| import { useTxReviewModal } from '../../../transaction-review/module/ReviewTxProvider'; | ||
| import { useStrings } from './useStrings'; | ||
| import { Vote } from '../../module/state'; | ||
| import { DREP_ALWAYS_ABSTAIN, DREP_ALWAYS_NO_CONFIDENCE } from '../../common/constants'; | ||
|
|
||
| type UseGovernanceDelegationResult = { | ||
| loadingUnsignTx: boolean; | ||
| error: string | null; | ||
| setError: (value: string | null) => void; | ||
|
|
||
| // 1) direct delegation to a specific DRep (Yoroi or any other) | ||
| delegateToDrep: (drepID: string) => Promise<void>; | ||
|
|
||
| // 2) open modal to choose DRep id & delegate | ||
| openDelegateModalForCustomDrep: () => void; | ||
|
|
||
| // 3) always abstain | ||
| delegateToAbstain: () => Promise<void>; | ||
|
|
||
| // 4) always no-confidence | ||
| delegateToNoConfidence: () => Promise<void>; | ||
| }; | ||
|
|
||
| export const useGovernanceDelegationToYoroiDrep = (): UseGovernanceDelegationResult => { | ||
| const [error, setError] = React.useState<string | null>(null); | ||
| const [loadingUnsignTx, setLoadingUnsignTx] = React.useState<boolean>(false); | ||
|
|
||
| const strings = useStrings(); | ||
|
|
||
| const { governanceVoteChanged, createDrepDelegationTransaction, signDelegationTransaction, selectedWallet, governanceManager } = | ||
| useGovernance(); | ||
|
|
||
| const { | ||
| openTxReviewModal, | ||
| startLoadingTxReview, | ||
| stopLoadingTxReview, | ||
| changePasswordInputValue, | ||
| showTxResultModal, | ||
| setDrepId, | ||
| setUnsignedTx, | ||
| drepCredentialHex, | ||
| } = useTxReviewModal(); | ||
|
|
||
| const signGovernanceTx = React.useCallback( | ||
| async (password: string) => { | ||
| try { | ||
| startLoadingTxReview(); | ||
| await signDelegationTransaction({ | ||
| password, | ||
| wallet: selectedWallet, | ||
| dialog: null, | ||
| }); | ||
| stopLoadingTxReview(); | ||
| changePasswordInputValue({ type: 'changeInputValue', passswordInput: '' }); | ||
| showTxResultModal(TransactionResult.SUCCESS); | ||
| } catch (error) { | ||
| console.warn('[createDrepDelegationTransaction,signDelegationTransaction]', error); | ||
| stopLoadingTxReview(); | ||
| showTxResultModal(TransactionResult.FAIL); | ||
| } | ||
| }, | ||
| [ | ||
| startLoadingTxReview, | ||
| stopLoadingTxReview, | ||
| signDelegationTransaction, | ||
| selectedWallet, | ||
| changePasswordInputValue, | ||
| showTxResultModal, | ||
| ] | ||
| ); | ||
|
|
||
| const createUnsignTx = React.useCallback( | ||
| async (dRepCredentialHex: string | null) => { | ||
| try { | ||
| setLoadingUnsignTx(true); | ||
| const txSignRequest: any = await createDrepDelegationTransaction(dRepCredentialHex || ''); | ||
|
|
||
| openTxReviewModal({ | ||
| modalView: 'transactionReview', | ||
| unsignedTx: txSignRequest.signTxRequest.unsignedTx, | ||
| submitTx: (password: string) => { | ||
| void signGovernanceTx(password); | ||
| }, | ||
| operations: { | ||
| kind: 'delegate vote', | ||
| }, | ||
| }); | ||
|
|
||
| setError(null); | ||
| } catch (e) { | ||
| if (e instanceof NotEnoughMoneyToSendError) { | ||
| setError(strings.notEnoughMoneyToSendError); | ||
| } else { | ||
| setError('Error trying to Vote. Please try again later'); | ||
| } | ||
| } finally { | ||
| setLoadingUnsignTx(false); | ||
| } | ||
| }, | ||
| [createDrepDelegationTransaction, openTxReviewModal, signGovernanceTx, strings] | ||
| ); | ||
|
|
||
| /** 1) Delegate to a specific DRep (Yoroi or any other) */ | ||
| const delegateToDrep = React.useCallback( | ||
| async (drepID: string) => { | ||
| const vote: Vote = { kind: 'delegate', drepID }; | ||
| const dRepCredentialHex: string | null = dRepToMaybeCredentialHex(drepID); | ||
|
|
||
| governanceVoteChanged(vote); | ||
| setDrepId({ drepID }); | ||
| await createUnsignTx(dRepCredentialHex); | ||
| }, | ||
| [governanceVoteChanged, setDrepId, createUnsignTx] | ||
| ); | ||
|
|
||
| /** 2) Open modal to choose a custom DRep */ | ||
| const openDelegateModalForCustomDrep = React.useCallback(() => { | ||
| if (!governanceManager) { | ||
| return; | ||
| } | ||
|
|
||
| const vote: Vote = { kind: 'delegate', drepID: drepCredentialHex ?? '' }; | ||
| governanceVoteChanged(vote); | ||
|
|
||
| openTxReviewModal({ | ||
| title: 'CHOOSE YOUR DREP', | ||
| modalView: 'chooseOtherDrepId', | ||
| createUnsignedTx: async (value: string) => { | ||
| try { | ||
| startLoadingTxReview(); | ||
| const txSignRequest: any = await createDrepDelegationTransaction(value); | ||
| setUnsignedTx({ | ||
| type: 'setUnsignedTx', | ||
| unsignedTx: txSignRequest.signTxRequest.unsignedTx, | ||
| }); | ||
| } finally { | ||
| stopLoadingTxReview(); | ||
| } | ||
| }, | ||
| submitTx: (password: string) => { | ||
| void signGovernanceTx(password); | ||
| }, | ||
| operations: { | ||
| kind: 'delegate vote', | ||
| }, | ||
| }); | ||
| }, [ | ||
| governanceManager, | ||
| governanceVoteChanged, | ||
| drepCredentialHex, | ||
| openTxReviewModal, | ||
| startLoadingTxReview, | ||
| stopLoadingTxReview, | ||
| createDrepDelegationTransaction, | ||
| setUnsignedTx, | ||
| signGovernanceTx, | ||
| ]); | ||
|
|
||
| /** 3) Always abstain */ | ||
| const delegateToAbstain = React.useCallback(async () => { | ||
| const vote: Vote = { kind: DREP_ALWAYS_ABSTAIN }; | ||
|
|
||
| governanceVoteChanged(vote); | ||
| // For abstain / no-confidence we usually just pass the constant to the tx creation | ||
| await createUnsignTx(DREP_ALWAYS_ABSTAIN); | ||
| }, [governanceVoteChanged, createUnsignTx]); | ||
|
|
||
| /** 4) Always no-confidence */ | ||
| const delegateToNoConfidence = React.useCallback(async () => { | ||
| const vote: Vote = { kind: DREP_ALWAYS_NO_CONFIDENCE }; | ||
|
|
||
| governanceVoteChanged(vote); | ||
| await createUnsignTx(DREP_ALWAYS_NO_CONFIDENCE); | ||
| }, [governanceVoteChanged, createUnsignTx]); | ||
|
|
||
| return { | ||
| loadingUnsignTx, | ||
| error, | ||
| setError, | ||
| delegateToDrep, | ||
| openDelegateModalForCustomDrep, | ||
| delegateToAbstain, | ||
| delegateToNoConfidence, | ||
| }; | ||
| }; |
34 changes: 34 additions & 0 deletions
34
packages/yoroi-extension/app/UI/features/governace/common/hooks/useGovernanceStatusState.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import * as React from 'react'; | ||
| import { useGovernance } from '../../module/GovernanceContextProvider'; | ||
| import { GOVERNANCE_STATUS, GovernanceStatusState } from '../../common/constants'; | ||
|
|
||
| type UseGovernanceStatusStateResult = { | ||
| governanceStatusState: GovernanceStatusState; | ||
| governanceStatus: ReturnType<typeof useGovernance>['governanceStatus']; | ||
| isPendingDrepDelegationTx: boolean; | ||
| }; | ||
|
|
||
| export const useGovernanceStatusState = (): UseGovernanceStatusStateResult => { | ||
| const { governanceStatus, submitedTransactions } = useGovernance(); | ||
|
|
||
| const isPendingDrepDelegationTx = submitedTransactions.length > 0 && submitedTransactions[0]?.isDrepDelegation === true; | ||
|
|
||
| const governanceStatusState: GovernanceStatusState = React.useMemo(() => { | ||
| if (governanceStatus.status === 'none' && governanceStatus.drep === null) { | ||
| return GOVERNANCE_STATUS.IDLE; | ||
| } | ||
| if (governanceStatus.status === 'delegate' && governanceStatus.drep !== null) { | ||
| return GOVERNANCE_STATUS.DELEGATED; | ||
| } | ||
| if (isPendingDrepDelegationTx) { | ||
| return GOVERNANCE_STATUS.DISABLED; | ||
| } | ||
| return GOVERNANCE_STATUS.IDLE; | ||
| }, [governanceStatus.status, governanceStatus.drep, isPendingDrepDelegationTx]); | ||
|
|
||
| return { | ||
| governanceStatusState, | ||
| governanceStatus, | ||
| isPendingDrepDelegationTx, | ||
| }; | ||
| }; | ||
20 changes: 20 additions & 0 deletions
20
packages/yoroi-extension/app/UI/features/governace/common/hooks/useIsGovernanceAllowed.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import { networks } from '../../../../../api/ada/lib/storage/database/prepackaged/networks'; | ||
| import { useGovernance } from '../../module/GovernanceContextProvider'; | ||
|
|
||
| export const useIsGovernanceAllowed = () => { | ||
| const { governanceStatus, walletAdaBalance, networkId } = useGovernance(); | ||
|
|
||
| const isParticipating = governanceStatus.status != null && governanceStatus.status !== 'none'; | ||
|
|
||
| const hasZeroAda = walletAdaBalance !== null && walletAdaBalance === 0; | ||
| const isTestnet = networkId !== networks.CardanoMainnet.NetworkId; | ||
|
|
||
| const isNotAllowed = !isParticipating && hasZeroAda; | ||
|
|
||
| return { | ||
| isNotAllowed, | ||
| isParticipating, | ||
| hasZeroAda, | ||
| isTestnet, | ||
| }; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.