gmgn-swap
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseUse the tool to submit a token swap or query an existing order. Requires private key ( in ).
gmgn-cliGMGN_PRIVATE_KEY.env使用工具提交代币兑换订单或查询现有订单状态。需配置私钥(在文件中设置)。
gmgn-cli.envGMGN_PRIVATE_KEYSub-commands
子命令
| Sub-command | Description |
|---|---|
| Submit a token swap |
| Query order status |
| 子命令 | 说明 |
|---|---|
| 提交代币兑换订单 |
| 查询订单状态 |
Supported Chains
支持的链
solbscbasesolbscbaseChain Currencies
链原生代币
Currency tokens are the base/native assets of each chain. They are used to buy other tokens or receive proceeds from selling. Knowing which tokens are currencies is critical for usage (see Swap Parameters below).
--percent| Chain | Currency tokens |
|---|---|
| SOL (native, So11111111111111111111111111111111111111112), USDC ( |
| BNB (native, 0x0000000000000000000000000000000000000000), USDC ( |
| ETH (native, 0x0000000000000000000000000000000000000000), USDC ( |
链原生代币是各条链的基础资产,可用于购买其他代币或接收出售代币的收益。了解哪些代币是原生代币对于参数的使用至关重要(见下方兑换参数)。
--percent| 链 | 原生代币 |
|---|---|
| SOL(原生代币,合约地址So11111111111111111111111111111111111111112)、USDC(合约地址 |
| BNB(原生代币,合约地址0x0000000000000000000000000000000000000000)、USDC(合约地址 |
| ETH(原生代币,合约地址0x0000000000000000000000000000000000000000)、USDC(合约地址 |
Prerequisites
前置条件
Both and must be set in . The private key must correspond to the wallet bound to the API Key.
GMGN_API_KEYGMGN_PRIVATE_KEY.env必须在文件中设置和,且私钥必须与API密钥绑定的钱包对应。
.envGMGN_API_KEYGMGN_PRIVATE_KEYSwap Usage
兑换使用示例
bash
undefinedbash
undefinedBasic swap
基础兑换
npx gmgn-cli swap
--chain sol
--from <wallet_address>
--input-token <input_token_address>
--output-token <output_token_address>
--amount <input_amount_smallest_unit>
--chain sol
--from <wallet_address>
--input-token <input_token_address>
--output-token <output_token_address>
--amount <input_amount_smallest_unit>
npx gmgn-cli swap
--chain sol
--from <钱包地址>
--input-token <输入代币合约地址>
--output-token <输出代币合约地址>
--amount <最小单位的输入数量>
--chain sol
--from <钱包地址>
--input-token <输入代币合约地址>
--output-token <输出代币合约地址>
--amount <最小单位的输入数量>
With slippage
设置滑点
npx gmgn-cli swap
--chain sol
--from <wallet_address>
--input-token <input_token_address>
--output-token <output_token_address>
--amount 1000000
--slippage 0.01
--chain sol
--from <wallet_address>
--input-token <input_token_address>
--output-token <output_token_address>
--amount 1000000
--slippage 0.01
npx gmgn-cli swap
--chain sol
--from <钱包地址>
--input-token <输入代币合约地址>
--output-token <输出代币合约地址>
--amount 1000000
--slippage 0.01
--chain sol
--from <钱包地址>
--input-token <输入代币合约地址>
--output-token <输出代币合约地址>
--amount 1000000
--slippage 0.01
With anti-MEV (SOL)
开启反MEV保护(Sol链)
npx gmgn-cli swap
--chain sol
--from <wallet_address>
--input-token <input_token_address>
--output-token <output_token_address>
--amount 1000000
--anti-mev
--chain sol
--from <wallet_address>
--input-token <input_token_address>
--output-token <output_token_address>
--amount 1000000
--anti-mev
npx gmgn-cli swap
--chain sol
--from <钱包地址>
--input-token <输入代币合约地址>
--output-token <输出代币合约地址>
--amount 1000000
--anti-mev
--chain sol
--from <钱包地址>
--input-token <输入代币合约地址>
--output-token <输出代币合约地址>
--amount 1000000
--anti-mev
Sell 50% of a token (input_token must NOT be a currency)
出售代币的50%(输入代币不能是原生代币)
npx gmgn-cli swap
--chain sol
--from <wallet_address>
--input-token <token_address>
--output-token <sol_or_usdc_address>
--percent 50
--chain sol
--from <wallet_address>
--input-token <token_address>
--output-token <sol_or_usdc_address>
--percent 50
undefinednpx gmgn-cli swap
--chain sol
--from <钱包地址>
--input-token <代币合约地址>
--output-token <SOL或USDC合约地址>
--percent 50
--chain sol
--from <钱包地址>
--input-token <代币合约地址>
--output-token <SOL或USDC合约地址>
--percent 50
undefinedOrder Query
订单查询示例
bash
npx gmgn-cli order get --chain sol --order-id <order_id>bash
npx gmgn-cli order get --chain sol --order-id <订单ID>Swap Parameters
兑换参数
| Parameter | Required | Description |
|---|---|---|
| Yes | |
| Yes | Wallet address (must match API Key binding) |
| Yes | Input token contract address |
| Yes | Output token contract address |
| No* | Input amount in smallest unit. Required unless |
| No* | Sell percentage of |
| No | Slippage tolerance, e.g. |
| No | Minimum output amount |
| No | Enable anti-MEV protection (default true) |
| No | Priority fee in SOL (≥ 0.00001, SOL only) |
| No | Tip fee (SOL ≥ 0.00001 / BSC ≥ 0.000001 BNB) |
| No | Max automatic fee cap |
| No | Gas price in gwei (BSC ≥ 0.05 / BASE/ETH ≥ 0.01) |
| No | EIP-1559 max fee per gas (Base only) |
| No | EIP-1559 max priority fee per gas (Base only) |
| 参数 | 是否必填 | 说明 |
|---|---|---|
| 是 | |
| 是 | 钱包地址(必须与API密钥绑定的钱包一致) |
| 是 | 输入代币的合约地址 |
| 是 | 输出代币的合约地址 |
| 否* | 最小单位的输入数量,除非使用 |
| 否* | 出售输入代币的百分比,例如 |
| 否 | 滑点容忍度,例如 |
| 否 | 最小输出数量 |
| 否 | 开启反MEV保护(默认开启) |
| 否 | 优先费用(以SOL为单位,≥0.00001,仅支持Sol链) |
| 否 | 小费(Sol链≥0.00001 SOL / BSC链≥0.000001 BNB) |
| 否 | 自动费用上限 |
| 否 | 燃气价格(以gwei为单位,BSC≥0.05 / BASE/ETH≥0.01) |
| 否 | EIP-1559最大燃气单价(仅支持Base链) |
| 否 | EIP-1559最大优先燃气单价(仅支持Base链) |
Swap Response Fields
兑换响应字段
| Field | Type | Description |
|---|---|---|
| string | Order ID for follow-up queries |
| string | Transaction hash |
| int | Order state code |
| string | |
| string | Confirmation detail message |
| string | Error code on failure |
| string | Error description on failure |
| number | Block height of the transaction |
| number | Block height when the order was placed |
| string | Input token contract address |
| string | Output token contract address |
| string | Actual input consumed (smallest unit); empty if not filled |
| string | Actual output received (smallest unit); empty if not filled |
| 字段 | 类型 | 说明 |
|---|---|---|
| 字符串 | 用于后续查询的订单ID |
| 字符串 | 交易哈希 |
| 整数 | 订单状态码 |
| 字符串 | |
| 字符串 | 确认详情信息 |
| 字符串 | 失败时的错误码 |
| 字符串 | 失败时的错误描述 |
| 数字 | 交易所在区块高度 |
| 数字 | 订单提交时的区块高度 |
| 字符串 | 输入代币的合约地址 |
| 字符串 | 输出代币的合约地址 |
| 字符串 | 实际消耗的输入数量(最小单位);未完成时为空 |
| 字符串 | 实际收到的输出数量(最小单位);未完成时为空 |
Notes
注意事项
- Swap uses critical auth (API Key + signature) — CLI handles signing automatically, no manual processing needed
- After submitting a swap, use to poll for confirmation
order get - is in the smallest unit (e.g., lamports for SOL)
--amount - Use to get single-line JSON for further processing
--raw
- 兑换使用严格认证(API密钥 + 签名)——CLI会自动处理签名,无需手动操作
- 提交兑换订单后,可使用命令轮询确认状态
order get - 参数使用最小单位(例如SOL的lamports单位)
--amount - 使用参数可获取单行JSON格式的响应,便于后续处理
--raw
Execution Guidelines
执行准则
-
Currency resolution — When the user names a currency (SOL/BNB/ETH/USDC) instead of providing an address, look up its address in the Chain Currencies table and apply it automatically — never ask the user for it.
- Buy ("buy X SOL of TOKEN", "spend 0.5 USDC on TOKEN") → resolve currency to
--input-token - Sell ("sell TOKEN for SOL", "sell 50% of TOKEN to USDC") → resolve currency to
--output-token
- Buy ("buy X SOL of TOKEN", "spend 0.5 USDC on TOKEN") → resolve currency to
-
Pre-trade confirmation — Before executing, always present the key parameters (chain, from, input-token, output-token, amount or percent) to the user and ask for explicit confirmation.
swap -
Percentage sell restriction —is ONLY valid when
--percentis NOT a currency. Do NOT useinput_tokenwhen--percentis SOL/BNB/ETH (native) or USDC. This includes: "sell 50% of my SOL", "use 30% of my BNB to buy X", "spend 50% of my USDC on X" — all unsupported. Explain the restriction to the user and ask for an explicit absolute amount instead.input_token -
Chain-wallet compatibility — SOL addresses are incompatible with EVM chains (bsc/base). Warn the user and abort if the address format does not match the chain.
-
Credential sensitivity —and
GMGN_API_KEYcan directly execute trades on the linked wallet. Never log, display, or expose these values.GMGN_PRIVATE_KEY -
Order polling — After a swap, ifis not yet
confirmation.state/confirmed/failed, poll withexpiredup to 3 times at 5-second intervals before reporting a timeout. Once confirmed, display the trade result usingorder getandfilled_input_amount(convert from smallest unit using token decimals), e.g. "Spent 0.1 SOL → received 98.5 USDC" or "Sold 1000 TOKEN → received 0.08 SOL".filled_output_amount -
Block explorer links — After a successful swap, display a clickable explorer link for the returned:
hashChain Explorer sol https://solscan.io/tx/<hash>bsc https://bscscan.com/tx/<hash>base https://basescan.org/tx/<hash>eth https://etherscan.io/tx/<hash>
-
原生代币解析——当用户指定原生代币(SOL/BNB/ETH/USDC)而非合约地址时,需从“链原生代币”表中查找其地址并自动填充,切勿询问用户。
- 买入操作(如“用X SOL买入TOKEN”、“花费0.5 USDC购买TOKEN”)→ 将原生代币解析为参数
--input-token - 卖出操作(如“将TOKEN卖出换SOL”、“卖出50%的TOKEN换USDC”)→ 将原生代币解析为参数
--output-token
- 买入操作(如“用X SOL买入TOKEN”、“花费0.5 USDC购买TOKEN”)→ 将原生代币解析为
-
交易前确认——执行命令前,必须向用户展示关键参数(链、钱包地址、输入代币、输出代币、数量或百分比),并请求用户明确确认。
swap -
百分比出售限制——参数仅当输入代币不是原生代币时有效。当输入代币为SOL/BNB/ETH(原生)或USDC时,不得使用
--percent参数。例如“卖出50%的SOL”、“用30%的BNB买入X”、“花费50%的USDC购买X”均不支持。需向用户说明限制,并请求用户提供明确的绝对数量。--percent -
链与钱包兼容性——Sol链地址与EVM链(bsc/base)不兼容。若地址格式与所选链不匹配,需向用户发出警告并终止操作。
-
凭证敏感性——和
GMGN_API_KEY可直接在绑定的钱包上执行交易。切勿记录、显示或泄露这些信息。GMGN_PRIVATE_KEY -
订单轮询——提交兑换订单后,若尚未变为
confirmation.state/confirmed/failed,需使用expired命令最多轮询3次,每次间隔5秒,之后若仍未确认则报告超时。确认后,需使用order get和filled_input_amount(通过代币小数位数转换为正常单位)展示交易结果,例如“花费0.1 SOL → 收到98.5 USDC”或“卖出1000 TOKEN → 收到0.08 SOL”。filled_output_amount -
区块浏览器链接——兑换成功后,需展示交易哈希对应的可点击浏览器链接:
链 浏览器链接 sol https://solscan.io/tx/<hash>bsc https://bscscan.com/tx/<hash>base https://basescan.org/tx/<hash>eth https://etherscan.io/tx/<hash>