useSendTransaction
The useSendTransaction
hook enables sending a transaction as a UserOperation on behalf of the user's Embedded Account.
Import
import { useSendTransaction } from "@alchemy/aa-alchemy/react";
Usage
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
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
// [!include ~/../packages/core/src/client/types.ts:SendTransactionResult]
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.