Function createBatchSession

  • createBatchSession

    Creates a session manager that handles multiple sessions at once for a given user's smart account. Useful for handling multiple granted sessions at once.

    Parameters

    Returns Promise<SessionGrantedPayload>

    Promise<SessionGrantedPayload> - An object containing the status of the transaction and the sessionID.

    Example

    import { createClient } from "viem"
    import { createSmartAccountClient } from "@biconomy/account"
    import { createWalletClient, http } from "viem";
    import { polygonAmoy } from "viem/chains";
    import { SessionFileStorage } from "@biconomy/session-file-storage";

    const signer = createWalletClient({
    account,
    chain: polygonAmoy,
    transport: http(),
    });

    const smartAccount = await createSmartAccountClient({ signer, bundlerUrl, paymasterUrl }); // Retrieve bundler/paymaster url from dashboard
    const smartAccountAddress = await smartAccount.getAccountAddress();
    const nftAddress = "0x1758f42Af7026fBbB559Dc60EcE0De3ef81f665e"
    const sessionStorage = new SessionFileStorage(smartAccountAddress);
    const sessionKeyAddress = (await sessionStorage.addSigner(undefined, polygonAmoy)).getAddress();

    const leaves: CreateSessionDataParams[] = [
    createERC20SessionDatum({
    interval: {
    validUntil: 0,
    validAfter: 0
    },
    sessionKeyAddress,
    sessionKeyData: encodeAbiParameters(
    [
    { type: "address" },
    { type: "address" },
    { type: "address" },
    { type: "uint256" }
    ],
    [sessionKeyAddress, token, recipient, amount]
    )
    }),
    createABISessionDatum({
    interval: {
    validUntil: 0,
    validAfter: 0
    },
    sessionKeyAddress,
    contractAddress: nftAddress,
    functionSelector: "safeMint(address)",
    rules: [
    {
    offset: 0,
    condition: 0,
    referenceValue: smartAccountAddress
    }
    ],
    valueLimit: 0n
    })
    ]

    const { wait, sessionID } = await createBatchSession(
    smartAccount,
    sessionStorageClient: sessionStorage,
    leaves,
    {
    paymasterServiceData: { mode: PaymasterMode.SPONSORED },
    }
    )

    const {
    receipt: { transactionHash },
    success
    } = await wait();

    console.log({ sessionID, success }); // Use the sessionID later to retrieve the sessionKey from the storage client