Skip to content
Merged
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
46 changes: 30 additions & 16 deletions packages/app-staking/src/Actions/Accounts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,58 @@
// 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';
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);

Expand All @@ -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}
Expand All @@ -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)
}]
)
);
23 changes: 13 additions & 10 deletions packages/app-staking/src/Query/Validator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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))
];
}

Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion packages/react-hooks/src/useSessionRewards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}));
}
Expand Down