Map non-account stashes (#1892)
This commit is contained in:
parent
6a27dbaafc
commit
e8745b4909
@ -3,16 +3,18 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { ApiProps } from '@polkadot/react-api/types';
|
||||
import { ComponentProps } from '../types';
|
||||
import { I18nProps } from '@polkadot/react-components/types';
|
||||
import { SubjectInfo } from '@polkadot/ui-keyring/observable/types';
|
||||
import { KeyringSectionOption } from '@polkadot/ui-keyring/options/types';
|
||||
import { withCalls, withMulti } from '@polkadot/react-api/with';
|
||||
import { AccountId, StakingLedger } from '@polkadot/types/interfaces';
|
||||
import { ComponentProps } from '../types';
|
||||
|
||||
import React, { useState } from 'react';
|
||||
import styled from 'styled-components';
|
||||
import { withCalls, withMulti } from '@polkadot/react-api/with';
|
||||
import { Button, CardGrid } from '@polkadot/react-components';
|
||||
import { AccountName } from '@polkadot/react-query';
|
||||
import { Option } from '@polkadot/types';
|
||||
import createOption from '@polkadot/ui-keyring/options/item';
|
||||
|
||||
import Account from './Account';
|
||||
@ -20,32 +22,39 @@ import StartStaking from './NewStake';
|
||||
import translate from '../translate';
|
||||
|
||||
interface Props extends I18nProps, ComponentProps, ApiProps {
|
||||
myControllers?: string[];
|
||||
queryBonded?: Option<AccountId>[];
|
||||
queryLedger?: Option<StakingLedger>[];
|
||||
}
|
||||
|
||||
function getMyStashes (myControllers?: string[], allAccounts?: SubjectInfo): string[] | null {
|
||||
function getMyStashes (queryBonded?: Option<AccountId>[], queryLedger?: Option<StakingLedger>[], allAccounts?: SubjectInfo): string[] | null {
|
||||
const result: string[] = [];
|
||||
|
||||
if (!myControllers) {
|
||||
if (!queryBonded || !queryLedger) {
|
||||
return null;
|
||||
}
|
||||
|
||||
myControllers.forEach((value, index): void => {
|
||||
if (value.toString() !== '') {
|
||||
allAccounts && result.push(Object.keys(allAccounts)[index]);
|
||||
queryBonded.forEach((value, index): void => {
|
||||
value.isSome && allAccounts && result.push(Object.keys(allAccounts)[index]);
|
||||
});
|
||||
|
||||
queryLedger.forEach((ledger): void => {
|
||||
if (ledger.isSome) {
|
||||
const stashId = ledger.unwrap().stash.toString();
|
||||
|
||||
!result.includes(stashId) && result.push(stashId);
|
||||
}
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function Accounts ({ allAccounts, allStashes, className, myControllers, recentlyOnline, t }: Props): React.ReactElement<Props> {
|
||||
function Accounts ({ allAccounts, allStashes, className, queryBonded, queryLedger, recentlyOnline, t }: Props): React.ReactElement<Props> {
|
||||
const [isNewStakeOpen, setIsNewStateOpen] = useState(false);
|
||||
const myStashes = getMyStashes(myControllers, allAccounts);
|
||||
const foundStashes = getMyStashes(queryBonded, queryLedger, allAccounts);
|
||||
const stashOptions = allStashes.map((stashId): KeyringSectionOption =>
|
||||
createOption(stashId, (<AccountName params={stashId} />) as any)
|
||||
);
|
||||
const isEmpty = !isNewStakeOpen && (!myStashes || myStashes.length === 0);
|
||||
const isEmpty = !isNewStakeOpen && (!foundStashes || foundStashes.length === 0);
|
||||
|
||||
const _toggleNewStake = (): void => setIsNewStateOpen(!isNewStakeOpen);
|
||||
|
||||
@ -67,7 +76,7 @@ function Accounts ({ allAccounts, allStashes, className, myControllers, recently
|
||||
{isNewStakeOpen && (
|
||||
<StartStaking onClose={_toggleNewStake} />
|
||||
)}
|
||||
{myStashes && myStashes.map((address, index): React.ReactNode => (
|
||||
{foundStashes && foundStashes.map((address, index): React.ReactNode => (
|
||||
address && (
|
||||
<Account
|
||||
allStashes={allStashes}
|
||||
@ -92,10 +101,15 @@ export default withMulti(
|
||||
withCalls<Props>(
|
||||
['query.staking.bonded', {
|
||||
isMulti: true,
|
||||
paramPick: ({ allAccounts }: Props): undefined | string[] => {
|
||||
return allAccounts && Object.keys(allAccounts);
|
||||
},
|
||||
propName: 'myControllers'
|
||||
propName: 'queryBonded',
|
||||
paramPick: ({ allAccounts }: Props): undefined | string[] =>
|
||||
allAccounts && Object.keys(allAccounts)
|
||||
}],
|
||||
['query.staking.ledger', {
|
||||
isMulti: true,
|
||||
propName: 'queryLedger',
|
||||
paramPick: ({ allAccounts }: Props): undefined | string[] =>
|
||||
allAccounts && Object.keys(allAccounts)
|
||||
}]
|
||||
)
|
||||
);
|
||||
|
||||
@ -40,14 +40,21 @@ const COLORS_OTHER = ['#acacac'];
|
||||
const COLORS_REWARD = ['#8c2200', '#008c22', '#acacac'];
|
||||
const COLORS_BLOCKS = [undefined, '#acacac'];
|
||||
|
||||
function balanceToNumber (amount: BN, divisor: BN): number {
|
||||
return amount.muln(1000).div(divisor).toNumber() / 1000;
|
||||
}
|
||||
|
||||
function extractStake (values: [Hash, Exposure][], divisor: BN): LineData {
|
||||
return [
|
||||
values.map(([, { total }]): BN =>
|
||||
total.unwrap().div(divisor))
|
||||
// exposures.map(({ own }): BN =>
|
||||
// own.unwrap().div(divisor)),
|
||||
// exposures.map(({ others }): BN =>
|
||||
// others.reduce((total, { value }): BN => total.add(value.unwrap()), new BN(0)).div(divisor))
|
||||
values.map(([, { total }]): number =>
|
||||
balanceToNumber(total.unwrap(), divisor))
|
||||
// exposures.map(({ own }): number =>
|
||||
// balanceToNumber(own.unwrap(), divisor)),
|
||||
// exposures.map(({ others }): number =>
|
||||
// balanceToNumber(
|
||||
// others.reduce((total, { value }): number =>
|
||||
// total.add(value.unwrap()), new BN(0)
|
||||
// ), divisor))
|
||||
];
|
||||
}
|
||||
|
||||
@ -79,10 +86,6 @@ function extractEraSlash (validatorId: string, slashes: Slash[]): BN {
|
||||
}, new BN(0));
|
||||
}
|
||||
|
||||
function balanceToNumber (amount: BN, divisor: BN): number {
|
||||
return amount.muln(1000).div(divisor).toNumber() / 1000;
|
||||
}
|
||||
|
||||
function Validator ({ className, sessionRewards, t, validatorId }: Props): React.ReactElement<Props> {
|
||||
const { api } = useApiContext();
|
||||
// FIXME There is something seriously wrong in these two with "any" horrors
|
||||
|
||||
@ -103,7 +103,9 @@ async function loadSome (api: ApiPromise, fromHash: Hash, toHash: Hash): Promise
|
||||
blockNumber: headers[index].number.unwrap(),
|
||||
isEventsEmpty: events[index].length === 0,
|
||||
reward: rewards[index] || createType('Balance'),
|
||||
sessionIndex: createType('SessionIndex', u8aToU8a(value.unwrap())),
|
||||
sessionIndex: createType('SessionIndex', u8aToU8a(
|
||||
value.isSome ? value.unwrap() : new Uint8Array([])
|
||||
)),
|
||||
slashes: slashes[index]
|
||||
}));
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user