ghost-portal/packages/react-hooks/src/useWeight.ts
Ewa Kowalska 541213f233
Ek extract type only imports into separate group (#4168)
* Plug new beta of eslint plugin simple import sort

* Apply new configuration to existing imports
2020-12-07 16:04:05 +01:00

37 lines
1.1 KiB
TypeScript

// Copyright 2017-2020 @polkadot/react-hooks authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { 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';
const EMPTY_STATE: [BN, number] = [BN_ZERO, 0];
// for a given call, calculate the weight
export function useWeight (call?: Call | null): [BN, number] {
const { api } = useApi();
const mountedRef = useIsMountedRef();
const [state, setState] = useState(EMPTY_STATE);
useEffect((): void => {
if (call) {
api.tx(call)
.paymentInfo(ZERO_ACCOUNT)
.then(({ weight }) => mountedRef.current && setState([weight, call.encodedLength]))
.catch(console.error);
} else {
setState(EMPTY_STATE);
}
}, [api, call, mountedRef]);
return state;
}