@polkadot/types/interfaces#Weight TypeScript Examples

The following examples show how to use @polkadot/types/interfaces#Weight. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: useTxBatch.ts    From crust-apps with Apache License 2.0 6 votes vote down vote up
export function useTxBatch (txs?: SubmittableExtrinsic<'promise'>[] | null | false, options?: Options): SubmittableExtrinsic<'promise'>[] | null {
  const { api } = useApi();
  const { allAccounts } = useAccounts();
  const [batchSize, setBatchSize] = useState(Math.floor(options?.batchSize || 64));

  useEffect((): void => {
    txs && txs.length && allAccounts[0] && isFunction(api.rpc.payment?.queryInfo) &&
      txs[0]
        .paymentInfo(allAccounts[0])
        .then((info) => setBatchSize((prev) =>
          info.weight.isZero()
            ? prev
            : Math.floor(
              (
                api.consts.system.blockWeights
                  ? api.consts.system.blockWeights.maxBlock
                  : api.consts.system.maximumBlockWeight as Weight
              )
                .muln(64) // 65% of the block weight on a single extrinsic (64 for safety)
                .div(info.weight)
                .toNumber() / 100
            )
        ))
        .catch(console.error);
  }, [allAccounts, api, options, txs]);

  return useMemo(
    () => txs && txs.length
      ? createBatches(api, txs, batchSize, options?.isBatchAll)
      : null,
    [api, batchSize, options, txs]
  );
}
Example #2
Source File: useTxBatch.ts    From subscan-multisig-react with Apache License 2.0 6 votes vote down vote up
export function useTxBatch(
  txs?: SubmittableExtrinsic<'promise'>[] | null | false,
  options?: Options
): SubmittableExtrinsic<'promise'>[] | null {
  const { api } = useApi();
  const { allAccounts } = useAccounts();
  const [batchSize, setBatchSize] = useState(Math.floor(options?.batchSize || 64));

  useEffect((): void => {
    // eslint-disable-next-line
    txs &&
      txs.length &&
      allAccounts[0] &&
      isFunction(api.rpc.payment?.queryInfo) &&
      txs[0]
        .paymentInfo(allAccounts[0])
        .then((info) =>
          setBatchSize((prev) =>
            info.weight.isZero()
              ? prev
              : Math.floor(
                  (api.consts.system.blockWeights
                    ? (api.consts.system.blockWeights as any).maxBlock
                    : (api.consts.system.maximumBlockWeight as Weight)
                  )
                    .muln(64) // 65% of the block weight on a single extrinsic (64 for safety)
                    .div(info.weight)
                    .toNumber() / 100
                )
          )
        )
        .catch(console.error);
  }, [allAccounts, api, options, txs]);

  return useMemo(
    () => (txs && txs.length ? createBatches(api, txs, batchSize, options?.isBatchAll) : null),
    [api, batchSize, options, txs]
  );
}
Example #3
Source File: useWeight.ts    From crust-apps with Apache License 2.0 5 votes vote down vote up
export default function useWeight (): UseWeight {
  const { api } = useApi();
  const [blockTime] = useBlockTime();
  const [megaGas, _setMegaGas] = useState<BN>(
    (api.consts.system.blockWeights
      ? api.consts.system.blockWeights.maxBlock
      : api.consts.system.maximumBlockWeight as Weight
    ).div(BN_MILLION).div(BN_TEN)
  );
  const [isEmpty, setIsEmpty] = useState(false);

  const setMegaGas = useCallback(
    (value?: BN | undefined) => _setMegaGas(value || (
      (api.consts.system.blockWeights
        ? api.consts.system.blockWeights.maxBlock
        : api.consts.system.maximumBlockWeight as Weight
      ).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.blockWeights
          ? api.consts.system.blockWeights.maxBlock
          : api.consts.system.maximumBlockWeight as Weight
      ).toNumber();
      percentage = (executionTime / blockTime) * 100;

      // execution is 2s of 6s blocks, i.e. 1/3
      executionTime = executionTime / 3000;
      isValid = !megaGas.isZero() && percentage < 65;
    }

    return {
      executionTime,
      isEmpty,
      isValid: isEmpty || isValid,
      megaGas: megaGas || BN_ZERO,
      percentage,
      setIsEmpty,
      setMegaGas,
      weight
    };
  }, [api, blockTime, isEmpty, megaGas, setIsEmpty, setMegaGas]);
}