Skip to content
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

chore(wallet-mobile): added support to drep scripts #3783

Merged
merged 5 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions apps/wallet-mobile/src/features/ReviewTx/common/operations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {wrappedCsl} from '../../../yoroi-wallets/cardano/wrappedCsl'
import {usePoolInfo} from '../../../yoroi-wallets/hooks'
import {formatTokenWithText} from '../../../yoroi-wallets/utils/format'
import {asQuantity, Quantities} from '../../../yoroi-wallets/utils/utils'
import {formatDrepHash} from '../../Staking/Governance/common/drep'
import {useSelectedWallet} from '../../WalletManager/common/hooks/useSelectedWallet'
import {useStrings} from './hooks/useStrings'
import {PoolDetails} from './PoolDetails'
Expand Down Expand Up @@ -129,26 +130,28 @@ export const NoConfidenceOperation = ({showWarning, strike}: {showWarning?: bool
}

export const VoteDelegationOperation = ({
drepID,
hash,
type,
showWarning,
strike,
}: {
drepID: string
hash: string
type: 'key' | 'script'
showWarning?: boolean
strike?: boolean
}) => {
const {styles} = useStyles()
const strings = useStrings()

const bech32DrepId = useDrepBech32Id(drepID)
const label = formatDrepHash(hash, type)

return (
<View style={styles.operation}>
<Label label={strings.delegateVotingToDRep} showWarning={showWarning} strike={strike} />

<Space width="lg" />

<Text style={[styles.operationValue, strike && styles.strike]}>{bech32DrepId ?? drepID}</Text>
<Text style={[styles.operationValue, strike && styles.strike]}>{label}</Text>
</View>
)
}
Expand Down Expand Up @@ -424,15 +427,17 @@ export const useOperations = (certificates: FormattedTx['certificates']) => {
totalFee: acc.totalFee,
}

const drepId = ('KeyHash' in drep ? drep.KeyHash : drep.ScriptHash) ?? ''
const hash = ('KeyHash' in drep ? drep.KeyHash : drep.ScriptHash) ?? ''
const type = 'KeyHash' in drep ? 'key' : 'script'
return {
components: [
...acc.components,
{
component: (
<VoteDelegationOperation
key={index}
drepID={drepId}
hash={hash}
type={type}
showWarning={isFirstElementDuplicated}
strike={isNotFirstElementDuplicated}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {convertDrepHashToCIP129Format} from '@yoroi/staking'

export const formatDrepHash = (hash: string, kind: 'script' | 'key'): string => {
try {
return convertDrepHashToCIP129Format(hash, kind)
} catch {
return hash
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const mapStakingKeyStateToGovernanceAction = (state: StakingKeyState): Go
? {kind: 'abstain'}
: vote.action === 'no-confidence'
? {kind: 'no-confidence'}
: {kind: 'delegate', drepID: vote.drepID}
: {kind: 'delegate', hash: vote.hash, type: vote.type}
}

export const useIsGovernanceFeatureEnabled = (wallet: YoroiWallet) => {
Expand Down Expand Up @@ -76,12 +76,20 @@ export const useGovernanceActions = () => {
const {updateLatestGovernanceAction} = useUpdateLatestGovernanceAction(wallet.id)
const {navigateToTxReview} = useWalletNavigation()

const handleDelegateAction = ({drepID, unsignedTx}: {drepID: string; unsignedTx: YoroiUnsignedTx}) => {
const handleDelegateAction = ({
hash,
unsignedTx,
type,
}: {
hash: string
type: 'key' | 'script'
unsignedTx: YoroiUnsignedTx
}) => {
unsignedTxChanged(unsignedTx)

navigateToTxReview({
onSuccess: (signedTx) => {
updateLatestGovernanceAction({kind: 'delegate-to-drep', drepID, txID: signedTx.signedTx.id})
updateLatestGovernanceAction({kind: 'delegate-to-drep', hash, type, txID: signedTx.signedTx.id})
navigateTo.submittedTx()
},
onError: navigateTo.failedTx,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ const votedDrepStakeKeyState: StakingKeyState = {
tx: 'txId',
slot: 1,
epoch: 1,
drepID: 'drepId',
hash: 'drepId',
type: 'key',
},
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export const useStrings = () => {
failedTxButton: intl.formatMessage(messages.failedTxButton),
failedTxText: intl.formatMessage(messages.failedTxText),
failedTxTitle: intl.formatMessage(messages.failedTxTitle),
invalidDRepId: intl.formatMessage(messages.invalidDRepId),
}
}

Expand Down Expand Up @@ -315,4 +316,8 @@ const messages = defineMessages({
id: 'components.governance.failedTxButton',
defaultMessage: '!!!Try again',
},
invalidDRepId: {
id: 'components.governance.invalidDRepId',
defaultMessage: '!!!Invalid DRep ID.',
},
})
5 changes: 4 additions & 1 deletion apps/wallet-mobile/src/features/Staking/Governance/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export type GovernanceVote = {kind: 'abstain'} | {kind: 'no-confidence'} | {kind: 'delegate'; drepID: string}
export type GovernanceVote =
| {kind: 'abstain'}
| {kind: 'no-confidence'}
| {kind: 'delegate'; hash: string; type: 'key' | 'script'}

export enum GovernanceKindMap {
abstain = 'Abstain',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const ChangeVoteScreen = () => {

if (!isNonNullable(action)) throw new Error('User has never voted')

const openDRepIdModal = (onSubmit: (drepId: string) => void) => {
const openDRepIdModal = (onSubmit: (options: {hash: string; type: 'script' | 'key'}) => void) => {
openModal(
strings.enterDRepID,
<GovernanceProvider manager={manager}>
Expand All @@ -59,13 +59,12 @@ export const ChangeVoteScreen = () => {
}

const handleDelegate = () => {
openDRepIdModal(async (drepID) => {
openDRepIdModal(async (options) => {
const stakingKey = await wallet.getStakingKey()
const vote = {kind: 'delegate', drepID} as const
setPendingVote(vote.kind)
setPendingVote('delegate')

createDelegationCertificate(
{drepID, stakingKey},
{hash: options.hash, type: options.type, stakingKey},
{
onSuccess: async (certificate) => {
const unsignedTx = await createGovernanceTxMutation.mutateAsync({
Expand All @@ -75,7 +74,8 @@ export const ChangeVoteScreen = () => {

governanceActions.handleDelegateAction({
unsignedTx,
drepID,
hash: options.hash,
type: options.type,
})
},
},
Expand All @@ -85,8 +85,7 @@ export const ChangeVoteScreen = () => {

const handleAbstain = async () => {
const stakingKey = await wallet.getStakingKey()
const vote = {kind: 'abstain'} as const
setPendingVote(vote.kind)
setPendingVote('abstain')

createVotingCertificate(
{vote: 'abstain', stakingKey},
Expand All @@ -107,8 +106,7 @@ export const ChangeVoteScreen = () => {

const handleNoConfidence = async () => {
const stakingKey = await wallet.getStakingKey()
const vote = {kind: 'no-confidence'} as const
setPendingVote(vote.kind)
setPendingVote('no-confidence')

createVotingCertificate(
{vote: 'no-confidence', stakingKey},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {CardanoMobile} from '../../../../../yoroi-wallets/wallets'
import {useStrings} from '../../common/strings'

type Props = {
onSubmit?: (drepId: string) => void
onSubmit?: (options: {type: 'key' | 'script'; hash: string}) => void
}

const FIND_DREPS_LINK = ''
Expand All @@ -24,16 +24,10 @@ export const EnterDrepIdModal = ({onSubmit}: Props) => {

const {error, isFetched, isFetching} = useIsValidDRepID(drepId, {retry: false, enabled: drepId.length > 0})

const handleOnPress = () => {
parseDrepId(drepId, CardanoMobile).then(({type, hash}) => {
if (type === 'key') {
onSubmit?.(hash)
return
}

Alert.alert(strings.error, strings.scriptNotSupported)
})
}
const handleOnPress = () =>
parseDrepId(drepId, CardanoMobile)
michaeljscript marked this conversation as resolved.
Show resolved Hide resolved
.then(({hash, type}) => onSubmit?.({hash, type}))
.catch(() => Alert.alert(strings.error, strings.invalidDRepId))

const handleOnLinkPress = () => {
Linking.openURL(FIND_DREPS_LINK)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {useFocusEffect} from '@react-navigation/native'
import {isNonNullable, isString} from '@yoroi/common'
import {
GovernanceProvider,
useBech32DRepID,
useDelegationCertificate,
useGovernance,
useLatestGovernanceAction,
Expand All @@ -27,6 +26,7 @@ import {
import {TransactionInfo} from '../../../../../yoroi-wallets/types/other'
import {useSelectedWallet} from '../../../../WalletManager/common/hooks/useSelectedWallet'
import {Action} from '../../common/Action/Action'
import {formatDrepHash} from '../../common/drep'
import {mapStakingKeyStateToGovernanceAction, useGovernanceActions} from '../../common/helpers'
import {LearnMoreLink} from '../../common/LearnMoreLink/LearnMoreLink'
import {useNavigateTo} from '../../common/navigation'
Expand Down Expand Up @@ -64,7 +64,7 @@ export const HomeScreen = () => {

if (txPendingDisplayed && isNonNullable(lastSubmittedTx)) {
if (lastSubmittedTx.kind === 'delegate-to-drep') {
const action: GovernanceVote = {kind: 'delegate', drepID: lastSubmittedTx.drepID}
const action: GovernanceVote = {kind: 'delegate', hash: lastSubmittedTx.hash, type: lastSubmittedTx.type}
return <ParticipatingInGovernanceVariant action={action} isTxPending />
}
if (lastSubmittedTx.kind === 'vote' && lastSubmittedTx.vote === 'abstain') {
Expand Down Expand Up @@ -95,9 +95,8 @@ const ParticipatingInGovernanceVariant = ({
const strings = useStrings()
const {styles} = useStyles()
const navigateTo = useNavigateTo()
const {data: bech32DrepId} = useBech32DRepID(action.kind === 'delegate' ? action.drepID : '', {
enabled: action.kind === 'delegate',
})

const displayedHash = action.kind === 'delegate' ? formatDrepHash(action.hash, action.type) : null

const actionTitles = {
abstain: strings.actionAbstainTitle,
Expand Down Expand Up @@ -133,7 +132,7 @@ const ParticipatingInGovernanceVariant = ({
>
<Text style={styles.drepInfoTitle}>{strings.drepID}</Text>

<Text style={styles.drepInfoDescription}>{bech32DrepId ?? action.drepID}</Text>
<Text style={styles.drepInfoDescription}>{displayedHash}</Text>
</Action>
)}

Expand Down Expand Up @@ -220,7 +219,7 @@ const NeverParticipatedInGovernanceVariant = () => {
},
})

const openDRepIdModal = (onSubmit: (drepId: string) => void) => {
const openDRepIdModal = (onSubmit: (options: {hash: string; type: 'key' | 'script'}) => void) => {
track.governanceChooseDrepPageViewed()

openModal(
Expand All @@ -233,14 +232,13 @@ const NeverParticipatedInGovernanceVariant = () => {
}

const handleDelegate = () => {
openDRepIdModal(async (drepID) => {
const vote = {kind: 'delegate', drepID} as const
openDRepIdModal(async (options) => {
const stakingKey = await wallet.getStakingKey()

setPendingVote(vote.kind)
setPendingVote('delegate')

createDelegationCertificate(
{drepID, stakingKey},
{hash: options.hash, type: options.type, stakingKey},
{
onSuccess: async (certificate) => {
const stakeCert = needsToRegisterStakingKey
Expand All @@ -251,7 +249,8 @@ const NeverParticipatedInGovernanceVariant = () => {

governanceActions.handleDelegateAction({
unsignedTx,
drepID,
hash: options.hash,
type: options.type,
})
},
},
Expand All @@ -261,8 +260,7 @@ const NeverParticipatedInGovernanceVariant = () => {

const handleAbstain = async () => {
const stakingKey = await wallet.getStakingKey()
const vote = {kind: 'abstain'} as const
setPendingVote(vote.kind)
setPendingVote('abstain')

createVotingCertificate(
{vote: 'abstain', stakingKey},
Expand All @@ -284,8 +282,7 @@ const NeverParticipatedInGovernanceVariant = () => {

const handleNoConfidence = async () => {
const stakingKey = await wallet.getStakingKey()
const vote = {kind: 'no-confidence'} as const
setPendingVote(vote.kind)
setPendingVote('no-confidence')

createVotingCertificate(
{vote: 'no-confidence', stakingKey},
Expand Down
1 change: 1 addition & 0 deletions apps/wallet-mobile/src/kernel/i18n/locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@
"components.governance.enterDrepIDInfo": "Identify your preferred DRep and enter their ID below to delegate your vote",
"components.governance.enterPassword": "Enter password to sign this transaction",
"components.governance.findDRepHere": "Find a DRep here",
"components.governance.invalidDRepId": "Invalid DRep ID",
"components.governance.goToGovernance": "Go to Governance",
"components.governance.goToStaking": "Go to staking",
"components.governance.goToWallet": "Go to wallet",
Expand Down
Loading
Loading