useTx

Creates and manages general Substrate transactions with improved type safety. Provides functions to sign and send transactions, estimate fees, and track transaction progress.

Props

Name
Type
Description

txBuilder

(tx) => TxFunction

Callback function that receives the tx object and returns a transaction method (e.g., (tx) => tx.system.remark).

options

NetworkOptions?

Optional network selection options (e.g., { networkId: 'polkadot' }).

Return Type

Name
Type
Description

signAndSend

(params?) => Promise<void>

Function to sign and send the transaction. Accepts { args?, txOptions?, callback? }.

getEstimatedFee

(params?) => Promise<bigint>

Function to estimate the transaction fee. Accepts { args?, txOptions? }.

inProgress

boolean

Whether a transaction is currently in progress (from signing to finalization).

inBestBlockProgress

boolean

Whether the transaction has been included in the best block.

Basic Usage

System remark transaction:

import { useTx } from 'typink';

function RemarkExample() {
  const remarkTx = useTx((tx) => tx.system.remark);

  const handleSend = async () => {
    await remarkTx.signAndSend({
      args: ['Hello from Typink!'],
      callback: (result) => {
        console.log('Transaction status:', result.status.type);
      },
    });
  };

  return (
    <button
      onClick={handleSend}
      disabled={remarkTx.inBestBlockProgress}>
      {remarkTx.inBestBlockProgress ? 'Sending...' : 'Send Remark'}
    </button>
  );
}

Balance transfer transaction:

function TransferExample() {
  const transferTx = useTx((tx) => tx.balances.transferKeepAlive);

  const handleTransfer = async () => {
    const recipient = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY';
    const amount = 1000000000000n; // 1 DOT (10 decimals)

    await transferTx.signAndSend({
      args: [recipient, amount],
      callback: (result) => {
        if (result.status.type === 'BestChainBlockIncluded') {
          console.log('Transfer included in best block');
        }
        if (result.status.type === 'Finalized') {
          console.log('Transfer finalized');
        }
      },
    });
  };

  return (
    <button
      onClick={handleTransfer}
      disabled={transferTx.inProgress}>
      Transfer
    </button>
  );
}

Last updated

Was this helpful?