Skip to content

useSendTransaction

The useSendTransaction hook enables sending a transaction as a UserOperation on behalf of the user's Embedded Account.

WARNING

This requires your user to be logged in. See useAuthenticate for more details.

Import

ts
import { useSendTransaction } from "@alchemy/aa-alchemy/react";

Usage

tsx
import {
  useSendTransaction,
  useSmartAccountClient,
} from "@alchemy/aa-alchemy/react";
import { sepolia } from "@alchemy/aa-core";

export function ComponentWithSendTransaction() {
  /**
   * Assumes the app has context of a signer with an authenticated user
   * by using the `AlchemyAccountProvider` from `@alchemy/aa-alchemy/react`.
   */
  const { client } = useSmartAccountClient({
    type: "MultiOwnerModularAccount",
  });
  const { sendTransaction, isSendingTransaction } = useSendTransaction({
    client,
    onSuccess: (hash) => {
      // [optional] Do something with the hash
    },
    onError: (error) => {
      // [optional] Do something with the error
    },
    // [optional] ...additional mutationArgs
  });

  return (
    <div>
      <button
        onClick={() =>
          sendTransaction({
            to: "0xTARGET_ADDRESS",
            data: "0x",
            value: 0n,
            account: "0xACCOUNT_ADDRESS",
            chain: sepolia,
            // ... other parameters
          })
        }
        disabled={isSendingTransaction}
      >
        {isSendingTransaction ? "Sending..." : "Send Txn"}
      </button>
    </div>
  );
}

Params

client

AlchemySmartAccountClient | undefined A AlchemySmartAccountClient with methods to interact with an Alchemy smart account.

...mutationArgs

You can optionally specify any of the React Query useMutation parameters as parameters to this hook.

Return Type

ts
import { type UseSendTransaction } from "@alchemy/aa-alchemy/react";

Returns an object containing the following state.

sendTransaction

UseMutateFunction A React query mutation function to send a transaction as a UserOperation. It returns a SendTransactionResult object.

SendTransactionResult
ts
import type { Address } from "abitype";
import type { Hash, Hex } from "viem";
import type { z } from "zod";
import type { UserOperationContext } from "../actions/smartAccount/types.js";
import type { EntryPointVersion } from "../entrypoint/types.js";
import type {
  ClientMiddleware,
  ClientMiddlewareFn,
} from "../middleware/types.js";
import type { UserOperationRequest } from "../types.js";
import type { ConnectionConfigSchema } from "./schema.js";

export type ConnectorData = {
  chainId?: Hex;
};

export type ConnectionConfig = z.input<typeof ConnectionConfigSchema>;

//#region SendUserOperationResult
export type SendUserOperationResult<
  TEntryPointVersion extends EntryPointVersion = EntryPointVersion
> = {
  hash: Hash;
  request: UserOperationRequest<TEntryPointVersion>;
};
//#endregion SendUserOperationResult

//#region UpgradeToData
export type UpgradeToData = {
  implAddress: Address;
  initializationData: Hex;
};
//#endregion UpgradeToData

//#region ClientMiddlewareConfig
export type ClientMiddlewareConfig<
  TContext extends UserOperationContext | undefined =
    | UserOperationContext
    | undefined
> = Omit<
  Partial<ClientMiddleware<TContext>>,
  "dummyPaymasterAndData" | "paymasterAndData"
> & {
  paymasterAndData?: {
    dummyPaymasterAndData: () =>
      | UserOperationRequest<"0.6.0">["paymasterAndData"]
      | Pick<UserOperationRequest<"0.7.0">, "paymaster" | "paymasterData">;
    paymasterAndData: ClientMiddlewareFn<TContext>;
  };
};
//#endregion ClientMiddlewareConfig

It takes in SendTransactionParameters which has this type from viem.

sendTransactionAsync

UseMutateAsyncFunction A React query async mutation function to send a transaction as a UserOperation. Via an awaitable promise, it returns a SendTransactionResult object shown above.

sendTransactionResult

An transaction hash if the mutation has run successfully, undefined otherwise.

isSendingTransaction

boolean A flag that determines whether the mutation is still running or not.

error

Error | null A field that relays any errors from the mutation. It is null if there is no error.