From 42cb6b84d33ae86376b6f780b592e4572767eee6 Mon Sep 17 00:00:00 2001 From: Jaco Date: Fri, 2 Jul 2021 11:42:13 +0300 Subject: [PATCH] Rework collective queries (#5732) * Rework collective queries * members derive * All via derive * Adjust society useMembers * Adjust optional consts * Detect council module locations * Adjust tech comm with module locations * Rework loading * Unused type import --- packages/apps-routing/src/council.ts | 10 ++---- packages/apps-routing/src/techcomm.ts | 3 +- packages/apps-routing/src/types.ts | 1 + packages/apps/src/Content/index.tsx | 13 ++++++-- packages/apps/src/Menu/index.tsx | 4 +-- packages/apps/src/endpoint.ts | 16 +++++---- .../BountyActions/BountyInitiateVoting.tsx | 2 +- .../BountyActions/ProposeCuratorAction.tsx | 2 +- .../src/BountyExtraActions/CloseBounty.tsx | 2 +- .../src/BountyExtraActions/SlashCurator.tsx | 2 +- .../src/BountyExtraActions/index.tsx | 2 +- .../src/BountyInfos/VotingSummary.tsx | 2 +- .../page-bounties/src/hooks/useUserRole.ts | 2 +- packages/page-council/src/Motions/Close.tsx | 8 +++-- packages/page-council/src/Motions/Motion.tsx | 10 +++--- .../src/Motions/ProposeExternal.tsx | 13 ++++++-- .../src/Motions/ProposeMotion.tsx | 12 +++++-- .../page-council/src/Motions/Slashing.tsx | 12 +++++-- packages/page-council/src/Motions/Voting.tsx | 10 +++--- packages/page-council/src/Motions/index.tsx | 4 +-- .../src/Overview/SubmitCandidacy.tsx | 33 ++++++++++--------- .../page-council/src/Overview/Summary.tsx | 8 ++--- packages/page-council/src/Overview/Vote.tsx | 20 +++++++---- packages/page-council/src/Overview/index.tsx | 2 +- packages/page-council/src/index.tsx | 7 +--- packages/page-council/src/useCounter.ts | 2 +- packages/page-council/src/useModuleCouncil.ts | 22 +++++++++++++ .../page-council/src/useModuleElections.ts | 22 +++++++++++++ packages/page-society/src/useMembers.ts | 11 ++++--- packages/page-staking/src/Slashes/index.tsx | 2 +- packages/page-tech-comm/src/App.tsx | 31 ++++++++--------- .../page-tech-comm/src/Overview/Summary.tsx | 10 +++--- .../page-tech-comm/src/Overview/index.tsx | 4 +-- .../page-tech-comm/src/Proposals/Close.tsx | 8 +++-- .../page-tech-comm/src/Proposals/Proposal.tsx | 33 ++++++++----------- .../page-tech-comm/src/Proposals/Propose.tsx | 12 +++++-- .../page-tech-comm/src/Proposals/Voting.tsx | 8 +++-- .../page-tech-comm/src/Proposals/index.tsx | 6 ++-- packages/page-tech-comm/src/types.ts | 4 +-- packages/page-tech-comm/src/useCounter.ts | 4 +-- .../page-tech-comm/src/useModuleCollective.ts | 22 +++++++++++++ packages/page-treasury/src/index.tsx | 2 +- packages/react-hooks/src/useMembers.ts | 4 +-- packages/react-hooks/src/useVotingStatus.ts | 15 +++++---- 44 files changed, 265 insertions(+), 157 deletions(-) create mode 100644 packages/page-council/src/useModuleCouncil.ts create mode 100644 packages/page-council/src/useModuleElections.ts create mode 100644 packages/page-tech-comm/src/useModuleCollective.ts diff --git a/packages/apps-routing/src/council.ts b/packages/apps-routing/src/council.ts index f91f7d3af5..a33f9c1b95 100644 --- a/packages/apps-routing/src/council.ts +++ b/packages/apps-routing/src/council.ts @@ -11,13 +11,9 @@ export default function create (t: TFunction): Route { Component, display: { needsApi: [ - 'query.council.prime', - [ - 'query.phragmenElection.candidates', - 'query.electionsPhragmen.candidates', - 'query.elections.candidates' - ] - ] + 'query.council.prime' + ], + needsApiInstances: true }, group: 'governance', icon: 'building', diff --git a/packages/apps-routing/src/techcomm.ts b/packages/apps-routing/src/techcomm.ts index 4ad99bbb51..f3c7e08517 100644 --- a/packages/apps-routing/src/techcomm.ts +++ b/packages/apps-routing/src/techcomm.ts @@ -13,7 +13,8 @@ export default function create (t: TFunction): Route { needsAccounts: true, needsApi: [ 'query.technicalCommittee.members' - ] + ], + needsApiInstances: true }, group: 'governance', icon: 'microchip', diff --git a/packages/apps-routing/src/types.ts b/packages/apps-routing/src/types.ts index cd2fe55814..41c024a14b 100644 --- a/packages/apps-routing/src/types.ts +++ b/packages/apps-routing/src/types.ts @@ -19,6 +19,7 @@ export interface Route { isModal?: boolean; needsAccounts?: boolean; needsApi?: (string | string[])[]; + needsApiInstances?: boolean; needsSudo?: boolean; needsTeleport?: boolean; }; diff --git a/packages/apps/src/Content/index.tsx b/packages/apps/src/Content/index.tsx index 12ee309d2a..8dff56dfbb 100644 --- a/packages/apps/src/Content/index.tsx +++ b/packages/apps/src/Content/index.tsx @@ -38,7 +38,7 @@ function Content ({ className }: Props): React.ReactElement { const { api, isApiConnected, isApiReady } = useApi(); const { queueAction } = useContext(StatusContext); - const { Component, display: { needsApi }, icon, name, text } = useMemo( + const { Component, display: { needsApi, needsApiInstances }, icon, name, text } = useMemo( (): Route => { const app = location.pathname.slice(1) || ''; @@ -47,11 +47,18 @@ function Content ({ className }: Props): React.ReactElement { [location, t] ); - const missingApis = findMissingApis(api, needsApi); + const missingApis = useMemo( + () => needsApi + ? isApiReady && isApiConnected + ? findMissingApis(api, needsApi, needsApiInstances) + : null + : [], + [api, isApiConnected, isApiReady, needsApi, needsApiInstances] + ); return (
- {needsApi && (!isApiReady || !isApiConnected) + {!missingApis ? (
('Initializing connection')} /> diff --git a/packages/apps/src/Menu/index.tsx b/packages/apps/src/Menu/index.tsx index 903ddd3cd1..7356d9611b 100644 --- a/packages/apps/src/Menu/index.tsx +++ b/packages/apps/src/Menu/index.tsx @@ -42,7 +42,7 @@ function createExternals (t: TFunction): ItemRoute[] { ]; } -function checkVisible ({ api, isApiConnected, isApiReady }: ApiProps, allowTeleport: boolean, hasAccounts: boolean, hasSudo: boolean, { isHidden, needsAccounts, needsApi, needsSudo, needsTeleport }: Route['display']): boolean { +function checkVisible ({ api, isApiConnected, isApiReady }: ApiProps, allowTeleport: boolean, hasAccounts: boolean, hasSudo: boolean, { isHidden, needsAccounts, needsApi, needsApiInstances, needsSudo, needsTeleport }: Route['display']): boolean { if (isHidden) { return false; } else if (needsAccounts && !hasAccounts) { @@ -57,7 +57,7 @@ function checkVisible ({ api, isApiConnected, isApiReady }: ApiProps, allowTelep return false; } - return findMissingApis(api, needsApi).length === 0; + return findMissingApis(api, needsApi, needsApiInstances).length === 0; } function extractGroups (routing: Routes, groupNames: Record, apiProps: ApiProps, allowTeleport: boolean, hasAccounts: boolean, hasSudo: boolean): Group[] { diff --git a/packages/apps/src/endpoint.ts b/packages/apps/src/endpoint.ts index 45212d64a8..967f0c5a21 100644 --- a/packages/apps/src/endpoint.ts +++ b/packages/apps/src/endpoint.ts @@ -4,26 +4,28 @@ import { ApiPromise } from '@polkadot/api'; import { isFunction } from '@polkadot/util'; -function hasEndpoint (api: ApiPromise, endpoint: string): boolean { - const [area, section, method] = endpoint.split('.'); +type ApiMapper = Record>>; + +function hasEndpoint (api: ApiPromise, endpoint: string, needsApiInstances: boolean): boolean { + const [area, _section, method] = endpoint.split('.'); + const [section] = (needsApiInstances && api.registry.getModuleInstances(api.runtimeVersion.specName.toString(), _section)) || [_section]; try { - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access - return isFunction((api as any)[area][section][method]); + return isFunction((api as unknown as ApiMapper)[area][section][method]); } catch (error) { return false; } } -export function findMissingApis (api: ApiPromise, needsApi?: (string | string[])[]): (string | string[])[] { +export function findMissingApis (api: ApiPromise, needsApi?: (string | string[])[], needsApiInstances = false): (string | string[])[] { if (!needsApi) { return []; } return needsApi.filter((endpoint: string | string[]): boolean => { const hasApi = Array.isArray(endpoint) - ? endpoint.reduce((hasApi, endpoint) => hasApi || hasEndpoint(api, endpoint), false) - : hasEndpoint(api, endpoint); + ? endpoint.reduce((hasApi, endpoint) => hasApi || hasEndpoint(api, endpoint, needsApiInstances), false) + : hasEndpoint(api, endpoint, needsApiInstances); return !hasApi; }); diff --git a/packages/page-bounties/src/BountyActions/BountyInitiateVoting.tsx b/packages/page-bounties/src/BountyActions/BountyInitiateVoting.tsx index c137905761..2ebe5706b2 100644 --- a/packages/page-bounties/src/BountyActions/BountyInitiateVoting.tsx +++ b/packages/page-bounties/src/BountyActions/BountyInitiateVoting.tsx @@ -26,7 +26,7 @@ const BOUNTY_METHODS = ['approveBounty', 'closeBounty']; function BountyInitiateVoting ({ description, index, proposals }: Props): React.ReactElement | null { const { t } = useTranslation(); const { api } = useApi(); - const { isMember, members } = useMembers(); + const { isMember, members } = useMembers('council'); const { approveBounty, closeBounty } = useBounties(); const [isOpen, toggleOpen] = useToggle(); const [accountId, setAccountId] = useState(null); diff --git a/packages/page-bounties/src/BountyActions/ProposeCuratorAction.tsx b/packages/page-bounties/src/BountyActions/ProposeCuratorAction.tsx index 9cabf101d3..2bc9de0f4d 100644 --- a/packages/page-bounties/src/BountyActions/ProposeCuratorAction.tsx +++ b/packages/page-bounties/src/BountyActions/ProposeCuratorAction.tsx @@ -28,7 +28,7 @@ const BOUNTY_METHODS = ['proposeCurator']; function ProposeCuratorAction ({ description, index, proposals, value }: Props): React.ReactElement | null { const { t } = useTranslation(); const { api } = useApi(); - const { isMember, members } = useMembers(); + const { isMember, members } = useMembers('council'); const { proposeCurator } = useBounties(); const [isOpen, toggleOpen] = useToggle(); const [accountId, setAccountId] = useState(null); diff --git a/packages/page-bounties/src/BountyExtraActions/CloseBounty.tsx b/packages/page-bounties/src/BountyExtraActions/CloseBounty.tsx index c555b02311..26332821fd 100644 --- a/packages/page-bounties/src/BountyExtraActions/CloseBounty.tsx +++ b/packages/page-bounties/src/BountyExtraActions/CloseBounty.tsx @@ -23,7 +23,7 @@ interface Props { function CloseBounty ({ description, index, toggleOpen }: Props): React.ReactElement | null { const { t } = useTranslation(); const { api } = useApi(); - const { members } = useMembers(); + const { members } = useMembers('council'); const { closeBounty } = useBounties(); const [accountId, setAccountId] = useState(null); const [threshold, setThreshold] = useState(); diff --git a/packages/page-bounties/src/BountyExtraActions/SlashCurator.tsx b/packages/page-bounties/src/BountyExtraActions/SlashCurator.tsx index a2e8174ccd..b2cd994484 100644 --- a/packages/page-bounties/src/BountyExtraActions/SlashCurator.tsx +++ b/packages/page-bounties/src/BountyExtraActions/SlashCurator.tsx @@ -38,7 +38,7 @@ interface ActionProperties { function SlashCurator ({ action, curatorId, description, index, toggleOpen }: Props): React.ReactElement | null { const { t } = useTranslation(); const { api } = useApi(); - const { members } = useMembers(); + const { members } = useMembers('council'); const { unassignCurator } = useBounties(); const [accountId, setAccountId] = useState(null); const [threshold, setThreshold] = useState(); diff --git a/packages/page-bounties/src/BountyExtraActions/index.tsx b/packages/page-bounties/src/BountyExtraActions/index.tsx index 017c98e457..9c6c4e4867 100644 --- a/packages/page-bounties/src/BountyExtraActions/index.tsx +++ b/packages/page-bounties/src/BountyExtraActions/index.tsx @@ -36,7 +36,7 @@ function Index ({ bestNumber, className, description, index, proposals, status } const [isGiveUpCuratorOpen, toggleGiveUpCurator] = useToggle(); const [selectedAction, setSlashAction] = useState(); const { t } = useTranslation(); - const { isMember } = useMembers(); + const { isMember } = useMembers('council'); const { curator, updateDue } = useBountyStatus(status); const blocksUntilUpdate = useMemo(() => updateDue?.sub(bestNumber), [bestNumber, updateDue]); diff --git a/packages/page-bounties/src/BountyInfos/VotingSummary.tsx b/packages/page-bounties/src/BountyInfos/VotingSummary.tsx index 4e4f7f9d5a..88bedd4812 100644 --- a/packages/page-bounties/src/BountyInfos/VotingSummary.tsx +++ b/packages/page-bounties/src/BountyInfos/VotingSummary.tsx @@ -20,7 +20,7 @@ interface Props { } function VotingSummary ({ className, proposal, status }: Props): JSX.Element { - const { members } = useMembers(); + const { members } = useMembers('council'); const { t } = useTranslation(); const ayes = useMemo(() => proposal?.votes?.ayes?.length, [proposal]); const nays = useMemo(() => proposal?.votes?.nays?.length, [proposal]); diff --git a/packages/page-bounties/src/hooks/useUserRole.ts b/packages/page-bounties/src/hooks/useUserRole.ts index 623eebd14e..0e7b6660bc 100644 --- a/packages/page-bounties/src/hooks/useUserRole.ts +++ b/packages/page-bounties/src/hooks/useUserRole.ts @@ -14,7 +14,7 @@ export type UserRolesInfo = { roles: UserRole[], isCurator: boolean }; export function useUserRole (curatorId?: AccountId): UserRolesInfo { const { allAccounts, hasAccounts } = useAccounts(); - const { isMember } = useMembers(); + const { isMember } = useMembers('council'); const isCurator = useMemo(() => curatorId && allAccounts.includes(curatorId.toString()), [allAccounts, curatorId]); diff --git a/packages/page-council/src/Motions/Close.tsx b/packages/page-council/src/Motions/Close.tsx index 1a127aa42a..8ab6388c6b 100644 --- a/packages/page-council/src/Motions/Close.tsx +++ b/packages/page-council/src/Motions/Close.tsx @@ -9,6 +9,7 @@ import { Button, InputAddress, Modal, ProposedAction, TxButton } from '@polkadot import { useApi, useToggle, useWeight } from '@polkadot/react-hooks'; import { useTranslation } from '../translate'; +import { useModuleCouncil } from '../useModuleCouncil'; interface Props { hasFailed: boolean; @@ -24,9 +25,10 @@ function Close ({ hasFailed, hash, idNumber, members, proposal }: Props): React. const [isOpen, toggleOpen] = useToggle(); const [accountId, setAccountId] = useState(null); const [proposalWeight, proposalLength] = useWeight(proposal); + const modLocation = useModuleCouncil(); // protect against older versions - if (!api.tx.council.close) { + if (!modLocation) { return null; } @@ -59,13 +61,13 @@ function Close ({ hasFailed, hash, idNumber, members, proposal }: Props): React. accountId={accountId} onStart={toggleOpen} params={ - api.tx.council.close.meta.args.length === 4 + api.tx[modLocation].close.meta.args.length === 4 ? hasFailed ? [hash, idNumber, 0, 0] : [hash, idNumber, proposalWeight, proposalLength] : [hash, idNumber] } - tx={api.tx.council.closeOperational || api.tx.council.close} + tx={api.tx[modLocation].closeOperational || api.tx[modLocation].close} /> diff --git a/packages/page-council/src/Motions/Motion.tsx b/packages/page-council/src/Motions/Motion.tsx index fe9bc53060..fd432da8e7 100644 --- a/packages/page-council/src/Motions/Motion.tsx +++ b/packages/page-council/src/Motions/Motion.tsx @@ -13,6 +13,7 @@ import { BlockToTime } from '@polkadot/react-query'; import { formatNumber } from '@polkadot/util'; import { useTranslation } from '../translate'; +import { useModuleCouncil } from '../useModuleCouncil'; import Close from './Close'; import Voters from './Voters'; import Voting from './Voting'; @@ -22,7 +23,7 @@ interface Props { isMember: boolean; members: string[]; motion: DeriveCollectiveProposal; - prime: AccountId | null; + prime?: AccountId | null; } interface VoterState { @@ -36,6 +37,7 @@ function Motion ({ className = '', isMember, members, motion: { hash, proposal, const { allAccounts } = useAccounts(); const { hasFailed, isCloseable, isVoteable, remainingBlocks } = useVotingStatus(votes, members.length, 'council'); const [proposalWeight, proposalLength] = useWeight(proposal); + const modLocation = useModuleCouncil(); const [councilId, isMultiMembers] = useMemo( (): [string | null, boolean] => { @@ -62,7 +64,7 @@ function Motion ({ className = '', isMember, members, motion: { hash, proposal, [allAccounts, votes] ); - if (!votes) { + if (!votes || !modLocation) { return null; } @@ -127,13 +129,13 @@ function Motion ({ className = '', isMember, members, motion: { hash, proposal, icon='times' label={t('Close')} params={ - api.tx.council.close?.meta.args.length === 4 + api.tx[modLocation].close?.meta.args.length === 4 ? hasFailed ? [hash, index, 0, 0] : [hash, index, proposalWeight, proposalLength] : [hash, index] } - tx={api.tx.council.closeOperational || api.tx.council.close} + tx={api.tx[modLocation].closeOperational || api.tx[modLocation].close} /> ) )} diff --git a/packages/page-council/src/Motions/ProposeExternal.tsx b/packages/page-council/src/Motions/ProposeExternal.tsx index 5863831fa0..13633c1f73 100644 --- a/packages/page-council/src/Motions/ProposeExternal.tsx +++ b/packages/page-council/src/Motions/ProposeExternal.tsx @@ -11,6 +11,7 @@ import { useApi, useToggle } from '@polkadot/react-hooks'; import { isHex } from '@polkadot/util'; import { useTranslation } from '../translate'; +import { useModuleCouncil } from '../useModuleCouncil'; interface Props { className?: string; @@ -28,13 +29,14 @@ interface ProposalState { proposalLength: number; } -function ProposeExternal ({ className = '', isMember, members }: Props): React.ReactElement { +function ProposeExternal ({ className = '', isMember, members }: Props): React.ReactElement | null { const { t } = useTranslation(); const { api } = useApi(); const [isVisible, toggleVisible] = useToggle(); const [accountId, setAcountId] = useState(null); const [{ proposal, proposalLength }, setProposal] = useState({ proposalLength: 0 }); const [{ hash, isHashValid }, setHash] = useState({ hash: '', isHashValid: false }); + const modLocation = useModuleCouncil(); const threshold = Math.ceil((members.length || 0) * getProposalThreshold(api)); @@ -59,6 +61,10 @@ function ProposeExternal ({ className = '', isMember, members }: Props): React.R } }, [api, hash, isHashValid]); + if (!modLocation) { + return null; + } + return ( <>