diff --git a/packages/page-accounts/src/Accounts/Account.tsx b/packages/page-accounts/src/Accounts/Account.tsx index b529cc6f926a..5c895721bd0a 100644 --- a/packages/page-accounts/src/Accounts/Account.tsx +++ b/packages/page-accounts/src/Accounts/Account.tsx @@ -7,7 +7,7 @@ import type { Ledger } from '@polkadot/hw-ledger'; import type { ActionStatus } from '@polkadot/react-components/Status/types'; import type { ThemeDef } from '@polkadot/react-components/types'; import type { Option } from '@polkadot/types'; -import type { ProxyDefinition, RecoveryConfig } from '@polkadot/types/interfaces'; +import type { ProxyDefinition, RecoveryConfig, Voting } from '@polkadot/types/interfaces'; import type { KeyringAddress, KeyringJson$Meta } from '@polkadot/ui-keyring/types'; import type { AccountBalance, Delegation } from '../types'; @@ -132,6 +132,7 @@ function Account ({ account: { address, meta }, className = '', delegation, filt const stakingInfo = useStakingInfo(address); const democracyLocks = useCall(api.api.derive.democracy?.locks, [address]); const recoveryInfo = useCall(api.api.query.recovery?.recoverable, [address], transformRecovery); + const votingOf = useCall(api.api.query.democracy?.votingOf, [address]); const multiInfos = useMultisigApprovals(address); const proxyInfo = useProxies(address); const { flags: { isDevelopment, isEditable, isEthereum, isExternal, isHardware, isInjected, isMultisig, isProxied }, genesisHash, identity, name: accName, onSetGenesisHash, tags } = useAccountInfo(address); @@ -172,23 +173,51 @@ function Account ({ account: { address, meta }, className = '', delegation, filt }, [address, api, balancesAll, setBalance, stakingInfo]); useEffect((): void => { - bestNumber && democracyLocks && setUnlockableIds( - (prev): DemocracyUnlockable => { + if (bestNumber) { + if (democracyLocks && democracyLocks.length) { + // referendum locks const ids = democracyLocks .filter(({ isFinished, unlockAt }) => isFinished && bestNumber.gt(unlockAt)) .map(({ referendumId }) => referendumId); - if (JSON.stringify(prev.ids) === JSON.stringify(ids)) { - return prev; + if (ids.length) { + return setUnlockableIds((prev) => + (JSON.stringify(prev.ids) !== JSON.stringify(ids)) + ? { + democracyUnlockTx: createClearDemocracyTx(api.api, address, ids), + ids + } + : prev + ); } + } + + if (votingOf && votingOf.isDirect) { + // delegations removed + const { prior: [unlockAt, balance] } = votingOf.asDirect; - return { - democracyUnlockTx: createClearDemocracyTx(api.api, address, ids), - ids - }; + if (balance.gt(BN_ZERO) && unlockAt.gt(BN_ZERO) && bestNumber.gt(unlockAt)) { + return setUnlockableIds((prev) => + prev.ids.length !== 0 || !prev.democracyUnlockTx + ? { + democracyUnlockTx: api.api.tx.democracy.unlock(address), + ids: [] + } + : prev + ); + } } - ); - }, [address, api, bestNumber, democracyLocks]); + + setUnlockableIds((prev) => + prev.democracyUnlockTx + ? { + democracyUnlockTx: null, + ids: [] + } + : prev + ); + } + }, [address, api, bestNumber, democracyLocks, votingOf]); const isVisible = useMemo( () => calcVisible(filter, accName, tags), diff --git a/packages/react-components/src/AddressInfo.tsx b/packages/react-components/src/AddressInfo.tsx index 4d61f978ca5f..e0c4c6620301 100644 --- a/packages/react-components/src/AddressInfo.tsx +++ b/packages/react-components/src/AddressInfo.tsx @@ -410,6 +410,7 @@ function createBalanceItems (formatIndex: number, lookup: Record ); if (democracyLocks && (democracyLocks.length !== 0)) { + // referendum locks allItems.push( ); } else if (votingOf && votingOf.isDirect) { + // delegation locks const { prior: [unlockAt, balance] } = votingOf.asDirect; balance.gt(BN_ZERO) && unlockAt.gt(BN_ZERO) && allItems.push(