From 259af2bc1157ac8bc4b27380301f19c973911be5 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 12 May 2020 13:02:18 +0200 Subject: [PATCH] Clearing of democracy votes (#2721) * Clearing of democracy votes * Indicate ongoing referendums --- package.json | 14 +- packages/apps/package.json | 4 +- packages/apps/webpack.config.js | 3 +- packages/page-accounts/package.json | 2 +- .../page-accounts/src/Accounts/Account.tsx | 63 +- packages/page-contracts/package.json | 2 +- packages/react-api/package.json | 2 +- packages/react-components/package.json | 8 +- packages/react-components/src/AddressInfo.tsx | 21 +- .../react-components/src/DemocracyLocks.tsx | 89 +++ .../react-components/src/StakingUnbonding.tsx | 20 +- packages/react-components/src/Tooltip.tsx | 2 +- yarn.lock | 559 +++++++++--------- 13 files changed, 467 insertions(+), 322 deletions(-) create mode 100644 packages/react-components/src/DemocracyLocks.tsx diff --git a/package.json b/package.json index 384a691b1f..342ad83b4c 100644 --- a/package.json +++ b/package.json @@ -8,10 +8,10 @@ "packages/*" ], "resolutions": { - "@polkadot/api": "^1.14.0-beta.6", - "@polkadot/api-contract": "^1.14.0-beta.6", + "@polkadot/api": "^1.14.0-beta.9", + "@polkadot/api-contract": "^1.14.0-beta.9", "@polkadot/keyring": "^2.10.0-beta.5", - "@polkadot/types": "^1.14.0-beta.6", + "@polkadot/types": "^1.14.0-beta.9", "@polkadot/util": "^2.10.0-beta.5", "@polkadot/util-crypto": "^2.10.0-beta.5", "babel-core": "^7.0.0-bridge.0", @@ -38,7 +38,7 @@ "@babel/core": "^7.9.6", "@babel/register": "^7.9.0", "@babel/runtime": "^7.9.6", - "@polkadot/dev": "^0.52.18", + "@polkadot/dev": "^0.52.19", "@polkadot/ts": "^0.3.21", "@types/bn.js": "^4.11.6", "@types/chart.js": "^2.9.20", @@ -47,9 +47,9 @@ "@types/jest": "^25.2.1", "@types/react-beautiful-dnd": "^12.1.2", "@types/react-copy-to-clipboard": "^4.3.0", - "@types/react-dom": "^16.9.7", + "@types/react-dom": "^16.9.8", "@types/react-router-dom": "^5.1.5", - "@types/react-tooltip": "^3.11.0", + "@types/react-tooltip": "^4.2.4", "@types/store": "^2.0.2", "@types/styled-components": "^5.1.0", "@types/styled-theming": "^2.2.2", @@ -61,6 +61,6 @@ "stylelint-config-styled-components": "^0.1.1", "webpack": "^4.43.0", "webpack-cli": "^3.3.11", - "webpack-plugin-serve": "^0.12.1" + "webpack-plugin-serve": "^1.0.0" } } diff --git a/packages/apps/package.json b/packages/apps/package.json index 05506b9b79..0a5675919a 100644 --- a/packages/apps/package.json +++ b/packages/apps/package.json @@ -15,7 +15,7 @@ "dependencies": { "@babel/polyfill": "^7.8.7", "@babel/runtime": "^7.9.6", - "@polkadot/dev": "^0.52.18", + "@polkadot/dev": "^0.52.19", "@polkadot/react-components": "0.42.0-beta.112", "@polkadot/react-signer": "0.42.0-beta.112", "query-string": "^6.12.1" @@ -27,7 +27,7 @@ "empty": "^0.10.1", "file-loader": "^6.0.0", "html-loader": "^1.1.0", - "html-webpack-plugin": "^4.2.1", + "html-webpack-plugin": "^4.3.0", "markdown-loader": "^5.1.0", "mini-css-extract-plugin": "^0.9.0", "source-map-explorer": "^2.4.2", diff --git a/packages/apps/webpack.config.js b/packages/apps/webpack.config.js index b910ba811e..38684c1b6d 100644 --- a/packages/apps/webpack.config.js +++ b/packages/apps/webpack.config.js @@ -147,7 +147,8 @@ function createWebpack ({ alias = {}, context, name = 'index' }) { chunkFilename: '[name].[chunkhash:8].js', filename: '[name].[hash:8].js', globalObject: '(typeof self !== \'undefined\' ? self : this)', - path: path.join(context, 'build') + path: path.join(context, 'build'), + publicPath: '' }, performance: { hints: false diff --git a/packages/page-accounts/package.json b/packages/page-accounts/package.json index 8ca4b068c2..cd86d5c1f8 100644 --- a/packages/page-accounts/package.json +++ b/packages/page-accounts/package.json @@ -15,7 +15,7 @@ "@polkadot/react-components": "0.42.0-beta.112", "@polkadot/react-qr": "^0.53.0-beta.3", "@polkadot/vanitygen": "^0.12.0-beta.1", - "detect-browser": "^5.0.0", + "detect-browser": "^5.1.0", "file-saver": "^2.0.2" } } diff --git a/packages/page-accounts/src/Accounts/Account.tsx b/packages/page-accounts/src/Accounts/Account.tsx index da72a67f2e..8c3aacd050 100644 --- a/packages/page-accounts/src/Accounts/Account.tsx +++ b/packages/page-accounts/src/Accounts/Account.tsx @@ -2,16 +2,18 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { DeriveBalancesAll } from '@polkadot/api-derive/types'; +import { SubmittableExtrinsic } from '@polkadot/api/types'; +import { DeriveBalancesAll, DeriveDemocracyLock } from '@polkadot/api-derive/types'; import { ActionStatus } from '@polkadot/react-components/Status/types'; import { H256, Multisig, RecoveryConfig } from '@polkadot/types/interfaces'; import { SortedAccount } from './types'; import BN from 'bn.js'; -import React, { useCallback, useState, useEffect } from 'react'; +import React, { useCallback, useContext, useState, useEffect } from 'react'; import styled from 'styled-components'; +import { ApiPromise } from '@polkadot/api'; import { getLedger } from '@polkadot/react-api'; -import { AddressInfo, AddressMini, AddressSmall, Badge, Button, ChainLock, CryptoType, Forget, Icon, IdentityIcon, LinkExternal, Menu, Popup, Tag } from '@polkadot/react-components'; +import { AddressInfo, AddressMini, AddressSmall, Badge, Button, ChainLock, CryptoType, Forget, Icon, IdentityIcon, LinkExternal, Menu, Popup, StatusContext, Tag } from '@polkadot/react-components'; import { useAccountInfo, useApi, useCall, useIncrement, useToggle } from '@polkadot/react-hooks'; import { Option, StorageKey } from '@polkadot/types'; import keyring from '@polkadot/ui-keyring'; @@ -34,6 +36,11 @@ interface Props extends SortedAccount { toggleFavorite: (address: string) => void; } +interface DemocracyUnlockable { + democracyUnlockTx: SubmittableExtrinsic<'promise'> | null; + ids: BN[]; +} + function calcVisible (filter: string, name: string, tags: string[]): boolean { if (filter.length === 0) { return true; @@ -46,11 +53,22 @@ function calcVisible (filter: string, name: string, tags: string[]): boolean { }, name.toLowerCase().includes(_filter)); } +function createClearDemocracyTx (api: ApiPromise, address: string, unlockableIds: BN[]): SubmittableExtrinsic<'promise'> { + return api.tx.utility.batch( + unlockableIds + .map((id) => api.tx.democracy.removeVote(id)) + .concat(api.tx.democracy.unlock(address)) + ); +} + function Account ({ account: { address, meta }, className, filter, isFavorite, setBalance, toggleFavorite }: Props): React.ReactElement | null { const { t } = useTranslation(); + const { queueExtrinsic } = useContext(StatusContext); const api = useApi(); const [multiInc, refreshMulti] = useIncrement(); + const bestNumber = useCall(api.api.derive.chain.bestNumber, []); const balancesAll = useCall(api.api.derive.balances.all, [address]); + const democracyLocks = useCall(api.api.derive.democracy.locks, [address]); const recoveryInfo = useCall(api.api.query.recovery?.recoverable, [address], { transform: (opt: Option) => opt.unwrapOr(null) }); @@ -61,6 +79,7 @@ function Account ({ account: { address, meta }, className, filter, isFavorite, s .map(([key, opt]) => [key.args[1] as H256, opt.unwrap()]) }); const { flags: { isDevelopment, isExternal, isHardware, isInjected, isMultisig }, genesisHash, name: accName, onSetGenesisHash, tags } = useAccountInfo(address); + const [{ democracyUnlockTx }, setUnlockableIds] = useState({ democracyUnlockTx: null, ids: [] }); const [isVisible, setIsVisible] = useState(true); const [isBackupOpen, toggleBackup] = useToggle(); const [isDeriveOpen, toggleDerive] = useToggle(); @@ -83,8 +102,27 @@ function Account ({ account: { address, meta }, className, filter, isFavorite, s ); }, [accName, filter, tags]); + useEffect((): void => { + bestNumber && democracyLocks && setUnlockableIds( + (prev): DemocracyUnlockable => { + const ids = democracyLocks + .filter(({ isFinished, unlockAt }) => isFinished && bestNumber.gt(unlockAt)) + .map(({ referendumId }) => referendumId); + + if (JSON.stringify(prev.ids) === JSON.stringify(ids)) { + return prev; + } + + return { + democracyUnlockTx: createClearDemocracyTx(api.api, address, ids), + ids + }; + } + ); + }, [address, api, bestNumber, democracyLocks]); + const _onFavorite = useCallback( - (): void => toggleFavorite(address), + () => toggleFavorite(address), [address, toggleFavorite] ); @@ -119,6 +157,16 @@ function Account ({ account: { address, meta }, className, filter, isFavorite, s [refreshMulti, toggleMultisig] ); + const _clearDemocracyLocks = useCallback( + (): void => { + democracyUnlockTx && queueExtrinsic({ + accountId: address, + extrinsic: democracyUnlockTx + }); + }, + [address, democracyUnlockTx, queueExtrinsic] + ); + const _showOnHardware = useCallback( (): void => { // TODO: we should check the hardwareType from metadata here as well, @@ -312,6 +360,13 @@ function Account ({ account: { address, meta }, className, filter, isFavorite, s > {t('Set on-chain identity')} + + {t('Clear expired democracy locks')} + + string): React.ReactNode { - const { address, balancesAll, stakingInfo, withBalance = true, withBalanceToggle = false } = props; + const { address, balancesAll, democracyLocks, stakingInfo, withBalance = true, withBalanceToggle = false } = props; const balanceDisplay = withBalance === true ? DEFAULT_BALANCES : withBalance || false; @@ -329,7 +331,15 @@ function renderBalances (props: Props, allAccounts: string[], t: (key: string) = <>