Hooks
useSendTransaction

useSendTransaction

Sign and send a transaction using the connected wallet.

Import

import { useSendTransaction } from '@wankmi/wankmi'

Usage

import { useSendTransaction, useConnection, useWallet } from '@wankmi/wankmi'
import {
  Transaction,
  SystemProgram,
  PublicKey,
  LAMPORTS_PER_SOL,
} from '@solana/web3.js'
 
function SendSOL() {
  const { connection } = useConnection()
  const { publicKey } = useWallet()
  const { sendTransaction, isPending, isSuccess, isError } = useSendTransaction()
 
  async function handleSend() {
    if (!publicKey) return
 
    const { blockhash } = await connection.getLatestBlockhash()
 
    const transaction = new Transaction().add(
      SystemProgram.transfer({
        fromPubkey: publicKey,
        toPubkey: new PublicKey('...recipient...'),
        lamports: 0.01 * LAMPORTS_PER_SOL,
      })
    )
    transaction.recentBlockhash = blockhash
    transaction.feePayer = publicKey
 
    sendTransaction(transaction)
  }
 
  return (
    <div>
      <button onClick={handleSend} disabled={isPending}>
        {isPending ? 'Sending...' : 'Send 0.01 SOL'}
      </button>
      {isSuccess && <p>Transaction sent!</p>}
      {isError && <p>Transaction failed.</p>}
    </div>
  )
}

Return Value

PropertyTypeDescription
sendTransaction(tx: Transaction) => voidCall this with a built Transaction to sign and send it
isPendingbooleanTrue while the transaction is being signed or confirmed
isSuccessbooleanTrue after the transaction is confirmed
isErrorbooleanTrue if signing or sending failed
errorError | nullThe error, if any
datastring | undefinedThe transaction signature on success
reset() => voidReset the mutation state
⚠️

You are responsible for building the Transaction object (including recentBlockhash and feePayer) before passing it to sendTransaction.

Notes

  • The transaction is signed by the connected wallet's adapter before being submitted.
  • Uses TanStack Query's useMutation internally — isPending, isSuccess, and isError behave exactly as documented in the TanStack Query mutation docs (opens in a new tab).