Use the useCreateSession
hook to create a session key for the connected Abstract Global Wallet.
Import
import { useCreateSession } from "@abstract-foundation/agw-react";
Usage
import { useCreateSession } from "@abstract-foundation/agw-react";
import { generatePrivateKey, privateKeyToAccount } from "viem/accounts";
import { LimitType } from "@abstract-foundation/agw-client/sessions";
import { toFunctionSelector, parseEther } from "viem";
export default function CreateSessionExample() {
const { createSessionAsync } = useCreateSession();
async function handleCreateSession() {
const sessionPrivateKey = generatePrivateKey();
const sessionSigner = privateKeyToAccount(sessionPrivateKey);
const { session, transactionHash } = await createSessionAsync({
session: {
signer: sessionSigner.address,
expiresAt: BigInt(Math.floor(Date.now() / 1000) + 60 * 60 * 24), // 24 hours
feeLimit: {
limitType: LimitType.Lifetime,
limit: parseEther("1"), // 1 ETH lifetime gas limit
period: BigInt(0),
},
callPolicies: [
{
target: "0xC4822AbB9F05646A9Ce44EFa6dDcda0Bf45595AA", // Contract address
selector: toFunctionSelector("mint(address,uint256)"), // Allowed function
valueLimit: {
limitType: LimitType.Unlimited,
limit: BigInt(0),
period: BigInt(0),
},
maxValuePerUse: BigInt(0),
constraints: [],
}
],
transferPolicies: [],
}
});
}
return <button onClick={handleCreateSession}>Create Session</button>;
}
Returns
Function to create a session key. Returns a Promise that resolves to the created session configuration.
{
transactionHash: Hash | undefined; // Transaction hash if deployment was needed
session: SessionConfig; // The created session configuration
}
Async mutation function to create a session key for async
await
syntax.
Whether the session creation is in progress.
Whether the session creation resulted in an error.
Error object if the session creation failed.