Skip to content

Commit

Permalink
feat: remove over subscribed (#2092)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ross Bulat authored Apr 22, 2024
1 parent 2f06328 commit 5c17a3a
Show file tree
Hide file tree
Showing 19 changed files with 16 additions and 222 deletions.
6 changes: 5 additions & 1 deletion src/contexts/FastUnstake/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,20 @@ export const FastUnstakeContext = createContext<FastUnstakeContextInterface>(
export const useFastUnstake = () => useContext(FastUnstakeContext);

export const FastUnstakeProvider = ({ children }: { children: ReactNode }) => {
const { activeEra } = useApi();
const { network } = useNetwork();
const { activeAccount } = useActiveAccounts();
const { inSetup, fetchEraStakers, isBonding } = useStaking();
const {
api,
consts,
isReady,
activeEra,
consts: { bondDuration },
networkMetrics: { fastUnstakeErasToCheckPerBlock },
} = useApi();

const { maxExposurePageSize } = consts;

// store whether a fast unstake check is in progress.
const [checking, setChecking] = useState<boolean>(false);
const checkingRef = useRef(checking);
Expand Down Expand Up @@ -262,6 +265,7 @@ export const FastUnstakeProvider = ({ children }: { children: ReactNode }) => {
who: activeAccount,
networkName: network,
exitOnExposed: true,
maxExposurePageSize: maxExposurePageSize.toString(),
exposures,
});
};
Expand Down
1 change: 1 addition & 0 deletions src/contexts/Payouts/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export const PayoutsProvider = ({ children }: { children: ReactNode }) => {
who: activeAccount,
networkName: network,
maxExposurePageSize: maxExposurePageSize.toString(),
exitOnExposed: false,
exposures,
});
}
Expand Down
7 changes: 0 additions & 7 deletions src/contexts/Staking/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: GPL-3.0-only
/* eslint-disable @typescript-eslint/no-unused-vars, @typescript-eslint/no-empty-function */

import BigNumber from 'bignumber.js';
import type {
EraStakers,
NominationStatuses,
Expand All @@ -16,11 +15,6 @@ export const defaultEraStakers: EraStakers = {
totalActiveNominators: 0,
};

const defaultLowestReward = {
lowest: new BigNumber(0),
oversubscribed: false,
};

export const defaultNominationStatus: NominationStatuses = {};

export const defaultStakingContext: StakingContextInterface = {
Expand All @@ -31,7 +25,6 @@ export const defaultStakingContext: StakingContextInterface = {
isBonding: () => false,
isNominating: () => false,
inSetup: () => true,
getLowestRewardFromStaker: (address) => defaultLowestReward,
eraStakers: defaultEraStakers,
getPagedErasStakers: (e) => new Promise((resolve) => resolve([])),
};
20 changes: 1 addition & 19 deletions src/contexts/Staking/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ export const StakingProvider = ({ children }: { children: ReactNode }) => {
const { getLedger, getNominations } = useBalances();
const { accounts: connectAccounts } = useImportedAccounts();
const { activeAccount, getActiveAccount } = useActiveAccounts();
const { isReady, api, apiStatus, consts, activeEra, isPagedRewardsActive } =
useApi();
const { maxExposurePageSize } = consts;
const { isReady, api, apiStatus, activeEra, isPagedRewardsActive } = useApi();

// Store eras stakers in state.
const [eraStakers, setEraStakers] = useState<EraStakers>(defaultEraStakers);
Expand Down Expand Up @@ -140,7 +138,6 @@ export const StakingProvider = ({ children }: { children: ReactNode }) => {
activeAccount,
units: networkData.units,
exposures,
maxExposurePageSize: maxExposurePageSize.toNumber(),
});
};

Expand Down Expand Up @@ -227,20 +224,6 @@ export const StakingProvider = ({ children }: { children: ReactNode }) => {
!activeAccount ||
(!hasController() && !isBonding() && !isNominating() && !isUnlocking());

// Helper function to get the lowest reward from an active validator.
const getLowestRewardFromStaker = (address: MaybeAddress) => {
const staker = eraStakersRef.current.stakers.find(
(s) => s.address === address
);
const lowest = new BigNumber(staker?.lowestReward || 0);
const oversubscribed = staker?.oversubscribed || false;

return {
lowest,
oversubscribed,
};
};

// If paged rewards are active for the era, fetch eras stakers from the new storage items,
// otherwise use the old storage items.
const getPagedErasStakers = async (era: string) => {
Expand Down Expand Up @@ -330,7 +313,6 @@ export const StakingProvider = ({ children }: { children: ReactNode }) => {
isBonding,
isNominating,
inSetup,
getLowestRewardFromStaker,
eraStakers,
getPagedErasStakers,
}}
Expand Down
9 changes: 0 additions & 9 deletions src/contexts/Staking/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright 2024 @paritytech/polkadot-staking-dashboard authors & contributors
// SPDX-License-Identifier: GPL-3.0-only

import type BigNumber from 'bignumber.js';
import type { NominationStatus } from 'library/ValidatorList/ValidatorItem/types';
import type { MaybeAddress } from 'types';

Expand Down Expand Up @@ -31,8 +30,6 @@ export interface ExposureValue {

export type Staker = ExposureValue & {
address: string;
lowestReward: string;
oversubscribed: boolean;
};

export interface ActiveAccountStaker {
Expand All @@ -45,11 +42,6 @@ export interface ExposureOther {
value: string;
}

interface LowestReward {
lowest: BigNumber;
oversubscribed: boolean;
}

export interface StakingContextInterface {
fetchEraStakers: (era: string) => Promise<Exposure[]>;
getNominationsStatusFromTargets: (
Expand All @@ -61,7 +53,6 @@ export interface StakingContextInterface {
isBonding: () => boolean;
isNominating: () => boolean;
inSetup: () => boolean;
getLowestRewardFromStaker: (a: MaybeAddress) => LowestReward;
eraStakers: EraStakers;
getPagedErasStakers: (e: string) => Promise<Exposure[]>;
}
Expand Down
34 changes: 5 additions & 29 deletions src/hooks/useNominationStatus/index.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,21 @@
// Copyright 2024 @paritytech/polkadot-staking-dashboard authors & contributors
// SPDX-License-Identifier: GPL-3.0-only

import { planckToUnit } from '@w3ux/utils';
import BigNumber from 'bignumber.js';
import { useTranslation } from 'react-i18next';
import { useActivePool } from 'contexts/Pools/ActivePool';
import { useStaking } from 'contexts/Staking';
import { useValidators } from 'contexts/Validators/ValidatorEntries';
import type { AnyJson, BondFor, MaybeAddress } from 'types';
import { useNetwork } from 'contexts/Network';
import { useSyncing } from 'hooks/useSyncing';
import { useBalances } from 'contexts/Balances';

export const useNominationStatus = () => {
const { t } = useTranslation();
const {
networkData: { units },
} = useNetwork();
const {
inSetup,
eraStakers,
getLowestRewardFromStaker,
getNominationsStatusFromTargets,
} = useStaking();
const { validators } = useValidators();
const { getNominations } = useBalances();
const { syncing } = useSyncing(['era-stakers']);
const { activePoolNominations } = useActivePool();
const { inSetup, eraStakers, getNominationsStatusFromTargets } = useStaking();

// Utility to get an account's nominees alongside their status.
const getNominationSetStatus = (who: MaybeAddress, type: BondFor) => {
Expand Down Expand Up @@ -64,23 +53,10 @@ export const useNominationStatus = () => {
?.others || [];

if (others.length) {
// If the provided account is a part of the validator's backers, check if they are above
// the lowest reward threshold. If so, they are earning rewards and this iteration can
// exit.
const stakedValue =
others?.find((o) => o.who === who)?.value ?? false;
if (stakedValue) {
const { lowest } = getLowestRewardFromStaker(nominee);
if (
planckToUnit(
new BigNumber(stakedValue),
units
).isGreaterThanOrEqualTo(lowest)
) {
earningRewards = true;
return false;
}
}
// If the provided account is a part of the validator's backers they are earning
// rewards.
earningRewards = true;
return false;
}
}
return true;
Expand Down
29 changes: 0 additions & 29 deletions src/hooks/useValidatorFilters/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import { u8aToString, u8aUnwrapBytes } from '@polkadot/util';
import { useTranslation } from 'react-i18next';
import { useValidators } from 'contexts/Validators/ValidatorEntries';
import type { AnyFunction, AnyJson } from 'types';
import { useStaking } from 'contexts/Staking';
import { MaxEraRewardPointsEras } from 'consts';
import type { AnyFilter } from 'library/Filter/types';
import { useSyncing } from 'hooks/useSyncing';

export const useValidatorFilters = () => {
const { t } = useTranslation('library');
Expand All @@ -19,8 +17,6 @@ export const useValidatorFilters = () => {
validatorSupers,
validatorEraPointsHistory,
} = useValidators();
const { syncing } = useSyncing(['era-stakers']);
const { getLowestRewardFromStaker } = useStaking();

/*
* filterMissingIdentity: Iterates through the supplied list and filters those with missing
Expand All @@ -47,29 +43,6 @@ export const useValidatorFilters = () => {
}
return filteredList;
};

/*
* filterOverSubscribed: Iterates through the supplied list and filters those who are over
* subscribed. Returns the updated filtered list.
*/
const filterOverSubscribed = (list: AnyFilter) => {
// Return list early if eraStakers is still syncing.
if (syncing) {
return list;
}

const filteredList: AnyFilter = [];
for (const validator of list) {
const { oversubscribed } = getLowestRewardFromStaker(validator.address);

if (!oversubscribed) {
filteredList.push(validator);
continue;
}
}
return filteredList;
};

/*
* filterAllCommission: Filters the supplied list and removes items with 100% commission. Returns
* the updated filtered list.
Expand Down Expand Up @@ -131,7 +104,6 @@ export const useValidatorFilters = () => {
};

const excludesToLabels: Record<string, string> = {
over_subscribed: t('overSubscribed'),
all_commission: t('100Commission'),
blocked_nominations: t('blockedNominations'),
missing_identity: t('missingIdentity'),
Expand All @@ -140,7 +112,6 @@ export const useValidatorFilters = () => {
const filterToFunction: Record<string, AnyFunction> = {
active: filterActive,
missing_identity: filterMissingIdentity,
over_subscribed: filterOverSubscribed,
all_commission: filterAllCommission,
blocked_nominations: filterBlockedNominations,
not_parachain_validator: filterNonParachainValidator,
Expand Down
64 changes: 0 additions & 64 deletions src/library/ListItem/Labels/Oversubscribed.tsx

This file was deleted.

13 changes: 0 additions & 13 deletions src/library/ListItem/Wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,19 +183,6 @@ export const Labels = styled.div`
}
`;

export const OverSubscribedWrapper = styled.div`
display: flex;
align-items: center;
width: 100%;
height: 100%;
.warning {
margin-right: 0.25rem;
@media (max-width: 500px) {
display: none;
}
}
`;
export const IdentityWrapper = styled(motion.div)`
display: flex;
margin-right: 0.5rem;
Expand Down
2 changes: 0 additions & 2 deletions src/library/ValidatorList/ValidatorItem/Default.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { Commission } from '../../ListItem/Labels/Commission';
import { EraStatus } from '../../ListItem/Labels/EraStatus';
import { FavoriteValidator } from '../../ListItem/Labels/FavoriteValidator';
import { Identity } from '../../ListItem/Labels/Identity';
import { Oversubscribed } from '../../ListItem/Labels/Oversubscribed';
import { Select } from '../../ListItem/Labels/Select';
import { getIdentityDisplay } from './Utils';
import type { ValidatorItemProps } from './types';
Expand Down Expand Up @@ -122,7 +121,6 @@ export const Default = ({
<div>
<Labels style={{ marginBottom: '0.9rem' }}>
<Quartile address={address} />
<Oversubscribed address={address} />
<Blocked prefs={prefs} />
<Commission commission={commission} />
<ParaValidator address={address} />
Expand Down
2 changes: 0 additions & 2 deletions src/library/ValidatorList/ValidatorItem/Nomination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { FavoriteValidator } from '../../ListItem/Labels/FavoriteValidator';
import { Identity } from '../../ListItem/Labels/Identity';
import { Metrics } from '../../ListItem/Labels/Metrics';
import { NominationStatus } from '../../ListItem/Labels/NominationStatus';
import { Oversubscribed } from '../../ListItem/Labels/Oversubscribed';
import { Select } from '../../ListItem/Labels/Select';
import { getIdentityDisplay } from './Utils';
import type { ValidatorItemProps } from './types';
Expand Down Expand Up @@ -63,7 +62,6 @@ export const Nomination = ({
<div>
<Labels style={{ marginBottom: '0.9rem' }}>
<Quartile address={address} />
<Oversubscribed address={address} />
<Blocked prefs={prefs} />
<Commission commission={commission} />
<ParaValidator address={address} />
Expand Down
Loading

0 comments on commit 5c17a3a

Please sign in to comment.