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
| Property | Type | Description |
|---|---|---|
sendTransaction | (tx: Transaction) => void | Call this with a built Transaction to sign and send it |
isPending | boolean | True while the transaction is being signed or confirmed |
isSuccess | boolean | True after the transaction is confirmed |
isError | boolean | True if signing or sending failed |
error | Error | null | The error, if any |
data | string | undefined | The transaction signature on success |
reset | () => void | Reset 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
useMutationinternally —isPending,isSuccess, andisErrorbehave exactly as documented in the TanStack Query mutation docs (opens in a new tab).