From 4b96a65ef334ad21c3ec04c7181a2d08f96561bf Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Mon, 13 Jan 2020 15:22:42 +0100 Subject: [PATCH] Own controller check on staking options (#2111) * Own controller check on staking options * Update SetSessionKey.tsx --- .../app-staking/src/Actions/Account/index.tsx | 39 ++++++++++++++----- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/packages/app-staking/src/Actions/Account/index.tsx b/packages/app-staking/src/Actions/Account/index.tsx index ec3e64ecb4..c281568c15 100644 --- a/packages/app-staking/src/Actions/Account/index.tsx +++ b/packages/app-staking/src/Actions/Account/index.tsx @@ -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 { const { api, isSubstrateV2 } = useApi(); + const { allAccounts } = useAccounts(); const validateInfo = useCall(api.query.staking.validators, [stashId]); const balancesAll = useCall(api.derive.balances.all as any, [stashId]); const stakingAccount = useCall(api.derive.staking.account as any, [stashId]); - const [{ controllerId, destination, hexSessionIdQueue, hexSessionIdNext, isLoading, isStashNominating, isStashValidating, nominees, sessionIds, validatorPrefs }, setStakeState] = useState({ 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({ 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')} )} - + {t('Unbond funds')} {t('Change controller account')} - + {t('Change reward destination')} {isStashValidating && - + {t('Change validator preferences')} } {!isStashNominating && - + {isSubstrateV2 ? t('Change session keys') : t('Change session account')} } {isStashNominating && - + {t('Change nominee(s)')} }