The user's BiconomySmartAccountV2 smartAccount instance.
The storage client to be used for storing the session data
An array of session configurations
Optional
buildUseropDto: BuildUserOpOptionsOptional. BuildUserOpOptions
Promise<SessionGrantedPayload> - An object containing the status of the transaction and the sessionID.
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
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.