From bdbd78a2ca493087a10b3ae556263b473609a647 Mon Sep 17 00:00:00 2001 From: Ewa Kowalska Date: Mon, 1 Feb 2021 14:52:17 +0100 Subject: [PATCH 01/10] Use correct thresholds --- .../src/api/params/proposalThresholds.ts | 11 +++++ .../BountyActions/BountyInitiateVoting.tsx | 23 +++------ .../src/BountyActions/CloseBounty.tsx | 15 ++---- .../BountyActions/ProposeCuratorAction.tsx | 12 ++--- .../src/BountyActions/SlashCurator.tsx | 14 ++---- .../src/Motions/ProposeExternal.tsx | 7 ++- .../page-council/src/Motions/Slashing.tsx | 6 +-- packages/page-staking/src/Slashes/index.tsx | 10 ++-- .../page-treasury/src/Overview/Council.tsx | 16 +++++-- packages/react-hooks/src/index.ts | 1 + packages/react-hooks/src/useThresholds.ts | 48 +++++++++++++++++++ 11 files changed, 96 insertions(+), 67 deletions(-) create mode 100644 packages/react-hooks/src/useThresholds.ts diff --git a/packages/apps-config/src/api/params/proposalThresholds.ts b/packages/apps-config/src/api/params/proposalThresholds.ts index a6fdb72e6cd..7182f73c78b 100644 --- a/packages/apps-config/src/api/params/proposalThresholds.ts +++ b/packages/apps-config/src/api/params/proposalThresholds.ts @@ -12,6 +12,13 @@ const PROPOSE_THRESHOLDS: Record = { default: 0.5 }; +// more than +const REJECT_THRESHOLDS: Record = { + [KUSAMA_GENESIS]: 0.5, + [POLKADOT_GENESIS]: 0.5, + default: 0.5 +}; + const SLASH_THRESHOLDS: Record = { [KUSAMA_GENESIS]: 0.5, [POLKADOT_GENESIS]: 0.75, @@ -36,3 +43,7 @@ export function getSlashProposalThreshold (api: ApiPromise): number { export function getTreasuryProposalThreshold (api: ApiPromise): number { return TREASURY_THRESHOLDS[api.genesisHash.toHex()] || TREASURY_THRESHOLDS.default; } + +export function getTreasuryRejectionThreshold (api: ApiPromise): number { + return REJECT_THRESHOLDS[api.genesisHash.toHex()] || REJECT_THRESHOLDS.default; +} diff --git a/packages/page-bounties/src/BountyActions/BountyInitiateVoting.tsx b/packages/page-bounties/src/BountyActions/BountyInitiateVoting.tsx index 52c27b4b23c..5c680e1f5b8 100644 --- a/packages/page-bounties/src/BountyActions/BountyInitiateVoting.tsx +++ b/packages/page-bounties/src/BountyActions/BountyInitiateVoting.tsx @@ -4,12 +4,11 @@ import type { DeriveCollectiveProposal } from '@polkadot/api-derive/types'; import type { BountyIndex } from '@polkadot/types/interfaces'; -import BN from 'bn.js'; -import React, { useEffect, useMemo, useRef, useState } from 'react'; +import React, { 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 { useThresholds } from '@polkadot/react-hooks/useThresholds'; import { useBounties } from '../hooks'; import { useTranslation } from '../translate'; @@ -28,13 +27,7 @@ function BountyInitiateVoting ({ index, proposals }: Props): React.ReactElement< const { approveBounty, closeBounty } = useBounties(); const [isOpen, toggleOpen] = useToggle(); const [accountId, setAccountId] = useState(null); - const [threshold, setThreshold] = useState(); - - useEffect((): void => { - members && setThreshold( - new BN(Math.ceil(members.length * getTreasuryProposalThreshold(api))) - ); - }, [api, members]); + const { treasuryProposalThreshold: threshold } = useThresholds(); const approveBountyProposal = useRef(approveBounty(index)); const closeBountyProposal = useRef(closeBounty(index)); @@ -56,13 +49,9 @@ function BountyInitiateVoting ({ index, proposals }: Props): React.ReactElement< size='large' > - - -

{t('This action will create a Council motion to either approve or reject the Bounty.')}

-
- - -
+ +

{t('This action will create a Council motion to either approve or reject the Bounty.')}

+
| const { api } = useApi(); const { members } = useMembers(); const { closeBounty } = useBounties(); - const [accountId, setAccountId] = useState(null); - const [threshold, setThreshold] = useState(); + const { treasuryRejectionThreshold: threshold } = useThresholds(); - useEffect((): void => { - members && setThreshold( - new BN(Math.ceil(members.length * getTreasuryProposalThreshold(api))) - ); - }, [api, members]); + const [accountId, setAccountId] = useState(null); const closeBountyProposal = useRef(closeBounty(index)); diff --git a/packages/page-bounties/src/BountyActions/ProposeCuratorAction.tsx b/packages/page-bounties/src/BountyActions/ProposeCuratorAction.tsx index 2ee1f3d09cc..9d1e78cfebb 100644 --- a/packages/page-bounties/src/BountyActions/ProposeCuratorAction.tsx +++ b/packages/page-bounties/src/BountyActions/ProposeCuratorAction.tsx @@ -8,9 +8,8 @@ import BN from 'bn.js'; import React, { useEffect, useMemo, useState } from 'react'; import { truncateTitle } from '@polkadot/app-bounties/helpers'; -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, useMembers, useThresholds, useToggle } from '@polkadot/react-hooks'; import { BN_ZERO } from '@polkadot/util'; import { useBounties } from '../hooks'; @@ -30,19 +29,14 @@ function ProposeCuratorAction ({ description, index, proposals, value }: Props): const { api } = useApi(); const { isMember, members } = useMembers(); const { proposeCurator } = useBounties(); + const { treasuryProposalThreshold: threshold } = useThresholds(); + const [isOpen, toggleOpen] = useToggle(); const [accountId, setAccountId] = useState(null); const [curatorId, setCuratorId] = useState(null); - const [threshold, setThreshold] = useState(); const [fee, setFee] = useState(BN_ZERO); const [isFeeValid, setIsFeeValid] = useState(false); - useEffect((): void => { - members && setThreshold( - new BN(Math.ceil(members.length * getTreasuryProposalThreshold(api))) - ); - }, [api, members]); - const proposeCuratorProposal = useMemo(() => curatorId && proposeCurator(index, curatorId, fee), [curatorId, fee, index, proposeCurator]); const isVotingInitiated = useMemo(() => proposals?.filter(({ proposal }) => BOUNTY_METHODS.includes(proposal.method)).length !== 0, [proposals]); diff --git a/packages/page-bounties/src/BountyActions/SlashCurator.tsx b/packages/page-bounties/src/BountyActions/SlashCurator.tsx index 4b2de36a7ff..edc01556d2e 100644 --- a/packages/page-bounties/src/BountyActions/SlashCurator.tsx +++ b/packages/page-bounties/src/BountyActions/SlashCurator.tsx @@ -3,13 +3,11 @@ import type { AccountId, BountyIndex } from '@polkadot/types/interfaces'; -import BN from 'bn.js'; -import React, { useEffect, useMemo, useState } from 'react'; +import React, { 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, useMembers, useThresholds } from '@polkadot/react-hooks'; import { truncateTitle } from '../helpers'; import { useBounties } from '../hooks'; @@ -41,15 +39,9 @@ function SlashCurator ({ action, curatorId, description, index, toggleOpen }: Pr const { members } = useMembers(); const { unassignCurator } = useBounties(); const [accountId, setAccountId] = useState(null); - const [threshold, setThreshold] = useState(); + const { treasuryRejectionThreshold: threshold } = useThresholds(); const { allAccounts } = useAccounts(); - useEffect((): void => { - members && setThreshold( - new BN(Math.ceil(members.length * getTreasuryProposalThreshold(api))) - ); - }, [api, members]); - const unassignCuratorProposal = useMemo(() => unassignCurator(index), [index, unassignCurator]); const actionProperties = useMemo>(() => ({ diff --git a/packages/page-council/src/Motions/ProposeExternal.tsx b/packages/page-council/src/Motions/ProposeExternal.tsx index c280d6fbe79..2b3ed1d6526 100644 --- a/packages/page-council/src/Motions/ProposeExternal.tsx +++ b/packages/page-council/src/Motions/ProposeExternal.tsx @@ -5,9 +5,8 @@ import type { SubmittableExtrinsic } from '@polkadot/api/types'; 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, useThresholds, useToggle } from '@polkadot/react-hooks'; import { isHex } from '@polkadot/util'; import { useTranslation } from '../translate'; @@ -31,13 +30,13 @@ interface ProposalState { function ProposeExternal ({ className = '', isMember, members }: Props): React.ReactElement { const { t } = useTranslation(); const { api } = useApi(); + const { proposalThreshold: threshold } = useThresholds(); + 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 threshold = Math.ceil((members.length || 0) * getProposalThreshold(api)); - const _onChangeHash = useCallback( (hash?: string): void => setHash({ hash, isHashValid: isHex(hash, 256) }), [] diff --git a/packages/page-council/src/Motions/Slashing.tsx b/packages/page-council/src/Motions/Slashing.tsx index 6eb2211de3c..e3f494c152a 100644 --- a/packages/page-council/src/Motions/Slashing.tsx +++ b/packages/page-council/src/Motions/Slashing.tsx @@ -5,9 +5,8 @@ import type { SubmittableExtrinsic } from '@polkadot/api/types'; 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, useThresholds, useToggle } from '@polkadot/react-hooks'; import { useTranslation } from '../translate'; @@ -30,14 +29,13 @@ interface ProposalState { function Slashing ({ className = '', isMember, members }: Props): React.ReactElement { const { t } = useTranslation(); const { api } = useApi(); + const { slashProposalThreshold: threshold } = useThresholds(); const slashes = useAvailableSlashes(); const [isVisible, toggleVisible] = useToggle(); const [accountId, setAcountId] = useState(null); const [{ proposal, proposalLength }, setProposal] = useState({ proposal: null, proposalLength: 0 }); const [selectedEra, setSelectedEra] = useState(0); - const threshold = Math.ceil((members.length || 0) * getSlashProposalThreshold(api)); - const eras = useMemo( () => (slashes || []).map(([era, slashes]): Option => ({ text: t('era {{era}}, {{count}} slashes', { diff --git a/packages/page-staking/src/Slashes/index.tsx b/packages/page-staking/src/Slashes/index.tsx index 399c127430b..32fa6e7249c 100644 --- a/packages/page-staking/src/Slashes/index.tsx +++ b/packages/page-staking/src/Slashes/index.tsx @@ -8,9 +8,8 @@ import type { Slash, SlashEra } from './types'; import BN from 'bn.js'; 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, useMembers, useThresholds } from '@polkadot/react-hooks'; import { formatNumber } from '@polkadot/util'; import { useTranslation } from '../translate'; @@ -88,9 +87,10 @@ function calcSlashEras (slashes: [BN, UnappliedSlash[]][], ownStashes: StakerSta function Slashes ({ ownStashes = [], slashes }: Props): React.ReactElement | null { const { t } = useTranslation(); - const { api } = useApi(); const { allAccounts } = useAccounts(); const { members } = useMembers(); + const { slashProposalThreshold: councilThreshold } = useThresholds(); + const [selectedIndex, setSelectedIndex] = useState(0); const rows = useMemo( @@ -120,8 +120,6 @@ function Slashes ({ ownStashes = [], slashes }: Props): React.ReactElement } councilId={councilId} - councilThreshold={councilThreshold} + councilThreshold={councilThreshold.toNumber()} key={rows[selectedIndex].era.toString()} slash={rows[selectedIndex]} /> diff --git a/packages/page-treasury/src/Overview/Council.tsx b/packages/page-treasury/src/Overview/Council.tsx index b8898cb37bb..4fdf84b8778 100644 --- a/packages/page-treasury/src/Overview/Council.tsx +++ b/packages/page-treasury/src/Overview/Council.tsx @@ -4,11 +4,11 @@ import type { SubmittableExtrinsic } from '@polkadot/api/types'; import type { ProposalIndex } from '@polkadot/types/interfaces'; +import BN from 'bn.js'; 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, useThresholds, useToggle } from '@polkadot/react-hooks'; import { useTranslation } from '../translate'; @@ -26,12 +26,13 @@ interface ProposalState { function Council ({ id, isDisabled, members }: Props): React.ReactElement | null { const { t } = useTranslation(); const { api } = useApi(); + const { treasuryProposalThreshold, treasuryRejectionThreshold } = useThresholds(); + const [isOpen, toggleOpen] = useToggle(); const [accountId, setAccountId] = useState(null); const [councilType, setCouncilType] = useState('accept'); const [{ proposal, proposalLength }, setProposal] = useState({ proposalLength: 0 }); - - const threshold = Math.ceil((members?.length || 0) * getTreasuryProposalThreshold(api)); + const [threshold, setThreshold] = useState(); const councilTypeOptRef = useRef([ { text: t('Acceptance proposal to council'), value: 'accept' }, @@ -43,8 +44,13 @@ function Council ({ id, isDisabled, members }: Props): React.ReactElement ? api.tx.treasury.rejectProposal(id) : api.tx.treasury.approveProposal(id); + const threshold = councilType === 'reject' + ? treasuryRejectionThreshold + : treasuryProposalThreshold; + setProposal({ proposal, proposalLength: proposal.length }); - }, [api, councilType, id]); + setThreshold(threshold); + }, [api, councilType, id, treasuryProposalThreshold, treasuryRejectionThreshold]); return ( <> diff --git a/packages/react-hooks/src/index.ts b/packages/react-hooks/src/index.ts index 62c41697450..e3fed510496 100644 --- a/packages/react-hooks/src/index.ts +++ b/packages/react-hooks/src/index.ts @@ -37,5 +37,6 @@ export { useSavedFlags } from './useSavedFlags'; export { useSudo } from './useSudo'; export { useToggle } from './useToggle'; export { useTreasury } from './useTreasury'; +export { useThresholds } from './useThresholds'; export { useVotingStatus } from './useVotingStatus'; export { useWeight } from './useWeight'; diff --git a/packages/react-hooks/src/useThresholds.ts b/packages/react-hooks/src/useThresholds.ts new file mode 100644 index 00000000000..41038a7b6c4 --- /dev/null +++ b/packages/react-hooks/src/useThresholds.ts @@ -0,0 +1,48 @@ +// Copyright 2017-2021 @polkadot/react-hooks authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import BN from 'bn.js'; +import { useEffect, useState } from 'react'; + +import { getProposalThreshold, + getSlashProposalThreshold, + getTreasuryProposalThreshold, + getTreasuryRejectionThreshold } from '@polkadot/apps-config'; +import { useApi } from '@polkadot/react-hooks/useApi'; +import { useMembers } from '@polkadot/react-hooks/useMembers'; +import { BN_ZERO } from '@polkadot/util'; + +export type ThresholdApi = { + proposalThreshold: BN; + treasuryProposalThreshold: BN; + treasuryRejectionThreshold: BN; + slashProposalThreshold: BN; +} + +export function useThresholds () : ThresholdApi { + const { api } = useApi(); + const { members } = useMembers(); + + const [proposalThreshold, setProposalThreshold] = useState(BN_ZERO); + const [slashProposalThreshold, setSlashProposalThreshold] = useState(BN_ZERO); + const [treasuryRejectionThreshold, setTreasuryRejectionThreshold] = useState(BN_ZERO); + const [treasuryProposalThreshold, setTreasuryProposalThreshold] = useState(BN_ZERO); + + useEffect((): void => { + const pThreshold = getProposalThreshold(api); + const slashThreshold = getSlashProposalThreshold(api); + const tRejectionThreshold = getTreasuryRejectionThreshold(api); + const tProposalThreshold = getTreasuryProposalThreshold(api); + + members && setProposalThreshold(new BN(Math.ceil(members.length * pThreshold))); + members && setSlashProposalThreshold(new BN(Math.ceil(members.length * slashThreshold))); + members && setTreasuryRejectionThreshold( + Math.ceil(members.length * tRejectionThreshold) === members.length * tRejectionThreshold + ? new BN((members.length * tRejectionThreshold) + 1) + : new BN(Math.ceil((members.length * tRejectionThreshold))) + ); + members && setTreasuryProposalThreshold(new BN(Math.ceil((members.length * tProposalThreshold)))); + }, [api, members]); + + return { proposalThreshold, slashProposalThreshold, treasuryProposalThreshold, treasuryRejectionThreshold }; +} From b0ec8083ea9d1e373c60b2fe0cf46b3c6aa57a0f Mon Sep 17 00:00:00 2001 From: Ewa Kowalska Date: Tue, 2 Feb 2021 12:39:02 +0100 Subject: [PATCH 02/10] Extract methods from useThresholds hook --- .../helpers/requiredMembersThresholds.spec.ts | 44 +++++++++++++++++++ .../helpers/requiredMembersThresholds.ts | 20 +++++++++ packages/react-hooks/src/useThresholds.ts | 29 ++++++------ 3 files changed, 80 insertions(+), 13 deletions(-) create mode 100644 packages/react-hooks/helpers/requiredMembersThresholds.spec.ts create mode 100644 packages/react-hooks/helpers/requiredMembersThresholds.ts diff --git a/packages/react-hooks/helpers/requiredMembersThresholds.spec.ts b/packages/react-hooks/helpers/requiredMembersThresholds.spec.ts new file mode 100644 index 00000000000..117f24e72b5 --- /dev/null +++ b/packages/react-hooks/helpers/requiredMembersThresholds.spec.ts @@ -0,0 +1,44 @@ +// Copyright 2017-2021 @polkadot/react-hooks authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import { BN_ZERO } from '@polkadot/util'; + +import { getAtLeastThresholdMembersCount, getMoreThanThresholdMembersCount } from './requiredMembersThresholds'; + +describe('minimal members count', () => { + describe('treasury proposal threshold', () => { + it('0 members', () => { + expect(getAtLeastThresholdMembersCount(0, 0.6)).toEqual(BN_ZERO); + }); + + it('6 members - special case', () => { + expect(getAtLeastThresholdMembersCount(6, 0.6).toNumber()).toEqual(4); + }); + + it('even number of members', () => { + expect(getAtLeastThresholdMembersCount(12, 0.6).toNumber()).toEqual(8); + }); + + it('uneven number of members', () => { + expect(getAtLeastThresholdMembersCount(19, 0.6).toNumber()).toEqual(12); + }); + }); + + describe('reject origin threshold', () => { + it('0 members', () => { + expect(getMoreThanThresholdMembersCount(0, 0.5)).toEqual(BN_ZERO); + }); + + it('6 members - special case', () => { + expect(getMoreThanThresholdMembersCount(6, 0.5).toNumber()).toEqual(4); + }); + + it('even number of members', () => { + expect(getMoreThanThresholdMembersCount(12, 0.5).toNumber()).toEqual(7); + }); + + it('uneven number of members', () => { + expect(getMoreThanThresholdMembersCount(19, 0.5).toNumber()).toEqual(10); + }); + }); +}); diff --git a/packages/react-hooks/helpers/requiredMembersThresholds.ts b/packages/react-hooks/helpers/requiredMembersThresholds.ts new file mode 100644 index 00000000000..114f677620b --- /dev/null +++ b/packages/react-hooks/helpers/requiredMembersThresholds.ts @@ -0,0 +1,20 @@ +// Copyright 2017-2021 @polkadot/react-hooks authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import BN from 'bn.js'; + +import { BN_ZERO } from '@polkadot/util'; + +export function getMoreThanThresholdMembersCount (membersCount: number, thresholdRate: number): BN { + if (membersCount === 0) { return BN_ZERO; } + + const atLeastThreshold = getAtLeastThresholdMembersCount(membersCount, thresholdRate).toNumber(); + + return atLeastThreshold === membersCount * thresholdRate + ? new BN(atLeastThreshold + 1) + : new BN(atLeastThreshold); +} + +export function getAtLeastThresholdMembersCount (membersCount: number, thresholdRate: number): BN { + return new BN(Math.ceil((membersCount * thresholdRate))); +} diff --git a/packages/react-hooks/src/useThresholds.ts b/packages/react-hooks/src/useThresholds.ts index 41038a7b6c4..bd036a008d8 100644 --- a/packages/react-hooks/src/useThresholds.ts +++ b/packages/react-hooks/src/useThresholds.ts @@ -12,6 +12,9 @@ import { useApi } from '@polkadot/react-hooks/useApi'; import { useMembers } from '@polkadot/react-hooks/useMembers'; import { BN_ZERO } from '@polkadot/util'; +import { getAtLeastThresholdMembersCount, + getMoreThanThresholdMembersCount } from '../helpers/requiredMembersThresholds'; + export type ThresholdApi = { proposalThreshold: BN; treasuryProposalThreshold: BN; @@ -29,19 +32,19 @@ export function useThresholds () : ThresholdApi { const [treasuryProposalThreshold, setTreasuryProposalThreshold] = useState(BN_ZERO); useEffect((): void => { - const pThreshold = getProposalThreshold(api); - const slashThreshold = getSlashProposalThreshold(api); - const tRejectionThreshold = getTreasuryRejectionThreshold(api); - const tProposalThreshold = getTreasuryProposalThreshold(api); - - members && setProposalThreshold(new BN(Math.ceil(members.length * pThreshold))); - members && setSlashProposalThreshold(new BN(Math.ceil(members.length * slashThreshold))); - members && setTreasuryRejectionThreshold( - Math.ceil(members.length * tRejectionThreshold) === members.length * tRejectionThreshold - ? new BN((members.length * tRejectionThreshold) + 1) - : new BN(Math.ceil((members.length * tRejectionThreshold))) - ); - members && setTreasuryProposalThreshold(new BN(Math.ceil((members.length * tProposalThreshold)))); + const proposalThreshold = getProposalThreshold(api); + const slashProposalThreshold = getSlashProposalThreshold(api); + const treasuryRejectionThreshold = getTreasuryRejectionThreshold(api); + const treasuryProposalThreshold = getTreasuryProposalThreshold(api); + + const membersCount = members?.length; + + if (membersCount && membersCount !== 0) { + setProposalThreshold(getAtLeastThresholdMembersCount(membersCount, proposalThreshold)); + setSlashProposalThreshold(getAtLeastThresholdMembersCount(membersCount, slashProposalThreshold)); + setTreasuryRejectionThreshold(getMoreThanThresholdMembersCount(membersCount, treasuryRejectionThreshold)); + setTreasuryProposalThreshold(getAtLeastThresholdMembersCount(membersCount, treasuryProposalThreshold)); + } }, [api, members]); return { proposalThreshold, slashProposalThreshold, treasuryProposalThreshold, treasuryRejectionThreshold }; From c4595644af9411603413fa641d24139dbb190592 Mon Sep 17 00:00:00 2001 From: Ewa Kowalska Date: Tue, 2 Feb 2021 17:28:15 +0100 Subject: [PATCH 03/10] Apply review suggestions --- packages/apps-config/src/api/params/proposalThresholds.ts | 1 - .../react-hooks/helpers/requiredMembersThresholds.spec.ts | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/apps-config/src/api/params/proposalThresholds.ts b/packages/apps-config/src/api/params/proposalThresholds.ts index 7182f73c78b..b37c793e521 100644 --- a/packages/apps-config/src/api/params/proposalThresholds.ts +++ b/packages/apps-config/src/api/params/proposalThresholds.ts @@ -12,7 +12,6 @@ const PROPOSE_THRESHOLDS: Record = { default: 0.5 }; -// more than const REJECT_THRESHOLDS: Record = { [KUSAMA_GENESIS]: 0.5, [POLKADOT_GENESIS]: 0.5, diff --git a/packages/react-hooks/helpers/requiredMembersThresholds.spec.ts b/packages/react-hooks/helpers/requiredMembersThresholds.spec.ts index 117f24e72b5..177ee69d3df 100644 --- a/packages/react-hooks/helpers/requiredMembersThresholds.spec.ts +++ b/packages/react-hooks/helpers/requiredMembersThresholds.spec.ts @@ -19,7 +19,7 @@ describe('minimal members count', () => { expect(getAtLeastThresholdMembersCount(12, 0.6).toNumber()).toEqual(8); }); - it('uneven number of members', () => { + it('odd number of members', () => { expect(getAtLeastThresholdMembersCount(19, 0.6).toNumber()).toEqual(12); }); }); @@ -37,7 +37,7 @@ describe('minimal members count', () => { expect(getMoreThanThresholdMembersCount(12, 0.5).toNumber()).toEqual(7); }); - it('uneven number of members', () => { + it('odd number of members', () => { expect(getMoreThanThresholdMembersCount(19, 0.5).toNumber()).toEqual(10); }); }); From 5e8d7a2f7801c3ba0a3ca8affb56dd796294de66 Mon Sep 17 00:00:00 2001 From: Ewa Kowalska Date: Wed, 3 Feb 2021 11:43:43 +0100 Subject: [PATCH 04/10] Apply review suggestions --- .../BountyActions/BountyInitiateVoting.tsx | 6 +-- .../src/BountyActions/CloseBounty.tsx | 4 +- .../BountyActions/ProposeCuratorAction.tsx | 4 +- .../src/BountyActions/SlashCurator.tsx | 8 ++-- .../src/Motions/ProposeExternal.tsx | 8 ++-- .../page-council/src/Motions/Slashing.tsx | 8 ++-- packages/page-staking/src/Slashes/index.tsx | 4 +- .../page-treasury/src/Overview/Council.tsx | 3 +- .../helpers/requiredMembersThresholds.spec.ts | 43 +++++-------------- .../helpers/requiredMembersThresholds.ts | 18 +++----- packages/react-hooks/src/useThresholds.ts | 18 ++++---- 11 files changed, 45 insertions(+), 79 deletions(-) diff --git a/packages/page-bounties/src/BountyActions/BountyInitiateVoting.tsx b/packages/page-bounties/src/BountyActions/BountyInitiateVoting.tsx index 5c680e1f5b8..3f09b5c811d 100644 --- a/packages/page-bounties/src/BountyActions/BountyInitiateVoting.tsx +++ b/packages/page-bounties/src/BountyActions/BountyInitiateVoting.tsx @@ -27,7 +27,7 @@ function BountyInitiateVoting ({ index, proposals }: Props): React.ReactElement< const { approveBounty, closeBounty } = useBounties(); const [isOpen, toggleOpen] = useToggle(); const [accountId, setAccountId] = useState(null); - const { treasuryProposalThreshold: threshold } = useThresholds(); + const { treasuryProposalThreshold } = useThresholds(); const approveBountyProposal = useRef(approveBounty(index)); const closeBountyProposal = useRef(closeBounty(index)); @@ -75,7 +75,7 @@ function BountyInitiateVoting ({ index, proposals }: Props): React.ReactElement< isDisabled={false} label={t('Approve')} onStart={toggleOpen} - params={[threshold, approveBountyProposal.current, approveBountyProposal.current.length]} + params={[treasuryProposalThreshold, approveBountyProposal.current, approveBountyProposal.current.length]} tx={api.tx.council.propose} /> ('Reject')} onStart={toggleOpen} - params={[threshold, closeBountyProposal.current, closeBountyProposal.current.length]} + params={[treasuryProposalThreshold, closeBountyProposal.current, closeBountyProposal.current.length]} tx={api.tx.council.propose} /> diff --git a/packages/page-bounties/src/BountyActions/CloseBounty.tsx b/packages/page-bounties/src/BountyActions/CloseBounty.tsx index 87235d9b549..f9ecf500ec2 100644 --- a/packages/page-bounties/src/BountyActions/CloseBounty.tsx +++ b/packages/page-bounties/src/BountyActions/CloseBounty.tsx @@ -21,7 +21,7 @@ function CloseBounty ({ index, toggleOpen }: Props): React.ReactElement | const { api } = useApi(); const { members } = useMembers(); const { closeBounty } = useBounties(); - const { treasuryRejectionThreshold: threshold } = useThresholds(); + const { treasuryRejectionThreshold } = useThresholds(); const [accountId, setAccountId] = useState(null); @@ -59,7 +59,7 @@ function CloseBounty ({ index, toggleOpen }: Props): React.ReactElement | isDisabled={false} label={t('Close Bounty')} onStart={toggleOpen} - params={[threshold, closeBountyProposal.current, closeBountyProposal.current.length]} + params={[treasuryRejectionThreshold, closeBountyProposal.current, closeBountyProposal.current.length]} tx={api.tx.council.propose} /> diff --git a/packages/page-bounties/src/BountyActions/ProposeCuratorAction.tsx b/packages/page-bounties/src/BountyActions/ProposeCuratorAction.tsx index 9d1e78cfebb..822081ea980 100644 --- a/packages/page-bounties/src/BountyActions/ProposeCuratorAction.tsx +++ b/packages/page-bounties/src/BountyActions/ProposeCuratorAction.tsx @@ -29,7 +29,7 @@ function ProposeCuratorAction ({ description, index, proposals, value }: Props): const { api } = useApi(); const { isMember, members } = useMembers(); const { proposeCurator } = useBounties(); - const { treasuryProposalThreshold: threshold } = useThresholds(); + const { treasuryProposalThreshold } = useThresholds(); const [isOpen, toggleOpen] = useToggle(); const [accountId, setAccountId] = useState(null); @@ -117,7 +117,7 @@ function ProposeCuratorAction ({ description, index, proposals, value }: Props): isDisabled={!isFeeValid} label={t('Assign curator')} onStart={toggleOpen} - params={[threshold, proposeCuratorProposal, proposeCuratorProposal?.length]} + params={[treasuryProposalThreshold, proposeCuratorProposal, proposeCuratorProposal?.length]} tx={api.tx.council.propose} /> diff --git a/packages/page-bounties/src/BountyActions/SlashCurator.tsx b/packages/page-bounties/src/BountyActions/SlashCurator.tsx index edc01556d2e..d005591fef0 100644 --- a/packages/page-bounties/src/BountyActions/SlashCurator.tsx +++ b/packages/page-bounties/src/BountyActions/SlashCurator.tsx @@ -39,7 +39,7 @@ function SlashCurator ({ action, curatorId, description, index, toggleOpen }: Pr const { members } = useMembers(); const { unassignCurator } = useBounties(); const [accountId, setAccountId] = useState(null); - const { treasuryRejectionThreshold: threshold } = useThresholds(); + const { treasuryRejectionThreshold } = useThresholds(); const { allAccounts } = useAccounts(); const unassignCuratorProposal = useMemo(() => unassignCurator(index), [index, unassignCurator]); @@ -69,7 +69,7 @@ function SlashCurator ({ action, curatorId, description, index, toggleOpen }: Pr filter: members, header: t('This action will create a Council motion to slash the Curator.'), helpMessage: t('The Curator that will be slashed.'), - params: [threshold, unassignCuratorProposal, unassignCuratorProposal?.length], + params: [treasuryRejectionThreshold, unassignCuratorProposal, unassignCuratorProposal?.length], proposingAccountTip: t('The council member that will create the motion, submission equates to an "aye" vote.'), tip: t("If the motion is outvoted, Curator's deposit will be slashed and Curator will be unassigned. Bounty will return to the Funded state."), title: t('Slash curator'), @@ -79,13 +79,13 @@ function SlashCurator ({ action, curatorId, description, index, toggleOpen }: Pr filter: members, header: t('This action will create a Council motion to unassign the Curator.'), helpMessage: t('The Curator that will be unassigned'), - params: [threshold, unassignCuratorProposal, unassignCuratorProposal?.length], + params: [treasuryRejectionThreshold, unassignCuratorProposal, unassignCuratorProposal?.length], proposingAccountTip: t('The council member that will create the motion, submission equates to an "aye" vote.'), tip: t('If the motion is outvoted, the current Curator will be unassigned and the Bounty will return to the Funded state.'), title: t('Unassign curator'), tx: api.tx.council.propose } - }), [t, curatorId, index, unassignCurator, api.tx.council.propose, allAccounts, members, threshold, unassignCuratorProposal]); + }), [t, curatorId, index, unassignCurator, api.tx.council.propose, allAccounts, members, treasuryRejectionThreshold, unassignCuratorProposal]); const { filter, header, helpMessage, params, proposingAccountTip, tip, title, tx } = actionProperties[action]; diff --git a/packages/page-council/src/Motions/ProposeExternal.tsx b/packages/page-council/src/Motions/ProposeExternal.tsx index 2b3ed1d6526..77203afc193 100644 --- a/packages/page-council/src/Motions/ProposeExternal.tsx +++ b/packages/page-council/src/Motions/ProposeExternal.tsx @@ -30,7 +30,7 @@ interface ProposalState { function ProposeExternal ({ className = '', isMember, members }: Props): React.ReactElement { const { t } = useTranslation(); const { api } = useApi(); - const { proposalThreshold: threshold } = useThresholds(); + const { proposalThreshold } = useThresholds(); const [isVisible, toggleVisible] = useToggle(); const [accountId, setAcountId] = useState(null); @@ -107,13 +107,13 @@ function ProposeExternal ({ className = '', isMember, members }: Props): React.R ('Propose')} onStart={toggleVisible} params={ api.tx.council.propose.meta.args.length === 3 - ? [threshold, proposal, proposalLength] - : [threshold, proposal] + ? [proposalThreshold, proposal, proposalLength] + : [proposalThreshold, proposal] } tx={api.tx.council.propose} /> diff --git a/packages/page-council/src/Motions/Slashing.tsx b/packages/page-council/src/Motions/Slashing.tsx index e3f494c152a..9a3f1da8bba 100644 --- a/packages/page-council/src/Motions/Slashing.tsx +++ b/packages/page-council/src/Motions/Slashing.tsx @@ -29,7 +29,7 @@ interface ProposalState { function Slashing ({ className = '', isMember, members }: Props): React.ReactElement { const { t } = useTranslation(); const { api } = useApi(); - const { slashProposalThreshold: threshold } = useThresholds(); + const { slashProposalThreshold } = useThresholds(); const slashes = useAvailableSlashes(); const [isVisible, toggleVisible] = useToggle(); const [accountId, setAcountId] = useState(null); @@ -121,13 +121,13 @@ function Slashing ({ className = '', isMember, members }: Props): React.ReactEle ('Revert')} onStart={toggleVisible} params={ api.tx.council.propose.meta.args.length === 3 - ? [threshold, proposal, proposalLength] - : [threshold, proposal] + ? [slashProposalThreshold, proposal, proposalLength] + : [slashProposalThreshold, proposal] } tx={api.tx.council.propose} /> diff --git a/packages/page-staking/src/Slashes/index.tsx b/packages/page-staking/src/Slashes/index.tsx index 32fa6e7249c..2c816ded491 100644 --- a/packages/page-staking/src/Slashes/index.tsx +++ b/packages/page-staking/src/Slashes/index.tsx @@ -89,7 +89,7 @@ function Slashes ({ ownStashes = [], slashes }: Props): React.ReactElement } councilId={councilId} - councilThreshold={councilThreshold.toNumber()} + councilThreshold={slashProposalThreshold} key={rows[selectedIndex].era.toString()} slash={rows[selectedIndex]} /> diff --git a/packages/page-treasury/src/Overview/Council.tsx b/packages/page-treasury/src/Overview/Council.tsx index 4fdf84b8778..ea1985b15ef 100644 --- a/packages/page-treasury/src/Overview/Council.tsx +++ b/packages/page-treasury/src/Overview/Council.tsx @@ -4,7 +4,6 @@ import type { SubmittableExtrinsic } from '@polkadot/api/types'; import type { ProposalIndex } from '@polkadot/types/interfaces'; -import BN from 'bn.js'; import React, { useEffect, useRef, useState } from 'react'; import { Button, Dropdown, InputAddress, Modal, TxButton } from '@polkadot/react-components'; @@ -32,7 +31,7 @@ function Council ({ id, isDisabled, members }: Props): React.ReactElement const [accountId, setAccountId] = useState(null); const [councilType, setCouncilType] = useState('accept'); const [{ proposal, proposalLength }, setProposal] = useState({ proposalLength: 0 }); - const [threshold, setThreshold] = useState(); + const [threshold, setThreshold] = useState(0); const councilTypeOptRef = useRef([ { text: t('Acceptance proposal to council'), value: 'accept' }, diff --git a/packages/react-hooks/helpers/requiredMembersThresholds.spec.ts b/packages/react-hooks/helpers/requiredMembersThresholds.spec.ts index 177ee69d3df..fdb5c765b10 100644 --- a/packages/react-hooks/helpers/requiredMembersThresholds.spec.ts +++ b/packages/react-hooks/helpers/requiredMembersThresholds.spec.ts @@ -1,44 +1,21 @@ // Copyright 2017-2021 @polkadot/react-hooks authors & contributors // SPDX-License-Identifier: Apache-2.0 -import { BN_ZERO } from '@polkadot/util'; - import { getAtLeastThresholdMembersCount, getMoreThanThresholdMembersCount } from './requiredMembersThresholds'; describe('minimal members count', () => { - describe('treasury proposal threshold', () => { - it('0 members', () => { - expect(getAtLeastThresholdMembersCount(0, 0.6)).toEqual(BN_ZERO); - }); - - it('6 members - special case', () => { - expect(getAtLeastThresholdMembersCount(6, 0.6).toNumber()).toEqual(4); - }); - - it('even number of members', () => { - expect(getAtLeastThresholdMembersCount(12, 0.6).toNumber()).toEqual(8); - }); - - it('odd number of members', () => { - expect(getAtLeastThresholdMembersCount(19, 0.6).toNumber()).toEqual(12); - }); + it('0 members', () => { + expect(getAtLeastThresholdMembersCount(0, 0.5)).toEqual(0); + expect(getMoreThanThresholdMembersCount(0, 0.5)).toEqual(0); }); - describe('reject origin threshold', () => { - it('0 members', () => { - expect(getMoreThanThresholdMembersCount(0, 0.5)).toEqual(BN_ZERO); - }); - - it('6 members - special case', () => { - expect(getMoreThanThresholdMembersCount(6, 0.5).toNumber()).toEqual(4); - }); - - it('even number of members', () => { - expect(getMoreThanThresholdMembersCount(12, 0.5).toNumber()).toEqual(7); - }); + it('even number of members', () => { + expect(getAtLeastThresholdMembersCount(12, 0.5)).toEqual(6); + expect(getMoreThanThresholdMembersCount(12, 0.5)).toEqual(7); + }); - it('odd number of members', () => { - expect(getMoreThanThresholdMembersCount(19, 0.5).toNumber()).toEqual(10); - }); + it('odd number of members', () => { + expect(getAtLeastThresholdMembersCount(19, 0.5)).toEqual(10); + expect(getMoreThanThresholdMembersCount(19, 0.5)).toEqual(10); }); }); diff --git a/packages/react-hooks/helpers/requiredMembersThresholds.ts b/packages/react-hooks/helpers/requiredMembersThresholds.ts index 114f677620b..ff39435b182 100644 --- a/packages/react-hooks/helpers/requiredMembersThresholds.ts +++ b/packages/react-hooks/helpers/requiredMembersThresholds.ts @@ -1,20 +1,12 @@ // Copyright 2017-2021 @polkadot/react-hooks authors & contributors // SPDX-License-Identifier: Apache-2.0 -import BN from 'bn.js'; +export function getMoreThanThresholdMembersCount (membersCount: number, thresholdRatio: number): number { + if (membersCount === 0) { return 0; } -import { BN_ZERO } from '@polkadot/util'; - -export function getMoreThanThresholdMembersCount (membersCount: number, thresholdRate: number): BN { - if (membersCount === 0) { return BN_ZERO; } - - const atLeastThreshold = getAtLeastThresholdMembersCount(membersCount, thresholdRate).toNumber(); - - return atLeastThreshold === membersCount * thresholdRate - ? new BN(atLeastThreshold + 1) - : new BN(atLeastThreshold); + return Math.floor(membersCount * thresholdRatio) + 1; } -export function getAtLeastThresholdMembersCount (membersCount: number, thresholdRate: number): BN { - return new BN(Math.ceil((membersCount * thresholdRate))); +export function getAtLeastThresholdMembersCount (membersCount: number, thresholdRatio: number): number { + return Math.ceil(membersCount * thresholdRatio); } diff --git a/packages/react-hooks/src/useThresholds.ts b/packages/react-hooks/src/useThresholds.ts index bd036a008d8..c8cbc1470d0 100644 --- a/packages/react-hooks/src/useThresholds.ts +++ b/packages/react-hooks/src/useThresholds.ts @@ -1,7 +1,6 @@ // Copyright 2017-2021 @polkadot/react-hooks authors & contributors // SPDX-License-Identifier: Apache-2.0 -import BN from 'bn.js'; import { useEffect, useState } from 'react'; import { getProposalThreshold, @@ -10,26 +9,25 @@ import { getProposalThreshold, getTreasuryRejectionThreshold } from '@polkadot/apps-config'; import { useApi } from '@polkadot/react-hooks/useApi'; import { useMembers } from '@polkadot/react-hooks/useMembers'; -import { BN_ZERO } from '@polkadot/util'; import { getAtLeastThresholdMembersCount, getMoreThanThresholdMembersCount } from '../helpers/requiredMembersThresholds'; export type ThresholdApi = { - proposalThreshold: BN; - treasuryProposalThreshold: BN; - treasuryRejectionThreshold: BN; - slashProposalThreshold: BN; + proposalThreshold: number; + treasuryProposalThreshold: number; + treasuryRejectionThreshold: number; + slashProposalThreshold: number; } export function useThresholds () : ThresholdApi { const { api } = useApi(); const { members } = useMembers(); - const [proposalThreshold, setProposalThreshold] = useState(BN_ZERO); - const [slashProposalThreshold, setSlashProposalThreshold] = useState(BN_ZERO); - const [treasuryRejectionThreshold, setTreasuryRejectionThreshold] = useState(BN_ZERO); - const [treasuryProposalThreshold, setTreasuryProposalThreshold] = useState(BN_ZERO); + const [proposalThreshold, setProposalThreshold] = useState(0); + const [slashProposalThreshold, setSlashProposalThreshold] = useState(0); + const [treasuryRejectionThreshold, setTreasuryRejectionThreshold] = useState(0); + const [treasuryProposalThreshold, setTreasuryProposalThreshold] = useState(0); useEffect((): void => { const proposalThreshold = getProposalThreshold(api); From e5721503163cf14f0f3f7a54446fcd758cfdb566 Mon Sep 17 00:00:00 2001 From: Ewa Kowalska Date: Wed, 3 Feb 2021 13:03:13 +0100 Subject: [PATCH 05/10] Apply review suggestions --- .../src/api/params/proposalThresholds.ts | 1 + .../helpers/requiredMembersThresholds.ts | 12 ------------ .../useThreshold.spec.ts} | 2 +- packages/react-hooks/src/useThresholds.ts | 16 ++++++++++++---- 4 files changed, 14 insertions(+), 17 deletions(-) delete mode 100644 packages/react-hooks/helpers/requiredMembersThresholds.ts rename packages/react-hooks/{helpers/requiredMembersThresholds.spec.ts => src/useThreshold.spec.ts} (93%) diff --git a/packages/apps-config/src/api/params/proposalThresholds.ts b/packages/apps-config/src/api/params/proposalThresholds.ts index b37c793e521..9f57eebd7a2 100644 --- a/packages/apps-config/src/api/params/proposalThresholds.ts +++ b/packages/apps-config/src/api/params/proposalThresholds.ts @@ -13,6 +13,7 @@ const PROPOSE_THRESHOLDS: Record = { }; const REJECT_THRESHOLDS: Record = { + [KULUPU_GENESIS]: 0.5, [KUSAMA_GENESIS]: 0.5, [POLKADOT_GENESIS]: 0.5, default: 0.5 diff --git a/packages/react-hooks/helpers/requiredMembersThresholds.ts b/packages/react-hooks/helpers/requiredMembersThresholds.ts deleted file mode 100644 index ff39435b182..00000000000 --- a/packages/react-hooks/helpers/requiredMembersThresholds.ts +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2017-2021 @polkadot/react-hooks authors & contributors -// SPDX-License-Identifier: Apache-2.0 - -export function getMoreThanThresholdMembersCount (membersCount: number, thresholdRatio: number): number { - if (membersCount === 0) { return 0; } - - return Math.floor(membersCount * thresholdRatio) + 1; -} - -export function getAtLeastThresholdMembersCount (membersCount: number, thresholdRatio: number): number { - return Math.ceil(membersCount * thresholdRatio); -} diff --git a/packages/react-hooks/helpers/requiredMembersThresholds.spec.ts b/packages/react-hooks/src/useThreshold.spec.ts similarity index 93% rename from packages/react-hooks/helpers/requiredMembersThresholds.spec.ts rename to packages/react-hooks/src/useThreshold.spec.ts index fdb5c765b10..ea5ff9125e0 100644 --- a/packages/react-hooks/helpers/requiredMembersThresholds.spec.ts +++ b/packages/react-hooks/src/useThreshold.spec.ts @@ -1,7 +1,7 @@ // Copyright 2017-2021 @polkadot/react-hooks authors & contributors // SPDX-License-Identifier: Apache-2.0 -import { getAtLeastThresholdMembersCount, getMoreThanThresholdMembersCount } from './requiredMembersThresholds'; +import { getAtLeastThresholdMembersCount, getMoreThanThresholdMembersCount } from './useThresholds'; describe('minimal members count', () => { it('0 members', () => { diff --git a/packages/react-hooks/src/useThresholds.ts b/packages/react-hooks/src/useThresholds.ts index c8cbc1470d0..b387f385111 100644 --- a/packages/react-hooks/src/useThresholds.ts +++ b/packages/react-hooks/src/useThresholds.ts @@ -7,11 +7,9 @@ import { getProposalThreshold, getSlashProposalThreshold, getTreasuryProposalThreshold, getTreasuryRejectionThreshold } from '@polkadot/apps-config'; -import { useApi } from '@polkadot/react-hooks/useApi'; -import { useMembers } from '@polkadot/react-hooks/useMembers'; -import { getAtLeastThresholdMembersCount, - getMoreThanThresholdMembersCount } from '../helpers/requiredMembersThresholds'; +import { useApi } from './useApi'; +import { useMembers } from './useMembers'; export type ThresholdApi = { proposalThreshold: number; @@ -20,6 +18,16 @@ export type ThresholdApi = { slashProposalThreshold: number; } +export function getMoreThanThresholdMembersCount (membersCount: number, thresholdRatio: number): number { + if (membersCount === 0) { return 0; } + + return Math.floor(membersCount * thresholdRatio) + 1; +} + +export function getAtLeastThresholdMembersCount (membersCount: number, thresholdRatio: number): number { + return Math.ceil(membersCount * thresholdRatio); +} + export function useThresholds () : ThresholdApi { const { api } = useApi(); const { members } = useMembers(); From 2465c86065ebd56875fe78cf451dcf24e01c4c51 Mon Sep 17 00:00:00 2001 From: Ewa Kowalska Date: Wed, 3 Feb 2021 17:33:47 +0100 Subject: [PATCH 06/10] Include proposalThresholds in useThresholds hook --- packages/apps-config/src/api/params/index.ts | 1 - .../src/api/params/proposalThresholds.ts | 49 ------------------- .../src/Motions/ProposeMotion.tsx | 8 +-- packages/react-hooks/src/useThresholds.ts | 49 +++++++++++++++++-- 4 files changed, 49 insertions(+), 58 deletions(-) delete mode 100644 packages/apps-config/src/api/params/proposalThresholds.ts diff --git a/packages/apps-config/src/api/params/index.ts b/packages/apps-config/src/api/params/index.ts index 1dd8010d59a..cac3632a41a 100644 --- a/packages/apps-config/src/api/params/index.ts +++ b/packages/apps-config/src/api/params/index.ts @@ -2,4 +2,3 @@ // SPDX-License-Identifier: Apache-2.0 export * from './inflation'; -export * from './proposalThresholds'; diff --git a/packages/apps-config/src/api/params/proposalThresholds.ts b/packages/apps-config/src/api/params/proposalThresholds.ts deleted file mode 100644 index 9f57eebd7a2..00000000000 --- a/packages/apps-config/src/api/params/proposalThresholds.ts +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright 2017-2021 @polkadot/app-config authors & contributors -// SPDX-License-Identifier: Apache-2.0 - -import type { ApiPromise } from '@polkadot/api'; - -import { KULUPU_GENESIS, KUSAMA_GENESIS, POLKADOT_GENESIS } from '../constants'; - -const PROPOSE_THRESHOLDS: Record = { - [KULUPU_GENESIS]: 1, - [KUSAMA_GENESIS]: 0.5, - [POLKADOT_GENESIS]: 0.6, - default: 0.5 -}; - -const REJECT_THRESHOLDS: Record = { - [KULUPU_GENESIS]: 0.5, - [KUSAMA_GENESIS]: 0.5, - [POLKADOT_GENESIS]: 0.5, - default: 0.5 -}; - -const SLASH_THRESHOLDS: Record = { - [KUSAMA_GENESIS]: 0.5, - [POLKADOT_GENESIS]: 0.75, - default: 0.5 -}; - -const TREASURY_THRESHOLDS: Record = { - [KULUPU_GENESIS]: 0.5, - [KUSAMA_GENESIS]: 0.6, - [POLKADOT_GENESIS]: 0.6, - default: 0.6 -}; - -export function getProposalThreshold (api: ApiPromise): number { - return PROPOSE_THRESHOLDS[api.genesisHash.toHex()] || PROPOSE_THRESHOLDS.default; -} - -export function getSlashProposalThreshold (api: ApiPromise): number { - return SLASH_THRESHOLDS[api.genesisHash.toHex()] || SLASH_THRESHOLDS.default; -} - -export function getTreasuryProposalThreshold (api: ApiPromise): number { - return TREASURY_THRESHOLDS[api.genesisHash.toHex()] || TREASURY_THRESHOLDS.default; -} - -export function getTreasuryRejectionThreshold (api: ApiPromise): number { - return REJECT_THRESHOLDS[api.genesisHash.toHex()] || REJECT_THRESHOLDS.default; -} diff --git a/packages/page-council/src/Motions/ProposeMotion.tsx b/packages/page-council/src/Motions/ProposeMotion.tsx index bc2b3f33d0b..9a0e76b66d1 100644 --- a/packages/page-council/src/Motions/ProposeMotion.tsx +++ b/packages/page-council/src/Motions/ProposeMotion.tsx @@ -6,9 +6,8 @@ import type { SubmittableExtrinsic } from '@polkadot/api/types'; import BN from 'bn.js'; 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, useThresholds, useToggle } from '@polkadot/react-hooks'; import { BN_ZERO } from '@polkadot/util'; import { useTranslation } from '../translate'; @@ -31,6 +30,7 @@ interface ProposalState { function Propose ({ isMember, members }: Props): React.ReactElement { const { t } = useTranslation(); const { api, apiDefaultTxSudo } = useApi(); + const { proposalThreshold } = useThresholds(); const [isOpen, toggleOpen] = useToggle(); const [accountId, setAcountId] = useState(null); const [{ proposal, proposalLength }, setProposal] = useState({ proposalLength: 0 }); @@ -39,9 +39,9 @@ function Propose ({ isMember, members }: Props): React.ReactElement { useEffect((): void => { members && setThreshold({ isThresholdValid: members.length !== 0, - threshold: new BN(Math.ceil(members.length * getProposalThreshold(api))) + threshold: new BN(proposalThreshold) }); - }, [api, members]); + }, [api, members, proposalThreshold]); const _setMethod = useCallback( (proposal?: SubmittableExtrinsic<'promise'> | null) => setProposal({ diff --git a/packages/react-hooks/src/useThresholds.ts b/packages/react-hooks/src/useThresholds.ts index b387f385111..1ec22846520 100644 --- a/packages/react-hooks/src/useThresholds.ts +++ b/packages/react-hooks/src/useThresholds.ts @@ -3,10 +3,8 @@ import { useEffect, useState } from 'react'; -import { getProposalThreshold, - getSlashProposalThreshold, - getTreasuryProposalThreshold, - getTreasuryRejectionThreshold } from '@polkadot/apps-config'; +import { ApiPromise } from '@polkadot/api'; +import { KULUPU_GENESIS, KUSAMA_GENESIS, POLKADOT_GENESIS } from '@polkadot/apps-config'; import { useApi } from './useApi'; import { useMembers } from './useMembers'; @@ -18,6 +16,49 @@ export type ThresholdApi = { slashProposalThreshold: number; } +const PROPOSE_THRESHOLDS: Record = { + [KULUPU_GENESIS]: 1, + [KUSAMA_GENESIS]: 0.5, + [POLKADOT_GENESIS]: 0.6, + default: 0.5 +}; + +const REJECT_THRESHOLDS: Record = { + [KULUPU_GENESIS]: 0.5, + [KUSAMA_GENESIS]: 0.5, + [POLKADOT_GENESIS]: 0.5, + default: 0.5 +}; + +const SLASH_THRESHOLDS: Record = { + [KUSAMA_GENESIS]: 0.5, + [POLKADOT_GENESIS]: 0.75, + default: 0.5 +}; + +const TREASURY_THRESHOLDS: Record = { + [KULUPU_GENESIS]: 0.5, + [KUSAMA_GENESIS]: 0.6, + [POLKADOT_GENESIS]: 0.6, + default: 0.6 +}; + +function getProposalThreshold (api: ApiPromise): number { + return PROPOSE_THRESHOLDS[api.genesisHash.toHex()] || PROPOSE_THRESHOLDS.default; +} + +function getSlashProposalThreshold (api: ApiPromise): number { + return SLASH_THRESHOLDS[api.genesisHash.toHex()] || SLASH_THRESHOLDS.default; +} + +function getTreasuryProposalThreshold (api: ApiPromise): number { + return TREASURY_THRESHOLDS[api.genesisHash.toHex()] || TREASURY_THRESHOLDS.default; +} + +function getTreasuryRejectionThreshold (api: ApiPromise): number { + return REJECT_THRESHOLDS[api.genesisHash.toHex()] || REJECT_THRESHOLDS.default; +} + export function getMoreThanThresholdMembersCount (membersCount: number, thresholdRatio: number): number { if (membersCount === 0) { return 0; } From 9ed5eb92b65235b0931b337ccca65ddd93ff4ed4 Mon Sep 17 00:00:00 2001 From: Ewa Kowalska Date: Wed, 3 Feb 2021 17:51:44 +0100 Subject: [PATCH 07/10] Restore config --- packages/apps-config/src/api/params/index.ts | 1 + .../src/api/params/proposalThresholds.ts | 31 +++++++++++ .../src/Motions/ProposeMotion.tsx | 1 - packages/react-hooks/src/useThresholds.ts | 54 ++----------------- 4 files changed, 37 insertions(+), 50 deletions(-) create mode 100644 packages/apps-config/src/api/params/proposalThresholds.ts diff --git a/packages/apps-config/src/api/params/index.ts b/packages/apps-config/src/api/params/index.ts index cac3632a41a..1dd8010d59a 100644 --- a/packages/apps-config/src/api/params/index.ts +++ b/packages/apps-config/src/api/params/index.ts @@ -2,3 +2,4 @@ // SPDX-License-Identifier: Apache-2.0 export * from './inflation'; +export * from './proposalThresholds'; diff --git a/packages/apps-config/src/api/params/proposalThresholds.ts b/packages/apps-config/src/api/params/proposalThresholds.ts new file mode 100644 index 00000000000..bf4e9ba661c --- /dev/null +++ b/packages/apps-config/src/api/params/proposalThresholds.ts @@ -0,0 +1,31 @@ +// Copyright 2017-2021 @polkadot/app-config authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import { KULUPU_GENESIS, KUSAMA_GENESIS, POLKADOT_GENESIS } from '../constants'; + +export const PROPOSE_THRESHOLDS: Record = { + [KULUPU_GENESIS]: 1, + [KUSAMA_GENESIS]: 0.5, + [POLKADOT_GENESIS]: 0.6, + default: 0.5 +}; + +export const REJECT_THRESHOLDS: Record = { + [KULUPU_GENESIS]: 0.5, + [KUSAMA_GENESIS]: 0.5, + [POLKADOT_GENESIS]: 0.5, + default: 0.5 +}; + +export const SLASH_THRESHOLDS: Record = { + [KUSAMA_GENESIS]: 0.5, + [POLKADOT_GENESIS]: 0.75, + default: 0.5 +}; + +export const TREASURY_THRESHOLDS: Record = { + [KULUPU_GENESIS]: 0.5, + [KUSAMA_GENESIS]: 0.6, + [POLKADOT_GENESIS]: 0.6, + default: 0.6 +}; diff --git a/packages/page-council/src/Motions/ProposeMotion.tsx b/packages/page-council/src/Motions/ProposeMotion.tsx index 9a0e76b66d1..ce2aad68fc4 100644 --- a/packages/page-council/src/Motions/ProposeMotion.tsx +++ b/packages/page-council/src/Motions/ProposeMotion.tsx @@ -42,7 +42,6 @@ function Propose ({ isMember, members }: Props): React.ReactElement { threshold: new BN(proposalThreshold) }); }, [api, members, proposalThreshold]); - const _setMethod = useCallback( (proposal?: SubmittableExtrinsic<'promise'> | null) => setProposal({ proposal, diff --git a/packages/react-hooks/src/useThresholds.ts b/packages/react-hooks/src/useThresholds.ts index 1ec22846520..d6a3d4ab926 100644 --- a/packages/react-hooks/src/useThresholds.ts +++ b/packages/react-hooks/src/useThresholds.ts @@ -3,8 +3,7 @@ import { useEffect, useState } from 'react'; -import { ApiPromise } from '@polkadot/api'; -import { KULUPU_GENESIS, KUSAMA_GENESIS, POLKADOT_GENESIS } from '@polkadot/apps-config'; +import { PROPOSE_THRESHOLDS, REJECT_THRESHOLDS, SLASH_THRESHOLDS, TREASURY_THRESHOLDS } from '@polkadot/apps-config'; import { useApi } from './useApi'; import { useMembers } from './useMembers'; @@ -16,49 +15,6 @@ export type ThresholdApi = { slashProposalThreshold: number; } -const PROPOSE_THRESHOLDS: Record = { - [KULUPU_GENESIS]: 1, - [KUSAMA_GENESIS]: 0.5, - [POLKADOT_GENESIS]: 0.6, - default: 0.5 -}; - -const REJECT_THRESHOLDS: Record = { - [KULUPU_GENESIS]: 0.5, - [KUSAMA_GENESIS]: 0.5, - [POLKADOT_GENESIS]: 0.5, - default: 0.5 -}; - -const SLASH_THRESHOLDS: Record = { - [KUSAMA_GENESIS]: 0.5, - [POLKADOT_GENESIS]: 0.75, - default: 0.5 -}; - -const TREASURY_THRESHOLDS: Record = { - [KULUPU_GENESIS]: 0.5, - [KUSAMA_GENESIS]: 0.6, - [POLKADOT_GENESIS]: 0.6, - default: 0.6 -}; - -function getProposalThreshold (api: ApiPromise): number { - return PROPOSE_THRESHOLDS[api.genesisHash.toHex()] || PROPOSE_THRESHOLDS.default; -} - -function getSlashProposalThreshold (api: ApiPromise): number { - return SLASH_THRESHOLDS[api.genesisHash.toHex()] || SLASH_THRESHOLDS.default; -} - -function getTreasuryProposalThreshold (api: ApiPromise): number { - return TREASURY_THRESHOLDS[api.genesisHash.toHex()] || TREASURY_THRESHOLDS.default; -} - -function getTreasuryRejectionThreshold (api: ApiPromise): number { - return REJECT_THRESHOLDS[api.genesisHash.toHex()] || REJECT_THRESHOLDS.default; -} - export function getMoreThanThresholdMembersCount (membersCount: number, thresholdRatio: number): number { if (membersCount === 0) { return 0; } @@ -79,10 +35,10 @@ export function useThresholds () : ThresholdApi { const [treasuryProposalThreshold, setTreasuryProposalThreshold] = useState(0); useEffect((): void => { - const proposalThreshold = getProposalThreshold(api); - const slashProposalThreshold = getSlashProposalThreshold(api); - const treasuryRejectionThreshold = getTreasuryRejectionThreshold(api); - const treasuryProposalThreshold = getTreasuryProposalThreshold(api); + const proposalThreshold = PROPOSE_THRESHOLDS[api.genesisHash.toHex()] || PROPOSE_THRESHOLDS.default; + const slashProposalThreshold = SLASH_THRESHOLDS[api.genesisHash.toHex()] || SLASH_THRESHOLDS.default; + const treasuryRejectionThreshold = REJECT_THRESHOLDS[api.genesisHash.toHex()] || REJECT_THRESHOLDS.default; + const treasuryProposalThreshold = TREASURY_THRESHOLDS[api.genesisHash.toHex()] || TREASURY_THRESHOLDS.default; const membersCount = members?.length; From fcdfc513f573f2221ce74f2d3e07c1c9c8c536a2 Mon Sep 17 00:00:00 2001 From: Ewa Kowalska Date: Thu, 4 Feb 2021 10:58:56 +0100 Subject: [PATCH 08/10] Simplify useThreshold hook --- .../src/api/params/proposalThresholds.ts | 2 +- packages/react-hooks/src/useThresholds.ts | 48 +++++++++++-------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/packages/apps-config/src/api/params/proposalThresholds.ts b/packages/apps-config/src/api/params/proposalThresholds.ts index bf4e9ba661c..348d0d0702d 100644 --- a/packages/apps-config/src/api/params/proposalThresholds.ts +++ b/packages/apps-config/src/api/params/proposalThresholds.ts @@ -4,7 +4,7 @@ import { KULUPU_GENESIS, KUSAMA_GENESIS, POLKADOT_GENESIS } from '../constants'; export const PROPOSE_THRESHOLDS: Record = { - [KULUPU_GENESIS]: 1, + [KULUPU_GENESIS]: 0.8, [KUSAMA_GENESIS]: 0.5, [POLKADOT_GENESIS]: 0.6, default: 0.5 diff --git a/packages/react-hooks/src/useThresholds.ts b/packages/react-hooks/src/useThresholds.ts index d6a3d4ab926..8466fb02470 100644 --- a/packages/react-hooks/src/useThresholds.ts +++ b/packages/react-hooks/src/useThresholds.ts @@ -1,14 +1,14 @@ // Copyright 2017-2021 @polkadot/react-hooks authors & contributors // SPDX-License-Identifier: Apache-2.0 -import { useEffect, useState } from 'react'; +import { useMemo } from 'react'; import { PROPOSE_THRESHOLDS, REJECT_THRESHOLDS, SLASH_THRESHOLDS, TREASURY_THRESHOLDS } from '@polkadot/apps-config'; import { useApi } from './useApi'; import { useMembers } from './useMembers'; -export type ThresholdApi = { +export interface Thresholds { proposalThreshold: number; treasuryProposalThreshold: number; treasuryRejectionThreshold: number; @@ -25,30 +25,36 @@ export function getAtLeastThresholdMembersCount (membersCount: number, threshold return Math.ceil(membersCount * thresholdRatio); } -export function useThresholds () : ThresholdApi { +export function useThresholds () : Thresholds { const { api } = useApi(); const { members } = useMembers(); - const [proposalThreshold, setProposalThreshold] = useState(0); - const [slashProposalThreshold, setSlashProposalThreshold] = useState(0); - const [treasuryRejectionThreshold, setTreasuryRejectionThreshold] = useState(0); - const [treasuryProposalThreshold, setTreasuryProposalThreshold] = useState(0); - - useEffect((): void => { - const proposalThreshold = PROPOSE_THRESHOLDS[api.genesisHash.toHex()] || PROPOSE_THRESHOLDS.default; - const slashProposalThreshold = SLASH_THRESHOLDS[api.genesisHash.toHex()] || SLASH_THRESHOLDS.default; - const treasuryRejectionThreshold = REJECT_THRESHOLDS[api.genesisHash.toHex()] || REJECT_THRESHOLDS.default; - const treasuryProposalThreshold = TREASURY_THRESHOLDS[api.genesisHash.toHex()] || TREASURY_THRESHOLDS.default; - + return useMemo((): Thresholds => { const membersCount = members?.length; - if (membersCount && membersCount !== 0) { - setProposalThreshold(getAtLeastThresholdMembersCount(membersCount, proposalThreshold)); - setSlashProposalThreshold(getAtLeastThresholdMembersCount(membersCount, slashProposalThreshold)); - setTreasuryRejectionThreshold(getMoreThanThresholdMembersCount(membersCount, treasuryRejectionThreshold)); - setTreasuryProposalThreshold(getAtLeastThresholdMembersCount(membersCount, treasuryProposalThreshold)); + if (!membersCount) { + return { proposalThreshold: 0, slashProposalThreshold: 0, treasuryProposalThreshold: 0, treasuryRejectionThreshold: 0 }; } - }, [api, members]); - return { proposalThreshold, slashProposalThreshold, treasuryProposalThreshold, treasuryRejectionThreshold }; + const genesisHash = api.genesisHash.toHex(); + + return { + proposalThreshold: getAtLeastThresholdMembersCount( + membersCount, + PROPOSE_THRESHOLDS[genesisHash] || PROPOSE_THRESHOLDS.default + ), + slashProposalThreshold: getAtLeastThresholdMembersCount( + membersCount, + SLASH_THRESHOLDS[genesisHash] || SLASH_THRESHOLDS.default + ), + treasuryProposalThreshold: getAtLeastThresholdMembersCount( + membersCount, + TREASURY_THRESHOLDS[genesisHash] || TREASURY_THRESHOLDS.default + ), + treasuryRejectionThreshold: getMoreThanThresholdMembersCount( + membersCount, + REJECT_THRESHOLDS[genesisHash] || REJECT_THRESHOLDS.default + ) + }; + }, [api, members]); } From 4c5bfcbf2d21bc44241f4cc99da3ebca7784c0c4 Mon Sep 17 00:00:00 2001 From: Ewa Kowalska Date: Thu, 4 Feb 2021 22:08:37 +0100 Subject: [PATCH 09/10] Add option to thresholds config --- .../src/api/params/proposalThresholds.ts | 43 +++++++++++-------- packages/react-hooks/src/useThresholds.ts | 28 ++++++++---- 2 files changed, 43 insertions(+), 28 deletions(-) diff --git a/packages/apps-config/src/api/params/proposalThresholds.ts b/packages/apps-config/src/api/params/proposalThresholds.ts index 348d0d0702d..befb17f6d1d 100644 --- a/packages/apps-config/src/api/params/proposalThresholds.ts +++ b/packages/apps-config/src/api/params/proposalThresholds.ts @@ -3,29 +3,34 @@ import { KULUPU_GENESIS, KUSAMA_GENESIS, POLKADOT_GENESIS } from '../constants'; -export const PROPOSE_THRESHOLDS: Record = { - [KULUPU_GENESIS]: 0.8, - [KUSAMA_GENESIS]: 0.5, - [POLKADOT_GENESIS]: 0.6, - default: 0.5 +export interface Threshold { + value: number; + option: 'AtLeast' | 'MoreThan'; +} + +export const PROPOSE_THRESHOLDS: Record = { + [KULUPU_GENESIS]: { option: 'MoreThan', value: 0.8 }, + [KUSAMA_GENESIS]: { option: 'AtLeast', value: 0.5 }, + [POLKADOT_GENESIS]: { option: 'AtLeast', value: 0.6 }, + default: { option: 'AtLeast', value: 0.5 } }; -export const REJECT_THRESHOLDS: Record = { - [KULUPU_GENESIS]: 0.5, - [KUSAMA_GENESIS]: 0.5, - [POLKADOT_GENESIS]: 0.5, - default: 0.5 +export const REJECT_THRESHOLDS: Record = { + [KULUPU_GENESIS]: { option: 'MoreThan', value: 0.5 }, + [KUSAMA_GENESIS]: { option: 'MoreThan', value: 0.5 }, + [POLKADOT_GENESIS]: { option: 'MoreThan', value: 0.5 }, + default: { option: 'MoreThan', value: 0.5 } }; -export const SLASH_THRESHOLDS: Record = { - [KUSAMA_GENESIS]: 0.5, - [POLKADOT_GENESIS]: 0.75, - default: 0.5 +export const SLASH_THRESHOLDS: Record = { + [KUSAMA_GENESIS]: { option: 'AtLeast', value: 0.5 }, + [POLKADOT_GENESIS]: { option: 'AtLeast', value: 0.75 }, + default: { option: 'AtLeast', value: 0.5 } }; -export const TREASURY_THRESHOLDS: Record = { - [KULUPU_GENESIS]: 0.5, - [KUSAMA_GENESIS]: 0.6, - [POLKADOT_GENESIS]: 0.6, - default: 0.6 +export const TREASURY_THRESHOLDS: Record = { + [KULUPU_GENESIS]: { option: 'MoreThan', value: 0.5 }, + [KUSAMA_GENESIS]: { option: 'AtLeast', value: 0.6 }, + [POLKADOT_GENESIS]: { option: 'AtLeast', value: 0.6 }, + default: { option: 'AtLeast', value: 0.6 } }; diff --git a/packages/react-hooks/src/useThresholds.ts b/packages/react-hooks/src/useThresholds.ts index 8466fb02470..b956844f45d 100644 --- a/packages/react-hooks/src/useThresholds.ts +++ b/packages/react-hooks/src/useThresholds.ts @@ -3,7 +3,11 @@ import { useMemo } from 'react'; -import { PROPOSE_THRESHOLDS, REJECT_THRESHOLDS, SLASH_THRESHOLDS, TREASURY_THRESHOLDS } from '@polkadot/apps-config'; +import { PROPOSE_THRESHOLDS, + REJECT_THRESHOLDS, + SLASH_THRESHOLDS, + Threshold, + TREASURY_THRESHOLDS } from '@polkadot/apps-config'; import { useApi } from './useApi'; import { useMembers } from './useMembers'; @@ -25,6 +29,16 @@ export function getAtLeastThresholdMembersCount (membersCount: number, threshold return Math.ceil(membersCount * thresholdRatio); } +function getThreshold (membersCount: number, threshold: Threshold): number { + return threshold.option === 'AtLeast' + ? getAtLeastThresholdMembersCount( + membersCount, + threshold.value + ) + : getMoreThanThresholdMembersCount(membersCount, + threshold.value); +} + export function useThresholds () : Thresholds { const { api } = useApi(); const { members } = useMembers(); @@ -39,20 +53,16 @@ export function useThresholds () : Thresholds { const genesisHash = api.genesisHash.toHex(); return { - proposalThreshold: getAtLeastThresholdMembersCount( - membersCount, + proposalThreshold: getThreshold(membersCount, PROPOSE_THRESHOLDS[genesisHash] || PROPOSE_THRESHOLDS.default ), - slashProposalThreshold: getAtLeastThresholdMembersCount( - membersCount, + slashProposalThreshold: getThreshold(membersCount, SLASH_THRESHOLDS[genesisHash] || SLASH_THRESHOLDS.default ), - treasuryProposalThreshold: getAtLeastThresholdMembersCount( - membersCount, + treasuryProposalThreshold: getThreshold(membersCount, TREASURY_THRESHOLDS[genesisHash] || TREASURY_THRESHOLDS.default ), - treasuryRejectionThreshold: getMoreThanThresholdMembersCount( - membersCount, + treasuryRejectionThreshold: getThreshold(membersCount, REJECT_THRESHOLDS[genesisHash] || REJECT_THRESHOLDS.default ) }; From d81035c418939d5421010aaf90513b8df29ec633 Mon Sep 17 00:00:00 2001 From: Ewa Kowalska Date: Fri, 5 Feb 2021 11:13:22 +0100 Subject: [PATCH 10/10] Apply review suggestions --- packages/react-hooks/src/useThresholds.ts | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/packages/react-hooks/src/useThresholds.ts b/packages/react-hooks/src/useThresholds.ts index b956844f45d..dbce5bde9bd 100644 --- a/packages/react-hooks/src/useThresholds.ts +++ b/packages/react-hooks/src/useThresholds.ts @@ -1,12 +1,13 @@ // Copyright 2017-2021 @polkadot/react-hooks authors & contributors // SPDX-License-Identifier: Apache-2.0 +import type { Threshold } from '@polkadot/apps-config'; + import { useMemo } from 'react'; import { PROPOSE_THRESHOLDS, REJECT_THRESHOLDS, SLASH_THRESHOLDS, - Threshold, TREASURY_THRESHOLDS } from '@polkadot/apps-config'; import { useApi } from './useApi'; @@ -31,12 +32,8 @@ export function getAtLeastThresholdMembersCount (membersCount: number, threshold function getThreshold (membersCount: number, threshold: Threshold): number { return threshold.option === 'AtLeast' - ? getAtLeastThresholdMembersCount( - membersCount, - threshold.value - ) - : getMoreThanThresholdMembersCount(membersCount, - threshold.value); + ? getAtLeastThresholdMembersCount(membersCount, threshold.value) + : getMoreThanThresholdMembersCount(membersCount, threshold.value); } export function useThresholds () : Thresholds {