diff --git a/apps/browser-extension-wallet/src/features/dapp/components/confirm-transaction/VotingProceduresContainer.tsx b/apps/browser-extension-wallet/src/features/dapp/components/confirm-transaction/VotingProceduresContainer.tsx index 771506372..fbe3515f7 100644 --- a/apps/browser-extension-wallet/src/features/dapp/components/confirm-transaction/VotingProceduresContainer.tsx +++ b/apps/browser-extension-wallet/src/features/dapp/components/confirm-transaction/VotingProceduresContainer.tsx @@ -1,10 +1,11 @@ import React, { useEffect, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { VotingProcedures } from '@lace/core'; -import { getDRepId, hasValidDrepRegistration, votingProceduresInspector } from './utils'; +import { getDRepId, votingProceduresInspector } from './utils'; import { useCexplorerBaseUrl, useDisallowSignTx } from './hooks'; import { getVote, getVoterType } from '@src/utils/tx-inspection'; import { Wallet } from '@lace/cardano'; + import { NonRegisteredUserModal } from './NonRegisteredUserModal/NonRegisteredUserModal'; import { useViewsFlowContext } from '@providers'; import { useWalletStore } from '@src/stores'; @@ -15,7 +16,7 @@ export const VotingProceduresContainer = (): React.ReactElement => { signTxRequest: { request }, dappInfo } = useViewsFlowContext(); - const { walletState } = useWalletStore(); + const { inMemoryWallet } = useWalletStore(); const [votingProcedures, setVotingProcedures] = useState([]); const [isNonRegisteredUserModalVisible, setIsNonRegisteredUserModalVisible] = useState(false); const [userAckNonRegisteredState, setUserAckNonRegisteredState] = useState(false); @@ -31,9 +32,15 @@ export const VotingProceduresContainer = (): React.ReactElement => { }, [request]); useEffect(() => { - if (!walletState?.transactions.history || userAckNonRegisteredState) return; - setIsNonRegisteredUserModalVisible(!hasValidDrepRegistration(walletState.transactions.history)); - }, [walletState?.transactions.history, userAckNonRegisteredState]); + if (userAckNonRegisteredState) return () => void 0; + const subscription = inMemoryWallet?.governance?.isRegisteredAsDRep$?.subscribe( + (hasValidDrepRegistration): void => { + setIsNonRegisteredUserModalVisible(!hasValidDrepRegistration); + } + ); + + return () => subscription?.unsubscribe(); + }, [inMemoryWallet?.governance?.isRegisteredAsDRep$, userAckNonRegisteredState]); const explorerBaseUrl = useCexplorerBaseUrl(); diff --git a/apps/browser-extension-wallet/src/features/dapp/components/confirm-transaction/__tests__/VotingProceduresContainer.test.tsx b/apps/browser-extension-wallet/src/features/dapp/components/confirm-transaction/__tests__/VotingProceduresContainer.test.tsx index bfb5f9646..6e14b70ef 100644 --- a/apps/browser-extension-wallet/src/features/dapp/components/confirm-transaction/__tests__/VotingProceduresContainer.test.tsx +++ b/apps/browser-extension-wallet/src/features/dapp/components/confirm-transaction/__tests__/VotingProceduresContainer.test.tsx @@ -9,7 +9,6 @@ const mockUseTranslation = jest.fn(() => ({ t })); const mockVotingProcedures = jest.fn(); const mockNonRegisteredUserModal = jest.fn(); const mockUseDisallowSignTx = jest.fn(); -const mockHasValidDrepRegistration = jest.fn(); const mockPreprodCexplorerBaseUrl = 'PREPROD_CEXPLORER_BASE_URL'; const mockCexplorerUrlPathsTx = 'CEXPLORER_URL_PATHS.TX'; import * as React from 'react'; @@ -23,6 +22,7 @@ import { getWrapper } from '../testing.utils'; import { getVoterType, getVote, VoterTypeEnum, VotesEnum } from '@src/utils/tx-inspection'; import { drepIDasBech32FromHash } from '../utils'; import { TransactionWitnessRequest } from '@cardano-sdk/web-extension'; +import { of } from 'rxjs'; jest.mock('@src/stores', () => ({ ...jest.requireActual('@src/stores'), @@ -68,11 +68,6 @@ jest.mock('react-i18next', () => { }; }); -jest.mock('../utils', () => ({ - ...jest.requireActual('../utils'), - hasValidDrepRegistration: mockHasValidDrepRegistration -})); - jest.mock('../hooks', () => { const original = jest.requireActual('../hooks'); return { @@ -179,11 +174,12 @@ jest.mock('@providers', () => ({ describe('Testing VotingProceduresContainer component', () => { beforeEach(() => { - mockHasValidDrepRegistration.mockReset(); - mockHasValidDrepRegistration.mockReturnValue(true); mockUseWalletStore.mockReset(); mockUseWalletStore.mockImplementation(() => ({ - environmentName: 'Preprod' + environmentName: 'Preprod', + inMemoryWallet: { + governance: {} + } })); mockVotingProcedures.mockReset(); mockVotingProcedures.mockReturnValue(); @@ -254,14 +250,12 @@ describe('Testing VotingProceduresContainer component', () => { }); test('should handle NonRegisteredUserModal onConfirm', async () => { - mockHasValidDrepRegistration.mockReset(); - mockHasValidDrepRegistration.mockReturnValue(false); mockUseWalletStore.mockReset(); mockUseWalletStore.mockImplementation(() => ({ environmentName: 'Preprod', - walletState: { - transactions: { - history: [] + inMemoryWallet: { + governance: { + isRegisteredAsDRep$: of(false) } } })); @@ -291,14 +285,12 @@ describe('Testing VotingProceduresContainer component', () => { const disallowSignTxMock = jest.fn(); mockUseDisallowSignTx.mockReset(); mockUseDisallowSignTx.mockReturnValue(disallowSignTxMock); - mockHasValidDrepRegistration.mockReset(); - mockHasValidDrepRegistration.mockReturnValue(false); mockUseWalletStore.mockReset(); mockUseWalletStore.mockImplementation(() => ({ environmentName: 'Preprod', - walletState: { - transactions: { - history: [] + inMemoryWallet: { + governance: { + isRegisteredAsDRep$: of(false) } } })); diff --git a/apps/browser-extension-wallet/src/features/dapp/components/confirm-transaction/__tests__/utils.test.tsx b/apps/browser-extension-wallet/src/features/dapp/components/confirm-transaction/__tests__/utils.test.tsx index ca920d6f4..919dad9d2 100644 --- a/apps/browser-extension-wallet/src/features/dapp/components/confirm-transaction/__tests__/utils.test.tsx +++ b/apps/browser-extension-wallet/src/features/dapp/components/confirm-transaction/__tests__/utils.test.tsx @@ -17,8 +17,7 @@ import { getTxType, drepIDasBech32FromHash, pubDRepKeyToHash, - depositPaidWithSymbol, - hasValidDrepRegistration + depositPaidWithSymbol } from '../utils'; jest.mock('@cardano-sdk/core', () => ({ @@ -171,59 +170,4 @@ describe('Testing utils', () => { 'coinId Unknown not supported' ); }); - - describe('hasValidDrepRegistration', () => { - test('should return false if there transactions', () => { - const transactions = [] as unknown as Wallet.Cardano.HydratedTx[]; - expect(hasValidDrepRegistration(transactions)).toBe(false); - }); - - test('should return false if there is no certificates', () => { - const transactions = [{ body: {} }, { body: { certificates: [] } }] as unknown as Wallet.Cardano.HydratedTx[]; - expect(hasValidDrepRegistration(transactions)).toBe(false); - }); - - test('should return true if first certificate has RegisterDelegateRepresentative __typename', () => { - const transactions = [ - { - body: { - certificates: [ - { __typename: Wallet.Cardano.CertificateType.RegisterDelegateRepresentative }, - { __typename: Wallet.Cardano.CertificateType.UnregisterDelegateRepresentative } - ] - } - }, - { - body: { - certificates: [ - { __typename: Wallet.Cardano.CertificateType.UnregisterDelegateRepresentative }, - { __typename: Wallet.Cardano.CertificateType.RegisterDelegateRepresentative } - ] - } - } - ] as unknown as Wallet.Cardano.HydratedTx[]; - expect(hasValidDrepRegistration(transactions)).toBe(true); - }); - test('should return false if first certificate has UnregisterDelegateRepresentative __typename', () => { - const transactions = [ - { - body: { - certificates: [ - { __typename: Wallet.Cardano.CertificateType.UnregisterDelegateRepresentative }, - { __typename: Wallet.Cardano.CertificateType.RegisterDelegateRepresentative } - ] - } - }, - { - body: { - certificates: [ - { __typename: Wallet.Cardano.CertificateType.RegisterDelegateRepresentative }, - { __typename: Wallet.Cardano.CertificateType.UnregisterDelegateRepresentative } - ] - } - } - ] as unknown as Wallet.Cardano.HydratedTx[]; - expect(hasValidDrepRegistration(transactions)).toBe(false); - }); - }); }); diff --git a/apps/browser-extension-wallet/src/features/dapp/components/confirm-transaction/utils.ts b/apps/browser-extension-wallet/src/features/dapp/components/confirm-transaction/utils.ts index aa50a42c2..be1de143b 100644 --- a/apps/browser-extension-wallet/src/features/dapp/components/confirm-transaction/utils.ts +++ b/apps/browser-extension-wallet/src/features/dapp/components/confirm-transaction/utils.ts @@ -168,21 +168,6 @@ export const depositPaidWithSymbol = (deposit: bigint, coinId: Wallet.CoinId): s } }; -export const hasValidDrepRegistration = (history: Wallet.Cardano.HydratedTx[]): boolean => { - for (const transaction of history) { - const drepRegistrationOrRetirementCerticicate = transaction.body.certificates?.find((cert) => - [CertificateType.UnregisterDelegateRepresentative, CertificateType.RegisterDelegateRepresentative].includes( - cert.__typename - ) - ); - - if (drepRegistrationOrRetirementCerticicate) { - return drepRegistrationOrRetirementCerticicate.__typename === CertificateType.RegisterDelegateRepresentative; - } - } - return false; -}; - export const getDRepId = (voter: Wallet.Cardano.Voter): Wallet.Cardano.DRepID | string => getVoterType(voter.__typename) === VoterTypeEnum.DREP ? drepIDasBech32FromHash(voter.credential.hash)