From adec4a7fcb9a4c2a1ce188972e1d0a9863fcf765 Mon Sep 17 00:00:00 2001 From: Jaco Date: Thu, 22 Dec 2022 09:15:43 +0200 Subject: [PATCH] Use convictionVoting votingPeriod in Vote display (#8599) * Use per track enactmentPeriod in Vote display * Pass track * Adjust time text * voteLockingPeriod pass-through * Remove unused track pass-through --- .../page-referenda/src/Referenda/Vote.tsx | 2 ++ .../src/Referenda/VoteStandard.tsx | 9 +++++-- .../src/ConvictionDropdown.tsx | 24 +++++++++---------- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/packages/page-referenda/src/Referenda/Vote.tsx b/packages/page-referenda/src/Referenda/Vote.tsx index b469e2a0de..dff62e434e 100644 --- a/packages/page-referenda/src/Referenda/Vote.tsx +++ b/packages/page-referenda/src/Referenda/Vote.tsx @@ -146,6 +146,7 @@ function Voting ({ className, id, isConvictionVote, isMember, members, palletVot id={id} isAye onChange={setParams} + voteLockingPeriod={api.consts.convictionVoting.voteLockingPeriod} /> ) : voteTypeIndex === 1 @@ -154,6 +155,7 @@ function Voting ({ className, id, isConvictionVote, isMember, members, palletVot accountId={accountId} id={id} onChange={setParams} + voteLockingPeriod={api.consts.convictionVoting.voteLockingPeriod} /> ) : voteTypeIndex === 2 diff --git a/packages/page-referenda/src/Referenda/VoteStandard.tsx b/packages/page-referenda/src/Referenda/VoteStandard.tsx index d3134586e5..36c1bb95bd 100644 --- a/packages/page-referenda/src/Referenda/VoteStandard.tsx +++ b/packages/page-referenda/src/Referenda/VoteStandard.tsx @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { BN } from '@polkadot/util'; -import type { VoteTypeProps as Props } from './types'; +import type { VoteTypeProps } from './types'; import React, { useEffect, useState } from 'react'; @@ -10,7 +10,11 @@ import { ConvictionDropdown, Modal, VoteValue } from '@polkadot/react-components import { useTranslation } from '../translate'; -function VoteStandard ({ accountId, id, isAye, onChange }: Props): React.ReactElement { +interface Props extends VoteTypeProps { + voteLockingPeriod?: BN; +} + +function VoteStandard ({ accountId, id, isAye, onChange, voteLockingPeriod }: Props): React.ReactElement { const { t } = useTranslation(); const [balance, setBalance] = useState(); const [conviction, setConviction] = useState(0); @@ -51,6 +55,7 @@ function VoteStandard ({ accountId, id, isAye, onChange }: Props): React.ReactEl label={t('conviction')} onChange={setConviction} value={conviction} + voteLockingPeriod={voteLockingPeriod} /> ); diff --git a/packages/react-components/src/ConvictionDropdown.tsx b/packages/react-components/src/ConvictionDropdown.tsx index 6a00525755..0237c340eb 100644 --- a/packages/react-components/src/ConvictionDropdown.tsx +++ b/packages/react-components/src/ConvictionDropdown.tsx @@ -7,7 +7,8 @@ import React, { useRef } from 'react'; import { ApiPromise } from '@polkadot/api'; import { useApi, useBlockInterval } from '@polkadot/react-hooks'; -import { BN, BN_THOUSAND } from '@polkadot/util'; +import { calcBlockTime } from '@polkadot/react-hooks/useBlockTime'; +import { BN } from '@polkadot/util'; import Dropdown from './Dropdown'; import { useTranslation } from './translate'; @@ -18,24 +19,23 @@ export interface Props { label?: React.ReactNode; onChange?: (value: number) => void; value?: number; + voteLockingPeriod?: BN; } const CONVICTIONS: [number, number, BN][] = [1, 2, 4, 8, 16, 32].map((lock, index) => [index + 1, lock, new BN(lock)]); -const SEC_DAY = 60 * 60 * 24; -function createOptions (api: ApiPromise, t: TFunction, blockTime: BN): { text: string; value: number }[] { +function createOptions (api: ApiPromise, t: TFunction, blockTime: BN, voteLockingPeriod?: BN): { text: string; value: number }[] { return [ { text: t('0.1x voting balance, no lockup period'), value: 0 }, ...CONVICTIONS.map(([value, lock, bnLock]): { text: string; value: number } => ({ - text: t('{{value}}x voting balance, locked for {{lock}}x duration ({{period}} days)', { + text: t('{{value}}x voting balance, locked for {{lock}}x duration ({{period}})', { replace: { lock, - period: ( - bnLock.mul( - api.consts.democracy.voteLockingPeriod || - api.consts.democracy.enactmentPeriod - ).mul(blockTime).div(BN_THOUSAND).toNumber() / SEC_DAY - ).toFixed(2), + period: calcBlockTime(blockTime, bnLock.mul( + voteLockingPeriod || + api.consts.democracy.voteLockingPeriod || + api.consts.democracy.enactmentPeriod + ), t)[1], value } }), @@ -44,12 +44,12 @@ function createOptions (api: ApiPromise, t: TFunction, blockTime: BN): { text: s ]; } -function Convictions ({ className = '', help, label, onChange, value }: Props): React.ReactElement | null { +function Convictions ({ className = '', help, label, onChange, value, voteLockingPeriod }: Props): React.ReactElement | null { const { api } = useApi(); const { t } = useTranslation(); const blockTime = useBlockInterval(); - const optionsRef = useRef(createOptions(api, t, blockTime)); + const optionsRef = useRef(createOptions(api, t, blockTime, voteLockingPeriod)); return (