Skip to content

Smart Swap Action

This tool allows the agent to swap tokens on decentralized exchanges (DEXs).

It leverages the power of ERC-4337 batched transactions to perform both an Approve transaction and a Swap transaction in a single, atomic UserOperation.

The LLM invokes this tool when the user asks:

  • “Swap 10 USDC for WETH.”
  • “Buy 0.1 ETH worth of UNI.”
ArgumentTypeDescription
tokenInstringThe contract address of the token the agent is selling. Use 0x...0 for native ETH/MATIC.
tokenOutstringThe contract address of the token the agent is buying. Use 0x...0 for native ETH/MATIC.
amountInstringThe amount to sell, in human-readable units (e.g. "10.5").
slippageTolerancenumberOptional. The maximum allowed slippage percentage. Defaults to 1 (1%).
import { AgentKit, SmartSwapAction } from "@0xgasless/agent-sdk";
const agentKit = new AgentKit({ apiKey: "YOUR_API_KEY" });
const action = new SmartSwapAction();
const result = await action.invoke(agentKit, {
tokenIn: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
tokenOut: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", // WETH
amountIn: "100", // $100 USDC
slippageTolerance: 0.5 // 0.5% max slippage
});
console.log(result);
// "Successfully swapped 100 USDC for WETH. Transaction hash: 0x..."

The SmartSwapAction integrates with a DEX Aggregator API (like 1inch, 0x, or Uniswap AutoRouter, depending on the network configuration of the SDK).

  1. It queries the aggregator for the best quote matching tokenIn, tokenOut, and amountIn.
  2. It fetches the required approval amount and spender address from the aggregator.
  3. It constructs an approve call for the tokenIn contract.
  4. It constructs the swap call data returned by the aggregator.
  5. It bundles both calls into a single UserOperation using sendBatchTransactions.
  6. The Paymaster sponsors the gas.

The user (or LLM) only needs to wait for one transaction to be confirmed.