From 0e54bb36436fc36467b8c5d74846436187d7e5ce Mon Sep 17 00:00:00 2001 From: Jaco Date: Wed, 6 Apr 2022 07:21:07 +0200 Subject: [PATCH] Adjust options for useTxBatch (#7311) --- .../src/modals/ProxyOverview.tsx | 6 +-- .../src/Overview/Create/Create.tsx | 7 ++- .../page-staking/src/Payouts/PayButton.tsx | 10 ++++- packages/react-hooks/src/types.ts | 7 +++ packages/react-hooks/src/useTxBatch.ts | 44 +++++++++---------- 5 files changed, 42 insertions(+), 32 deletions(-) diff --git a/packages/page-accounts/src/modals/ProxyOverview.tsx b/packages/page-accounts/src/modals/ProxyOverview.tsx index 220dbc95a0..482fcece9f 100644 --- a/packages/page-accounts/src/modals/ProxyOverview.tsx +++ b/packages/page-accounts/src/modals/ProxyOverview.tsx @@ -3,6 +3,7 @@ import type { ApiPromise } from '@polkadot/api'; import type { SubmittableExtrinsic } from '@polkadot/api/types'; +import type { BatchOptions } from '@polkadot/react-hooks/types'; import type { AccountId } from '@polkadot/types/interfaces'; import type { NodeRuntimeProxyType, PalletProxyProxyDefinition } from '@polkadot/types/lookup'; import type { BN } from '@polkadot/util'; @@ -42,8 +43,7 @@ interface PrevProxyProps extends ValueProps { onRemove: (accountId: AccountId, type: NodeRuntimeProxyType, index: number) => void; } -const optTxBatch = { isBatchAll: true }; - +const BATCH_OPTS: BatchOptions = { type: 'all' }; const EMPTY_EXISTING: [PalletProxyProxyDefinition[], BN] = [[], BN_ZERO]; function createAddProxy (api: ApiPromise, account: AccountId, type: NodeRuntimeProxyType, delay = 0): SubmittableExtrinsic<'promise'> { @@ -170,7 +170,7 @@ function ProxyOverview ({ className, onClose, previousProxy: [existing] = EMPTY_ const [txs, setTxs] = useState[] | null>(null); const [previous, setPrevious] = useState(() => existing.map(({ delegate, proxyType }) => [delegate, proxyType])); const [added, setAdded] = useState([]); - const extrinsics = useTxBatch(txs, optTxBatch); + const extrinsics = useTxBatch(txs, BATCH_OPTS); const reservedAmount = useMemo( () => api.consts.proxy.proxyDepositFactor diff --git a/packages/page-assets/src/Overview/Create/Create.tsx b/packages/page-assets/src/Overview/Create/Create.tsx index 33cc83a24a..f28d82a3fc 100644 --- a/packages/page-assets/src/Overview/Create/Create.tsx +++ b/packages/page-assets/src/Overview/Create/Create.tsx @@ -1,6 +1,7 @@ // Copyright 2017-2022 @polkadot/app-assets authors & contributors // SPDX-License-Identifier: Apache-2.0 +import type { BatchOptions } from '@polkadot/react-hooks/types'; import type { BN } from '@polkadot/util'; import type { InfoState, TeamState } from './types'; @@ -20,9 +21,7 @@ interface Props { openId: BN; } -const BATCH_OPTIONS = { - isBatchAll: true -}; +const BATCH_OPTS: BatchOptions = { type: 'all' }; function Create ({ assetIds, className, onClose, openId }: Props): React.ReactElement { const { t } = useTranslation(); @@ -57,7 +56,7 @@ function Create ({ assetIds, className, onClose, openId }: Props): React.ReactEl [createTx, metadataTx, team, teamTx] ); - const extrinsic = useTxBatch(txs, BATCH_OPTIONS); + const extrinsic = useTxBatch(txs, BATCH_OPTS); return ( (null); const [txs, setTxs] = useState[] | null>(null); - const extrinsics = useTxBatch(txs, { batchSize: 36 * 64 / (api.consts.staking.maxNominatorRewardedPerValidator?.toNumber() || 64) }); + const batchOpts = useMemo( + () => ({ + max: 36 * 64 / (api.consts.staking.maxNominatorRewardedPerValidator?.toNumber() || 64) + }), + [api] + ); + const extrinsics = useTxBatch(txs, batchOpts); useEffect((): void => { payout && setTxs( diff --git a/packages/react-hooks/src/types.ts b/packages/react-hooks/src/types.ts index 4ac07f9d93..74d8ae9a08 100644 --- a/packages/react-hooks/src/types.ts +++ b/packages/react-hooks/src/types.ts @@ -159,3 +159,10 @@ export interface Judgement { } export type UseJudgements = Judgement[] + +export type BatchType = 'all' | 'default'; + +export interface BatchOptions { + max?: number; + type?: BatchType; +} diff --git a/packages/react-hooks/src/useTxBatch.ts b/packages/react-hooks/src/useTxBatch.ts index 2773edd1d3..02f6c9a92f 100644 --- a/packages/react-hooks/src/useTxBatch.ts +++ b/packages/react-hooks/src/useTxBatch.ts @@ -4,6 +4,7 @@ import type { ApiPromise } from '@polkadot/api'; import type { SubmittableExtrinsic } from '@polkadot/api/types'; import type { Weight } from '@polkadot/types/interfaces'; +import type { BatchOptions, BatchType } from './types'; import { useEffect, useMemo, useState } from 'react'; @@ -13,12 +14,7 @@ import { createNamedHook } from './createNamedHook'; import { useAccounts } from './useAccounts'; import { useApi } from './useApi'; -interface Options { - batchSize?: number; - isBatchAll?: boolean; -} - -function createBatches (api: ApiPromise, txs: SubmittableExtrinsic<'promise'>[], batchSize: number, isBatchAll = false): SubmittableExtrinsic<'promise'>[] { +function createBatches (api: ApiPromise, txs: SubmittableExtrinsic<'promise'>[], batchSize: number, type: BatchType = 'default'): SubmittableExtrinsic<'promise'>[] { if (batchSize === 1 || !isFunction(api.tx.utility?.batch)) { return txs; } @@ -38,41 +34,43 @@ function createBatches (api: ApiPromise, txs: SubmittableExtrinsic<'promise'>[], .map((batch): SubmittableExtrinsic<'promise'> => batch.length === 1 ? batch[0] - : isBatchAll && isFunction(api.tx.utility.batchAll) + : type === 'all' && isFunction(api.tx.utility.batchAll) ? api.tx.utility.batchAll(batch) : api.tx.utility.batch(batch) ); } -function useTxBatchImpl (txs?: SubmittableExtrinsic<'promise'>[] | null | false, options?: Options): SubmittableExtrinsic<'promise'>[] | null { +function useTxBatchImpl (txs?: SubmittableExtrinsic<'promise'>[] | null | false, options?: BatchOptions): SubmittableExtrinsic<'promise'>[] | null { const { api } = useApi(); const { allAccounts } = useAccounts(); - const [batchSize, setBatchSize] = useState(Math.floor(options?.batchSize || 64)); + const [batchSize, setBatchSize] = useState(() => Math.floor(options?.max || 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 + .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 ) - .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) + ? createBatches(api, txs, batchSize, options?.type) : null, [api, batchSize, options, txs] );