Hook for creating a session key.
useCreateSession
import { useCreateSession } from "@abstract-foundation/agw-react";
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>; }
{ transactionHash: Hash | undefined; // Transaction hash if deployment was needed session: SessionConfig; // The created session configuration }
async
await
Was this page helpful?