Use useCollectiveModule for all collective locations (#5762)
* Use useCollectiveModule for all collective locations * staking useCollectiveModule * Bounties useCollectiveModule * Mock collective * Adjust * useMember -> useCollectiveMembers
This commit is contained in:
parent
08f4089fab
commit
97d2866e6e
@ -27,8 +27,12 @@ jest.mock('@polkadot/react-hooks/useTreasury', () => ({
|
||||
useTreasury: () => mockHooks.treasury
|
||||
}));
|
||||
|
||||
jest.mock('@polkadot/react-hooks/useMembers', () => ({
|
||||
useMembers: () => mockHooks.members
|
||||
jest.mock('@polkadot/react-hooks/useCollectiveInstance', () => ({
|
||||
useCollectiveInstance: () => 'council'
|
||||
}));
|
||||
|
||||
jest.mock('@polkadot/react-hooks/useCollectiveMembers', () => ({
|
||||
useCollectiveMembers: () => mockHooks.members
|
||||
}));
|
||||
|
||||
jest.mock('@polkadot/react-hooks/useBlockTime', () => ({
|
||||
|
||||
@ -9,7 +9,7 @@ import React, { useEffect, useMemo, useRef, useState } from 'react';
|
||||
|
||||
import { getTreasuryProposalThreshold } from '@polkadot/apps-config';
|
||||
import { Button, InputAddress, Modal, TxButton } from '@polkadot/react-components';
|
||||
import { useApi, useMembers, useToggle } from '@polkadot/react-hooks';
|
||||
import { useApi, useCollectiveInstance, useCollectiveMembers, useToggle } from '@polkadot/react-hooks';
|
||||
|
||||
import { truncateTitle } from '../helpers';
|
||||
import { useBounties } from '../hooks';
|
||||
@ -26,7 +26,8 @@ const BOUNTY_METHODS = ['approveBounty', 'closeBounty'];
|
||||
function BountyInitiateVoting ({ description, index, proposals }: Props): React.ReactElement<Props> | null {
|
||||
const { t } = useTranslation();
|
||||
const { api } = useApi();
|
||||
const { isMember, members } = useMembers('council');
|
||||
const { isMember, members } = useCollectiveMembers('council');
|
||||
const councilMod = useCollectiveInstance('council');
|
||||
const { approveBounty, closeBounty } = useBounties();
|
||||
const [isOpen, toggleOpen] = useToggle();
|
||||
const [accountId, setAccountId] = useState<string | null>(null);
|
||||
@ -43,7 +44,7 @@ function BountyInitiateVoting ({ description, index, proposals }: Props): React.
|
||||
|
||||
const isVotingInitiated = useMemo(() => proposals?.filter(({ proposal }) => BOUNTY_METHODS.includes(proposal.method)).length !== 0, [proposals]);
|
||||
|
||||
return isMember && !isVotingInitiated
|
||||
return isMember && !isVotingInitiated && councilMod
|
||||
? (
|
||||
<>
|
||||
<Button
|
||||
@ -77,7 +78,7 @@ function BountyInitiateVoting ({ description, index, proposals }: Props): React.
|
||||
label={t<string>('Approve')}
|
||||
onStart={toggleOpen}
|
||||
params={[threshold, approveBountyProposal.current, approveBountyProposal.current.length]}
|
||||
tx={api.tx.council.propose}
|
||||
tx={api.tx[councilMod].propose}
|
||||
/>
|
||||
<TxButton
|
||||
accountId={accountId}
|
||||
@ -86,7 +87,7 @@ function BountyInitiateVoting ({ description, index, proposals }: Props): React.
|
||||
label={t<string>('Reject')}
|
||||
onStart={toggleOpen}
|
||||
params={[threshold, closeBountyProposal.current, closeBountyProposal.current.length]}
|
||||
tx={api.tx.council.propose}
|
||||
tx={api.tx[councilMod].propose}
|
||||
/>
|
||||
</Modal.Actions>
|
||||
</Modal>
|
||||
|
||||
@ -9,7 +9,7 @@ import React, { useEffect, useMemo, useState } from 'react';
|
||||
|
||||
import { getTreasuryProposalThreshold } from '@polkadot/apps-config';
|
||||
import { Button, InputAddress, InputBalance, MarkError, Modal, TxButton } from '@polkadot/react-components';
|
||||
import { useApi, useMembers, useToggle } from '@polkadot/react-hooks';
|
||||
import { useApi, useCollectiveInstance, useCollectiveMembers, useToggle } from '@polkadot/react-hooks';
|
||||
import { BN_ZERO } from '@polkadot/util';
|
||||
|
||||
import { truncateTitle } from '../helpers';
|
||||
@ -28,7 +28,8 @@ const BOUNTY_METHODS = ['proposeCurator'];
|
||||
function ProposeCuratorAction ({ description, index, proposals, value }: Props): React.ReactElement<Props> | null {
|
||||
const { t } = useTranslation();
|
||||
const { api } = useApi();
|
||||
const { isMember, members } = useMembers('council');
|
||||
const { isMember, members } = useCollectiveMembers('council');
|
||||
const councilMod = useCollectiveInstance('council');
|
||||
const { proposeCurator } = useBounties();
|
||||
const [isOpen, toggleOpen] = useToggle();
|
||||
const [accountId, setAccountId] = useState<string | null>(null);
|
||||
@ -51,7 +52,7 @@ function ProposeCuratorAction ({ description, index, proposals, value }: Props):
|
||||
setIsFeeValid(!!value?.gt(fee));
|
||||
}, [value, fee]);
|
||||
|
||||
return isMember && !isVotingInitiated
|
||||
return isMember && !isVotingInitiated && councilMod
|
||||
? (
|
||||
<>
|
||||
<Button
|
||||
@ -107,7 +108,7 @@ function ProposeCuratorAction ({ description, index, proposals, value }: Props):
|
||||
label={t<string>('Propose curator')}
|
||||
onStart={toggleOpen}
|
||||
params={[threshold, proposeCuratorProposal, proposeCuratorProposal?.length]}
|
||||
tx={api.tx.council.propose}
|
||||
tx={api.tx[councilMod].propose}
|
||||
/>
|
||||
</Modal.Actions>
|
||||
</Modal>
|
||||
|
||||
@ -8,7 +8,7 @@ import React, { useEffect, useRef, useState } from 'react';
|
||||
|
||||
import { getTreasuryProposalThreshold } from '@polkadot/apps-config';
|
||||
import { InputAddress, Modal, TxButton } from '@polkadot/react-components';
|
||||
import { useApi, useMembers } from '@polkadot/react-hooks';
|
||||
import { useApi, useCollectiveInstance, useCollectiveMembers } from '@polkadot/react-hooks';
|
||||
|
||||
import { truncateTitle } from '../helpers';
|
||||
import { useBounties } from '../hooks';
|
||||
@ -23,7 +23,8 @@ interface Props {
|
||||
function CloseBounty ({ description, index, toggleOpen }: Props): React.ReactElement<Props> | null {
|
||||
const { t } = useTranslation();
|
||||
const { api } = useApi();
|
||||
const { members } = useMembers('council');
|
||||
const { members } = useCollectiveMembers('council');
|
||||
const councilMod = useCollectiveInstance('council');
|
||||
const { closeBounty } = useBounties();
|
||||
const [accountId, setAccountId] = useState<string | null>(null);
|
||||
const [threshold, setThreshold] = useState<BN>();
|
||||
@ -36,6 +37,10 @@ function CloseBounty ({ description, index, toggleOpen }: Props): React.ReactEle
|
||||
|
||||
const closeBountyProposal = useRef(closeBounty(index));
|
||||
|
||||
if (!councilMod) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<Modal
|
||||
header={`${t<string>('close bounty')} - "${truncateTitle(description, 30)}"`}
|
||||
@ -61,7 +66,7 @@ function CloseBounty ({ description, index, toggleOpen }: Props): React.ReactEle
|
||||
label={t<string>('Close Bounty')}
|
||||
onStart={toggleOpen}
|
||||
params={[threshold, closeBountyProposal.current, closeBountyProposal.current.length]}
|
||||
tx={api.tx.council.propose}
|
||||
tx={api.tx[councilMod].propose}
|
||||
/>
|
||||
</Modal.Actions>
|
||||
</Modal>
|
||||
|
||||
@ -9,7 +9,7 @@ import React, { useEffect, useMemo, useState } from 'react';
|
||||
import { SubmittableExtrinsic } from '@polkadot/api/types';
|
||||
import { getTreasuryProposalThreshold } from '@polkadot/apps-config';
|
||||
import { InputAddress, Modal, TxButton } from '@polkadot/react-components';
|
||||
import { useAccounts, useApi, useMembers } from '@polkadot/react-hooks';
|
||||
import { useAccounts, useApi, useCollectiveInstance, useCollectiveMembers } from '@polkadot/react-hooks';
|
||||
|
||||
import { truncateTitle } from '../helpers';
|
||||
import { useBounties } from '../hooks';
|
||||
@ -32,13 +32,14 @@ interface ActionProperties {
|
||||
proposingAccountTip: string;
|
||||
tip: string;
|
||||
title: string;
|
||||
tx: ((...args: any[]) => SubmittableExtrinsic<'promise'>);
|
||||
tx: null | ((...args: any[]) => SubmittableExtrinsic<'promise'>);
|
||||
}
|
||||
|
||||
function SlashCurator ({ action, curatorId, description, index, toggleOpen }: Props): React.ReactElement<Props> | null {
|
||||
const { t } = useTranslation();
|
||||
const { api } = useApi();
|
||||
const { members } = useMembers('council');
|
||||
const { members } = useCollectiveMembers('council');
|
||||
const councilMod = useCollectiveInstance('council');
|
||||
const { unassignCurator } = useBounties();
|
||||
const [accountId, setAccountId] = useState<string | null>(null);
|
||||
const [threshold, setThreshold] = useState<BN>();
|
||||
@ -71,7 +72,7 @@ function SlashCurator ({ action, curatorId, description, index, toggleOpen }: Pr
|
||||
proposingAccountTip: t('The council member that will create the motion, submission equates to an "aye" vote.'),
|
||||
tip: t("If the motion is approved, Curator's deposit will be slashed and Curator will be unassigned. Bounty will return to the Funded state."),
|
||||
title: t('Slash curator'),
|
||||
tx: api.tx.council.propose
|
||||
tx: councilMod && api.tx[councilMod].propose
|
||||
},
|
||||
UnassignCurator: {
|
||||
filter: members,
|
||||
@ -81,12 +82,16 @@ function SlashCurator ({ action, curatorId, description, index, toggleOpen }: Pr
|
||||
proposingAccountTip: t('The council member that will create the motion, submission equates to an "aye" vote.'),
|
||||
tip: t('If the motion is approved, the current Curator will be unassigned and the Bounty will return to the Funded state.'),
|
||||
title: t('Unassign curator'),
|
||||
tx: api.tx.council.propose
|
||||
tx: councilMod && api.tx[councilMod].propose
|
||||
}
|
||||
}), [t, index, unassignCurator, api.tx.council.propose, allAccounts, members, threshold, unassignCuratorProposal]);
|
||||
}), [t, index, unassignCurator, api, allAccounts, councilMod, members, threshold, unassignCuratorProposal]);
|
||||
|
||||
const { filter, helpMessage, params, proposingAccountTip, tip, title, tx } = actionProperties[action];
|
||||
|
||||
if (!tx) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<Modal
|
||||
header={`${title} - "${truncateTitle(description, 30)}"`}
|
||||
|
||||
@ -5,7 +5,7 @@ import React, { useMemo, useRef, useState } from 'react';
|
||||
|
||||
import { DeriveCollectiveProposal } from '@polkadot/api-derive/types';
|
||||
import { Button, Menu, Popup } from '@polkadot/react-components';
|
||||
import { useMembers, useToggle } from '@polkadot/react-hooks';
|
||||
import { useCollectiveMembers, useToggle } from '@polkadot/react-hooks';
|
||||
import { BlockNumber, BountyIndex, BountyStatus } from '@polkadot/types/interfaces';
|
||||
|
||||
import { determineUnassignCuratorAction } from '../helpers';
|
||||
@ -36,7 +36,7 @@ function Index ({ bestNumber, className, description, index, proposals, status }
|
||||
const [isGiveUpCuratorOpen, toggleGiveUpCurator] = useToggle();
|
||||
const [selectedAction, setSlashAction] = useState<ValidUnassignCuratorAction>();
|
||||
const { t } = useTranslation();
|
||||
const { isMember } = useMembers('council');
|
||||
const { isMember } = useCollectiveMembers('council');
|
||||
const { curator, updateDue } = useBountyStatus(status);
|
||||
|
||||
const blocksUntilUpdate = useMemo(() => updateDue?.sub(bestNumber), [bestNumber, updateDue]);
|
||||
|
||||
@ -8,7 +8,7 @@ import React, { useMemo } from 'react';
|
||||
import styled from 'styled-components';
|
||||
|
||||
import VotingDescriptionInfo from '@polkadot/app-bounties/BountyInfos/VotingDescriptionInfo';
|
||||
import { useMembers } from '@polkadot/react-hooks';
|
||||
import { useCollectiveMembers } from '@polkadot/react-hooks';
|
||||
|
||||
import { useTranslation } from '../translate';
|
||||
import VotingLink from './VotingLink';
|
||||
@ -20,7 +20,7 @@ interface Props {
|
||||
}
|
||||
|
||||
function VotingSummary ({ className, proposal, status }: Props): JSX.Element {
|
||||
const { members } = useMembers('council');
|
||||
const { members } = useCollectiveMembers('council');
|
||||
const { t } = useTranslation();
|
||||
const ayes = useMemo(() => proposal?.votes?.ayes?.length, [proposal]);
|
||||
const nays = useMemo(() => proposal?.votes?.nays?.length, [proposal]);
|
||||
|
||||
@ -5,7 +5,7 @@ import type { AccountId } from '@polkadot/types/interfaces';
|
||||
|
||||
import { useMemo } from 'react';
|
||||
|
||||
import { useAccounts, useMembers } from '@polkadot/react-hooks';
|
||||
import { useAccounts, useCollectiveMembers } from '@polkadot/react-hooks';
|
||||
|
||||
import { UserRole } from '../types';
|
||||
|
||||
@ -14,7 +14,7 @@ export type UserRolesInfo = { roles: UserRole[], isCurator: boolean };
|
||||
export function useUserRole (curatorId?: AccountId): UserRolesInfo {
|
||||
const { allAccounts, hasAccounts } = useAccounts();
|
||||
|
||||
const { isMember } = useMembers('council');
|
||||
const { isMember } = useCollectiveMembers('council');
|
||||
|
||||
const isCurator = useMemo(() => curatorId && allAccounts.includes(curatorId.toString()), [allAccounts, curatorId]);
|
||||
|
||||
|
||||
@ -6,10 +6,9 @@ import type { Hash, Proposal, ProposalIndex } from '@polkadot/types/interfaces';
|
||||
import React, { useState } from 'react';
|
||||
|
||||
import { Button, InputAddress, Modal, ProposedAction, TxButton } from '@polkadot/react-components';
|
||||
import { useApi, useToggle, useWeight } from '@polkadot/react-hooks';
|
||||
import { useApi, useCollectiveInstance, useToggle, useWeight } from '@polkadot/react-hooks';
|
||||
|
||||
import { useTranslation } from '../translate';
|
||||
import { useModuleCouncil } from '../useModuleCouncil';
|
||||
|
||||
interface Props {
|
||||
hasFailed: boolean;
|
||||
@ -25,7 +24,7 @@ function Close ({ hasFailed, hash, idNumber, members, proposal }: Props): React.
|
||||
const [isOpen, toggleOpen] = useToggle();
|
||||
const [accountId, setAccountId] = useState<string | null>(null);
|
||||
const [proposalWeight, proposalLength] = useWeight(proposal);
|
||||
const modLocation = useModuleCouncil();
|
||||
const modLocation = useCollectiveInstance('council');
|
||||
|
||||
// protect against older versions
|
||||
if (!modLocation) {
|
||||
|
||||
@ -8,12 +8,11 @@ import React, { useMemo } from 'react';
|
||||
|
||||
import ProposalCell from '@polkadot/app-democracy/Overview/ProposalCell';
|
||||
import { Icon, LinkExternal, TxButton } from '@polkadot/react-components';
|
||||
import { useAccounts, useApi, useVotingStatus, useWeight } from '@polkadot/react-hooks';
|
||||
import { useAccounts, useApi, useCollectiveInstance, useVotingStatus, useWeight } from '@polkadot/react-hooks';
|
||||
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';
|
||||
@ -37,7 +36,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 modLocation = useCollectiveInstance('council');
|
||||
|
||||
const [councilId, isMultiMembers] = useMemo(
|
||||
(): [string | null, boolean] => {
|
||||
|
||||
@ -7,11 +7,10 @@ import React, { useCallback, useEffect, useState } from 'react';
|
||||
|
||||
import { getProposalThreshold } from '@polkadot/apps-config';
|
||||
import { Button, Input, InputAddress, Modal, TxButton } from '@polkadot/react-components';
|
||||
import { useApi, useToggle } from '@polkadot/react-hooks';
|
||||
import { useApi, useCollectiveInstance, useToggle } from '@polkadot/react-hooks';
|
||||
import { isHex } from '@polkadot/util';
|
||||
|
||||
import { useTranslation } from '../translate';
|
||||
import { useModuleCouncil } from '../useModuleCouncil';
|
||||
|
||||
interface Props {
|
||||
className?: string;
|
||||
@ -36,7 +35,7 @@ function ProposeExternal ({ className = '', isMember, members }: Props): React.R
|
||||
const [accountId, setAcountId] = useState<string | null>(null);
|
||||
const [{ proposal, proposalLength }, setProposal] = useState<ProposalState>({ proposalLength: 0 });
|
||||
const [{ hash, isHashValid }, setHash] = useState<HashState>({ hash: '', isHashValid: false });
|
||||
const modLocation = useModuleCouncil();
|
||||
const modLocation = useCollectiveInstance('council');
|
||||
|
||||
const threshold = Math.ceil((members.length || 0) * getProposalThreshold(api));
|
||||
|
||||
|
||||
@ -8,11 +8,10 @@ import React, { useCallback, useEffect, useState } from 'react';
|
||||
|
||||
import { getProposalThreshold } from '@polkadot/apps-config';
|
||||
import { Button, Extrinsic, InputAddress, InputNumber, Modal, TxButton } from '@polkadot/react-components';
|
||||
import { useApi, useToggle } from '@polkadot/react-hooks';
|
||||
import { useApi, useCollectiveInstance, useToggle } from '@polkadot/react-hooks';
|
||||
import { BN_ZERO } from '@polkadot/util';
|
||||
|
||||
import { useTranslation } from '../translate';
|
||||
import { useModuleCouncil } from '../useModuleCouncil';
|
||||
|
||||
interface Props {
|
||||
isMember: boolean;
|
||||
@ -36,7 +35,7 @@ function Propose ({ isMember, members }: Props): React.ReactElement<Props> | nul
|
||||
const [accountId, setAcountId] = useState<string | null>(null);
|
||||
const [{ proposal, proposalLength }, setProposal] = useState<ProposalState>({ proposalLength: 0 });
|
||||
const [{ isThresholdValid, threshold }, setThreshold] = useState<Threshold>({ isThresholdValid: false });
|
||||
const modLocation = useModuleCouncil();
|
||||
const modLocation = useCollectiveInstance('council');
|
||||
|
||||
useEffect((): void => {
|
||||
members && setThreshold({
|
||||
|
||||
@ -7,10 +7,9 @@ import React, { useEffect, useMemo, useState } from 'react';
|
||||
|
||||
import { getSlashProposalThreshold } from '@polkadot/apps-config';
|
||||
import { Button, Dropdown, Input, InputAddress, Modal, TxButton } from '@polkadot/react-components';
|
||||
import { useApi, useAvailableSlashes, useToggle } from '@polkadot/react-hooks';
|
||||
import { useApi, useAvailableSlashes, useCollectiveInstance, useToggle } from '@polkadot/react-hooks';
|
||||
|
||||
import { useTranslation } from '../translate';
|
||||
import { useModuleCouncil } from '../useModuleCouncil';
|
||||
|
||||
interface Props {
|
||||
className?: string;
|
||||
@ -36,7 +35,7 @@ function Slashing ({ className = '', isMember, members }: Props): React.ReactEle
|
||||
const [accountId, setAcountId] = useState<string | null>(null);
|
||||
const [{ proposal, proposalLength }, setProposal] = useState<ProposalState>({ proposal: null, proposalLength: 0 });
|
||||
const [selectedEra, setSelectedEra] = useState(0);
|
||||
const modLocation = useModuleCouncil();
|
||||
const modLocation = useCollectiveInstance('council');
|
||||
|
||||
const threshold = Math.ceil((members.length || 0) * getSlashProposalThreshold(api));
|
||||
|
||||
|
||||
@ -6,10 +6,9 @@ import type { AccountId, Hash, Proposal, ProposalIndex } from '@polkadot/types/i
|
||||
import React, { useState } from 'react';
|
||||
|
||||
import { Button, MarkWarning, Modal, ProposedAction, TxButton, VoteAccount } from '@polkadot/react-components';
|
||||
import { useAccounts, useApi, useToggle } from '@polkadot/react-hooks';
|
||||
import { useAccounts, useApi, useCollectiveInstance, useToggle } from '@polkadot/react-hooks';
|
||||
|
||||
import { useTranslation } from '../translate';
|
||||
import { useModuleCouncil } from '../useModuleCouncil';
|
||||
|
||||
interface Props {
|
||||
hash: Hash;
|
||||
@ -26,7 +25,7 @@ function Voting ({ hash, idNumber, isDisabled, members, prime, proposal }: Props
|
||||
const { hasAccounts } = useAccounts();
|
||||
const [isVotingOpen, toggleVoting] = useToggle();
|
||||
const [accountId, setAccountId] = useState<string | null>(null);
|
||||
const modLocation = useModuleCouncil();
|
||||
const modLocation = useCollectiveInstance('council');
|
||||
|
||||
if (!hasAccounts || !modLocation) {
|
||||
return null;
|
||||
|
||||
@ -7,7 +7,7 @@ import type { AccountId } from '@polkadot/types/interfaces';
|
||||
import React, { useRef } from 'react';
|
||||
|
||||
import { Button, Table } from '@polkadot/react-components';
|
||||
import { useMembers } from '@polkadot/react-hooks';
|
||||
import { useCollectiveMembers } from '@polkadot/react-hooks';
|
||||
|
||||
import { useTranslation } from '../translate';
|
||||
import Motion from './Motion';
|
||||
@ -23,7 +23,7 @@ interface Props {
|
||||
|
||||
function Proposals ({ className = '', motions, prime }: Props): React.ReactElement<Props> {
|
||||
const { t } = useTranslation();
|
||||
const { isMember, members } = useMembers('council');
|
||||
const { isMember, members } = useCollectiveMembers('council');
|
||||
|
||||
const headerRef = useRef([
|
||||
[t('motions'), 'start', 2],
|
||||
|
||||
@ -1,22 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/app-council authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { useMemo } from 'react';
|
||||
|
||||
import { useApi } from '@polkadot/react-hooks';
|
||||
import { isFunction } from '@polkadot/util';
|
||||
|
||||
export function useModuleCouncil (): string | null {
|
||||
const { api } = useApi();
|
||||
|
||||
return useMemo(
|
||||
(): string | null => {
|
||||
const [instance] = api.registry.getModuleInstances(api.runtimeVersion.specName.toString(), 'council') || ['council'];
|
||||
|
||||
return isFunction(api.tx[instance as 'council']?.close)
|
||||
? instance
|
||||
: null;
|
||||
},
|
||||
[api]
|
||||
);
|
||||
}
|
||||
@ -6,7 +6,7 @@ import type { DeriveProposalExternal } from '@polkadot/api-derive/types';
|
||||
import React from 'react';
|
||||
|
||||
import { AddressMini, Button } from '@polkadot/react-components';
|
||||
import { useMembers } from '@polkadot/react-hooks';
|
||||
import { useCollectiveMembers } from '@polkadot/react-hooks';
|
||||
import { FormatBalance } from '@polkadot/react-query';
|
||||
|
||||
import Fasttrack from './Fasttrack';
|
||||
@ -19,7 +19,7 @@ interface Props {
|
||||
}
|
||||
|
||||
function External ({ className = '', value: { image, imageHash, threshold } }: Props): React.ReactElement<Props> | null {
|
||||
const { isMember, members } = useMembers('technicalCommittee');
|
||||
const { isMember, members } = useCollectiveMembers('technicalCommittee');
|
||||
|
||||
return (
|
||||
<tr className={className}>
|
||||
|
||||
@ -8,7 +8,7 @@ import BN from 'bn.js';
|
||||
import React, { useEffect, useMemo, useState } from 'react';
|
||||
|
||||
import { Button, Input, InputAddress, InputNumber, Modal, TxButton } from '@polkadot/react-components';
|
||||
import { useApi, useToggle } from '@polkadot/react-hooks';
|
||||
import { useApi, useCollectiveInstance, useToggle } from '@polkadot/react-hooks';
|
||||
|
||||
import { useTranslation } from '../translate';
|
||||
|
||||
@ -36,6 +36,7 @@ function Fasttrack ({ imageHash, members, threshold }: Props): React.ReactElemen
|
||||
const [delayBlocks, setDelayBlocks] = useState<BN | undefined>(DEF_DELAY);
|
||||
const [votingBlocks, setVotingBlocks] = useState<BN | undefined>(api.consts.democracy.fastTrackVotingPeriod || DEF_VOTING);
|
||||
const [{ proposal, proposalLength }, setProposal] = useState<ProposalState>(() => ({ proposalLength: 0 }));
|
||||
const modLocation = useCollectiveInstance('technicalCommittee');
|
||||
|
||||
const memberThreshold = useMemo(
|
||||
() => new BN(
|
||||
@ -57,6 +58,10 @@ function Fasttrack ({ imageHash, members, threshold }: Props): React.ReactElemen
|
||||
});
|
||||
}, [api, delayBlocks, imageHash, members, votingBlocks]);
|
||||
|
||||
if (!modLocation) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
{isFasttrackOpen && (
|
||||
@ -108,11 +113,11 @@ function Fasttrack ({ imageHash, members, threshold }: Props): React.ReactElemen
|
||||
label={t<string>('Fast track')}
|
||||
onStart={toggleFasttrack}
|
||||
params={
|
||||
api.tx.technicalCommittee.propose.meta.args.length === 3
|
||||
api.tx[modLocation].propose.meta.args.length === 3
|
||||
? [memberThreshold, proposal, proposalLength]
|
||||
: [memberThreshold, proposal]
|
||||
}
|
||||
tx={api.tx.technicalCommittee.propose}
|
||||
tx={api.tx[modLocation].propose}
|
||||
/>
|
||||
</Modal.Actions>
|
||||
</Modal>
|
||||
|
||||
@ -7,7 +7,7 @@ import type { SlashEra } from './types';
|
||||
import React, { useCallback, useRef, useState } from 'react';
|
||||
|
||||
import { Button, Table, TxButton } from '@polkadot/react-components';
|
||||
import { useApi } from '@polkadot/react-hooks';
|
||||
import { useApi, useCollectiveInstance } from '@polkadot/react-hooks';
|
||||
import { isFunction } from '@polkadot/util';
|
||||
|
||||
import { useTranslation } from '../translate';
|
||||
@ -35,12 +35,13 @@ interface Selected {
|
||||
function Slashes ({ buttons, councilId, councilThreshold, slash }: Props): React.ReactElement<Props> | null {
|
||||
const { t } = useTranslation();
|
||||
const { api } = useApi();
|
||||
const councilMod = useCollectiveInstance('council');
|
||||
const [{ selected, txAll, txSome }, setSelected] = useState<Selected>((): Selected => {
|
||||
const proposal = api.tx.staking.cancelDeferredSlash(slash.era, slash.slashes.map((_, index) => index));
|
||||
|
||||
return {
|
||||
selected: [],
|
||||
txAll: isFunction(api.tx.council?.propose)
|
||||
txAll: councilMod
|
||||
? { length: proposal.encodedLength, proposal }
|
||||
: null,
|
||||
txSome: null
|
||||
@ -69,12 +70,12 @@ function Slashes ({ buttons, councilId, councilThreshold, slash }: Props): React
|
||||
return {
|
||||
selected,
|
||||
txAll: state.txAll,
|
||||
txSome: proposal && isFunction(api.tx.council?.propose)
|
||||
txSome: proposal && councilMod && isFunction(api.tx[councilMod].propose)
|
||||
? { length: proposal.encodedLength, proposal }
|
||||
: null
|
||||
};
|
||||
}),
|
||||
[api, slash]
|
||||
[api, councilMod, slash]
|
||||
);
|
||||
|
||||
return (
|
||||
@ -82,7 +83,7 @@ function Slashes ({ buttons, councilId, councilThreshold, slash }: Props): React
|
||||
<Summary slash={slash} />
|
||||
<Button.Group>
|
||||
{buttons}
|
||||
{isFunction(api.tx.council?.propose) && (
|
||||
{councilMod && (
|
||||
<>
|
||||
<TxButton
|
||||
accountId={councilId}
|
||||
@ -90,11 +91,11 @@ function Slashes ({ buttons, councilId, councilThreshold, slash }: Props): React
|
||||
isToplevel
|
||||
label={t('Cancel selected')}
|
||||
params={txSome && (
|
||||
api.tx.council.propose.meta.args.length === 3
|
||||
api.tx[councilMod].propose.meta.args.length === 3
|
||||
? [councilThreshold, txSome.proposal, txSome.length]
|
||||
: [councilThreshold, txSome.proposal]
|
||||
)}
|
||||
tx={api.tx.council.propose}
|
||||
tx={api.tx[councilMod].propose}
|
||||
/>
|
||||
<TxButton
|
||||
accountId={councilId}
|
||||
@ -102,11 +103,11 @@ function Slashes ({ buttons, councilId, councilThreshold, slash }: Props): React
|
||||
isToplevel
|
||||
label={t('Cancel all')}
|
||||
params={txAll && (
|
||||
api.tx.council.propose.meta.args.length === 3
|
||||
api.tx[councilMod].propose.meta.args.length === 3
|
||||
? [councilThreshold, txAll.proposal, txAll.length]
|
||||
: [councilThreshold, txAll.proposal]
|
||||
)}
|
||||
tx={api.tx.council.propose}
|
||||
tx={api.tx[councilMod].propose}
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
|
||||
@ -10,7 +10,7 @@ import React, { useMemo, useState } from 'react';
|
||||
|
||||
import { getSlashProposalThreshold } from '@polkadot/apps-config';
|
||||
import { Table, ToggleGroup } from '@polkadot/react-components';
|
||||
import { useAccounts, useApi, useMembers } from '@polkadot/react-hooks';
|
||||
import { useAccounts, useApi, useCollectiveMembers } from '@polkadot/react-hooks';
|
||||
import { formatNumber } from '@polkadot/util';
|
||||
|
||||
import { useTranslation } from '../translate';
|
||||
@ -90,7 +90,7 @@ function Slashes ({ ownStashes = [], slashes }: Props): React.ReactElement<Props
|
||||
const { t } = useTranslation();
|
||||
const { api } = useApi();
|
||||
const { allAccounts } = useAccounts();
|
||||
const { members } = useMembers('council');
|
||||
const { members } = useCollectiveMembers('council');
|
||||
const [selectedIndex, setSelectedIndex] = useState(0);
|
||||
|
||||
const rows = useMemo(
|
||||
|
||||
@ -7,7 +7,7 @@ import React, { useMemo } from 'react';
|
||||
import { Route, Switch } from 'react-router';
|
||||
|
||||
import { Tabs } from '@polkadot/react-components';
|
||||
import { useApi, useCall, useMembers } from '@polkadot/react-hooks';
|
||||
import { useApi, useCall, useCollectiveMembers } from '@polkadot/react-hooks';
|
||||
|
||||
import Overview from './Overview';
|
||||
import Proposals from './Proposals';
|
||||
@ -25,7 +25,7 @@ const HIDDEN_PROPOSALS: string[] = ['proposals'];
|
||||
function TechCommApp ({ basePath, className, type }: Props): React.ReactElement<Props> {
|
||||
const { t } = useTranslation();
|
||||
const { api } = useApi();
|
||||
const { isMember, members } = useMembers(type);
|
||||
const { isMember, members } = useCollectiveMembers(type);
|
||||
const prime = useCall<AccountId | null>(api.derive[type].prime);
|
||||
const hasProposals = useCall<boolean>(api.derive[type].hasProposals);
|
||||
const proposalHashes = useCall<Hash[]>(api.derive[type].proposalHashes);
|
||||
|
||||
@ -6,10 +6,9 @@ import type { Hash, Proposal, ProposalIndex } from '@polkadot/types/interfaces';
|
||||
import React, { useState } from 'react';
|
||||
|
||||
import { Button, InputAddress, Modal, ProposedAction, TxButton } from '@polkadot/react-components';
|
||||
import { useApi, useToggle, useWeight } from '@polkadot/react-hooks';
|
||||
import { useApi, useCollectiveInstance, useToggle, useWeight } from '@polkadot/react-hooks';
|
||||
|
||||
import { useTranslation } from '../translate';
|
||||
import { useModuleCollective } from '../useModuleCollective';
|
||||
|
||||
interface Props {
|
||||
hasFailed: boolean;
|
||||
@ -26,7 +25,7 @@ function Close ({ hasFailed, hash, idNumber, members, proposal, type }: Props):
|
||||
const [isOpen, toggleOpen] = useToggle();
|
||||
const [accountId, setAccountId] = useState<string | null>(null);
|
||||
const [proposalWeight, proposalLength] = useWeight(proposal);
|
||||
const modLocation = useModuleCollective(type);
|
||||
const modLocation = useCollectiveInstance(type);
|
||||
|
||||
if (!modLocation) {
|
||||
return null;
|
||||
|
||||
@ -8,12 +8,11 @@ import React, { useMemo } from 'react';
|
||||
|
||||
import ProposalCell from '@polkadot/app-democracy/Overview/ProposalCell';
|
||||
import { AddressMini, TxButton } from '@polkadot/react-components';
|
||||
import { useAccounts, useApi, useCall, useVotingStatus, useWeight } from '@polkadot/react-hooks';
|
||||
import { useAccounts, useApi, useCall, useCollectiveInstance, useVotingStatus, useWeight } from '@polkadot/react-hooks';
|
||||
import { BlockToTime } from '@polkadot/react-query';
|
||||
import { formatNumber } from '@polkadot/util';
|
||||
|
||||
import { useTranslation } from '../translate';
|
||||
import { useModuleCollective } from '../useModuleCollective';
|
||||
import Close from './Close';
|
||||
import Voting from './Voting';
|
||||
|
||||
@ -33,7 +32,7 @@ function Proposal ({ className = '', imageHash, members, prime, type }: Props):
|
||||
const derive = useCall<DeriveCollectiveProposal>(api.derive[type].proposal, [imageHash]);
|
||||
const { hasFailed, isCloseable, isVoteable, remainingBlocks } = useVotingStatus(derive?.votes, members.length, type);
|
||||
const [proposalWeight, proposalLength] = useWeight(derive?.proposal);
|
||||
const modLocation = useModuleCollective(type);
|
||||
const modLocation = useCollectiveInstance(type);
|
||||
|
||||
const [councilId, isMultiMembers] = useMemo(
|
||||
(): [string | null, boolean] => {
|
||||
|
||||
@ -7,10 +7,9 @@ import BN from 'bn.js';
|
||||
import React, { useCallback, useState } from 'react';
|
||||
|
||||
import { Button, Extrinsic, InputAddress, InputNumber, Modal, TxButton } from '@polkadot/react-components';
|
||||
import { useApi, useModal } from '@polkadot/react-hooks';
|
||||
import { useApi, useCollectiveInstance, useModal } from '@polkadot/react-hooks';
|
||||
|
||||
import { useTranslation } from '../translate';
|
||||
import { useModuleCollective } from '../useModuleCollective';
|
||||
|
||||
interface Props {
|
||||
isMember: boolean;
|
||||
@ -33,7 +32,7 @@ function Propose ({ isMember, members, type }: Props): React.ReactElement<Props>
|
||||
new BN(members.length / 2 + 1),
|
||||
true
|
||||
]);
|
||||
const modLocation = useModuleCollective(type);
|
||||
const modLocation = useCollectiveInstance(type);
|
||||
|
||||
const _hasThreshold = useCallback(
|
||||
(threshold?: BN | null): boolean =>
|
||||
|
||||
@ -7,10 +7,9 @@ import type { AccountId, Hash } from '@polkadot/types/interfaces';
|
||||
import React, { useState } from 'react';
|
||||
|
||||
import { Button, MarkWarning, Modal, TxButton, VoteAccount } from '@polkadot/react-components';
|
||||
import { useAccounts, useApi, useToggle } from '@polkadot/react-hooks';
|
||||
import { useAccounts, useApi, useCollectiveInstance, useToggle } from '@polkadot/react-hooks';
|
||||
|
||||
import { useTranslation } from '../translate';
|
||||
import { useModuleCollective } from '../useModuleCollective';
|
||||
|
||||
interface Props {
|
||||
hash: Hash | string;
|
||||
@ -26,7 +25,7 @@ function Voting ({ hash, members, prime, proposalId, type }: Props): React.React
|
||||
const { hasAccounts } = useAccounts();
|
||||
const [accountId, setAccountId] = useState<string | null>(null);
|
||||
const [isVotingOpen, toggleVoting] = useToggle();
|
||||
const modLocation = useModuleCollective(type);
|
||||
const modLocation = useCollectiveInstance(type);
|
||||
|
||||
if (!modLocation || !hasAccounts) {
|
||||
return null;
|
||||
|
||||
@ -1,22 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/app-tech-comm authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { useMemo } from 'react';
|
||||
|
||||
import { useApi } from '@polkadot/react-hooks';
|
||||
import { isFunction } from '@polkadot/util';
|
||||
|
||||
export function useModuleCollective (type: 'membership' | 'technicalCommittee'): string | null {
|
||||
const { api } = useApi();
|
||||
|
||||
return useMemo(
|
||||
(): string | null => {
|
||||
const [instance] = api.registry.getModuleInstances(api.runtimeVersion.specName.toString(), type) || [type];
|
||||
|
||||
return isFunction(api.tx[instance as 'technicalCommittee']?.close)
|
||||
? instance
|
||||
: null;
|
||||
},
|
||||
[api, type]
|
||||
);
|
||||
}
|
||||
@ -8,7 +8,7 @@ import React, { useEffect, useRef, useState } from 'react';
|
||||
|
||||
import { getTreasuryProposalThreshold } from '@polkadot/apps-config';
|
||||
import { Button, Dropdown, InputAddress, Modal, TxButton } from '@polkadot/react-components';
|
||||
import { useApi, useToggle } from '@polkadot/react-hooks';
|
||||
import { useApi, useCollectiveInstance, useToggle } from '@polkadot/react-hooks';
|
||||
|
||||
import { useTranslation } from '../translate';
|
||||
|
||||
@ -28,8 +28,9 @@ function Council ({ id, isDisabled, members }: Props): React.ReactElement<Props>
|
||||
const { api } = useApi();
|
||||
const [isOpen, toggleOpen] = useToggle();
|
||||
const [accountId, setAccountId] = useState<string | null>(null);
|
||||
const [councilType, setCouncilType] = useState('accept');
|
||||
const [{ proposal, proposalLength }, setProposal] = useState<ProposalState>({ proposalLength: 0 });
|
||||
const [proposalType, setProposalType] = useState('accept');
|
||||
const [{ proposal, proposalLength }, setProposal] = useState<ProposalState>(() => ({ proposalLength: 0 }));
|
||||
const modCouncil = useCollectiveInstance('council');
|
||||
|
||||
const threshold = Math.ceil((members?.length || 0) * getTreasuryProposalThreshold(api));
|
||||
|
||||
@ -39,12 +40,16 @@ function Council ({ id, isDisabled, members }: Props): React.ReactElement<Props>
|
||||
]);
|
||||
|
||||
useEffect((): void => {
|
||||
const proposal = councilType === 'reject'
|
||||
const proposal = proposalType === 'reject'
|
||||
? api.tx.treasury.rejectProposal(id)
|
||||
: api.tx.treasury.approveProposal(id);
|
||||
|
||||
setProposal({ proposal, proposalLength: proposal.length });
|
||||
}, [api, councilType, id]);
|
||||
}, [api, id, proposalType]);
|
||||
|
||||
if (!modCouncil) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
@ -68,9 +73,9 @@ function Council ({ id, isDisabled, members }: Props): React.ReactElement<Props>
|
||||
<Dropdown
|
||||
help={t<string>('The type of council proposal to submit.')}
|
||||
label={t<string>('council proposal type')}
|
||||
onChange={setCouncilType}
|
||||
onChange={setProposalType}
|
||||
options={councilTypeOptRef.current}
|
||||
value={councilType}
|
||||
value={proposalType}
|
||||
/>
|
||||
</Modal.Columns>
|
||||
</Modal.Content>
|
||||
@ -82,11 +87,11 @@ function Council ({ id, isDisabled, members }: Props): React.ReactElement<Props>
|
||||
label={t<string>('Send to council')}
|
||||
onStart={toggleOpen}
|
||||
params={
|
||||
api.tx.council.propose.meta.args.length === 3
|
||||
api.tx[modCouncil].propose.meta.args.length === 3
|
||||
? [threshold, proposal, proposalLength]
|
||||
: [threshold, proposal]
|
||||
}
|
||||
tx={api.tx.council.propose}
|
||||
tx={api.tx[modCouncil].propose}
|
||||
/>
|
||||
</Modal.Actions>
|
||||
</Modal>
|
||||
|
||||
@ -5,7 +5,7 @@ import React, { useMemo } from 'react';
|
||||
import { Route, Switch } from 'react-router';
|
||||
|
||||
import { HelpOverlay, Tabs } from '@polkadot/react-components';
|
||||
import { useApi, useMembers } from '@polkadot/react-hooks';
|
||||
import { useApi, useCollectiveMembers } from '@polkadot/react-hooks';
|
||||
import { isFunction } from '@polkadot/util';
|
||||
|
||||
import basicMd from './md/basic.md';
|
||||
@ -30,7 +30,7 @@ interface TabItem {
|
||||
function TreasuryApp ({ basePath }: Props): React.ReactElement<Props> {
|
||||
const { t } = useTranslation();
|
||||
const { api } = useApi();
|
||||
const { isMember, members } = useMembers('council');
|
||||
const { isMember, members } = useCollectiveMembers('council');
|
||||
const tipHashes = useTipHashes();
|
||||
|
||||
const items = useMemo(() => [
|
||||
|
||||
@ -15,6 +15,8 @@ export { useBlocksPerDays } from './useBlocksPerDays';
|
||||
export { useCacheKey } from './useCacheKey';
|
||||
export { useCall } from './useCall';
|
||||
export { useCallMulti } from './useCallMulti';
|
||||
export { useCollectiveInstance } from './useCollectiveInstance';
|
||||
export { useCollectiveMembers } from './useCollectiveMembers';
|
||||
export { useFormField } from './useFormField';
|
||||
export { useDebounce } from './useDebounce';
|
||||
export { useEventTrigger } from './useEventTrigger';
|
||||
@ -28,7 +30,6 @@ export { useLedger } from './useLedger';
|
||||
export { useLoadingDelay } from './useLoadingDelay';
|
||||
export { useMapEntries } from './useMapEntries';
|
||||
export { useMapKeys } from './useMapKeys';
|
||||
export { useMembers } from './useMembers';
|
||||
export { useModal } from './useModal';
|
||||
export { useNonEmptyString } from './useNonEmptyString';
|
||||
export { useNonZeroBn } from './useNonZeroBn';
|
||||
|
||||
@ -25,6 +25,8 @@ export type TxDefs = SubmittableExtrinsic<'promise'> | IExtrinsic | Call | TxDef
|
||||
|
||||
export type TxSource<T extends TxDefs> = [T, boolean];
|
||||
|
||||
export type CollectiveType = 'council' | 'membership' | 'technicalCommittee';
|
||||
|
||||
export interface ModalState {
|
||||
isOpen: boolean;
|
||||
onOpen: () => void;
|
||||
|
||||
28
packages/react-hooks/src/useCollectiveInstance.ts
Normal file
28
packages/react-hooks/src/useCollectiveInstance.ts
Normal file
@ -0,0 +1,28 @@
|
||||
// Copyright 2017-2021 @polkadot/app-tech-comm authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { CollectiveType } from './types';
|
||||
|
||||
import { useMemo } from 'react';
|
||||
|
||||
import { useApi } from '@polkadot/react-hooks';
|
||||
import { isFunction } from '@polkadot/util';
|
||||
|
||||
export function useCollectiveInstance (instanceType: CollectiveType, instanceIndex?: number): CollectiveType | null {
|
||||
const { api } = useApi();
|
||||
|
||||
return useMemo(
|
||||
(): CollectiveType | null => {
|
||||
const index = instanceIndex || 0;
|
||||
const instances = api.registry.getModuleInstances(api.runtimeVersion.specName.toString(), instanceType);
|
||||
const instance = instances && (index < instances.length)
|
||||
? instances[index] as 'council'
|
||||
: instanceType;
|
||||
|
||||
return api.tx[instance] && isFunction(api.tx[instance].close)
|
||||
? instance
|
||||
: null;
|
||||
},
|
||||
[api, instanceIndex, instanceType]
|
||||
);
|
||||
}
|
||||
@ -2,6 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { AccountId } from '@polkadot/types/interfaces';
|
||||
import type { CollectiveType } from './types';
|
||||
|
||||
import { useMemo } from 'react';
|
||||
|
||||
@ -19,7 +20,7 @@ const transformMembers = {
|
||||
accounts.map((accountId) => accountId.toString())
|
||||
};
|
||||
|
||||
export function useMembers (collective: 'council' | 'membership' | 'technicalCommittee'): Result {
|
||||
export function useCollectiveMembers (collective: CollectiveType): Result {
|
||||
const { api } = useApi();
|
||||
const { allAccounts, hasAccounts } = useAccounts();
|
||||
const retrieved = useCall<string[]>(hasAccounts && api.derive[collective]?.members, undefined, transformMembers);
|
||||
Loading…
Reference in New Issue
Block a user