useTxFee

Estimates transaction fees with automatic loading and error state management. Accepts a transaction created with useTx and automatically estimates fees based on the provided arguments.

Props

Name
Type
Description

tx

UseTxReturnType

Transaction instance from useTx hook.

args

any[]

Arguments for the transaction. Required if transaction has parameters.

txOptions

Partial<PayloadOptions>?

Transaction options to include in fee estimation (e.g., tip, nonce).

enabled

boolean?

Whether to automatically fetch the fee estimate. Default: true.

networkId

NetworkId?

Optional network ID to specify which client to use.

Return Type

Name
Type
Description

fee

bigint | null

The estimated fee in the smallest unit (e.g., Planck for Polkadot). null if not available.

isLoading

boolean

Whether fee estimation is in progress.

error

string | null

Error message if estimation failed, null otherwise.

refresh

() => Promise<void>

Function to manually trigger fee estimation.

Basic Usage

Fee estimation with validation:

import { useTx, useTxFee, formatBalance } from 'typink';

function RemarkWithFee() {
  const [message, setMessage] = useState('Hello!');
  const remarkTx = useTx((tx) => tx.system.remark);

  const { fee, isLoading, error } = useTxFee({
    tx: remarkTx,
    args: [message],
    enabled: message.trim().length > 0,
  });

  return (
    <div>
      <input
        value={message}
        onChange={(e) => setMessage(e.target.value)}
        placeholder="Enter message"
      />

      {isLoading && <p>Calculating fee...</p>}
      {error && <p style={{ color: 'red' }}>Error: {error}</p>}
      {fee && <p>Estimated Fee: {formatBalance(fee)}</p>}

      <button onClick={() => remarkTx.signAndSend({ args: [message] })}>
        Send Remark
      </button>
    </div>
  );
}

Conditional fee estimation:

Last updated

Was this helpful?