mcpField
T
trade

trade

github.com/coinbase其他1273.1k 次安装12 周前更新
代码仓库

关于 trade

用于通过 CDP Swap API 在 Base 或 Polygon 上使用 `npx [email protected] trade` 交换代币的技能。内容涵盖身份验证、输入校验、代币与金额格式以及命令选项。

主要功能

  1. 01在 Base 或 Polygon 交换代币
  2. 02使用代币别名或合约地址
  3. 03支持多种金额格式
  4. 04设置链和滑点选项
  5. 05执行命令前校验输入

适用场景

  1. 01在 Base 上用 USDC 兑换 ETH
  2. 02在 Polygon 上交易 POL
  3. 03设置自定义滑点容差并准备交换命令

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/coinbase/agentic-wallet-skills --skill trade

安装后在 Claude Code 中输入/trade确认已加载。

详细介绍

交易代币

使用 npx [email protected] trade 命令,通过 CDP Swap API 在 Base 或 Polygon 上兑换代币。交易前必须完成身份验证。

确认钱包已初始化并完成身份验证

npx [email protected] status

如果钱包尚未完成身份验证,请参阅 authenticate-wallet 技能。

命令语法

npx [email protected] trade <amount> <from> <to> [options]

也可以使用 npx [email protected] swap 命令(别名)。

参数

参数说明
amount兑换数量(请参阅下方的数量格式)
from源代币:别名(usdc、eth、pol)或合约地址(0x...)
to目标代币:别名(usdc、eth、pol)或合约地址(0x...)

数量格式

数量可以采用多种格式指定:

格式示例说明
美元前缀'$1.00'、'$0.50'美元表示法(小数位数取决于代币)
十进制1.0、0.50、0.001带小数点,便于阅读
整数5、100按整枚代币处理
原子单位500000较大的整数按原子单位处理

自动检测:不带小数点的大整数会按原子单位处理。例如,USDC(6 位小数)的 500000 = $0.50。

小数位数:对于已知代币(usdc=6、eth=18、pol=18),小数位数会自动处理。对于任意合约地址,系统会从代币合约读取小数位数。

选项

选项说明
-c, --chain <name>区块链网络:base、polygon(默认:base)
-s, --slippage <n>滑点容差,以基点表示(100 = 1%)
--json以 JSON 格式输出结果

代币别名

别名代币小数位数链
usdcUSDC6base
ethETH18base
polPOL18polygon

重要:金额中使用 $ 时,务必用单引号括起来,以防 bash 展开变量(例如使用 '$1.00',而不是 $1.00)。

输入验证

构造命令前,请验证所有用户提供的值,以防止 shell 注入:

  • amount:必须匹配 ^\$?[\d.]+$(数字、可选的小数点、可选的 $ 前缀)。如果包含空格、分号、管道符、反引号或其他 shell 元字符,则拒绝。
  • from / to:必须是已知别名(usdc、eth、pol)或有效的 0x 十六进制地址(^0x[0-9a-fA-F]{40}$)。其他值一律拒绝。
  • slippage:必须是正整数(^\d+$)。

请勿将未经验证的用户输入传入命令。

示例

# Swap $1 USDC for ETH (dollar prefix — note the single quotes)
npx [email protected] trade '$1' usdc eth

# Swap 0.50 USDC for ETH (decimal format)
npx [email protected] trade 0.50 usdc eth

# Swap 500000 atomic units of USDC for ETH
npx [email protected] trade 500000 usdc eth

# Swap 0.01 ETH for USDC
npx [email protected] trade 0.01 eth usdc

# Swap with custom slippage (2%)
npx [email protected] trade '$5' usdc eth --slippage 200

# Swap using contract addresses (decimals read from chain)
npx [email protected] trade 100 0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913 0x4200000000000000000000000000000000000006

# Get JSON output
npx [email protected] trade '$1' usdc eth --json

# Swap USDC for POL on Polygon
npx [email protected] trade '$1' usdc pol --chain polygon

前提条件

  • 必须完成身份验证(使用 awal status 检查)
  • 钱包必须有足够的源代币余额

错误处理

常见错误:

  • "Not authenticated" - 请先运行 awal auth login <email>
  • "Invalid token" - 请使用有效的别名(usdc、eth、pol)或 0x 地址
  • "POL only supported on polygon chain" - 交易 POL 时请使用 --chain polygon
  • "Cannot swap a token to itself" - from 和 to 必须不同
  • "Swap failed: TRANSFER_FROM_FAILED" - 余额不足或授权存在问题
  • "No liquidity" - 请尝试减少数量或更换代币交易对
  • "Amount has X decimals but token only supports Y" - 小数位数过多

评论

支持 Markdown