# useGrantPermission

## Returns:

| Property             | Type                                                                                                       | Description                                                                                                                 |
| -------------------- | ---------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------- |
| data                 | GrantPermissionMutateResponse or undefined                                                                 | The response object containing the transaction hash and session details if the mutation is successful, 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.                                              |
| grantPermission      | (variables: GrantPermissionParameters\<ComethSafeSmartAccount>) => void                                    | A function to trigger the grant permission for a sessionKey.                                                                |
| grantPermissionAsync | (variables: GrantPermissionParameters\<ComethSafeSmartAccount>) => Promise\<GrantPermissionMutateResponse> | A function to trigger the grant permission for a sessionKey and return a promise resolving to the transaction details.      |

GrantPermissionMutateResponse Structure:

```typescript
type GrantPermissionMutateResponse = {
  txHash: Hash;
  createSessionsResponse: GrantPermissionResponse;
};
```

## Explanation:

• txHash: The hash of the transaction that was sent to the blockchain.

• createSessionsResponse: The response from the grantPermission operation, including details about the session and its status.

## Usage Example:

```typescript
import { useGrantPermission } from "path/to/hook";

const Component = () => {
  const {
    data,
    error,
    isPending,
    isSuccess,
    isError,
    grantPermission,
    grantPermissionAsync,
  } = useGrantPermission();

  const handleGrantPermission = () => {
    grantPermission(permission);
  };

  const handleAsyncGrantPermission = async () => {
    try {
      const result = await grantPermissionAsync(permission);
      console.log("Transaction hash:", result.txHash);
    } catch (err) {
      console.error("Permission grant failed:", err);
    }
  };

  return (
    <div>
      {isPending && <p>Granting permission...</p>}
      {isSuccess && <p>Permission granted successfully!</p>}
      {isError && <p>Error: {error?.message}</p>}
      <button onClick={handleGrantPermission}>Grant Permission</button>
      <button onClick={handleAsyncGrantPermission}>Grant Permission (Async)</button>
    </div>
  );
};
```

## How It Works:

The hook relies on the smartAccountClient to extend actions from the cometh/connect-sdk-4337 package. It executes the following steps:

1\. Extends the smart account with necessary actions (erc7579Actions, smartSessionActions).

2\. Calls the grantPermission method with the provided parameters.

3\. Waits for confirmation using waitForUserOperationReceipt.

4\. Returns the transaction hash and the session response.

## Example of Expected Response:

```json
{
  "txHash": "0x123abc456def...",
  "createSessionsResponse": {
    "userOpHash": "0x789xyz...",
    "session": ...,
    "permissionIds": ...;
    "action": ...;
  }
}
```

## Error Handling:

If the mutation fails, the error object provides details about the failure, which can be used to display custom error messages to users.

## Mutation Props (mutationProps):

The hook accepts optional custom mutation parameters through the MutationOptionsWithoutMutationFn object, allowing you to adjust its behavior if needed.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.cometh.io/integrations/react-hooks/session-keys/usegrantpermission.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
