Own controller check on staking options (#2111)

* Own controller check on staking options

* Update SetSessionKey.tsx
This commit is contained in:
Jaco Greeff 2020-01-13 15:22:42 +01:00 committed by GitHub
parent fe2c189486
commit 4b96a65ef3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -10,7 +10,7 @@ import { Codec, ITuple } from '@polkadot/types/types';
import React, { useEffect, useState } from 'react';
import styled from 'styled-components';
import { AddressInfo, AddressMini, AddressSmall, Button, Menu, Popup, TxButton } from '@polkadot/react-components';
import { useApi, useCall, useToggle } from '@polkadot/react-hooks';
import { useAccounts, useApi, useCall, useToggle } from '@polkadot/react-hooks';
import { u8aConcat, u8aToHex } from '@polkadot/util';
import translate from '../../translate';
@ -41,6 +41,7 @@ interface StakeState {
hexSessionIdNext: string | null;
hexSessionIdQueue: string | null;
isLoading: boolean;
isOwnController: boolean;
isStashNominating: boolean;
isStashValidating: boolean;
nominees?: string[];
@ -56,18 +57,20 @@ function toIdString (id?: AccountId | null): string | null {
: null;
}
function getStakeState (allStashes: string[] | undefined, { controllerId, nextSessionIds, nominators, rewardDestination, sessionIds, stakers, stakingLedger, validatorPrefs }: DerivedStakingAccount, stashId: string, validateInfo: ValidatorInfo): StakeState {
function getStakeState (allAccounts: string[], allStashes: string[] | undefined, { controllerId: _controllerId, nextSessionIds, nominators, rewardDestination, sessionIds, stakers, stakingLedger, validatorPrefs }: DerivedStakingAccount, stashId: string, validateInfo: ValidatorInfo): StakeState {
const isStashNominating = !!(nominators?.length);
const isStashValidating = !validateInfo[1].isEmpty || !!allStashes?.includes(stashId);
const nextConcat = u8aConcat(...nextSessionIds.map((id): Uint8Array => id.toU8a()));
const currConcat = u8aConcat(...sessionIds.map((id): Uint8Array => id.toU8a()));
const controllerId = toIdString(_controllerId);
return {
controllerId: toIdString(controllerId),
controllerId,
destination: rewardDestination?.toNumber() || 0,
hexSessionIdNext: u8aToHex(nextConcat, 48),
hexSessionIdQueue: u8aToHex(currConcat.length ? currConcat : nextConcat, 48),
isLoading: false,
isOwnController: allAccounts.includes(controllerId || ''),
isStashNominating,
isStashValidating,
// we assume that all ids are non-null
@ -85,10 +88,11 @@ function getStakeState (allStashes: string[] | undefined, { controllerId, nextSe
function Account ({ allStashes, className, isOwnStash, next, onUpdateType, stakingOverview, stashId, t }: Props): React.ReactElement<Props> {
const { api, isSubstrateV2 } = useApi();
const { allAccounts } = useAccounts();
const validateInfo = useCall<ValidatorInfo>(api.query.staking.validators, [stashId]);
const balancesAll = useCall<DerivedBalances>(api.derive.balances.all as any, [stashId]);
const stakingAccount = useCall<DerivedStakingAccount>(api.derive.staking.account as any, [stashId]);
const [{ controllerId, destination, hexSessionIdQueue, hexSessionIdNext, isLoading, isStashNominating, isStashValidating, nominees, sessionIds, validatorPrefs }, setStakeState] = useState<StakeState>({ controllerId: null, destination: 0, hexSessionIdNext: null, hexSessionIdQueue: null, isLoading: true, isStashNominating: false, isStashValidating: false, sessionIds: [] });
const [{ controllerId, destination, hexSessionIdQueue, hexSessionIdNext, isLoading, isOwnController, isStashNominating, isStashValidating, nominees, sessionIds, validatorPrefs }, setStakeState] = useState<StakeState>({ controllerId: null, destination: 0, hexSessionIdNext: null, hexSessionIdQueue: null, isLoading: true, isOwnController: false, isStashNominating: false, isStashValidating: false, sessionIds: [] });
const [isBondExtraOpen, toggleBondExtra] = useToggle();
const [isInjectOpen, toggleInject] = useToggle();
const [isNominateOpen, toggleNominate] = useToggle();
@ -101,7 +105,7 @@ function Account ({ allStashes, className, isOwnStash, next, onUpdateType, staki
useEffect((): void => {
if (stakingAccount && validateInfo) {
const state = getStakeState(allStashes, stakingAccount, stashId, validateInfo);
const state = getStakeState(allAccounts, allStashes, stakingAccount, stashId, validateInfo);
setStakeState(state);
@ -303,7 +307,10 @@ function Account ({ allStashes, className, isOwnStash, next, onUpdateType, staki
{t('Bond more funds')}
</Menu.Item>
)}
<Menu.Item onClick={toggleUnbond}>
<Menu.Item
disabled={!isOwnController}
onClick={toggleUnbond}
>
{t('Unbond funds')}
</Menu.Item>
<Menu.Item
@ -312,21 +319,33 @@ function Account ({ allStashes, className, isOwnStash, next, onUpdateType, staki
>
{t('Change controller account')}
</Menu.Item>
<Menu.Item onClick={toggleRewardDestination}>
<Menu.Item
disabled={!isOwnController}
onClick={toggleRewardDestination}
>
{t('Change reward destination')}
</Menu.Item>
{isStashValidating &&
<Menu.Item onClick={toggleValidate}>
<Menu.Item
disabled={!isOwnController}
onClick={toggleValidate}
>
{t('Change validator preferences')}
</Menu.Item>
}
{!isStashNominating &&
<Menu.Item onClick={toggleSetSession}>
<Menu.Item
disabled={!isOwnController}
onClick={toggleSetSession}
>
{isSubstrateV2 ? t('Change session keys') : t('Change session account')}
</Menu.Item>
}
{isStashNominating &&
<Menu.Item onClick={toggleNominate}>
<Menu.Item
disabled={!isOwnController}
onClick={toggleNominate}
>
{t('Change nominee(s)')}
</Menu.Item>
}