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

fix to enable vote delegatee to vote without needing to also lock #171

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
20 changes: 19 additions & 1 deletion src/features/transactions/TransactionFlow.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { DelegationForm } from 'src/features/delegation/DelegationForm';
import * as useDelegatees from 'src/features/delegation/hooks/useDelegatees';
import * as useDelegationBalances from 'src/features/delegation/hooks/useDelegationBalances';
import * as useGovernanceProposals from 'src/features/governance/hooks/useGovernanceProposals';
import * as votingHooks from 'src/features/governance/hooks/useVotingStatus';
import { VoteForm } from 'src/features/governance/VoteForm';
import * as useLockedStatus from 'src/features/locking/useLockedStatus';
import * as useStakingBalances from 'src/features/staking/useStakingBalances';
Expand Down Expand Up @@ -145,13 +146,25 @@ describe('<TransactionFlow />', () => {
await waitFor(async () => expect(await flow.findByTestId('register-form')).toBeTruthy());
});
});
describe('as a account which has been delegated votes', () => {
test('renders <VoteForm /> for account', async () => {
setupHooks({ isRegistered: true, lockedGoldBalance: 0n, votingPower: 1n });

const flow = render(
<TransactionFlow header="Test header" FormComponent={VoteForm} closeModal={() => {}} />,
);

await waitFor(async () => expect(await flow.findByTestId('vote-form')).toBeTruthy());
});
});
});
});

type SetupHooksOptions = {
isRegistered?: boolean;
lockedGoldBalance?: bigint;
voteSignerForAddress?: string;
votingPower?: bigint;
};

// Mocks all necessary hooks for the TransactionFlow component
Expand All @@ -166,14 +179,19 @@ const setupHooks = (options?: SetupHooksOptions) => {
isError: false,
isLoading: false,
} as any);

vi.spyOn(useDelegatees, 'useDelegatees').mockReturnValue({} as any);
vi.spyOn(useDelegationBalances, 'useDelegationBalances').mockReturnValue({} as any);
vi.spyOn(useLockedStatus, 'useLockedStatus').mockReturnValue({} as any);
vi.spyOn(useStakingBalances, 'useStakingBalances').mockReturnValue({} as any);
vi.spyOn(useGovernanceProposals, 'useGovernanceProposals').mockReturnValue({} as any);
vi.spyOn(useGovernanceProposals, 'useGovernanceProposal').mockReturnValue({} as any);

vi.spyOn(votingHooks, 'useGovernanceVotingPower').mockReturnValue({
isLoading: false,
votingPower: options?.votingPower ?? 0n,
isError: false,
});

vi.spyOn(hooks, 'useAccountDetails').mockReturnValue({
isError: false,
isLoading: false,
Expand Down
25 changes: 23 additions & 2 deletions src/features/transactions/TransactionFlow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { SpinnerWithLabel } from 'src/components/animation/Spinner';
import { AccountRegisterForm } from 'src/features/account/AccountRegisterForm';
import { useAccountDetails, useLockedBalance, useVoteSigner } from 'src/features/account/hooks';
import { DelegationForm } from 'src/features/delegation/DelegationForm';
import { useGovernanceVotingPower } from 'src/features/governance/hooks/useVotingStatus';
import { VoteForm } from 'src/features/governance/VoteForm';
import { LockForm } from 'src/features/locking/LockForm';
import { TransactionConfirmation } from 'src/features/transactions/TransactionConfirmation';
import { ConfirmationDetails, OnConfirmedFn } from 'src/features/transactions/types';
Expand Down Expand Up @@ -33,12 +35,31 @@ export function TransactionFlow<FormDefaults extends {}>({
const isDelegatingAsVoteSigner =
FormComponent.name === DelegationForm.name && voteSigner && voteSigner !== address;

const votingPower = useGovernanceVotingPower(address);

const hasVotingPower =
typeof votingPower.votingPower === 'bigint' && votingPower.votingPower > 0n;

// voting power includes votes delegated to the account and locked celo
const willVoteAndHasVotingPower = FormComponent.name === VoteForm.name && hasVotingPower;

let Component: ReactNode;
if (!address || isNullish(lockedBalance) || isNullish(isRegistered) || isVoteSignerLoading) {
if (
!address ||
isNullish(lockedBalance) ||
isNullish(isRegistered) ||
isVoteSignerLoading ||
votingPower.isLoading
) {
Component = <SpinnerWithLabel className="py-20">Loading account data...</SpinnerWithLabel>;
} else if (!isRegistered && !isDelegatingAsVoteSigner) {
Component = <AccountRegisterForm refetchAccountDetails={refetchAccountDetails} />;
} else if (lockedBalance <= 0n && requiresLockedFunds && !isDelegatingAsVoteSigner) {
} else if (
lockedBalance <= 0n &&
requiresLockedFunds &&
!isDelegatingAsVoteSigner &&
!willVoteAndHasVotingPower
) {
Component = <LockForm showTip={true} />;
} else if (!confirmationDetails) {
Component = <FormComponent defaultFormValues={defaultFormValues} onConfirmed={onConfirmed} />;
Expand Down