Check for api.call.transactionPaymentApi for fees (#7996)
This commit is contained in:
parent
8f7cf2799e
commit
08ea57ee24
@ -12,7 +12,7 @@ import { checkAddress } from '@polkadot/phishing';
|
||||
import { InputAddress, InputBalance, MarkError, MarkWarning, Modal, Toggle, TxButton } from '@polkadot/react-components';
|
||||
import { useApi, useCall } from '@polkadot/react-hooks';
|
||||
import { Available } from '@polkadot/react-query';
|
||||
import { BN_HUNDRED, BN_ZERO, isFunction } from '@polkadot/util';
|
||||
import { BN_HUNDRED, BN_ZERO, isFunction, nextTick } from '@polkadot/util';
|
||||
|
||||
import { useTranslation } from '../translate';
|
||||
|
||||
@ -58,27 +58,23 @@ function Transfer ({ className = '', onClose, recipientId: propRecipientId, send
|
||||
const fromId = propSenderId || senderId as string;
|
||||
const toId = propRecipientId || recipientId as string;
|
||||
|
||||
if (balances && balances.accountId?.eq(fromId) && fromId && toId && isFunction(api.rpc.payment?.queryInfo)) {
|
||||
setTimeout((): void => {
|
||||
if (balances && balances.accountId?.eq(fromId) && fromId && toId && api.call.transactionPaymentApi && api.tx.balances) {
|
||||
nextTick(async (): Promise<void> => {
|
||||
try {
|
||||
api.tx.balances
|
||||
?.transfer(toId, balances.availableBalance)
|
||||
.paymentInfo(fromId)
|
||||
.then(({ partialFee }): void => {
|
||||
const adjFee = partialFee.muln(110).div(BN_HUNDRED);
|
||||
const maxTransfer = balances.availableBalance.sub(adjFee);
|
||||
const extrinsic = api.tx.balances.transfer(toId, balances.availableBalance);
|
||||
const { partialFee } = await extrinsic.paymentInfo(fromId);
|
||||
const adjFee = partialFee.muln(110).div(BN_HUNDRED);
|
||||
const maxTransfer = balances.availableBalance.sub(adjFee);
|
||||
|
||||
setMaxTransfer(
|
||||
maxTransfer.gt(api.consts.balances?.existentialDeposit)
|
||||
? [maxTransfer, false]
|
||||
: [null, true]
|
||||
);
|
||||
})
|
||||
.catch(console.error);
|
||||
setMaxTransfer(
|
||||
api.consts.balances && maxTransfer.gt(api.consts.balances.existentialDeposit)
|
||||
? [maxTransfer, false]
|
||||
: [null, true]
|
||||
);
|
||||
} catch (error) {
|
||||
console.error((error as Error).message);
|
||||
console.error(error);
|
||||
}
|
||||
}, 0);
|
||||
});
|
||||
} else {
|
||||
setMaxTransfer([null, false]);
|
||||
}
|
||||
|
||||
@ -8,7 +8,7 @@ import type { BatchOptions, BatchType } from './types';
|
||||
|
||||
import { useEffect, useMemo, useState } from 'react';
|
||||
|
||||
import { isFunction } from '@polkadot/util';
|
||||
import { isFunction, nextTick } from '@polkadot/util';
|
||||
|
||||
import { createNamedHook } from './createNamedHook';
|
||||
import { useAccounts } from './useAccounts';
|
||||
@ -46,26 +46,28 @@ function useTxBatchImpl (txs?: SubmittableExtrinsic<'promise'>[] | null | false,
|
||||
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) =>
|
||||
txs && txs.length && allAccounts[0] && api.call.transactionPaymentApi &&
|
||||
nextTick(async (): Promise<void> => {
|
||||
try {
|
||||
const { weight } = await txs[0].paymentInfo(allAccounts[0]);
|
||||
const maxBlock = api.consts.system.blockWeights
|
||||
? api.consts.system.blockWeights.maxBlock
|
||||
: api.consts.system.maximumBlockWeight as Weight;
|
||||
|
||||
setBatchSize((prev) =>
|
||||
info.weight.isZero()
|
||||
weight.isZero()
|
||||
? prev
|
||||
: Math.floor(
|
||||
(
|
||||
api.consts.system.blockWeights
|
||||
? api.consts.system.blockWeights.maxBlock
|
||||
: api.consts.system.maximumBlockWeight as Weight
|
||||
)
|
||||
maxBlock
|
||||
.muln(64) // 65% of the block weight on a single extrinsic (64 for safety)
|
||||
.div(info.weight)
|
||||
.div(weight)
|
||||
.toNumber() / 100
|
||||
)
|
||||
)
|
||||
)
|
||||
.catch(console.error);
|
||||
);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
});
|
||||
}, [allAccounts, api, options, txs]);
|
||||
|
||||
return useMemo(
|
||||
|
||||
@ -6,7 +6,7 @@ import type { BN } from '@polkadot/util';
|
||||
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
import { BN_ZERO, isFunction } from '@polkadot/util';
|
||||
import { BN_ZERO, nextTick } from '@polkadot/util';
|
||||
|
||||
import { createNamedHook } from './createNamedHook';
|
||||
import { useApi } from './useApi';
|
||||
@ -23,11 +23,17 @@ function useWeightImpl (call?: Call | null): [BN, number] {
|
||||
const [state, setState] = useState(EMPTY_STATE);
|
||||
|
||||
useEffect((): void => {
|
||||
if (call && isFunction(api.rpc.payment?.queryInfo)) {
|
||||
api.tx(call)
|
||||
.paymentInfo(ZERO_ACCOUNT)
|
||||
.then(({ weight }) => mountedRef.current && setState([weight, call.encodedLength]))
|
||||
.catch(console.error);
|
||||
if (call && api.call.transactionPaymentApi) {
|
||||
nextTick(async (): Promise<void> => {
|
||||
try {
|
||||
const extrinsic = api.tx(call);
|
||||
const { weight } = await extrinsic.paymentInfo(ZERO_ACCOUNT);
|
||||
|
||||
mountedRef.current && setState([weight, call.encodedLength]);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
setState(EMPTY_STATE);
|
||||
}
|
||||
|
||||
@ -11,7 +11,7 @@ import { Trans } from 'react-i18next';
|
||||
|
||||
import { Expander, MarkWarning } from '@polkadot/react-components';
|
||||
import { useApi, useCall, useIsMountedRef } from '@polkadot/react-hooks';
|
||||
import { formatBalance, isFunction } from '@polkadot/util';
|
||||
import { formatBalance, nextTick } from '@polkadot/util';
|
||||
|
||||
import { useTranslation } from './translate';
|
||||
|
||||
@ -32,17 +32,16 @@ function PaymentInfo ({ accountId, className = '', extrinsic }: Props): React.Re
|
||||
const mountedRef = useIsMountedRef();
|
||||
|
||||
useEffect((): void => {
|
||||
accountId && extrinsic && isFunction(extrinsic.paymentInfo) && isFunction(api.rpc.payment?.queryInfo) &&
|
||||
setTimeout((): void => {
|
||||
accountId && extrinsic && api.call.transactionPaymentApi &&
|
||||
nextTick(async (): Promise<void> => {
|
||||
try {
|
||||
extrinsic
|
||||
.paymentInfo(accountId)
|
||||
.then((info) => mountedRef.current && setDispatchInfo(info))
|
||||
.catch(console.error);
|
||||
const info = await extrinsic.paymentInfo(accountId);
|
||||
|
||||
mountedRef.current && setDispatchInfo(info);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}, 0);
|
||||
});
|
||||
}, [api, accountId, extrinsic, mountedRef]);
|
||||
|
||||
if (!dispatchInfo || !extrinsic) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user