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

Build with Altana

Let an agent trade on a DEX, capped

What you need
Goal
I want my agent to trade on a DEX, but capped so it can only spend what I allow and only on that exchange.
Who it's for
Developers giving a trading agent access to one DEX on BNB Smart Chain, without exposing the rest of the wallet.
What you'll use
grantSession scoped to the DEX router with a spend cap and expiry, then execute to run the swap.
Prerequisites
A smart agentic wallet set up in Give an agent a wallet and a policy, funded on BNB with a stablecoin to trade.

The example below uses PancakeSwap on BNB. Swap in any DEX router address to use a different exchange; the pattern is identical. The flow is three steps: connect to the chain, grant a session scoped to the DEX, then let the agent trade within it.

Step 1: Connect and load your signer

Create a client for BNB Smart Chain and load the admin signer that owns the wallet.
This is the key you control, and the agent never sees it.

import {
  createClient,
  BNB,
  signerFromPrivateKey,
} from "@altananetwork/sdk";
 
const client = createClient({ chains: [BNB] });
const signer = signerFromPrivateKey(process.env.PRIVATE_KEY as `0x${string}`);

Step 2: Grant a session scoped to the DEX

Give the agent a session key that can reach only one DEX router, with a daily spend cap in your stablecoin and an expiry. These limits are enforced onchain by the wallet, not by your application code.

// wallet from the "Give an agent a wallet and a policy" guide
const session = await client.grantSession({
  wallet,
  signer,
  permissions: {
    calls: [{ to: "0xPancakeRouter..." }],                          // only this router
    spend: [{ limit: 100_000_000_000_000_000_000n, period: "day", token: "0xStable..." }],
  },
  expiry: Math.floor(Date.now() / 1000) + 3 * 24 * 60 * 60,
});

Step 3: Execute a swap within the cap

Pass the session to your agent. It builds the swap calldata and calls execute to trade within the limits you set.

// The agent builds swap calldata and executes within the cap.
await client.execute({
  session,
  calls: [{ to: "0xPancakeRouter...", data: "0xSwapCalldata...", value: 0n }],
});

The moment that makes it click

A swap inside the cap goes through. An oversized swap is blocked at validation before it ever touches the chain.

Why it is different

The trading bot holds a key that can only hit that one router and only up to the cap. It cannot drain the wallet, cannot touch any other contract, and expires on its own.

What's next

Ready for more than one agent? Run a portfolio with multiple agents puts several agents on the same wallet, each with its own scoped session.