diff --git a/packages/page-contracts/src/Contracts/Call.tsx b/packages/page-contracts/src/Contracts/Call.tsx index 2ec76f24f4..e5062499f0 100644 --- a/packages/page-contracts/src/Contracts/Call.tsx +++ b/packages/page-contracts/src/Contracts/Call.tsx @@ -163,7 +163,7 @@ function Call ({ className = '', contract, messageIndex, onCallResult, onChangeM ('The maximum amount of gas to use for this contract call. If the call requires more, it will fail.')} - {...weight} + weight={weight} /> {message.isMutating && ( ('The maximum amount of gas that can be used by this deployment, if the code requires more, the deployment will fail.')} - {...weight} + weight={weight} /> diff --git a/packages/page-contracts/src/shared/InputMegaGas.tsx b/packages/page-contracts/src/shared/InputMegaGas.tsx index a1cef369a5..4f67028494 100644 --- a/packages/page-contracts/src/shared/InputMegaGas.tsx +++ b/packages/page-contracts/src/shared/InputMegaGas.tsx @@ -1,6 +1,8 @@ // Copyright 2017-2020 @polkadot/app-contracts authors & contributors // SPDX-License-Identifier: Apache-2.0 +import { UseWeight } from '../types'; + import BN from 'bn.js'; import React, { useEffect, useMemo, useState } from 'react'; import styled from 'styled-components'; @@ -10,16 +12,12 @@ import { useTranslation } from '../translate'; interface Props { className?: string; - estimatedWeight?: BN | null; - executionTime: number; + estimatedWeight?: BN; help: React.ReactNode; - isValid: boolean; - megaGas: BN; - percentage: number; - setMegaGas: (value?: BN) => void; + weight: UseWeight; } -function InputMegaGas ({ className, estimatedWeight, executionTime, help, isValid, megaGas, percentage, setMegaGas }: Props): React.ReactElement { +function InputMegaGas ({ className, estimatedWeight, help, weight: { executionTime, isValid, megaGas, percentage, setMegaGas } }: Props): React.ReactElement { const { t } = useTranslation(); const [withEstimate, setWithEstimate] = useState(false); @@ -30,15 +28,8 @@ function InputMegaGas ({ className, estimatedWeight, executionTime, help, isVali [estimatedWeight] ); - const label = useMemo( - () => estimatedMg - ? t('max gas allowed (M, {{estimatedMg}} estimated)', { replace: { estimatedMg: estimatedMg.toString() } }) - : t('max gas allowed (M)'), - [estimatedMg, t] - ); - useEffect((): void => { - estimatedMg && withEstimate && setMegaGas(estimatedMg); + withEstimate && estimatedMg && setMegaGas(estimatedMg); }, [estimatedMg, setMegaGas, withEstimate]); const isDisabled = !!estimatedMg && withEstimate; @@ -50,7 +41,11 @@ function InputMegaGas ({ className, estimatedWeight, executionTime, help, isVali help={help} isDisabled={isDisabled} isError={!isValid} - label={label} + label={ + estimatedMg + ? t('max gas allowed (M, {{estimatedMg}} estimated)', { replace: { estimatedMg: estimatedMg.toString() } }) + : t('max gas allowed (M)') + } onChange={isDisabled ? undefined : setMegaGas} value={isDisabled ? undefined : megaGas} > diff --git a/packages/page-contracts/src/types.ts b/packages/page-contracts/src/types.ts index 55a902f7c1..256705e402 100644 --- a/packages/page-contracts/src/types.ts +++ b/packages/page-contracts/src/types.ts @@ -25,10 +25,10 @@ export interface ContractJsonOld { } export interface UseWeight { - isValid: boolean; - weight: BN; executionTime: number; + isValid: boolean; megaGas: BN; percentage: number; - setMegaGas: React.Dispatch + setMegaGas: React.Dispatch; + weight: BN; } diff --git a/packages/page-contracts/src/useWeight.ts b/packages/page-contracts/src/useWeight.ts index 4e8f0eede4..071fbea487 100644 --- a/packages/page-contracts/src/useWeight.ts +++ b/packages/page-contracts/src/useWeight.ts @@ -4,43 +4,45 @@ import { UseWeight } from './types'; import BN from 'bn.js'; -import { useMemo, useState } from 'react'; +import { useCallback, useMemo, useState } from 'react'; import { useApi, useBlockTime } from '@polkadot/react-hooks'; import { BN_TEN, BN_ZERO } from '@polkadot/util'; const BN_MILLION = new BN(1e6); -export default function useWeight (initialValue?: BN): UseWeight { +export default function useWeight (): UseWeight { const { api } = useApi(); const [blockTime] = useBlockTime(); - const [megaGas, setMegaGas] = useState( - initialValue || api.consts.system.maximumBlockWeight.div(BN_MILLION).div(BN_TEN) - ); - const [executionTime, percentage, weight, isValid] = useMemo( - (): [number, number, BN, boolean] => { - if (!megaGas) { - return [0, 0, BN_ZERO, false]; - } + const [megaGas, _setMegaGas] = useState(api.consts.system.maximumBlockWeight.div(BN_MILLION).div(BN_TEN)); - const weight = megaGas.mul(BN_MILLION); - const executionTime = weight.muln(blockTime).div(api.consts.system.maximumBlockWeight).toNumber(); - const percentage = (executionTime / blockTime) * 100; + const setMegaGas = useCallback( + (value?: BN | undefined) => _setMegaGas(value || api.consts.system.maximumBlockWeight.div(BN_MILLION).div(BN_TEN)), + [api] + ); + + return useMemo((): UseWeight => { + let executionTime = 0; + let percentage = 0; + let weight = BN_ZERO; + let isValid = false; + + if (megaGas) { + weight = megaGas.mul(BN_MILLION); + executionTime = weight.muln(blockTime).div(api.consts.system.maximumBlockWeight).toNumber(); + percentage = (executionTime / blockTime) * 100; // execution is 2s of 6s blocks, i.e. 1/3 - return [executionTime / 3000, percentage, weight, !megaGas.isZero() && percentage < 100]; - }, - [api, blockTime, megaGas] - ); + executionTime = executionTime / 3000; + isValid = !megaGas.isZero() && percentage < 65; + } - return useMemo( - () => ({ + return { executionTime, isValid, megaGas: megaGas || BN_ZERO, percentage, setMegaGas, weight - }), - [executionTime, isValid, megaGas, percentage, setMegaGas, weight] - ); + }; + }, [api, blockTime, megaGas, setMegaGas]); }