Skip to content

useSendTransaction

The useSendTransaction hook provides functionality to send transactions to the blockchain with comprehensive status tracking and error handling.

Import

tsx
import { useSendTransaction } from '@luno-kit/react'

Usage

Basic Usage

tsx
import { useSendTransaction } from '@luno-kit/react'

function SendTransactionButton() {
  const { api } = useApi();
  const { sendTransaction, isPending, error, detailedStatus } = useSendTransaction()

  const handleSend = () => {
    const transferExtrinsic = api.tx.balances.transferKeepAlive(to, amount)

    sendTransaction({
      extrinsic: transferExtrinsic
    })
  }

  return (
    <div>
      <button onClick={handleSend} disabled={isPending}>
        {'Send Transaction'}
      </button>
      {error && <p>Error: {error.message}</p>}
      <p>Status: {detailedStatus}</p>
    </div>
  )
}
ts
import { createConfig, kusama, polkadot, polkadotjsConnector } from '@luno-kit/react'

const config = createConfig({
  appName: 'luno with-vite example',
  chains: [polkadot, kusama],
  connectors: [polkadotjsConnector()],
  autoConnect: true,
});

Async Usage with Callbacks

tsx
import { useSendTransaction } from '@luno-kit/react'

function AsyncTransactionButton() {
  const { api } = useApi();
  const { sendTransactionAsync, isPending } = useSendTransaction()

  const handleSendAsync = async () => {
    try {
      const transferExtrinsic = api.tx.balances.transferKeepAlive(to, amount)

      const receipt = await sendTransactionAsync({
        extrinsic: transferExtrinsic
      })

      if (receipt.status === 'success') {
        console.log('Transaction successful:', receipt.transactionHash)
      } else {
        console.log('Transaction failed:', receipt.errorMessage)
      }
    } catch (error) {
      console.error('Transaction error:', error)
    }
  }

  return (
    <button onClick={handleSendAsync} disabled={isPending}>
      Send Transaction (Async)
    </button>
  )
}
ts
import { createConfig, kusama, polkadot, polkadotjsConnector } from '@luno-kit/react'

const config = createConfig({
  appName: 'luno with-vite example',
  chains: [polkadot, kusama],
  connectors: [polkadotjsConnector()],
  autoConnect: true,
});

Parameters

The hook accepts an optional configuration object:

ParameterTypeRequiredDescription
onSuccess(data: TransactionReceipt) => voidNoCallback when transaction succeeds
onError(error: Error) => voidNoCallback when transaction fails
onSettled() => voidNoCallback when transaction completes (success or failure)

Return Value

The hook returns an object with the following properties:

Methods

These are the functions you can call to send transactions.

PropertyTypeDescription
sendTransaction(variables: { extrinsic: ISubmittableExtrinsic }, options?: { onSuccess?: () => void; onError?: (error: Error) => void; onSettled?: () => void }) => voidSend transaction synchronously
sendTransactionAsync(variables: { extrinsic: ISubmittableExtrinsic }, options?: { onSuccess?: () => void; onError?: (error: Error) => void; onSettled?: () => void }) => Promise<TransactionReceipt>Send transaction asynchronously
reset() => voidReset the transaction state

Data & State

These properties provide information about the current state of the transaction.

PropertyTypeDescription
dataTransactionReceipt | undefinedTransaction receipt after completion
errorError | nullTransaction error if any
isErrorbooleanWhether there's an error
isIdlebooleanWhether the hook is idle
isPendingbooleanWhether transaction is in progress
isSuccessbooleanWhether transaction was successful
status'idle' | 'pending' | 'error' | 'success'Overall transaction status
variablesSendTransactionVariables | undefinedLast used transaction variables
txStatusTxStatusHigh-level transaction status
detailedStatusDetailedTxStatusDetailed transaction status

SendTransactionVariables

tsx
interface SendTransactionVariables {
  extrinsic: ISubmittableExtrinsic;  // The transaction to send
}

TransactionReceipt

tsx
interface TransactionReceipt {
  transactionHash: HexString;         // Transaction hash, `0x${string}`
  blockHash: HexString;               // Block hash where transaction was included
  blockNumber?: number;               // Block number
  readonly events: EventRecord[];     // Events emitted during transaction
  status: 'failed' | 'success';      // Transaction result
  dispatchError?: DispatchError;      // Dispatch error if failed
  errorMessage?: string;              // Human-readable error message
  dispatchInfo?: DispatchInfo;        // Dispatch information
}

TxStatus

tsx
type TxStatus = 'idle' | 'signing' | 'success' | 'failed';

DetailedTxStatus

tsx
type DetailedTxStatus = 'idle' | 'broadcasting' | 'inBlock' | 'finalized' | 'invalid' | 'dropped';

WARNING

In Polkadot, transaction hashes are not guaranteed to be unique across the entire chain, although they are generally unique within a block. This means you cannot reliably query transaction information by hash alone. For more information, refer to the Polkadot.js documentation.

Released under the MIT License.