useSendPermission

This hook handles the process of sending permissions and monitors the transaction’s status.

Returns:

Property

Type

Description

data

Hash or undefined

The hash of the transaction if it was successfully sent, otherwise undefined.

error

Error or null

An error object if the transaction failed, otherwise null.

isPending

boolean

A boolean indicating whether the transaction is currently pending.

isSuccess

boolean

A boolean indicating whether the transaction was successfully sent.

isError

boolean

A boolean indicating whether an error occurred during the transaction process.

sendPermission

(variables: UsePermissionParameters) => void

A function to trigger the permission request using a sessionKey.

sendPermissionAsync

(variables: UsePermissionParameters) => Promise<Hash>

A function to trigger the permission request and return a promise resolving to the transaction hash.

Usage Example:

import { useSendPermission } from "path/to/hook";

const Component = () => {
  const {
    data,
    error,
    isPending,
    isSuccess,
    isError,
    sendPermission,
    sendPermissionAsync,
  } = useSendPermission({
    sessionData: ..., 
    privateKey: ...,
  });

  const handleSendPermission = () => {
    sendPermission({
      actions: [
        {
          target: 0x012345...",
          callData: "0x012345...",
          value: BigInt(0),
        },
      ],
    });
  };

  const handleAsyncSendPermission = async () => {
    try {
      const txHash = await sendPermissionAsync({
        actions: [
          {
            target: 0x012345...",
            callData: "0x012345...",
            value: BigInt(0),
          },
        ],
      });
      console.log("Transaction hash:", txHash);
    } catch (err) {
      console.error("Permission sending failed:", err);
    }
  };

  return (
    <div>
      {isPending && <p>Sending permission...</p>}
      {isSuccess && <p>Permission sent successfully!</p>}
      {isError && <p>Error: {error?.message}</p>}
      <button onClick={handleSendPermission}>Send Permission</button>
      <button onClick={handleAsyncSendPermission}>Send Permission (Async)</button>
    </div>
  );
};

How It Works:

The hook relies on the smartAccountClient and sessionKeySigner to handle permission operations via the cometh/connect-sdk-4337. The mutation follows these steps:

1. Initializes the session key signer using useSessionKeySigner.

2. Verifies the required context and API key are available.

3. Creates a sessionKeyClient using createSessionSmartAccountClient.

4. Calls the usePermission method with the provided parameters.

5. Waits for confirmation using waitForUserOperationReceipt.

6. Returns the transaction hash of the operation.

Example of Expected Response:

"0x123abc456def..."

Error Handling:

If the mutation fails, the error object provides information about what went wrong, allowing you to display user-friendly error messages.

Mutation Props (mutationProps):

You can pass optional mutation options through the mutationProps parameter. This allows you to customize aspects of the mutation, like retry mechanisms, onSuccess callbacks, and more.

Last updated