useWeight hook (collective and soon on multisig) (#2955)

* useWeight hook (collective and soon on multisig)

* zero address

* Remove debug log
This commit is contained in:
Jaco Greeff 2020-06-15 10:36:20 +02:00 committed by GitHub
parent 0f9fec8846
commit 4ab3e73a3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 41 additions and 25 deletions

View File

@ -2,11 +2,11 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { Hash, Proposal, ProposalIndex, Weight } from '@polkadot/types/interfaces';
import { Hash, Proposal, ProposalIndex } from '@polkadot/types/interfaces';
import React, { useEffect, useState } from 'react';
import React, { useState } from 'react';
import { Button, InputAddress, Modal, ProposedAction, TxButton } from '@polkadot/react-components';
import { useAccounts, useApi, useToggle } from '@polkadot/react-hooks';
import { useApi, useToggle, useWeight } from '@polkadot/react-hooks';
import { useTranslation } from '../translate';
@ -22,17 +22,10 @@ interface Props {
function Close ({ hasFailed, hash, idNumber, isDisabled, members, proposal }: Props): React.ReactElement<Props> | null {
const { t } = useTranslation();
const { api } = useApi();
const { allAccounts } = useAccounts();
const [isOpen, toggleOpen] = useToggle();
const [accountId, setAccountId] = useState<string | null>(null);
const [proposalLength] = useState(proposal.encodedLength);
const [proposalWeight, setProposalWeight] = useState<Weight | null>(null);
useEffect((): void => {
allAccounts.length && (async () => setProposalWeight(
(await api.tx(proposal).paymentInfo(allAccounts[0])).weight
))().catch(console.error);
}, [api, allAccounts, proposal]);
const proposalWeight = useWeight(proposal);
return (
<>
@ -71,7 +64,6 @@ function Close ({ hasFailed, hash, idNumber, isDisabled, members, proposal }: Pr
<Modal.Actions onCancel={toggleOpen}>
<TxButton
accountId={accountId}
isDisabled={!proposalWeight}
onStart={toggleOpen}
params={
api.tx.council.close.meta.args.length === 4

View File

@ -2,11 +2,11 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { Hash, Proposal, ProposalIndex, Weight } from '@polkadot/types/interfaces';
import { Hash, Proposal, ProposalIndex } from '@polkadot/types/interfaces';
import React, { useEffect, useState } from 'react';
import React, { useState } from 'react';
import { Button, InputAddress, Modal, ProposedAction, TxButton } from '@polkadot/react-components';
import { useAccounts, useApi, useToggle } from '@polkadot/react-hooks';
import { useApi, useToggle, useWeight } from '@polkadot/react-hooks';
import { useTranslation } from '../translate';
@ -22,17 +22,10 @@ interface Props {
function Close ({ hasFailed, hash, idNumber, isDisabled, members, proposal }: Props): React.ReactElement<Props> | null {
const { t } = useTranslation();
const { api } = useApi();
const { allAccounts } = useAccounts();
const [isOpen, toggleOpen] = useToggle();
const [accountId, setAccountId] = useState<string | null>(null);
const [proposalLength] = useState(proposal.encodedLength);
const [proposalWeight, setProposalWeight] = useState<Weight | null>(null);
useEffect((): void => {
allAccounts.length && (async () => setProposalWeight(
(await api.tx(proposal).paymentInfo(allAccounts[0])).weight
))().catch(console.error);
}, [api, allAccounts, proposal]);
const proposalWeight = useWeight(proposal);
return (
<>
@ -71,7 +64,6 @@ function Close ({ hasFailed, hash, idNumber, isDisabled, members, proposal }: Pr
<Modal.Actions onCancel={toggleOpen}>
<TxButton
accountId={accountId}
isDisabled={!proposalWeight}
onStart={toggleOpen}
params={
api.tx.technicalCommittee.close.meta.args.length === 4

View File

@ -30,3 +30,4 @@ export { default as useStashIds } from './useStashIds';
export { default as useSudo } from './useSudo';
export { default as useToggle } from './useToggle';
export { default as useVotingStatus } from './useVotingStatus';
export { default as useWeight } from './useWeight';

View File

@ -20,7 +20,7 @@ export default function useSudo (): UseSudo {
const [isMine, setIsMine] = useState(false);
useEffect((): void => {
setIsMine(!!sudoKey && !!allAccounts && allAccounts.some((key): boolean => key === sudoKey));
setIsMine(!!sudoKey && !!allAccounts && allAccounts.some((key) => key === sudoKey));
}, [allAccounts, sudoKey]);
return { allAccounts, isMine, sudoKey };

View File

@ -0,0 +1,31 @@
// Copyright 2017-2020 @polkadot/react-hooks 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 { Call } from '@polkadot/types/interfaces';
import BN from 'bn.js';
import { useEffect, useState } from 'react';
import { BN_ZERO } from '@polkadot/util';
import useApi from './useApi';
import useIsMountedRef from './useIsMountedRef';
// a random address that we are using for our queries
const ZERO_ACCOUNT = '5CAUdnwecHGxxyr5vABevAfZ34Fi4AaraDRMwfDQXQ52PXqg';
// for a given call, calculate the weight
export default function useWeight (call: Call): BN {
const { api } = useApi();
const mountedRef = useIsMountedRef();
const [weight, setWeight] = useState<BN>(BN_ZERO);
useEffect((): void => {
api.tx(call)
.paymentInfo(ZERO_ACCOUNT)
.then(({ weight }) => mountedRef.current && setWeight(weight))
.catch(console.error);
}, [api, call, mountedRef]);
return weight;
}