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

Alter display info of Staking summary overview #7179

Open
wants to merge 39 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
2fc7495
add minExposedThreshold into useSortedTarget
wirednkod Mar 16, 2022
f9eb19c
Fix the minExposedThreshold scripts/hook
wirednkod Mar 16, 2022
42359e8
fix missing api error
wirednkod Mar 17, 2022
4be121a
add implementation based on issue #6918
wirednkod Mar 17, 2022
cddbd7e
add translation strings
wirednkod Mar 17, 2022
deaa75e
remove stray logs
wirednkod Mar 17, 2022
e60c7b3
revert formatBalance
wirednkod Mar 21, 2022
4019138
fix code to show correctly the min active nominator stake
wirednkod Mar 21, 2022
0fa8264
Add tooltip; Fix breaking lint
wirednkod Mar 21, 2022
ba6b934
merge
wirednkod Apr 14, 2022
6111da4
Merge branch 'master' of github.com:wirednkod/apps into nik-fix-staki…
wirednkod Apr 14, 2022
6ee2b5b
Fix performance issue
wirednkod Apr 14, 2022
b635be3
readdress performance
wirednkod Apr 14, 2022
306d909
resolve merge conflicts
wirednkod Apr 19, 2022
c801839
Seperate pages Overview/Validator
wirednkod Apr 19, 2022
e6d2158
Remove not-necessery components; Revert changes of SummaryBox
wirednkod Apr 19, 2022
8099177
Finalize Overview page by concentrating Summaries
wirednkod Apr 21, 2022
5489dac
merge with master and resolve conflicts
wirednkod Apr 21, 2022
5e4ba99
Fix merge issue
wirednkod Apr 21, 2022
0af248e
Add condition rendering for bags Card in Overview
wirednkod Apr 22, 2022
de0e9ea
Merge branch 'master' into nik-fix-staking-numbers
wirednkod May 6, 2022
ac124fd
Merge branch 'polkadot-js:master' into nik-fix-staking-numbers
wirednkod May 17, 2022
4e53f1b
Update translation string
wirednkod May 17, 2022
e234c9e
Merge branch 'nik-fix-staking-numbers' of github.com:wirednkod/apps i…
wirednkod May 17, 2022
e7038c8
Address more PR comments
wirednkod May 17, 2022
fc5ba15
Correct the max electing nominators
wirednkod May 17, 2022
7347707
Add notice for nominators
wirednkod May 17, 2022
fe3fc30
Merge branch 'polkadot-js:master' into nik-fix-staking-numbers
wirednkod May 24, 2022
157b58b
move notification inside nominator's panel
wirednkod May 24, 2022
113eb7d
satisfy linter
wirednkod May 24, 2022
b046c48
satisfy linter
wirednkod May 24, 2022
9aa37fe
resolve conflicts
wirednkod Jun 9, 2022
cada3da
Correct staking page for older staking networks
wirednkod Jun 15, 2022
72be4b5
merge with master
wirednkod Jun 15, 2022
db83a8a
merge with master
wirednkod Jun 15, 2022
d31a7ec
Merge branch 'master' of github.com:wirednkod/apps into nik-fix-staki…
wirednkod Jun 20, 2022
c3122b2
Address issues on Docker PoS Mainnet
wirednkod Jun 22, 2022
cdf1106
Merge branch 'polkadot-js:master' into nik-fix-staking-numbers
wirednkod Jul 12, 2022
b200658
Address issue of Automata
wirednkod Jul 12, 2022
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
3 changes: 2 additions & 1 deletion packages/apps/public/locales/en/app-staking.json
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@
"last #": "last #",
"last reward": "last reward",
"lowest / avg staked": "lowest / avg staked",
"min active stake": "min active stake",
"min nominated": "min nominated",
"min nominated / threshold": "min nominated / threshold",
"next": "next",
Expand Down Expand Up @@ -274,4 +275,4 @@
"{{currency}} slashed": "{{currency}} slashed",
"{{currency}} total": "{{currency}} total",
"{{days}} days": "{{days}} days"
}
}
3 changes: 2 additions & 1 deletion packages/apps/public/locales/it/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -1466,6 +1466,7 @@
"https://example.com": "https://example.com",
"id": "id",
"ideal staked": "",
"min active stake": "",
"identity": "identità",
"imminent preimage (proposal already passed)": "proposta imminente (già approvate)",
"immortal": "perpetua",
Expand Down Expand Up @@ -2135,4 +2136,4 @@
"{{threshold}}, passing": "{{threshold}}, approvato",
"{{type}} copied": "{{type}} copiato",
"{{value}}x voting balance, locked for {{lock}}x enactment ({{period}} days)": "{{value}} x moltiplicatore di voto, fondi bloccati per {{lock}}x = {{period}} giorni"
}
}
3 changes: 2 additions & 1 deletion packages/apps/public/locales/ru/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -1492,6 +1492,7 @@
"https://example.com": "https://primer.com",
"id": "",
"ideal staked": "",
"min active stake": "",
"identity": "личность",
"imminent preimage (proposal already passed)": "неизбежный прообраз (предложение уже одобрено)",
"immortal": "бессмертный",
Expand Down Expand Up @@ -2215,4 +2216,4 @@
"{{threshold}}, passing": "{{threshold}}, проходит",
"{{type}} copied": "",
"{{value}}x voting balance, locked for {{lock}}x enactment ({{period}} days)": "{{value}}x баланс голосования, заблокировано на {{lock}}x ({{period}} дней)"
}
}
46 changes: 7 additions & 39 deletions packages/page-staking/src/Overview/Summary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import styled from 'styled-components';

import SummarySession from '@polkadot/app-explorer/SummarySession';
import { CardSummary, Spinner, SummaryBox } from '@polkadot/react-components';
import { formatNumber } from '@polkadot/util';

import { useTranslation } from '../translate';

Expand All @@ -21,49 +20,19 @@ interface Props {
targets: SortedTargets;
}

function Summary ({ className = '', isVisible, stakingOverview, targets: { counterForNominators, inflation: { idealStake, inflation, stakedFraction }, nominators, waitingIds } }: Props): React.ReactElement<Props> {
function Summary ({ className = '', isVisible, targets: { inflation: { idealStake, inflation, stakedFraction }, nominatorMinActiveThreshold, validatorMinActiveThreshold } }: Props): React.ReactElement<Props> {
const { t } = useTranslation();

return (
<SummaryBox className={`${className}${!isVisible ? ' staking--hidden' : ''}`}>
<section>
<CardSummary label={t<string>('validators')}>
{stakingOverview
? <>{formatNumber(stakingOverview.validators.length)}&nbsp;/&nbsp;{formatNumber(stakingOverview.validatorCount)}</>
: <Spinner noLabel />
}
<CardSummary label={<>{t<string>('nominators')}<div>{t<string>('min active stake')}</div></>}>
wirednkod marked this conversation as resolved.
Show resolved Hide resolved
{nominatorMinActiveThreshold === '0' ? <Spinner noLabel /> : nominatorMinActiveThreshold}
</CardSummary>
<CardSummary
className='media--900'
label={t<string>('waiting')}
>
{waitingIds
? formatNumber(waitingIds.length)
: <Spinner noLabel />
}
<CardSummary label={<>{t<string>('validators')}<div>{t<string>('min active stake')}</div></>}>
{validatorMinActiveThreshold === '' ? <Spinner noLabel /> : validatorMinActiveThreshold}
</CardSummary>
<CardSummary
className='media--1000'
label={
counterForNominators
? t<string>('active / nominators')
: t<string>('nominators')
}
>
{nominators
? (
<>
{formatNumber(nominators.length)}
{counterForNominators && (
<>&nbsp;/&nbsp;{formatNumber(counterForNominators)}</>
)}
</>
)
: <Spinner noLabel />
}
</CardSummary>
</section>
<section>
</section><section>
{(idealStake > 0) && Number.isFinite(idealStake) && (
<CardSummary
className='media--1400'
Expand All @@ -88,8 +57,7 @@ function Summary ({ className = '', isVisible, stakingOverview, targets: { count
<>{inflation.toFixed(1)}%</>
</CardSummary>
)}
</section>
<section>
</section><section>
<SummarySession />
</section>
</SummaryBox>
Expand Down
2 changes: 2 additions & 0 deletions packages/page-staking/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,6 @@ export interface SortedTargets {
validators?: ValidatorInfo[];
validatorIds?: string[];
waitingIds?: string[];
nominatorMinActiveThreshold?: string;
validatorMinActiveThreshold?: string;
}
51 changes: 48 additions & 3 deletions packages/page-staking/src/useSortedTargets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
import type { ApiPromise } from '@polkadot/api';
import type { DeriveSessionInfo, DeriveStakingElected, DeriveStakingWaiting } from '@polkadot/api-derive/types';
import type { Inflation } from '@polkadot/react-hooks/types';
import type { Option, u32 } from '@polkadot/types';
import type { Option, StorageKey, u32 } from '@polkadot/types';
import type { Codec } from '@polkadot/types/types';
import type { SortedTargets, TargetSortBy, ValidatorInfo } from './types';

import { useMemo } from 'react';
import { useEffect, useMemo, useState } from 'react';

import { createNamedHook, useAccounts, useApi, useCall, useCallMulti, useInflation } from '@polkadot/react-hooks';
import { AccountId32 } from '@polkadot/types/interfaces';
import { PalletStakingIndividualExposure } from '@polkadot/types/lookup';
import { arrayFlatten, BN, BN_HUNDRED, BN_MAX_INTEGER, BN_ONE, BN_ZERO } from '@polkadot/util';

interface LastEra {
Expand Down Expand Up @@ -244,6 +247,28 @@ function extractBaseInfo (api: ApiPromise, allAccounts: string[], electedDerive:
};
}

const b = (x: BN, api: ApiPromise): string => api.createType('Balance', x).toHuman();
wirednkod marked this conversation as resolved.
Show resolved Hide resolved

function getMinActiveThreshold (api: ApiPromise, stakers: [StorageKey<[u32, AccountId32]>, Codec][]) {
wirednkod marked this conversation as resolved.
Show resolved Hide resolved
const assignments: Map<AccountId32, BN> = new Map();

stakers.sort((a, b) => a[1].total.toBn().cmp(b[1].total.toBn()));

stakers.map((x) => x[1].others).flat(1).forEach((x) => {
const nominator = (x as PalletStakingIndividualExposure).who;
const amount = (x as PalletStakingIndividualExposure).value;
const val = assignments.get(nominator);

assignments.set(nominator, val ? amount.toBn().add(val) : amount.toBn());
});

const nominatorStakes = Array.from(assignments.values());

nominatorStakes.sort((a, b) => a.cmp(b));

return b(nominatorStakes[0], api);
}

const transformEra = {
transform: ({ activeEra, eraLength, sessionLength }: DeriveSessionInfo): LastEra => ({
activeEra,
Expand Down Expand Up @@ -287,6 +312,14 @@ function useSortedTargetsImpl (favorites: string[], withLedger: boolean): Sorted
const electedInfo = useCall<DeriveStakingElected>(api.derive.staking.electedInfo, [{ ...DEFAULT_FLAGS_ELECTED, withLedger }]);
const waitingInfo = useCall<DeriveStakingWaiting>(api.derive.staking.waitingInfo, [{ ...DEFAULT_FLAGS_WAITING, withLedger }]);
const lastEraInfo = useCall<LastEra>(api.derive.session.info, undefined, transformEra);
const [stakers, setStakers] = useState<[StorageKey<[u32, AccountId32]>, Codec][]>([]);
const [stakersTotal, setStakersTotal] = useState<BN | undefined>();

useEffect(() => {
if (stakers[0] && stakers[0][1]) {
setStakersTotal(stakers[0][1].total.toBn() as BN);
}
}, [stakers]);

const baseInfo = useMemo(
() => electedInfo && lastEraInfo && totalIssuance && waitingInfo
Expand All @@ -304,6 +337,16 @@ function useSortedTargetsImpl (favorites: string[], withLedger: boolean): Sorted
[baseInfo, inflation]
);

const curEra = useCall<Option<u32>>(api.query.staking.currentEra);

const getStakers = useMemo(() => async (currentEra: u32) => {
setStakers(await api.query.staking.erasStakers.entries(currentEra));
}, [api.query.staking.erasStakers]);

curEra && getStakers(curEra?.unwrap());
const nominatorMinActiveThreshold = getMinActiveThreshold(api, stakers);
const validatorMinActiveThreshold = stakersTotal ? b(stakersTotal, api) : '';

return {
counterForNominators,
counterForValidators,
Expand All @@ -314,7 +357,9 @@ function useSortedTargetsImpl (favorites: string[], withLedger: boolean): Sorted
minNominated: BN_ZERO,
minNominatorBond,
minValidatorBond,
...partial
...partial,
nominatorMinActiveThreshold,
validatorMinActiveThreshold
};
}

Expand Down