Skip to main content
Version: SDK V4 (latest)

Use a distributed session

info

Building in React? check here

Overview

This tutorial demonstrates how to use a distributed session with viem and the Biconomy Smart Account with the @biconomy/account SDK. The provided code assumes you have a Biconomy Paymaster API key, and a valid session setup during the previous step. The following is appropriately viewed from the perspective of a dapp, looking to make txs on a users behalf.

You can get your Biconomy Paymaster API key from the dashboard here.

Prerequisites

  • Biconomy Paymaster API key
  • A Bundler url if you don't want to use the testnet one, for Amoy you can use
https://bundler.biconomy.io/api/v2/80002/nJPK7B3ru.dd7f7861-190d-41bd-af80-6877f74b8f44

Step 1: Setup

import { polygonAmoy as chain } from "viem/chains";
import {
PaymasterMode,
createSessionSmartAccountClient,
createSession,
Rule,
Policy,
Session,
} from "@biconomy/account";

const nftAddress = "0x1758f42Af7026fBbB559Dc60EcE0De3ef81f665e";
const usersSmartAccountAddress = sessionStorageClient.smartAccountAddress;

Step 2: Create the sessionSmartAccountClient

The createSessionSmartAccountClient helper here is very similar to the createSmartAccountClient, in that it returns a BiconomySmartAccountV2, but there are subtle differences in the configuration. createSessionSmartAccountClient expects no signer and a mandatory smartAccountAddress in the config (so that a dapp can emulate the users smartAccount without the original signer).

const emulatedUsersSmartAccount = await createSessionSmartAccountClient(
{
accountAddress: usersSmartAccountAddress, // Dapp can set the account address on behalf of the user
bundlerUrl,
paymasterUrl,
chainId,
},
"DEFAULT_STORE",
"DISTRIBUTED"
);

Step 3: Execute a tx on the users behalf

Next we can use the users smartAccount in the usual manner to generate txs. Txs generated for the emulatedSmartAccount must be relevant to the policy over which the user has previously signed.

const nftMintTx = {
to: nftAddress,
data: encodeFunctionData({
abi: parseAbi(["function safeMint(address _to)"]),
functionName: "safeMint",
args: [usersSmartAccountAddress],
}),
};

const { wait } = await emulatedUsersSmartAccount.sendTransaction(nftMintTx, {
paymasterServiceData: { mode: PaymasterMode.SPONSORED }
}, {
leafIndex: "LAST_LEAF"
});

const { success } = await wait();

Send the transaction using the users smartAccount and get the transaction hash. The transaction will be built into a User Operation and then send to the Bundler.

That's it! You've successfully emulated a users smartAccount to execute a transaction on their behalf using their session.