@frak-labs/react-sdk
Version:
React SDK of the Frak wallet, low level library to interact directly with the frak ecosystem.
63 lines (57 loc) • 2.26 kB
text/typescript
import type { SendTransactionReturnType } from "@frak-labs/core-sdk";
import {
type SendTransactionParams,
sendTransaction,
} from "@frak-labs/core-sdk/actions";
import { ClientNotFound, type FrakRpcError } from "@frak-labs/frame-connector";
import { type UseMutationOptions, useMutation } from "@tanstack/react-query";
import { useFrakClient } from "./useFrakClient";
/** @ignore */
type MutationOptions = Omit<
UseMutationOptions<
SendTransactionReturnType,
FrakRpcError,
SendTransactionParams
>,
"mutationFn" | "mutationKey"
>;
/** @inline */
interface UseSendTransactionParams {
/**
* Optional mutation options, see {@link @tanstack/react-query!useMutation | `useMutation()`} for more infos
*/
mutations?: MutationOptions;
}
/**
* Hook that return a mutation helping to send a transaction
*
* It's a {@link @tanstack/react-query!home | `tanstack`} wrapper around the {@link @frak-labs/core-sdk!actions.sendTransaction | `sendTransaction()`} action
*
* @param args - Optional config object with `mutations` for customizing the underlying {@link @tanstack/react-query!useMutation | `useMutation()`}
*
* @group hooks
*
* @returns
* The mutation hook wrapping the `sendTransaction()` action
* The `mutate` and `mutateAsync` argument is of type {@link @frak-labs/core-sdk!actions.SendTransactionParams | `SendTransactionParams`}
* The `data` result is a {@link @frak-labs/core-sdk!index.SendTransactionReturnType | `SendTransactionReturnType`}
*
* @see {@link @frak-labs/core-sdk!actions.sendTransaction | `sendTransaction()`} for more info about the underlying action
* @see {@link @tanstack/react-query!useMutation | `useMutation()`} for more info about the mutation options and response
*/
export function useSendTransactionAction({
mutations,
}: UseSendTransactionParams = {}) {
const client = useFrakClient();
return useMutation({
...mutations,
mutationKey: ["frak-sdk", "send-transaction"],
mutationFn: async (params: SendTransactionParams) => {
if (!client) {
throw new ClientNotFound();
}
// Send the transaction
return sendTransaction(client, params);
},
});
}