Swapping Tokens
Use the fdx call swapTokens command to swap between tokens via decentralized exchanges on any supported chain. Unlike centralized exchange swaps, these execute directly on-chain through DEX protocols.
Confirm wallet is authenticated
CODEBLOCK0
If the wallet is not authenticated, refer to the authenticate skill.
Check Balance Before Swapping
Verify the wallet holds enough of the source token:
CODEBLOCK1
Executing a Swap
CODEBLOCK2
Parameters
| Parameter | Required | Description |
|---|
| INLINECODE2 | Yes | Blockchain to swap on (e.g. ethereum, polygon, base, solana) |
| INLINECODE7 |
Yes | Source token — symbol (e.g.
USDC,
ETH) or contract address |
|
--tokenOut | Yes | Destination token — symbol or contract address |
|
--amount | Yes | Amount of
tokenIn to swap (human-readable) |
|
--mode | No | Swap mode (e.g. exact input, exact output) |
|
--objective | No | Optimization objective (e.g. best price, lowest gas) |
|
--maxSlippageBps | No | Maximum slippage tolerance in basis points (100 = 1%) |
|
--deadlineSeconds | No | Transaction deadline in seconds |
Examples
Basic swaps
CODEBLOCK3
Swap with slippage control
CODEBLOCK4
Swap using contract addresses
CODEBLOCK5
Flow
- 1. Check authentication with INLINECODE17
- Check balance with INLINECODE18
- Confirm the swap details with the human (amount, tokens, chain, slippage)
- Execute with INLINECODE19
- Optionally check updated balance with INLINECODE20
Important: DEX swaps are subject to slippage — the final output amount may differ slightly from the quoted amount. For large swaps, consider setting --maxSlippageBps explicitly.
Prerequisites
- - Must be authenticated (
fdx status to check, see authenticate skill) - Wallet must hold sufficient balance of the source token on the target chain
- If the wallet has insufficient funds, suggest using the
fund-wallet skill
Error Handling
- - "Not authenticated" — Run
fdx setup first, or see authenticate skill - "Insufficient balance" — Check balance with
getWalletOverview; see fund-wallet skill - "Cannot swap a token to itself" —
tokenIn and tokenOut must be different - "No liquidity" — Try a smaller amount or a different token pair
- "Swap failed" — May be a slippage issue; try with a higher INLINECODE31
代币兑换
使用 fdx call swapTokens 命令,通过任何支持链上的去中心化交易所进行代币兑换。与中心化交易所兑换不同,这些交易通过DEX协议直接在链上执行。
确认钱包已认证
bash
fdx status
如果钱包未认证,请参考 authenticate 技能。
兑换前检查余额
确认钱包持有足够的源代币:
bash
fdx call getWalletOverview --chainKey
执行兑换
bash
fdx call swapTokens \
--chainKey \
--tokenIn \
--tokenOut \
--amount
参数
| 参数 | 必填 | 描述 |
|---|
| --chainKey | 是 | 进行兑换的区块链(例如 ethereum、polygon、base、solana) |
| --tokenIn |
是 | 源代币 — 符号(例如 USDC、ETH)或合约地址 |
| --tokenOut | 是 | 目标代币 — 符号或合约地址 |
| --amount | 是 | 要兑换的 tokenIn 数量(人类可读格式) |
| --mode | 否 | 兑换模式(例如精确输入、精确输出) |
| --objective | 否 | 优化目标(例如最佳价格、最低Gas费用) |
| --maxSlippageBps | 否 | 最大滑点容忍度(基点,100 = 1%) |
| --deadlineSeconds | 否 | 交易截止时间(秒) |
示例
基础兑换
bash
在以太坊上用100 USDC兑换ETH
fdx call swapTokens \
--chainKey ethereum \
--tokenIn USDC \
--tokenOut ETH \
--amount 100
在Base上用0.05 ETH兑换USDC
fdx call swapTokens \
--chainKey base \
--tokenIn ETH \
--tokenOut USDC \
--amount 0.05
在Solana上用SOL兑换USDC
fdx call swapTokens \
--chainKey solana \
--tokenIn SOL \
--tokenOut USDC \
--amount 2
带滑点控制的兑换
bash
兑换时设置最大滑点0.5%
fdx call swapTokens \
--chainKey ethereum \
--tokenIn USDC \
--tokenOut ETH \
--amount 500 \
--maxSlippageBps 50
使用合约地址进行兑换
bash
使用明确的代币合约地址进行兑换
fdx call swapTokens \
--chainKey ethereum \
--tokenIn 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48 \
--tokenOut 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2 \
--amount 100
流程
- 1. 使用 fdx status 检查认证状态
- 使用 fdx call getWalletOverview --chainKey 检查余额
- 与用户确认兑换详情(金额、代币、链、滑点)
- 使用 fdx call swapTokens 执行兑换
- 可选:使用 fdx call getWalletOverview 检查更新后的余额
重要提示: DEX兑换存在滑点风险——最终输出金额可能与报价略有差异。对于大额兑换,建议明确设置 --maxSlippageBps。
前置条件
- - 必须已认证(使用 fdx status 检查,参见 authenticate 技能)
- 钱包在目标链上必须持有足够的源代币余额
- 如果钱包资金不足,建议使用 fund-wallet 技能
错误处理
- - 未认证 — 先运行 fdx setup,或参见 authenticate 技能
- 余额不足 — 使用 getWalletOverview 检查余额;参见 fund-wallet 技能
- 无法将代币兑换为自身 — tokenIn 和 tokenOut 必须不同
- 无流动性 — 尝试更小的金额或不同的代币对
- 兑换失败 — 可能是滑点问题;尝试设置更高的 --maxSlippageBps