From 4ab3e73a3b364e8996c224b30f0fc6f5c0c75606 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Mon, 15 Jun 2020 10:36:20 +0200 Subject: [PATCH] useWeight hook (collective and soon on multisig) (#2955) * useWeight hook (collective and soon on multisig) * zero address * Remove debug log --- packages/page-council/src/Motions/Close.tsx | 16 +++------- .../page-tech-comm/src/Proposals/Close.tsx | 16 +++------- packages/react-hooks/src/index.ts | 1 + packages/react-hooks/src/useSudo.ts | 2 +- packages/react-hooks/src/useWeight.ts | 31 +++++++++++++++++++ 5 files changed, 41 insertions(+), 25 deletions(-) create mode 100644 packages/react-hooks/src/useWeight.ts diff --git a/packages/page-council/src/Motions/Close.tsx b/packages/page-council/src/Motions/Close.tsx index 4f994b27f6..c7dd92ba14 100644 --- a/packages/page-council/src/Motions/Close.tsx +++ b/packages/page-council/src/Motions/Close.tsx @@ -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 | null { const { t } = useTranslation(); const { api } = useApi(); - const { allAccounts } = useAccounts(); const [isOpen, toggleOpen] = useToggle(); const [accountId, setAccountId] = useState(null); const [proposalLength] = useState(proposal.encodedLength); - const [proposalWeight, setProposalWeight] = useState(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 | null { const { t } = useTranslation(); const { api } = useApi(); - const { allAccounts } = useAccounts(); const [isOpen, toggleOpen] = useToggle(); const [accountId, setAccountId] = useState(null); const [proposalLength] = useState(proposal.encodedLength); - const [proposalWeight, setProposalWeight] = useState(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 { - setIsMine(!!sudoKey && !!allAccounts && allAccounts.some((key): boolean => key === sudoKey)); + setIsMine(!!sudoKey && !!allAccounts && allAccounts.some((key) => key === sudoKey)); }, [allAccounts, sudoKey]); return { allAccounts, isMine, sudoKey }; diff --git a/packages/react-hooks/src/useWeight.ts b/packages/react-hooks/src/useWeight.ts new file mode 100644 index 0000000000..631196a7fa --- /dev/null +++ b/packages/react-hooks/src/useWeight.ts @@ -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_ZERO); + + useEffect((): void => { + api.tx(call) + .paymentInfo(ZERO_ACCOUNT) + .then(({ weight }) => mountedRef.current && setWeight(weight)) + .catch(console.error); + }, [api, call, mountedRef]); + + return weight; +}