Skip to content

Commit

Permalink
refactor certification types
Browse files Browse the repository at this point in the history
  • Loading branch information
jorbuedo committed Oct 24, 2024
1 parent 6078843 commit daf0830
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// import {CredKind} from '@emurgo/csl-mobile-bridge'
import {CredKind} from '@emurgo/cross-csl-core'
import {isNonNullable} from '@yoroi/common'
import {infoExtractName} from '@yoroi/portfolio'
Expand All @@ -14,8 +13,7 @@ import {asQuantity} from '../../../../yoroi-wallets/utils/utils'
import {usePortfolioTokenInfos} from '../../../Portfolio/common/hooks/usePortfolioTokenInfos'
import {useSelectedWallet} from '../../../WalletManager/common/hooks/useSelectedWallet'
import {
Certificates,
FormattedCertificates,
FormattedCertificate,
FormattedFee,
FormattedInputs,
FormattedOutputs,
Expand Down Expand Up @@ -52,7 +50,7 @@ export const useFormattedTx = (data: TransactionBody): FormattedTx => {
const formattedInputs = useFormattedInputs(wallet, inputs, portfolioTokenInfos)
const formattedOutputs = useFormattedOutputs(wallet, outputs, portfolioTokenInfos)
const formattedFee = formatFee(wallet, data)
const formattedCertificates = formatCertificates(data.certs as Certificates)
const formattedCertificates = formatCertificates(data.certs)

return {
inputs: formattedInputs,
Expand Down Expand Up @@ -223,8 +221,13 @@ export const formatFee = (wallet: YoroiWallet, data: TransactionBody): Formatted
}
}

const formatCertificates = (certificates: Certificates) => {
return certificates.flatMap(Object.entries) as FormattedCertificates
const formatCertificates = (certificates: TransactionBody['certs']) => {
return (
certificates?.map((cert) => {
const [type, certificate] = Object.entries(cert)[0]
return {type, certificate} as unknown as FormattedCertificate
}) ?? null
)
}

const deriveAddress = async (address: string, chainId: number) => {
Expand Down
20 changes: 10 additions & 10 deletions apps/wallet-mobile/src/features/ReviewTx/common/operations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {asQuantity} from '../../../yoroi-wallets/utils/utils'
import {useSelectedNetwork} from '../../WalletManager/common/hooks/useSelectedNetwork'
import {useSelectedWallet} from '../../WalletManager/common/hooks/useSelectedWallet'
import {useStrings} from './hooks/useStrings'
import {Certificate, CertificateTypes, FormattedCertificates} from './types'
import {CertificateType, FormattedTx} from './types'

export const StakeRegistrationOperation = () => {
const {styles} = useStyles()
Expand Down Expand Up @@ -119,25 +119,25 @@ export const VoteDelegationOperation = ({drepID}: {drepID: string}) => {
)
}

export const useOperations = (certificates: FormattedCertificates | null) => {
export const useOperations = (certificates: FormattedTx['certificates']) => {
if (certificates === null) return []

return certificates.reduce<React.ReactNode[]>((acc, [certificateKind, CertificateData], index) => {
switch (certificateKind) {
case CertificateTypes.StakeRegistration:
return certificates.reduce<React.ReactNode[]>((acc, certificate, index) => {
switch (certificate.type) {
case CertificateType.StakeRegistration:
return [...acc, <StakeRegistrationOperation key={index} />]

case CertificateTypes.StakeDeregistration:
case CertificateType.StakeDeregistration:
return [...acc, <StakeDeregistrationOperation key={index} />]

case CertificateTypes.StakeDelegation: {
const poolKeyHash = (CertificateData as Certificate[CertificateTypes.StakeDelegation]).pool_keyhash ?? null
case CertificateType.StakeDelegation: {
const poolKeyHash = certificate.value.pool_keyhash ?? null
if (poolKeyHash == null) return acc
return [...acc, <StakeDelegateOperation key={index} poolId={poolKeyHash} />]
}

case CertificateTypes.VoteDelegation: {
const drep = (CertificateData as Certificate[CertificateTypes.VoteDelegation]).drep
case CertificateType.VoteDelegation: {
const drep = certificate.value.drep

if (drep === 'AlwaysAbstain') return [...acc, <AbstainOperation key={index} />]
if (drep === 'AlwaysNoConfidence') return [...acc, <NoConfidenceOperation key={index} />]
Expand Down
96 changes: 38 additions & 58 deletions apps/wallet-mobile/src/features/ReviewTx/common/types.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
import {
CommitteeColdResignJSON,
CommitteeHotAuthJSON,
DRepDeregistrationJSON,
DRepRegistrationJSON,
DRepUpdateJSON,
GenesisKeyDelegationJSON,
MoveInstantaneousRewardsCertJSON,
PoolRegistrationJSON,
PoolRetirementJSON,
StakeAndVoteDelegationJSON,
StakeDelegationJSON,
StakeDeregistrationJSON,
StakeRegistrationAndDelegationJSON,
StakeRegistrationJSON,
StakeVoteRegistrationAndDelegationJSON,
CertificateJSON,
TransactionBodyJSON,
TransactionInputsJSON,
TransactionOutputsJSON,
VoteDelegationJSON,
VoteRegistrationAndDelegationJSON,
} from '@emurgo/cardano-serialization-lib-nodejs'
import {CredKind} from '@emurgo/cross-csl-core'
import {Balance, Portfolio} from '@yoroi/types'
Expand Down Expand Up @@ -73,53 +57,49 @@ export type FormattedTx = {
inputs: FormattedInputs
outputs: FormattedOutputs
fee: FormattedFee
certificates: FormattedCertificates | null
certificates: FormattedCertificate[] | null
}

export type FormattedMetadata = {
hash: string | null
metadata: {msg: Array<unknown>} | null
}

export type Certificates = Array<Certificate>
export type FormattedCertificates = Array<[CertificateTypes, Certificate[CertificateTypes]]>
type AssertEqual<T, Expected> = T extends Expected
? Expected extends T
? true
: ['Type', Expected, 'is not equal to', T]
: ['Type', T, 'is not equal to', Expected]

export type Certificate = {
StakeRegistration: StakeRegistrationJSON
StakeDeregistration: StakeDeregistrationJSON
StakeDelegation: StakeDelegationJSON
PoolRegistration: PoolRegistrationJSON
PoolRetirement: PoolRetirementJSON
GenesisKeyDelegation: GenesisKeyDelegationJSON
MoveInstantaneousRewardsCert: MoveInstantaneousRewardsCertJSON
CommitteeHotAuth: CommitteeHotAuthJSON
CommitteeColdResign: CommitteeColdResignJSON
DRepDeregistration: DRepDeregistrationJSON
DRepRegistration: DRepRegistrationJSON
DRepUpdate: DRepUpdateJSON
StakeAndVoteDelegation: StakeAndVoteDelegationJSON
StakeRegistrationAndDelegation: StakeRegistrationAndDelegationJSON
StakeVoteRegistrationAndDelegation: StakeVoteRegistrationAndDelegationJSON
VoteDelegation: VoteDelegationJSON
VoteRegistrationAndDelegation: VoteRegistrationAndDelegationJSON
}
type UnionToIntersection<U> = (U extends unknown ? (x: U) => void : never) extends (x: infer I) => void ? I : never

export enum CertificateTypes {
StakeRegistration = 'StakeRegistration',
StakeDeregistration = 'StakeDeregistration',
StakeDelegation = 'StakeDelegation',
PoolRegistration = 'PoolRegistration',
PoolRetirement = 'PoolRetirement',
GenesisKeyDelegation = 'GenesisKeyDelegation',
MoveInstantaneousRewardsCert = 'MoveInstantaneousRewardsCert',
CommitteeHotAuth = 'CommitteeHotAuth',
CommitteeColdResign = 'CommitteeColdResign',
DRepDeregistration = 'DRepDeregistration',
DRepRegistration = 'DRepRegistration',
DRepUpdate = 'DRepUpdate',
StakeAndVoteDelegation = 'StakeAndVoteDelegation',
StakeRegistrationAndDelegation = 'StakeRegistrationAndDelegation',
StakeVoteRegistrationAndDelegation = 'StakeVoteRegistrationAndDelegation',
VoteDelegation = 'VoteDelegation',
VoteRegistrationAndDelegation = 'VoteRegistrationAndDelegation',
}
type Transformed<T> = {
[K in keyof UnionToIntersection<T>]: {type: K; value: UnionToIntersection<T>[K]}
}[keyof UnionToIntersection<T>]

export type FormattedCertificate = Transformed<CertificateJSON>

export const CertificateType = {
StakeRegistration: 'StakeRegistration',
StakeDeregistration: 'StakeDeregistration',
StakeDelegation: 'StakeDelegation',
PoolRegistration: 'PoolRegistration',
PoolRetirement: 'PoolRetirement',
GenesisKeyDelegation: 'GenesisKeyDelegation',
MoveInstantaneousRewardsCert: 'MoveInstantaneousRewardsCert',
CommitteeHotAuth: 'CommitteeHotAuth',
CommitteeColdResign: 'CommitteeColdResign',
DRepDeregistration: 'DRepDeregistration',
DRepRegistration: 'DRepRegistration',
DRepUpdate: 'DRepUpdate',
StakeAndVoteDelegation: 'StakeAndVoteDelegation',
StakeRegistrationAndDelegation: 'StakeRegistrationAndDelegation',
StakeVoteRegistrationAndDelegation: 'StakeVoteRegistrationAndDelegation',
VoteDelegation: 'VoteDelegation',
VoteRegistrationAndDelegation: 'VoteRegistrationAndDelegation',
} as const

export type CerificateType = (typeof CertificateType)[keyof typeof CertificateType]

// Makes sure CertificateType lists all the certificates in CertificateJSON
export type AssertAllImplementedCertTypes = AssertEqual<CerificateType, keyof UnionToIntersection<CertificateJSON>>

0 comments on commit daf0830

Please sign in to comment.