Use Permission
Important: The smartSessionValidation module must be the active nexus module for using smart sessions. This is automatically handled when you extend your client with
smartSessionUseActions
.
Once a session has been created, you can use it to execute transactions on behalf of the account that granted the permissions. The Smart Sessions module provides the usePermission
action through smartSessionUseActions
.
usePermission
Executes a transaction using the granted session permissions.
Usage
example.ts
import { nexusClient } from "./nexusClient"
import { smartSessionUseActions } from "@biconomy/sdk"
import { encodeFunctionData } from "viem"
// Initialize the module for using permissions
const usePermissionsModule = toSmartSessionsValidator({
account: nexusClient.account,
signer: sessionKey, // The session key granted permission
moduleData: sessionData.moduleData // This is the module data returned from the createSessions step
});
// Extend the client with session usage capabilities
const sessionClient = nexusClient
.extend(smartSessionUseActions(usePermissionsModule));
// Use the session to make a transaction
const userOpHash = await sessionClient.usePermission({
calls: [{
to: targetContract,
data: encodeFunctionData({
abi: contractABI,
functionName: "transfer",
args: [recipientAddress, amount]
})
}]
});
// Wait for the transaction to be mined
const receipt = await sessionClient.waitForUserOperationReceipt({ hash: userOpHash });
Parameters
calls
: Array of transaction calls to executeto
:Address
- Target contract addressdata
:Hex
- Encoded function call datavalue?
:bigint
- Optional ETH value to send with the call
Response
Returns a promise that resolves to a user operation hash. This hash can be used with waitForUserOperationReceipt
to track the transaction status.