diff --git a/packages/app-staking/src/Actions/Accounts.tsx b/packages/app-staking/src/Actions/Accounts.tsx index 1112008fdc..101133b939 100644 --- a/packages/app-staking/src/Actions/Accounts.tsx +++ b/packages/app-staking/src/Actions/Accounts.tsx @@ -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[]; + queryLedger?: Option[]; } -function getMyStashes (myControllers?: string[], allAccounts?: SubjectInfo): string[] | null { +function getMyStashes (queryBonded?: Option[], queryLedger?: Option[], 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 { +function Accounts ({ allAccounts, allStashes, className, queryBonded, queryLedger, recentlyOnline, t }: Props): React.ReactElement { const [isNewStakeOpen, setIsNewStateOpen] = useState(false); - const myStashes = getMyStashes(myControllers, allAccounts); + const foundStashes = getMyStashes(queryBonded, queryLedger, allAccounts); const stashOptions = allStashes.map((stashId): KeyringSectionOption => createOption(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 && ( )} - {myStashes && myStashes.map((address, index): React.ReactNode => ( + {foundStashes && foundStashes.map((address, index): React.ReactNode => ( address && ( ( ['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) }] ) ); diff --git a/packages/app-staking/src/Query/Validator.tsx b/packages/app-staking/src/Query/Validator.tsx index 076b0a4c2c..9276cb88e5 100644 --- a/packages/app-staking/src/Query/Validator.tsx +++ b/packages/app-staking/src/Query/Validator.tsx @@ -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 { const { api } = useApiContext(); // FIXME There is something seriously wrong in these two with "any" horrors diff --git a/packages/react-hooks/src/useSessionRewards.tsx b/packages/react-hooks/src/useSessionRewards.tsx index 571982e496..096ce0df36 100644 --- a/packages/react-hooks/src/useSessionRewards.tsx +++ b/packages/react-hooks/src/useSessionRewards.tsx @@ -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] })); }