Allow democracy fasttrack (#2424)

* Allow democarcy fasttrack

* lint.
This commit is contained in:
Jaco Greeff 2020-03-20 09:15:25 +01:00 committed by GitHub
parent 497033bc78
commit e03611221b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 179 additions and 66 deletions

View File

@ -52,11 +52,12 @@ function DispatchEntry ({ blockNumber, hash, referendumIndex }: Props): React.Re
proposal={proposal}
/>
<td className='together number top'>
<PreImageButton
hash={hash}
isImminent
proposal={proposal}
/>
{!proposal && (
<PreImageButton
hash={hash}
isImminent
/>
)}
<LinkExternal
data={referendumIndex}
type='referendum'

View File

@ -6,7 +6,7 @@ import { AccountId, Balance, BlockNumber, Hash, Proposal, VoteThreshold } from '
import { ITuple } from '@polkadot/types/types';
import React, { useEffect, useState } from 'react';
import { AddressSmall, Table } from '@polkadot/react-components';
import { AddressSmall, Table, Button } from '@polkadot/react-components';
import { useApi, useCall } from '@polkadot/react-hooks';
import { FormatBalance } from '@polkadot/react-query';
import { Bytes, Option } from '@polkadot/types';
@ -14,6 +14,7 @@ import { Bytes, Option } from '@polkadot/types';
import { useTranslation } from '../translate';
import PreImageButton from './PreImageButton';
import ProposalCell from './ProposalCell';
import Fasttrack from './Fasttrack';
interface Props {
className?: string;
@ -23,15 +24,15 @@ function Externals ({ className }: Props): React.ReactElement<Props> | null {
const { t } = useTranslation();
const { api } = useApi();
const external = useCall<Option<ITuple<[Hash, VoteThreshold]>>>(api.query.democracy.nextExternal, []);
const [hash, setHash] = useState<Hash | null>(null);
const [[hash, threshold], setInfo] = useState<[Hash | null, VoteThreshold | null]>([null, null]);
const [expanded, setExpanded] = useState<{ at: BlockNumber; balance: Balance; proposer: AccountId; proposal: Proposal } | null>(null);
const preimage = useCall<Option<ITuple<[Bytes, AccountId, Balance, BlockNumber]>>>(api.query.democracy.preimages, [hash]);
useEffect((): void => {
setHash(
external?.isSome
? external.unwrap()[0]
: null
setInfo(
external
? external.unwrapOr([null, null])
: [null, null]
);
}, [external]);
@ -72,11 +73,21 @@ function Externals ({ className }: Props): React.ReactElement<Props> | null {
proposal={expanded?.proposal}
/>
<td className='together number top'>
<PreImageButton
hash={hash}
proposal={expanded?.proposal}
withoutOr
/>
{!expanded?.proposal && (
<>
<PreImageButton
hash={hash}
withoutOr
/>
<Button.Or />
</>
)}
{hash && threshold && (
<Fasttrack
hash={hash}
threshold={threshold}
/>
)}
</td>
</tr>
</Table.Body>

View File

@ -0,0 +1,111 @@
// Copyright 2017-2020 @polkadot/app-democracy authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { SubmittableExtrinsic } from '@polkadot/api/types';
import { Hash, VoteThreshold } from '@polkadot/types/interfaces';
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, useMembers, useToggle } from '@polkadot/react-hooks';
import { useTranslation } from '../translate';
interface Props {
hash: Hash;
threshold: VoteThreshold;
}
const ONE_MIN = (1 * 60) / 6;
const DEF_DELAY = new BN(ONE_MIN);
const DEF_VOTING = new BN(ONE_MIN * 60 * 3);
function Fasttrack ({ hash, threshold }: Props): React.ReactElement<Props> | null {
const { t } = useTranslation();
const { api } = useApi();
const { isMember, members } = useMembers('technicalCommittee');
const [isFasttrackOpen, toggleFasttrack] = useToggle();
const [allowFast, setAllowFast] = useState(false);
const [accountId, setAcountId] = useState<string | null>(null);
const [delayBlocks, setDelayBlocks] = useState<BN | undefined>(DEF_DELAY);
const [votingBlocks, setVotingBlocks] = useState<BN | undefined>(DEF_VOTING);
const [proposal, setProposal] = useState<SubmittableExtrinsic<'promise'> | null>(null);
const memberThreshold = useMemo(
() => Math.ceil(members.length * 0.5),
[members]
);
useEffect((): void => {
setAllowFast(isMember && threshold.isSimplemajority);
}, [isMember, threshold]);
useEffect((): void => {
setProposal(
() => delayBlocks?.gtn(0) && votingBlocks?.gtn(0)
? api.tx.democracy.fastTrack(hash, votingBlocks, delayBlocks)
: null
);
}, [delayBlocks, hash, votingBlocks]);
return (
<>
{isFasttrackOpen && (
<Modal
header={t('Fast track proposal')}
size='small'
>
<Modal.Content>
<InputAddress
filter={members}
help={t('Select the account you wish to make the proposal with.')}
label={t('propose from account')}
onChange={setAcountId}
type='account'
withLabel
/>
<Input
help={t('The external proposal to send to the technical committee')}
isDisabled
label={t('external hash')}
value={hash}
/>
<InputNumber
autoFocus
help={t('The voting period to apply in blocks')}
label={t('voting period')}
onChange={setVotingBlocks}
value={votingBlocks}
/>
<InputNumber
help={t('The delay period to apply in blocks')}
label={t('delay')}
onChange={setDelayBlocks}
value={delayBlocks}
/>
</Modal.Content>
<Modal.Actions onCancel={toggleFasttrack}>
<TxButton
accountId={accountId}
isDisabled={!accountId || !proposal || !memberThreshold}
isPrimary
label={t('Fast track')}
icon='fast forward'
onStart={toggleFasttrack}
params={[memberThreshold, proposal]}
tx='technicalCommittee.propose'
/>
</Modal.Actions>
</Modal>
)}
<Button
icon='fast forward'
isDisabled={!allowFast}
label={t('Fast track')}
onClick={toggleFasttrack}
/>
</>
);
}
export default React.memo(Fasttrack);

View File

@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { Proposal, Hash } from '@polkadot/types/interfaces';
import { Hash } from '@polkadot/types/interfaces';
import React from 'react';
import { Button } from '@polkadot/react-components';
@ -14,18 +14,13 @@ import PreImage from './PreImage';
interface Props {
hash: Hash;
isImminent?: boolean;
proposal?: Proposal;
withoutOr?: boolean;
}
function PreImageButton ({ hash, isImminent, proposal, withoutOr }: Props): React.ReactElement<Props> | null {
function PreImageButton ({ hash, isImminent, withoutOr }: Props): React.ReactElement<Props> | null {
const { t } = useTranslation();
const [isPreimageOpen, togglePreimage] = useToggle();
if (proposal) {
return null;
}
return (
<>
{!withoutOr && <Button.Or />}

View File

@ -63,10 +63,9 @@ function Proposal ({ className, value: { balance, hash, index, proposal, propose
proposalId={index}
proposal={proposal}
/>
<PreImageButton
hash={hash}
proposal={proposal}
/>
{!proposal && (
<PreImageButton hash={hash} />
)}
</Button.Group>
<LinkExternal
data={index}

View File

@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/camelcase */
// Copyright 2017-2020 @polkadot/app-democracy authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
@ -34,6 +33,35 @@ interface State {
votedTotal: BN;
}
function calcState (votesFor: DerivedReferendumVote[]): State {
return votesFor.reduce((state, { balance, vote }): State => {
const isDefault = vote.conviction.index === 0;
const counted = balance
.muln(isDefault ? 1 : vote.conviction.index)
.divn(isDefault ? 10 : 1);
if (vote.isAye) {
state.voteCountAye++;
state.votedAye = state.votedAye.add(counted);
} else {
state.voteCountNay++;
state.votedNay = state.votedNay.add(counted);
}
state.voteCount++;
state.votedTotal = state.votedTotal.add(counted);
return state;
}, {
voteCount: 0,
voteCountAye: 0,
voteCountNay: 0,
votedAye: new BN(0),
votedNay: new BN(0),
votedTotal: new BN(0)
});
}
function Referendum ({ className, idNumber, value }: Props): React.ReactElement<Props> | null {
const { t } = useTranslation();
const { api } = useApi();
@ -49,40 +77,9 @@ function Referendum ({ className, idNumber, value }: Props): React.ReactElement<
});
useEffect((): void => {
if (votesFor) {
const newState: State = votesFor.reduce((state, { balance, vote }): State => {
const isDefault = vote.conviction.index === 0;
const counted = balance
.muln(isDefault ? 1 : vote.conviction.index)
.divn(isDefault ? 10 : 1);
if (vote.isAye) {
state.voteCountAye++;
state.votedAye = state.votedAye.add(counted);
} else {
state.voteCountNay++;
state.votedNay = state.votedNay.add(counted);
}
state.voteCount++;
state.votedTotal = state.votedTotal.add(counted);
return state;
}, {
voteCount: 0,
voteCountAye: 0,
voteCountNay: 0,
votedAye: new BN(0),
votedNay: new BN(0),
votedTotal: new BN(0)
});
if (newState.votedAye.eq(votedNay) && newState.votedNay.eq(votedNay)) {
return;
}
setState(newState);
}
votesFor && setState(
calcState(votesFor)
);
}, [votesFor]);
if (!bestNumber || value.info.end.sub(bestNumber).lten(0)) {
@ -124,10 +121,9 @@ function Referendum ({ className, idNumber, value }: Props): React.ReactElement<
proposal={value.proposal}
referendumId={value.index}
/>
<PreImageButton
hash={value.hash}
proposal={value.proposal}
/>
{!value.proposal && (
<PreImageButton hash={value.hash} />
)}
</Button.Group>
<LinkExternal
data={value.index}