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.
LLM Usage
Section titled “LLM Usage”The LLM invokes this tool when the user asks:
- “Swap 10 USDC for WETH.”
- “Buy 0.1 ETH worth of UNI.”
Arguments
Section titled “Arguments”| Argument | Type | Description |
|---|---|---|
tokenIn | string | The contract address of the token the agent is selling. Use 0x...0 for native ETH/MATIC. |
tokenOut | string | The contract address of the token the agent is buying. Use 0x...0 for native ETH/MATIC. |
amountIn | string | The amount to sell, in human-readable units (e.g. "10.5"). |
slippageTolerance | number | Optional. The maximum allowed slippage percentage. Defaults to 1 (1%). |
Direct Invocation
Section titled “Direct Invocation”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..."How the Routing Works
Section titled “How the Routing Works”The SmartSwapAction integrates with a DEX Aggregator API (like 1inch, 0x, or Uniswap AutoRouter, depending on the network configuration of the SDK).
- It queries the aggregator for the best quote matching
tokenIn,tokenOut, andamountIn. - It fetches the required approval amount and spender address from the aggregator.
- It constructs an
approvecall for thetokenIncontract. - It constructs the
swapcall data returned by the aggregator. - It bundles both calls into a single
UserOperationusingsendBatchTransactions. - The Paymaster sponsors the gas.
The user (or LLM) only needs to wait for one transaction to be confirmed.