Adjust options for useTxBatch (#7311)
This commit is contained in:
parent
12c132a515
commit
0e54bb3643
@ -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<SubmittableExtrinsic<'promise'>[] | null>(null);
|
||||
const [previous, setPrevious] = useState<PrevProxy[]>(() => existing.map(({ delegate, proxyType }) => [delegate, proxyType]));
|
||||
const [added, setAdded] = useState<PrevProxy[]>([]);
|
||||
const extrinsics = useTxBatch(txs, optTxBatch);
|
||||
const extrinsics = useTxBatch(txs, BATCH_OPTS);
|
||||
|
||||
const reservedAmount = useMemo(
|
||||
() => api.consts.proxy.proxyDepositFactor
|
||||
|
||||
@ -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<Props> {
|
||||
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 (
|
||||
<Modal
|
||||
|
||||
@ -5,7 +5,7 @@ import type { SubmittableExtrinsic } from '@polkadot/api/types';
|
||||
import type { EraIndex } from '@polkadot/types/interfaces';
|
||||
import type { PayoutValidator } from './types';
|
||||
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import React, { useEffect, useMemo, useState } from 'react';
|
||||
import styled from 'styled-components';
|
||||
|
||||
import { ApiPromise } from '@polkadot/api';
|
||||
@ -60,7 +60,13 @@ function PayButton ({ className, isAll, isDisabled, payout }: Props): React.Reac
|
||||
const [isVisible, togglePayout] = useToggle();
|
||||
const [accountId, setAccount] = useState<string | null>(null);
|
||||
const [txs, setTxs] = useState<SubmittableExtrinsic<'promise'>[] | 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(
|
||||
|
||||
@ -159,3 +159,10 @@ export interface Judgement {
|
||||
}
|
||||
|
||||
export type UseJudgements = Judgement[]
|
||||
|
||||
export type BatchType = 'all' | 'default';
|
||||
|
||||
export interface BatchOptions {
|
||||
max?: number;
|
||||
type?: BatchType;
|
||||
}
|
||||
|
||||
@ -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]
|
||||
);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user