The toSessionClient function creates a new SessionClient instance that can submit transactions and perform actions (e.g. writeContract) from the Abstract Global wallet signed by a session key.

If a transaction violates any of the session key’s policies, it will be rejected.

Usage

import { useAbstractClient } from "@abstract-foundation/agw-react";
import { parseAbi } from "viem";
import { abstractTestnet } from "viem/chains";
import { useAccount } from "wagmi";

export default function Example() {
    const { address } = useAccount();
    const { data: agwClient } = useAbstractClient();

    async function sendTransactionWithSessionKey() {
        if (!agwClient || !address) return;

        // Use the existing session signer and session that you created with useCreateSession
        // Likely you want to store these inside a database or solution like AWS KMS and load them
        const sessionClient = agwClient.toSessionClient(sessionSigner, session);

        const hash = await sessionClient.writeContract({
            abi: parseAbi(["function mint(address,uint256) external"]),
            account: sessionClient.account,
            chain: abstractTestnet,
            address: "0xC4822AbB9F05646A9Ce44EFa6dDcda0Bf45595AA",
            functionName: "mint",
            args: [address, BigInt(1)],
        });
    }

    return <button onClick={sendTransactionWithSessionKey}>Send Transaction with Session Key</button>;
}

Parameters

sessionSigner
Account
required

The account that will be used to sign transactions. This must match the signer address specified in the session configuration.

session
SessionConfig
required

The session configuration created by createSession.

Returns

sessionClient
AbstractClient

A new AbstractClient instance that uses the session key for signing transactions. All transactions will be validated against the session’s policies.