Skip to content

Commit

Permalink
chore(extension): [LW-10236]use drep registration tracker (#1115)
Browse files Browse the repository at this point in the history
  • Loading branch information
vetalcore authored May 7, 2024
1 parent 5a2aa15 commit 0bb7467
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 96 deletions.
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -15,7 +16,7 @@ export const VotingProceduresContainer = (): React.ReactElement => {
signTxRequest: { request },
dappInfo
} = useViewsFlowContext();
const { walletState } = useWalletStore();
const { inMemoryWallet } = useWalletStore();
const [votingProcedures, setVotingProcedures] = useState<Wallet.Cardano.VotingProcedures>([]);
const [isNonRegisteredUserModalVisible, setIsNonRegisteredUserModalVisible] = useState<boolean>(false);
const [userAckNonRegisteredState, setUserAckNonRegisteredState] = useState<boolean>(false);
Expand All @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<any>('@src/stores'),
Expand Down Expand Up @@ -68,11 +68,6 @@ jest.mock('react-i18next', () => {
};
});

jest.mock('../utils', () => ({
...jest.requireActual<any>('../utils'),
hasValidDrepRegistration: mockHasValidDrepRegistration
}));

jest.mock('../hooks', () => {
const original = jest.requireActual('../hooks');
return {
Expand Down Expand Up @@ -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(<span data-testid="VotingProcedures" />);
Expand Down Expand Up @@ -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)
}
}
}));
Expand Down Expand Up @@ -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)
}
}
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import {
getTxType,
drepIDasBech32FromHash,
pubDRepKeyToHash,
depositPaidWithSymbol,
hasValidDrepRegistration
depositPaidWithSymbol
} from '../utils';

jest.mock('@cardano-sdk/core', () => ({
Expand Down Expand Up @@ -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);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 0bb7467

Please sign in to comment.