Are you an LLM? Read llms.txt for a summary of the docs, or llms-full.txt for the full context.
Skip to content

Use the SDK with a smart wallet

Pass a smart wallet's client to createStable as walletClient, and every SDK operation goes out as one atomic batch. A swap's approve and swap land in a single user operation, so the user signs once and never ends up with an approval and no swap. Requires @stablechain/sdk 1.2.0 or later.

const stable = createStable({ network: Network.Testnet, walletClient: smartWalletClient });
const { txHash } = await stable.swap({ fromToken, toToken, amount: 10 }); // approve + swap, one batch
{ txHash: "0x4b7e...c210", toAmount: 0.0024 }

The SDK adds no dependency. You build the client with the smart wallet library you already use, and the SDK detects what it can do at runtime. This guide covers a Safe through permissionless and an Alchemy smart wallet, both on Stable Testnet. To batch from a plain private key without a smart wallet library, see Batch from a private key with EIP-7702.

1. Install the SDK and a wallet library

Safe
npm install @stablechain/sdk viem permissionless
added 3 packages, audited 4 packages in 3s

2. Build the smart wallet client

Safe
import { createSmartAccountClient } from "permissionless";
import { toSafeSmartAccount } from "permissionless/accounts";
import { createPublicClient, http } from "viem";
import { entryPoint07Address } from "viem/account-abstraction";
import { privateKeyToAccount } from "viem/accounts";
import { stableTestnet } from "viem/chains";
 
const owner = privateKeyToAccount(process.env.OWNER_PRIVATE_KEY as `0x${string}`);
const publicClient = createPublicClient({ chain: stableTestnet, transport: http() });
 
const safe = await toSafeSmartAccount({
  client: publicClient,
  owners: [owner],
  version: "1.4.1",
  entryPoint: { address: entryPoint07Address, version: "0.7" },
});
 
const walletClient = createSmartAccountClient({
  account: safe,
  chain: stableTestnet,
  client: publicClient,
  bundlerTransport: http(process.env.STABLE_BUNDLER_URL), // ERC-4337 bundler, EntryPoint v0.7
});
 
console.log(safe.address);
0x3fA1...92c4

The Safe has its own address, derived from its owners. The Alchemy smart wallet upgrades the owner EOA to a Modular Account v2 through EIP-7702 on its first call, so the account keeps the owner's address. Use a key you're happy to see upgraded.

Fund the account address with testnet USDT0 from the faucet, unless a paymaster sponsors gas.

3. Pass it to createStable

import { createStable, Network } from "@stablechain/sdk";
 
const stable = createStable({ network: Network.Testnet, walletClient });
StableClient { transfer, quoteBridge, bridge, prepareBridgeCalldata, estimateBridgeTime, trackBridge, quoteSwap, swap, authorization, earn }

No cast is needed: walletClient is typed StableWalletClient, which accepts a viem WalletClient or any viem Client. createStable throws StableValidationError for a client that has no way to send.

4. Send an operation

Call any write method as usual. Operations that need an approval (bridge, swap, and earn.deposit) put it in the same batch as the action. The rest (transfer, earn.withdraw, earn.redeem, and earn.incentiveRewards.claim) go through the same path as single calls.

const { txHash } = await stable.transfer({
  from: "0xYourSmartAccount", // safe.address, or owner.address with Alchemy
  to: "0xRecipient",
  amount: 0.01,
  token: "0x78Cf24370174180738C5B8E352B6D14c83a6c9A9", // USDT0 on Stable Testnet
});
 
console.log(`https://testnet.stablescan.xyz/tx/${txHash}`);
https://testnet.stablescan.xyz/tx/0x4b7e...c210

txHash is the transaction that carried the batch. With a Safe, the SDK also checks the user operation's own success flag, because a user operation can revert inside a bundle transaction that succeeds. A revert throws StableTransactionError.

5. Sign ERC-3009 authorizations as the account

stable.authorization signs as the smart account itself. USDT0 checks the signature through the account's isValidSignature (ERC-1271), so the account can pay with an ERC-3009 authorization that anyone submits.

const USDT0 = "0x78Cf24370174180738C5B8E352B6D14c83a6c9A9"; // USDT0 on Stable Testnet
 
const signed = await stable.authorization.signTransferAuthorization({
  token: USDT0,
  to: "0xVendor",
  value: 1_250_000n, // 1.25 USDT0, raw 6-decimal units
  validBefore: BigInt(Math.floor(Date.now() / 1000) + 3600),
});
 
const check = await stable.authorization.verifyAuthorization({ token: USDT0, signed });
console.log(signed.signatureType, check.ok);
erc1271 true

Use a passkey as the owner

A Safe's owner can be a passkey: a WebAuthn credential such as Touch ID, Windows Hello, or a security key. Every user operation and every ERC-3009 authorization is then a device prompt, and no private key exists. The SDK needs no change, because it only sees the Safe's client.

Create the credential once and store its id and publicKey. Then make it the Safe's owner and point the Safe at the P-256 verifier deployed on Stable:

import { toSafeSmartAccount } from "permissionless/accounts";
import { createWebAuthnCredential, entryPoint07Address, toWebAuthnAccount } from "viem/account-abstraction";
 
const credential = await createWebAuthnCredential({ name: "My app" }); // device prompt
const passkey = toWebAuthnAccount({ credential });
 
const safe = await toSafeSmartAccount({
  client: publicClient,
  owners: [passkey],
  version: "1.4.1",
  entryPoint: { address: entryPoint07Address, version: "0.7" },
  safeP256VerifierAddress: "0xc2b78104907F722DABAc4C69f826a522B2754De4", // Daimo P-256 verifier
});
 
console.log(safe.address);
0x83E5...67Bd

Build the client and call the SDK exactly as in steps 2 to 5. The first operation deploys the Safe and registers the passkey with SafeWebAuthnSharedSigner, so the Safe needs no signer contract per credential.

The passkey is bound to your app's domain (the WebAuthn relying party), so a credential created on localhost doesn't work on your production domain. The examples/core app in the SDK repository has a Passkey tab that runs this flow on testnet.

Batch from a private key with EIP-7702

A server or script that signs with a private key can batch too, with no bundler and no smart wallet library. Wrap the wallet client with withEip7702 from the @stablechain/sdk/eip7702 subpath. The first write upgrades the EOA to Alchemy's Modular Account v2 through EIP-7702, and every operation on Stable then goes out as one transaction from the EOA to itself. The address and key stay the same. Requires @stablechain/sdk 1.3.0 or later.

import { createStable, Network } from "@stablechain/sdk";
import { withEip7702 } from "@stablechain/sdk/eip7702";
import { createWalletClient, http } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { stableTestnet } from "viem/chains";
 
const owner = privateKeyToAccount(process.env.OWNER_PRIVATE_KEY as `0x${string}`);
const walletClient = createWalletClient({ account: owner, chain: stableTestnet, transport: http() });
 
const stable = createStable({ network: Network.Testnet, walletClient: withEip7702(walletClient) });
 
const { txHash } = await stable.transfer({
  from: owner.address,
  to: "0xRecipient",
  amount: 0.01,
  token: "0x78Cf24370174180738C5B8E352B6D14c83a6c9A9", // USDT0 on Stable Testnet
});
 
console.log(`https://testnet.stablescan.xyz/tx/${txHash}`);
https://testnet.stablescan.xyz/tx/0x9c1d...7a02

The first transaction is a type-4 transaction that carries the delegation and the batch. Later ones are ordinary EIP-1559 transactions. Fund the EOA with testnet USDT0 for gas before the first call.

  • Wrap last. Call withEip7702 after any .extend() on the client, or the extended client loses the batch sender and sends one transaction at a time.
  • Keep the key idle during the first call. If the key sends another transaction while the delegation is in flight, the chain skips the authorization and the batch runs nothing. The SDK detects this and throws StableTransactionError with phase: "receipt". Retry once the key is idle.
  • Existing delegations. An EOA already on Modular Account v2, for example one set up by the Alchemy client in step 2, is used as is. An EOA delegated to any other contract is refused, never replaced.
  • ERC-3009 still works. After the upgrade the account has code, so USDT0 checks signatures through ERC-1271. The delegate accepts the owner's ECDSA signature, so stable.authorization signs as before.

The examples/core app in the SDK repository runs this flow on testnet with bun run eip7702.

How the SDK picks the send path

ClientSends with
viem smart account client, such as a Safe through permissionlesssendUserOperation, then checks the user operation's success.
Client with sendCalls but no sendTransaction, such as an Alchemy smart walletsendCalls, then waitForCallsStatus.
Browser wallet that reports atomic: "supported" for the chain (EIP-5792)sendCalls with forceAtomic: true.
Private-key client wrapped with withEip7702, on StableOne transaction from the EOA to itself, calling executeBatch.
Anything else, including an unwrapped private key or a wallet that reports atomic: "ready"One transaction at a time. The SDK never asks a wallet to upgrade an EOA.

A browser wallet with EIP-5792 atomic batching needs no setup: pass its WalletClient and the SDK batches through it.

Limits

  • One chain per client. A smart account client is bound to its chain. To bridge from another chain, build a client for that chain and a second createStable with it. The SDK throws StableValidationError with field: "walletClient" rather than send from the wrong one.
  • No chain switching. The SDK never calls switchChain on a smart account client.
  • The bridge fee isn't sponsored. A bridge's LayerZero messaging fee rides as msg.value, which paymasters don't cover. Hold native gas on the source chain even with a paymaster.
  • Approvals, not permits. earn.deposit sends an approve in the batch instead of signing a permit.
  • Reads need a public client. Smart wallet clients send through a bundler or a wallet API, which doesn't answer eth_call. Read chain state through a public client, or pass rpc to createStable for the SDK's own reads.

Next recommended

  • SDK reference: See every method that batches, and the full StableConfig.
  • Wallets on Stable: Compare Safe, Alchemy, and other smart account providers on Stable.
  • ERC-3009: Learn how signed authorizations let a payer transfer without holding gas.