Extract batch creation into hook (#5005)
* Extract batch creation into hook * Adjust * Adjustfallback weight (just in-case...) * Batch
This commit is contained in:
parent
73bcc477ad
commit
7c9aba6e4d
@ -10,8 +10,8 @@ import React, { useCallback, useEffect, useRef, useState } from 'react';
|
||||
import styled from 'styled-components';
|
||||
|
||||
import { BatchWarning, Button, Dropdown, InputAddress, MarkError, Modal, TxButton } from '@polkadot/react-components';
|
||||
import { useApi } from '@polkadot/react-hooks';
|
||||
import { BN_ZERO, isFunction } from '@polkadot/util';
|
||||
import { useApi, useTxBatch } from '@polkadot/react-hooks';
|
||||
import { BN_ZERO } from '@polkadot/util';
|
||||
|
||||
import { useTranslation } from '../translate';
|
||||
|
||||
@ -41,20 +41,10 @@ interface PrevProxyProps extends ValueProps {
|
||||
onRemove: (accountId: AccountId, type: ProxyType, index: number) => void;
|
||||
}
|
||||
|
||||
const optTxBatch = { isBatchAll: true };
|
||||
|
||||
const EMPTY_EXISTING: [ProxyDefinition[], BN] = [[], BN_ZERO];
|
||||
|
||||
function createExtrinsic (api: ApiPromise, batchPrevious: SubmittableExtrinsic<'promise'>[], batchAdded: SubmittableExtrinsic<'promise'>[]): SubmittableExtrinsic<'promise'> | null {
|
||||
if (batchPrevious.length + batchAdded.length === 1) {
|
||||
return batchPrevious.length
|
||||
? batchPrevious[0]
|
||||
: batchAdded[0];
|
||||
}
|
||||
|
||||
return isFunction(api.tx.utility.batchAll)
|
||||
? api.tx.utility.batchAll([...batchPrevious, ...batchAdded])
|
||||
: api.tx.utility.batch([...batchPrevious, ...batchAdded]);
|
||||
}
|
||||
|
||||
function createAddProxy (api: ApiPromise, account: AccountId, type: ProxyType, delay = 0): SubmittableExtrinsic<'promise'> {
|
||||
return api.tx.proxy.addProxy.meta.args.length === 2
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
@ -161,11 +151,12 @@ function ProxyOverview ({ className, onClose, previousProxy: [existing] = EMPTY_
|
||||
const { api } = useApi();
|
||||
const [batchPrevious, setBatchPrevious] = useState<SubmittableExtrinsic<'promise'>[]>([]);
|
||||
const [batchAdded, setBatchAdded] = useState<SubmittableExtrinsic<'promise'>[]>([]);
|
||||
const [extrinsic, setExtrinsic] = useState<SubmittableExtrinsic<'promise'> | null>(null);
|
||||
const [txs, setTxs] = useState<SubmittableExtrinsic<'promise'>[] | null>(null);
|
||||
const [previous, setPrevious] = useState<PrevProxy[]>(
|
||||
existing.map(({ delegate, proxyType }): [AccountId, ProxyType] => [delegate, proxyType])
|
||||
);
|
||||
const [added, setAdded] = useState<PrevProxy[]>([]);
|
||||
const extrinsics = useTxBatch(txs, optTxBatch);
|
||||
|
||||
const typeOpts = useRef(
|
||||
api.createType('ProxyType').defKeys.map((text, value) => ({ text, value }))
|
||||
@ -178,9 +169,8 @@ function ProxyOverview ({ className, onClose, previousProxy: [existing] = EMPTY_
|
||||
}, [api, added]);
|
||||
|
||||
useEffect((): void => {
|
||||
(batchPrevious.length || batchAdded.length) &&
|
||||
setExtrinsic(() => createExtrinsic(api, batchPrevious, batchAdded));
|
||||
}, [api, batchPrevious, batchAdded]);
|
||||
setTxs(() => [...batchPrevious, ...batchAdded]);
|
||||
}, [batchPrevious, batchAdded]);
|
||||
|
||||
const _addProxy = useCallback(
|
||||
() => setAdded((added) =>
|
||||
@ -295,7 +285,7 @@ function ProxyOverview ({ className, onClose, previousProxy: [existing] = EMPTY_
|
||||
)}
|
||||
<TxButton
|
||||
accountId={proxiedAccount}
|
||||
extrinsic={extrinsic}
|
||||
extrinsic={extrinsics}
|
||||
icon='sign-in-alt'
|
||||
isDisabled={isSameAdd || (!batchPrevious.length && !batchAdded.length)}
|
||||
onStart={onClose}
|
||||
|
||||
@ -1,15 +1,13 @@
|
||||
// Copyright 2017-2021 @polkadot/app-parachains authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { ApiPromise } from '@polkadot/api';
|
||||
import type { SubmittableExtrinsic } from '@polkadot/api/types';
|
||||
import type { ParaId } from '@polkadot/types/interfaces';
|
||||
|
||||
import React, { useEffect, useMemo, useState } from 'react';
|
||||
|
||||
import { Button, Dropdown, InputAddress, Modal, TxButton } from '@polkadot/react-components';
|
||||
import { useAccounts, useApi, useToggle } from '@polkadot/react-hooks';
|
||||
import { isFunction } from '@polkadot/util';
|
||||
import { useAccounts, useApi, useToggle, useTxBatch } from '@polkadot/react-hooks';
|
||||
|
||||
import { useTranslation } from '../translate';
|
||||
|
||||
@ -20,32 +18,7 @@ interface Props {
|
||||
paraId: ParaId;
|
||||
}
|
||||
|
||||
function createExtrinsics (api: ApiPromise, addresses: string[], paraId: ParaId, batchSize: number): SubmittableExtrinsic<'promise'>[] {
|
||||
return addresses
|
||||
.reduce((batches: SubmittableExtrinsic<'promise'>[][], address): SubmittableExtrinsic<'promise'>[][] => {
|
||||
const tx = api.tx.crowdloan.withdraw(address, paraId);
|
||||
const batch = batches[batches.length - 1];
|
||||
|
||||
if (batch.length >= batchSize) {
|
||||
batches.push([tx]);
|
||||
} else {
|
||||
batch.push(tx);
|
||||
}
|
||||
|
||||
return batches;
|
||||
}, [[]])
|
||||
.reduce((extrinsics: SubmittableExtrinsic<'promise'>[], batch): SubmittableExtrinsic<'promise'>[] => {
|
||||
if (batch.length === 1) {
|
||||
extrinsics.push(batch[0]);
|
||||
} else if (isFunction(api.tx.utility?.batch)) {
|
||||
extrinsics.push(api.tx.utility.batch(batch));
|
||||
} else {
|
||||
extrinsics.push(...batch);
|
||||
}
|
||||
|
||||
return extrinsics;
|
||||
}, []);
|
||||
}
|
||||
const optTxs = { batchSize: 128 };
|
||||
|
||||
function Refund ({ allAccounts, className, myAccounts, paraId }: Props): React.ReactElement<Props> {
|
||||
const { t } = useTranslation();
|
||||
@ -53,9 +26,9 @@ function Refund ({ allAccounts, className, myAccounts, paraId }: Props): React.R
|
||||
const { hasAccounts } = useAccounts();
|
||||
const [isOpen, toggleOpen] = useToggle();
|
||||
const [accountId, setAccountId] = useState<string | null>(null);
|
||||
const [extrinsics, setExtrinsics] = useState<SubmittableExtrinsic<'promise'>[] | null>(null);
|
||||
const [batchSize, setBatchSize] = useState(0);
|
||||
const [txs, setTxs] = useState<SubmittableExtrinsic<'promise'>[] | null>(null);
|
||||
const [withdrawType, setWithdrawType] = useState(0);
|
||||
const extrinsics = useTxBatch(txs, optTxs);
|
||||
|
||||
const typeOptions = useMemo(
|
||||
() => [
|
||||
@ -71,34 +44,17 @@ function Refund ({ allAccounts, className, myAccounts, paraId }: Props): React.R
|
||||
[myAccounts, t]
|
||||
);
|
||||
|
||||
useEffect((): void => {
|
||||
api.tx.crowdloan
|
||||
.withdraw(allAccounts[0], paraId)
|
||||
.paymentInfo(allAccounts[0])
|
||||
.then((info) => setBatchSize(
|
||||
info.weight.isZero()
|
||||
? 128
|
||||
: Math.floor(
|
||||
api.consts.system.blockWeights.maxBlock
|
||||
.muln(64) // 65% of the block weight on a single extrinsic (64 for safety)
|
||||
.div(info.weight)
|
||||
.toNumber() / 100
|
||||
)
|
||||
))
|
||||
.catch(console.error);
|
||||
}, [api, allAccounts, paraId]);
|
||||
|
||||
useEffect((): void => {
|
||||
const addresses = withdrawType === 0
|
||||
? allAccounts
|
||||
: myAccounts;
|
||||
|
||||
setExtrinsics(() =>
|
||||
batchSize && addresses.length
|
||||
? createExtrinsics(api, addresses, paraId, batchSize)
|
||||
setTxs(() =>
|
||||
addresses.length
|
||||
? addresses.map((a) => api.tx.crowdloan.withdraw(a, paraId))
|
||||
: null
|
||||
);
|
||||
}, [api, allAccounts, batchSize, myAccounts, paraId, withdrawType]);
|
||||
}, [api, allAccounts, myAccounts, paraId, withdrawType]);
|
||||
|
||||
return (
|
||||
<>
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { SubmittableExtrinsic } from '@polkadot/api/types';
|
||||
import type { EraIndex, Weight } from '@polkadot/types/interfaces';
|
||||
import type { EraIndex } from '@polkadot/types/interfaces';
|
||||
import type { PayoutValidator } from './types';
|
||||
|
||||
import React, { useEffect, useState } from 'react';
|
||||
@ -10,7 +10,7 @@ import styled from 'styled-components';
|
||||
|
||||
import { ApiPromise } from '@polkadot/api';
|
||||
import { AddressMini, Button, InputAddress, Modal, Static, TxButton } from '@polkadot/react-components';
|
||||
import { useAccounts, useApi, useToggle } from '@polkadot/react-hooks';
|
||||
import { useApi, useToggle, useTxBatch } from '@polkadot/react-hooks';
|
||||
|
||||
import { useTranslation } from '../translate';
|
||||
|
||||
@ -26,92 +26,47 @@ interface SinglePayout {
|
||||
validatorId: string;
|
||||
}
|
||||
|
||||
function createBatches (api: ApiPromise, maxPayouts: number, payouts: SinglePayout[]): SubmittableExtrinsic<'promise'>[] {
|
||||
function createStream (api: ApiPromise, payouts: SinglePayout[]): SubmittableExtrinsic<'promise'>[] {
|
||||
return payouts
|
||||
.sort((a, b) => a.era.cmp(b.era))
|
||||
.reduce((batches: SubmittableExtrinsic<'promise'>[][], { era, validatorId }): SubmittableExtrinsic<'promise'>[][] => {
|
||||
const tx = api.tx.staking.payoutStakers(validatorId, era);
|
||||
const batch = batches[batches.length - 1];
|
||||
|
||||
if (batch.length >= maxPayouts) {
|
||||
batches.push([tx]);
|
||||
} else {
|
||||
batch.push(tx);
|
||||
}
|
||||
|
||||
return batches;
|
||||
}, [[]])
|
||||
.map((batch) =>
|
||||
batch.length === 1
|
||||
? batch[0]
|
||||
: api.tx.utility.batch(batch)
|
||||
.map(({ era, validatorId }) =>
|
||||
api.tx.staking.payoutStakers(validatorId, era)
|
||||
);
|
||||
}
|
||||
|
||||
function createExtrinsics (api: ApiPromise, payout: PayoutValidator | PayoutValidator[], maxPayouts: number): SubmittableExtrinsic<'promise'>[] | null {
|
||||
if (Array.isArray(payout)) {
|
||||
if (payout.length === 1) {
|
||||
return createExtrinsics(api, payout[0], maxPayouts);
|
||||
}
|
||||
function createExtrinsics (api: ApiPromise, payout: PayoutValidator | PayoutValidator[]): SubmittableExtrinsic<'promise'>[] | null {
|
||||
if (!Array.isArray(payout)) {
|
||||
const { eras, validatorId } = payout;
|
||||
|
||||
return createBatches(api, maxPayouts, payout.reduce((payouts: SinglePayout[], { eras, validatorId }): SinglePayout[] => {
|
||||
eras.forEach(({ era }): void => {
|
||||
payouts.push({ era, validatorId });
|
||||
});
|
||||
|
||||
return payouts;
|
||||
}, []));
|
||||
return eras.length === 1
|
||||
? [api.tx.staking.payoutStakers(validatorId, eras[0].era)]
|
||||
: createStream(api, eras.map((era): SinglePayout => ({ era: era.era, validatorId })));
|
||||
} else if (payout.length === 1) {
|
||||
return createExtrinsics(api, payout[0]);
|
||||
}
|
||||
|
||||
const { eras, validatorId } = payout;
|
||||
return createStream(api, payout.reduce((payouts: SinglePayout[], { eras, validatorId }): SinglePayout[] => {
|
||||
eras.forEach(({ era }): void => {
|
||||
payouts.push({ era, validatorId });
|
||||
});
|
||||
|
||||
return eras.length === 1
|
||||
? [api.tx.staking.payoutStakers(validatorId, eras[0].era)]
|
||||
: createBatches(api, maxPayouts, eras.map((era): SinglePayout => ({ era: era.era, validatorId })));
|
||||
return payouts;
|
||||
}, []));
|
||||
}
|
||||
|
||||
function PayButton ({ className, isAll, isDisabled, payout }: Props): React.ReactElement<Props> {
|
||||
const { t } = useTranslation();
|
||||
const { api } = useApi();
|
||||
const { allAccounts } = useAccounts();
|
||||
const [isVisible, togglePayout] = useToggle();
|
||||
const [accountId, setAccount] = useState<string | null>(null);
|
||||
const [extrinsics, setExtrinsics] = useState<SubmittableExtrinsic<'promise'>[] | null>(null);
|
||||
const [maxPayouts, setMaxPayouts] = useState(0);
|
||||
const [txs, setTxs] = useState<SubmittableExtrinsic<'promise'>[] | null>(null);
|
||||
const extrinsics = useTxBatch(txs, { batchSize: 36 * 64 / (api.consts.staking.maxNominatorRewardedPerValidator?.toNumber() || 64) });
|
||||
|
||||
useEffect((): void => {
|
||||
if (api.tx.utility && allAccounts.length && payout && (!Array.isArray(payout) || payout.length !== 0)) {
|
||||
const { eras, validatorId } = Array.isArray(payout)
|
||||
? payout[0]
|
||||
: payout;
|
||||
|
||||
api.tx.staking
|
||||
.payoutStakers(validatorId, eras[0].era)
|
||||
.paymentInfo(allAccounts[0])
|
||||
.then((info) => setMaxPayouts(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);
|
||||
} else {
|
||||
// at 64 payouts we can fit in 36 (as per tests)
|
||||
setMaxPayouts(Math.floor(
|
||||
36 * 64 / (api.consts.staking.maxNominatorRewardedPerValidator?.toNumber() || 64)
|
||||
));
|
||||
}
|
||||
}, [allAccounts, api, payout]);
|
||||
|
||||
useEffect((): void => {
|
||||
api.tx.utility && payout && setExtrinsics(
|
||||
() => createExtrinsics(api, payout, maxPayouts)
|
||||
payout && setTxs(
|
||||
() => createExtrinsics(api, payout)
|
||||
);
|
||||
}, [api, maxPayouts, payout]);
|
||||
}, [api, payout]);
|
||||
|
||||
const isPayoutEmpty = !payout || (Array.isArray(payout) && payout.length === 0);
|
||||
|
||||
|
||||
@ -41,5 +41,6 @@ export { useStepper } from './useStepper';
|
||||
export { useSudo } from './useSudo';
|
||||
export { useToggle } from './useToggle';
|
||||
export { useTreasury } from './useTreasury';
|
||||
export { useTxBatch } from './useTxBatch';
|
||||
export { useVotingStatus } from './useVotingStatus';
|
||||
export { useWeight } from './useWeight';
|
||||
|
||||
78
packages/react-hooks/src/useTxBatch.ts
Normal file
78
packages/react-hooks/src/useTxBatch.ts
Normal file
@ -0,0 +1,78 @@
|
||||
// Copyright 2017-2020 @polkadot/react-hooks authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { ApiPromise } from '@polkadot/api';
|
||||
import type { SubmittableExtrinsic } from '@polkadot/api/types';
|
||||
import type { Weight } from '@polkadot/types/interfaces';
|
||||
|
||||
import { useEffect, useMemo, useState } from 'react';
|
||||
|
||||
import { isFunction } from '@polkadot/util';
|
||||
|
||||
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'>[] {
|
||||
if (batchSize === 1 || !isFunction(api.tx.utility?.batch)) {
|
||||
return txs;
|
||||
}
|
||||
|
||||
return txs
|
||||
.reduce((batches: SubmittableExtrinsic<'promise'>[][], tx): SubmittableExtrinsic<'promise'>[][] => {
|
||||
const batch = batches[batches.length - 1];
|
||||
|
||||
if (batch.length >= batchSize) {
|
||||
batches.push([tx]);
|
||||
} else {
|
||||
batch.push(tx);
|
||||
}
|
||||
|
||||
return batches;
|
||||
}, [[]])
|
||||
.map((batch): SubmittableExtrinsic<'promise'> =>
|
||||
batch.length === 1
|
||||
? batch[0]
|
||||
: isBatchAll && isFunction(api.tx.utility.batchAll)
|
||||
? api.tx.utility.batchAll(batch)
|
||||
: api.tx.utility.batch(batch)
|
||||
);
|
||||
}
|
||||
|
||||
export function useTxBatch (txs?: SubmittableExtrinsic<'promise'>[] | null, 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 &&
|
||||
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]
|
||||
);
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user