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

feat: add graceful support for max Celo votes #856

Merged
Merged
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
6 changes: 6 additions & 0 deletions .changeset/rich-ghosts-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"ledger-live-desktop": minor
"@ledgerhq/live-common": minor
---

Gracefully handle when user reaches the maximum number of Celo validator groups they can vote for.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import React, { useCallback } from "react";
import { useDispatch } from "react-redux";
import { Trans } from "react-i18next";
import { Trans, useTranslation } from "react-i18next";
import { openModal } from "~/renderer/actions/modals";
import Box from "~/renderer/components/Box";
import Modal, { ModalBody } from "~/renderer/components/Modal";
Expand All @@ -27,6 +27,7 @@ type Props = {
};

const ManageModal = ({ name, account, ...rest }: Props) => {
const { t } = useTranslation();
const { celoResources } = account;

invariant(celoResources, "celo account expected");
Expand All @@ -45,9 +46,10 @@ const ManageModal = ({ name, account, ...rest }: Props) => {
},
[dispatch, account],
);

const groupsVotedFor = [...new Set(celoResources.votes.map(v => v.validatorGroup))];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe useMemo would be useful here?...

const canVoteForNewGroup = celoResources.maxNumGroupsVotedFor.gt(groupsVotedFor.length);
const unlockingEnabled = celoResources.nonvotingLockedBalance?.gt(0);
const votingEnabled = celoResources.nonvotingLockedBalance?.gt(0);
const votingEnabled = celoResources.nonvotingLockedBalance?.gt(0) && canVoteForNewGroup;
const withdrawEnabled = availablePendingWithdrawals(account).length;
const activatingEnabled = activatableVotes(account).length;
const revokingEnabled = revokableVotes(account).length;
Expand Down Expand Up @@ -100,7 +102,15 @@ const ManageModal = ({ name, account, ...rest }: Props) => {
<Trans i18nKey="celo.manage.vote.title" />
</S.Title>
<S.Description>
<Trans i18nKey="celo.manage.vote.description" />
{canVoteForNewGroup ? (
<Trans i18nKey="celo.manage.vote.description" />
) : (
<>
{t("celo.manage.vote.descriptionMaxVotes", {
maxVotes: celoResources.maxNumGroupsVotedFor.toString(),
})}
</>
)}
</S.Description>
</S.InfoWrapper>
</S.ManageButton>
Expand Down
3 changes: 2 additions & 1 deletion apps/ledger-live-desktop/static/i18n/en/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -2180,7 +2180,8 @@
},
"vote": {
"title": "Vote",
"description": "Vote for validator groups. Ensure you activate your votes to earn rewards."
"description": "Vote for validator groups. Ensure you activate your votes to earn rewards.",
"descriptionMaxVotes": "You may vote for up to {{maxVotes}} validator groups. Revoke a vote to vote for a new validator group"
},
"activate": {
"title": "Activate vote",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Array [
"electionAddress": "0x8D6677192144292870907E3Fa8A5527fE55A7ff6",
"lockedBalance": "200000000000000000",
"lockedGoldAddress": "0x6cC083Aed9e3ebe302A6336dBC7c921C9f03349E",
"maxNumGroupsVotedFor": "10",
"nonvotingLockedBalance": "100000000000000000",
"pendingWithdrawals": Array [],
"registrationStatus": true,
Expand Down Expand Up @@ -60,6 +61,7 @@ Array [
"electionAddress": "0x8D6677192144292870907E3Fa8A5527fE55A7ff6",
"lockedBalance": "0",
"lockedGoldAddress": "0x6cC083Aed9e3ebe302A6336dBC7c921C9f03349E",
"maxNumGroupsVotedFor": "10",
"nonvotingLockedBalance": "0",
"pendingWithdrawals": Array [],
"registrationStatus": false,
Expand Down
2 changes: 1 addition & 1 deletion libs/ledger-live-common/src/families/celo/api/hubble.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const transactionToOperation = (
: false;
const data = transaction.data;
const sender = data?.Account || data?.from;
const recipient = data?.Group || data?.to;
const recipient = data?.Group || data?.to || data?.Raw?.address;
const fee = new BigNumber(transaction.data?.gas_used || 0).times(
new BigNumber(transaction.data?.gas_price || 0)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const getAccountShape: GetAccountShape = async (info) => {
const { address, currency, initialAccount, derivationMode } = info;
const oldOperations = initialAccount?.operations || [];
const election = await kit.contracts.getElection();
const electionConfig = await election.getConfig();
const lockedGold = await kit.contracts.getLockedGold();

const accountId = encodeAccountId({
Expand Down Expand Up @@ -56,6 +57,7 @@ const getAccountShape: GetAccountShape = async (info) => {
votes,
electionAddress: election.address,
lockedGoldAddress: lockedGold.address,
maxNumGroupsVotedFor: electionConfig.maxNumGroupsVotedFor,
},
};
return { ...shape, operations };
Expand Down
3 changes: 3 additions & 0 deletions libs/ledger-live-common/src/families/celo/serialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export function toCeloResourcesRaw(r: CeloResources): CeloResourcesRaw {
votes,
electionAddress,
lockedGoldAddress,
maxNumGroupsVotedFor,
} = r ?? {};
return {
registrationStatus,
Expand All @@ -31,6 +32,7 @@ export function toCeloResourcesRaw(r: CeloResources): CeloResourcesRaw {
})),
electionAddress,
lockedGoldAddress,
maxNumGroupsVotedFor: maxNumGroupsVotedFor?.toString(),
};
}

Expand All @@ -54,5 +56,6 @@ export function fromCeloResourcesRaw(r: CeloResourcesRaw): CeloResources {
})),
electionAddress: r.electionAddress,
lockedGoldAddress: r.lockedGoldAddress,
maxNumGroupsVotedFor: new BigNumber(r.maxNumGroupsVotedFor),
};
}
2 changes: 2 additions & 0 deletions libs/ledger-live-common/src/families/celo/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export type CeloResources = {
votes: CeloVote[] | null | undefined;
electionAddress: string | null | undefined;
lockedGoldAddress: string | null | undefined;
maxNumGroupsVotedFor: BigNumber;
};
export type CeloResourcesRaw = {
registrationStatus: boolean;
Expand All @@ -48,6 +49,7 @@ export type CeloResourcesRaw = {
votes: CeloVoteRaw[] | null | undefined;
electionAddress: string | null | undefined;
lockedGoldAddress: string | null | undefined;
maxNumGroupsVotedFor: string;
};
export type Transaction = TransactionCommon & {
family: "celo";
Expand Down