Skip to content

feat: validators msr endpoint + atom + calc #1807

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

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
2 changes: 1 addition & 1 deletion apps/namadillo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"@cosmjs/encoding": "^0.32.3",
"@keplr-wallet/types": "^0.12.136",
"@namada/chain-registry": "^1.0.0",
"@namada/indexer-client": "2.2.0",
"@namada/indexer-client": "../../../namada-indexer-client",
"@tailwindcss/container-queries": "^0.1.1",
"@tanstack/query-core": "^5.40.0",
"@tanstack/react-query": "^5.40.0",
Expand Down
4 changes: 2 additions & 2 deletions apps/namadillo/src/App/Staking/AllValidatorsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { TableRowLoading } from "App/Common/TableRowLoading";
import { WalletAddress } from "App/Common/WalletAddress";
import { routes } from "App/routes";
import { atomsAreLoading, atomsAreNotInitialized } from "atoms/utils";
import { allValidatorsAtom } from "atoms/validators";
import { allValidatorsWithMSRAtom } from "atoms/validators";
import BigNumber from "bignumber.js";
import { useUserHasAccount } from "hooks/useIsAuthenticated";
import { useValidatorFilter } from "hooks/useValidatorFilter";
Expand All @@ -28,7 +28,7 @@ export const AllValidatorsTable = ({
resultsPerPage = 100,
initialPage = 0,
}: AllValidatorsProps): JSX.Element => {
const validators = useAtomValue(allValidatorsAtom);
const validators = useAtomValue(allValidatorsWithMSRAtom);
const [searchTerm, setSearchTerm] = useState("");
const userHasAccount = useUserHasAccount();

Expand Down
6 changes: 6 additions & 0 deletions apps/namadillo/src/atoms/chain/atoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ export const chainParametersAtom = atomWithQuery<ChainParameters>((get) => {
...parameters,
apr: BigNumber(parameters.apr),
unbondingPeriod: calculateUnbondingPeriod(parameters),
duplicateVoteMinSlashRate: BigNumber(
parameters.duplicateVoteMinSlashRate
),
lightClientAttackMinSlashRate: BigNumber(
parameters.lightClientAttackMinSlashRate
),
};
},
};
Expand Down
36 changes: 35 additions & 1 deletion apps/namadillo/src/atoms/validators/atoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { indexerApiAtom } from "atoms/api";
import { chainParametersAtom } from "atoms/chain";
import { shouldUpdateBalanceAtom } from "atoms/etc";
import { queryDependentFn } from "atoms/utils";
import BigNumber from "bignumber.js";
import { atomWithQuery } from "jotai-tanstack-query";
import { MyValidator, Validator } from "types";
import { MyValidator, Validator, ValidatorWithMSR } from "types";
import { toMyValidators } from "./functions";
import {
fetchAllValidators,
Expand Down Expand Up @@ -33,6 +34,39 @@ export const allValidatorsAtom = atomWithQuery((get) => {
};
});

export const allValidatorsWithMSRAtom = atomWithQuery((get) => {
const validators = get(allValidatorsAtom);
const totalVotingPower = get(votingPowerAtom);
const params = get(chainParametersAtom);

return {
queryKey: ["all-validators"],
...queryDependentFn(async (): Promise<ValidatorWithMSR[]> => {
const { duplicateVoteMinSlashRate, lightClientAttackMinSlashRate } =
params.data!;
const minSlashRateParam = BigNumber.min(
duplicateVoteMinSlashRate,
lightClientAttackMinSlashRate
);

return validators.data!.map((v) => {
const votingPower = BigNumber(v.votingPowerInNAM || 0);
const slashRateParam = BigNumber(9).times(
votingPower.div(totalVotingPower.data!.totalVotingPower).pow(2)
);

// Slash rate should not exceed 100%
const minSlashRate = BigNumber.min(
BigNumber(1),
BigNumber.max(minSlashRateParam, slashRateParam)
);

return { ...v, msr: minSlashRate };
});
}, [validators, params, totalVotingPower]),
};
});

// eslint-disable-next-line
export const myValidatorsAtom = atomWithQuery((get) => {
const account = get(defaultAccountAtom);
Expand Down
3 changes: 3 additions & 0 deletions apps/namadillo/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ export type ChainParameters = {
nativeTokenAddress: Address;
unbondingPeriod: string;
checksums: Record<string, string>;
duplicateVoteMinSlashRate: BigNumber;
lightClientAttackMinSlashRate: BigNumber;
};

export type SettingsStorage = {
Expand Down Expand Up @@ -106,6 +108,7 @@ export type Validator = Unique & {
status: ValidatorStatus;
};

export type ValidatorWithMSR = Validator & { msr: BigNumber };
export type ValidatorFilterOptions = "all" | "active" | ValidatorStatus;

export type UnbondEntry = {
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3831,12 +3831,12 @@ __metadata:
languageName: unknown
linkType: soft

"@namada/indexer-client@npm:2.2.0":
version: 2.2.0
resolution: "@namada/indexer-client@npm:2.2.0"
"@namada/indexer-client@file:../../../namada-indexer-client::locator=%40namada%2Fnamadillo%40workspace%3Aapps%2Fnamadillo":
version: 1.5.1
resolution: "@namada/indexer-client@file:../../../namada-indexer-client#../../../namada-indexer-client::hash=54f384&locator=%40namada%2Fnamadillo%40workspace%3Aapps%2Fnamadillo"
dependencies:
axios: "npm:^1.6.1"
checksum: 10c0/146740e9291170e8efad519b83988939a4e86f3506913a3ec25d2a6780281e92775e640782e0554c83e494b444d89909194d68de749b42dfca255712bfcc32e6
checksum: 10c0/e6dc4f4633cfdae1d8c06a10c7044f66cd0e5ee9d5bfcf5997aa5a3e744a24b7cfef58332edebdfe08805144ed71280669c5028989ff964bafaf48fce9c9e17b
languageName: node
linkType: hard

Expand Down Expand Up @@ -3878,7 +3878,7 @@ __metadata:
"@eslint/js": "npm:^9.9.1"
"@keplr-wallet/types": "npm:^0.12.136"
"@namada/chain-registry": "npm:^1.0.0"
"@namada/indexer-client": "npm:2.2.0"
"@namada/indexer-client": ../../../namada-indexer-client
"@playwright/test": "npm:^1.24.1"
"@svgr/webpack": "npm:^6.5.1"
"@tailwindcss/container-queries": "npm:^0.1.1"
Expand Down
Loading