Skip to content

Commit

Permalink
feat: showing warning for top validators on staking (#1448)
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrorezende authored Dec 26, 2024
1 parent 9c789c2 commit 739910d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
30 changes: 30 additions & 0 deletions apps/namadillo/src/App/Common/IconTooltip.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Tooltip } from "@namada/components";
import clsx from "clsx";
import { twMerge } from "tailwind-merge";

type IconTooltipProps = {
icon: React.ReactNode;
text: string;
className?: string;
};

export const IconTooltip = ({
icon,
text,
className,
}: IconTooltipProps): JSX.Element => {
return (
<i
className={twMerge(
clsx(
"group/tooltip bg-neutral-600 inline-flex not-italic text-[8px]",
"w-3.5 h-3.5 rounded-full items-center justify-center",
className
)
)}
>
{icon}
<Tooltip className="z-50 w-68">{text}</Tooltip>
</i>
);
};
20 changes: 19 additions & 1 deletion apps/namadillo/src/App/Staking/IncrementBondingTable.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { TableRow } from "@namada/components";
import { formatPercentage } from "@namada/utils";
import { IconTooltip } from "App/Common/IconTooltip";
import { NamCurrency } from "App/Common/NamCurrency";
import BigNumber from "bignumber.js";
import clsx from "clsx";
import { useValidatorTableSorting } from "hooks/useValidatorTableSorting";
import { getTopValidatorsAddresses } from "lib/staking";
import { useMemo } from "react";
import { FaExclamation } from "react-icons/fa6";
import { Validator } from "types";
import { AmountField } from "./AmountField";
import { ValidatorCard } from "./ValidatorCard";
Expand All @@ -29,6 +33,10 @@ export const IncrementBondingTable = ({
stakedAmountByAddress,
});

const topValidatorsByRank = useMemo(() => {
return getTopValidatorsAddresses(validators);
}, [validators]);

const headers = [
{ children: "Validator" },
"Amount to Stake",
Expand Down Expand Up @@ -73,8 +81,18 @@ export const IncrementBondingTable = ({
// Amount Text input
<div
key={`increment-bonding-new-amounts-${validator.address}`}
className="min-w-[24ch]"
className="min-w-[24ch] relative"
>
{topValidatorsByRank.includes(validator.address) ?
<IconTooltip
className={clsx(
"hidden absolute -left-6 bg-fail text-black top-1/2 -translate-y-1/2 z-50",
{ "inline-flex": updatedAmountByAddress[validator.address] }
)}
icon={<FaExclamation />}
text="Consider staking to validators outside of the top 10 to increase decentralization"
/>
: null}
<AmountField
placeholder="Select to increase stake"
value={updatedAmountByAddress[validator.address]}
Expand Down
16 changes: 15 additions & 1 deletion apps/namadillo/src/lib/staking.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import BigNumber from "bignumber.js";
import { RedelegateChange } from "types";
import { RedelegateChange, Validator } from "types";

export const getReducedAmounts = (
stakedAmounts: Record<string, BigNumber>,
Expand All @@ -17,6 +17,20 @@ export const getReducedAmounts = (
return reducedAmounts;
};

export const getTopValidatorsAddresses = (
validators: Validator[],
topNumber = 10
): string[] => {
return validators
.sort((v1: Validator, v2: Validator): number => {
if (!v1.votingPowerInNAM) return -1;
if (!v2.votingPowerInNAM) return 1;
return v1.votingPowerInNAM.gt(v2.votingPowerInNAM) ? -1 : 1;
})
.slice(0, topNumber)
.map((validator) => validator.address);
};

export const getIncrementedAmounts = (
stakedAmounts: Record<string, BigNumber>,
updatedAmounts: Record<string, BigNumber>
Expand Down

1 comment on commit 739910d

@github-actions
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.