Skip to content
Docs
Prepare Hooks
usePrepareSendTransaction

usePrepareSendTransaction

Hook for preparing a transaction to be sent via useSendTransaction.

Eagerly fetches the parameters required for sending a transaction such as the gas estimate and resolving an ENS address (if required).

import { usePrepareSendTransaction } from 'wagmi'

Usage

usePrepareSendTransaction gives back a "prepared config" to be sent through to useSendTransaction.

import { usePrepareSendTransaction, useSendTransaction } from 'wagmi'

function App() {
  const { config, error } = usePrepareSendTransaction({
    request: {
      to: 'moxey.eth',
      value: parseEther('1'),
    },
  })
  const { sendTransaction } = useSendTransaction(config)

  return (
    <>
      <button disabled={!sendTransaction} onClick={() => sendTransaction?.()}>
        Send Transaction
      </button>
      {error && (
        <div>An error occurred preparing the transaction: {error.message}</div>
      )}
    </>
  )
}

Note: The sendTransaction function will be undefined if the request has not been prepared (still in-flight or errored).

Return value

{
  data?: PrepareSendTransactionResult
  error?: Error
  isIdle: boolean
  isLoading: boolean
  isFetching: boolean
  isSuccess: boolean
  isError: boolean
  isFetched: boolean
  isRefetching: boolean
  refetch: (options: {
    throwOnError: boolean
    cancelRefetch: boolean
  }) => Promise<PrepareSendTransactionResult>
  status: 'idle' | 'error' | 'loading' | 'success'
}

Configuration

request

Request data to prepare the transaction. See TransactionRequest for more info.

import { usePrepareSendTransaction } from 'wagmi'

function App() {
  const { config } = usePrepareSendTransaction({
    request: {
      to: 'awkweb.eth',
      value: parseEther('1'), // 1 ETH
    },
  })
}

cacheTime (optional)

Time (in ms) which the data should remain in the cache.

import { usePrepareSendTransaction } from 'wagmi'

function App() {
  const { config } = usePrepareSendTransaction({
    request: {
      to: 'awkweb.eth',
      value: parseEther('1'), // 1 ETH
    },
    cacheTime: 2_000,
  })
}

enabled (optional)

Set this to false to disable this query from automatically running. Defaults to true.

import { usePrepareSendTransaction } from 'wagmi'

function App() {
  const { config } = usePrepareSendTransaction({
    request: {
      to: 'awkweb.eth',
      value: parseEther('1'), // 1 ETH
    },
    enabled: false,
  })
}

staleTime (optional)

Time (in ms) after data is considered stale. If set to Infinity the data will never be considered stale.

import { usePrepareSendTransaction } from 'wagmi'

function App() {
  const { config } = usePrepareSendTransaction({
    request: {
      to: 'awkweb.eth',
      value: parseEther('1'), // 1 ETH
    },
    staleTime: 2_000,
  })
}

suspense (optional)

Set this to true to enable suspense mode.

import { usePrepareSendTransaction } from 'wagmi'

function App() {
  const { config } = usePrepareSendTransaction({
    request: {
      to: 'awkweb.eth',
      value: parseEther('1'), // 1 ETH
    },
    suspense: true,
  })
}

onSuccess (optional)

Function to invoke when fetching new data is successful.

import { usePrepareSendTransaction } from 'wagmi'

function App() {
  const { config } = usePrepareSendTransaction({
    request: {
      to: 'awkweb.eth',
      value: parseEther('1'), // 1 ETH
    },
    onSuccess(data) {
      console.log('Success', data)
    },
  })
}

onError (optional)

Function to invoke when an error is thrown while fetching new data.

import { usePrepareSendTransaction } from 'wagmi'

function App() {
  const { config } = usePrepareSendTransaction({
    request: {
      to: 'awkweb.eth',
      value: parseEther('1'), // 1 ETH
    },
    onError(error) {
      console.log('Error', error)
    },
  })
}

onSettled (optional)

Function to invoke when fetching is settled (either successfully fetched, or an error has thrown).

import { usePrepareSendTransaction } from 'wagmi'

function App() {
  const { config } = usePrepareSendTransaction({
    request: {
      to: 'awkweb.eth',
      value: parseEther('1'), // 1 ETH
    },
    onSettled(data, error) {
      console.log('Settled', { data, error })
    },
  })
}