Function useCreateSession

  • Parameters

    Returns UseMutationResult<UserOpResponse, any, UseCreateSessionProps, any>

    Description

    Creates a session to be used when submitting tx in the context of a users smart account.

    Mutation function args: UseCreateSessionProps

    Example


    import { useCreateSession, useUserOpWait, Options } from "@biconomy/useAA"
    import { polygonAmoy } from "viem/chains"
    import { encodeFunctionData, parseAbi } from "wagmi"

    export const CreateSession = ({userSmartAccountAddress}) => {

    const policy = [
    {
    contractAddress: "0x1758f42Af7026fBbB559Dc60EcE0De3ef81f665e",
    functionSelector: "safeMint(address)",
    rules: [
    {
    offset: 0,
    condition: 0,
    referenceValue: userSmartAccountAddress,
    },
    ],
    interval: {
    validUntil: 0,
    validAfter: 0,
    },
    valueLimit: 0n,
    },
    ];

    const {
    mutate,
    data: userOpResponse,
    error,
    isPending,
    } = useCreateSession();

    const {
    isLoading: waitIsLoading,
    isSuccess: waitIsSuccess,
    error: waitError,
    data: waitData,
    } = useUserOpWait(userOpResponse);

    useEffect(() => {
    if (waitData?.success === "true") {
    console.log(waitData?.receipt?.transactionHash);
    }
    }, [waitData]);


    const createSessionHandler = () =>
    mutate({
    policy,
    options: Options.Sponsored,
    });

    return (
    <ErrorGuard errors={[error, waitError]}>
    <Button
    title="Create a session"
    onClickFunc={createSessionHandler}
    isLoading={isPending || waitIsLoading}
    />
    </ErrorGuard>
    );
    };