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
This commit is contained in:
Jaco 2022-12-22 09:15:43 +02:00 committed by GitHub
parent 240ff3ebfc
commit adec4a7fcb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 14 deletions

View File

@ -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

View File

@ -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<Props> {
interface Props extends VoteTypeProps {
voteLockingPeriod?: BN;
}
function VoteStandard ({ accountId, id, isAye, onChange, voteLockingPeriod }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const [balance, setBalance] = useState<BN | undefined>();
const [conviction, setConviction] = useState(0);
@ -51,6 +55,7 @@ function VoteStandard ({ accountId, id, isAye, onChange }: Props): React.ReactEl
label={t<string>('conviction')}
onChange={setConviction}
value={conviction}
voteLockingPeriod={voteLockingPeriod}
/>
</Modal.Columns>
);

View File

@ -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<string>('0.1x voting balance, no lockup period'), value: 0 },
...CONVICTIONS.map(([value, lock, bnLock]): { text: string; value: number } => ({
text: t<string>('{{value}}x voting balance, locked for {{lock}}x duration ({{period}} days)', {
text: t<string>('{{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<Props> | null {
function Convictions ({ className = '', help, label, onChange, value, voteLockingPeriod }: Props): React.ReactElement<Props> | 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 (
<Dropdown