Okx official dex swap

From jiayaoqijia/cryptoskill · View interactive version

Okx official dex swap is an AI agent skill from the GitHub repository jiayaoqijia/cryptoskill. Use this skill to 'swap tokens', 'trade OKB for USDC', 'buy tokens', 'sell tokens', 'exchange crypto', 'convert tokens', 'swap SOL for USDC', 'get a swap quote', 'execute a trade', 'find the best swap route', 'cheapest way to swap', 'optimal swap', 'compare swap rates', '换币', '买币', '卖币', '兑换', '交易',

How to use this skill

Skill file

Onchain OS DEX Swap

5 commands for multi-chain swap aggregation — quote, approve, and execute.

Pre-flight Checks

Every time before running any onchainos command, always follow these steps in order. Do not echo routine command output to the user; only provide a brief status update when installing, updating, or handling a failure.

  1. Resolve latest stable version: Fetch the latest stable release tag from the GitHub API:

```

curl -sSL "https://api.github.com/repos/okx/onchainos-skills/releases/latest"

```

Extract the tag_name field (e.g., v1.0.5) into LATEST_TAG.

If the API call fails and onchainos is already installed locally, skip steps 2-3

and proceed to run the command (the user may be offline or rate-limited; a stale

binary is better than blocking). If onchainos is not installed, stop and

tell the user to check their network connection or install manually from

https://github.com/okx/onchainos-skills.

  1. Install or update: If onchainos is not found, or if the cache at ~/.onchainos/last_check ($env:USERPROFILE\.onchainos\last_check on Windows) is older than 12 hours:

curl -sSL "https://raw.githubusercontent.com/okx/onchainos-skills/${LATEST_TAG}/install.sh" -o /tmp/onchainos-install.sh

curl -sSL "https://github.com/okx/onchainos-skills/releases/download/${LATEST_TAG}/installer-checksums.txt" -o /tmp/installer-checksums.txt

Invoke-WebRequest -Uri "https://raw.githubusercontent.com/okx/onchainos-skills/${LATEST_TAG}/install.ps1" -OutFile "$env:TEMP\onchainos-install.ps1"

Invoke-WebRequest -Uri "https://github.com/okx/onchainos-skills/releases/download/${LATEST_TAG}/installer-checksums.txt" -OutFile "$env:TEMP\installer-checksums.txt"

The installer handles version comparison internally and only downloads the binary if needed.

  1. Verify binary integrity (once per session): Run onchainos --version to get the installed

version (e.g., 1.0.5 or 2.0.0-beta.0). Construct the installed tag as v<version>.

Download checksums.txt for the installed version's tag (not necessarily LATEST_TAG):

curl -sSL "https://github.com/okx/onchainos-skills/releases/download/v<version>/checksums.txt" -o /tmp/onchainos-checksums.txt

Look up the platform target and compare the installed binary's SHA256 against the checksum.

On mismatch, reinstall (step 2) and re-verify. If still mismatched, stop and warn.

  1. Check for skill version drift (once per session): If onchainos --version is newer

than this skill's metadata.version, display a one-time notice that the skill may be

outdated and suggest the user re-install skills via their platform's method. Do not block.

  1. Do NOT auto-reinstall on command failures. Report errors and suggest

onchainos --version or manual reinstall from https://github.com/okx/onchainos-skills.

  1. Rate limit errors. If a command hits rate limits, the shared API key may

be throttled. Suggest creating a personal key at the

OKX Developer Portal. If the

user creates a .env file, remind them to add .env to .gitignore.

Skill Routing

Quickstart

EVM Swap (quote → approve → contract-call)

# 1. Quote — sell 100 USDC for OKB on XLayer
onchainos swap quote \
  --from 0x74b7f16337b8972027f6196a17a631ac6de26d22 \
  --to 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee \
  --amount 100000000 \
  --chain xlayer
# → Expected: X.XX OKB, gas fee, price impact

# 2. Approve — ERC-20 tokens need approval before swap (skip for native OKB)
onchainos swap approve \
  --token 0x74b7f16337b8972027f6196a17a631ac6de26d22 \
  --amount 100000000 \
  --chain xlayer
# → Returns approval calldata → sign & broadcast via wallet contract-call
onchainos wallet contract-call --to 0x74b7f16337b8972027f6196a17a631ac6de26d22 --chain 196 --input-data <approve_calldata>

# 3. Swap
onchainos swap swap \
  --from 0x74b7f16337b8972027f6196a17a631ac6de26d22 \
  --to 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee \
  --amount 100000000 \
  --chain xlayer \
  --wallet <local_wallet_addr>
# → Returns swap calldata → sign & broadcast via wallet contract-call
onchainos wallet contract-call --to <tx.to> --chain 196 --value <value_in_UI_units> --input-data <tx.data> \
  --aa-dex-token-addr 0x74b7f16337b8972027f6196a17a631ac6de26d22 --aa-dex-token-amount <fromTokenAmount>

Solana Native Token Swap

# 1. Quote
onchainos swap quote \
  --from 11111111111111111111111111111111 \
  --to DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263 \
  --amount 1000000000 \
  --chain solana

# 2. Swap
onchainos swap swap \
  --from 11111111111111111111111111111111 \
  --to DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263 \
  --amount 1000000000 \
  --chain solana \
  --wallet <local_wallet_addr>
# → Returns unsigned tx → sign & broadcast via wallet contract-call
onchainos wallet contract-call --to <contract> --chain 501 --unsigned-tx <unsigned_tx>

Chain Name Support

The CLI accepts human-readable chain names and resolves them automatically.

| Chain | Name | chainIndex |

|---|---|---|

| XLayer | xlayer | 196 |

| Solana | solana | 501 |

| Ethereum | ethereum | 1 |

| Base | base | 8453 |

| BSC | bsc | 56 |

| Arbitrum | arbitrum | 42161 |

Native Token Addresses

<IMPORTANT>

CRITICAL: Each chain has a specific native token address. Using the wrong address will cause swap transactions to fail. If the user intends to swap with a native token (e.g., ETH, BNB, SOL), you MUST use the native token address from the table below — do NOT search for it via token search.

</IMPORTANT>

| Chain | Native Token Address |

|---|---|

| EVM (Ethereum, BSC, Polygon, Arbitrum, Base, etc.) | 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee |

| Solana | 11111111111111111111111111111111 |

| Sui | 0x2::sui::SUI |

| Tron | T9yD14Nj9j7xAB4dbGeiX9h8unkKHxuWwb |

| Ton | EQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAM9c |

<IMPORTANT>

CRITICAL — Solana native SOL: You MUST use 11111111111111111111111111111111 (Solana system program). NEVER use So11111111111111111111111111111111111111112 (wSOL SPL token) — it is a completely different token and WILL cause the transaction to fail and unable to be packaged on-chain. This rule has no exceptions.

</IMPORTANT>

Command Index

| # | Command | Description |

|---|---|---|

| 1 | onchainos swap chains | Get supported chains for DEX aggregator |

| 2 | onchainos swap liquidity --chain <chain> | Get available liquidity sources on a chain |

| 3 | onchainos swap approve --token ... --amount ... --chain ... | Get ERC-20 approval transaction data |

| 4 | onchainos swap quote --from ... --to ... --amount ... --chain ... | Get swap quote (read-only price estimate). No --slippage param — slippage only applies to swap swap. |

| 5 | onchainos swap swap --from ... --to ... --amount ... --chain ... --wallet ... [--slippage <pct>] [--gas-level <level>] | Get swap transaction data. --slippage sets fixed slippage (e.g. 30 for 30%); omit to use autoSlippage. |

Boundary Table

| Neighbor Skill | This Skill (okx-dex-swap) | Neighbor Handles | How to Decide |

|---|---|---|---|

| okx-dex-market | Executing swaps (quote, approve, swap) | Price queries, charts, PnL analysis | If user wants to trade → here; if user wants to check price → market |

| okx-dex-token | Swap execution | Token search, metadata, rankings | If user wants to swap → here; if user wants to find/lookup a token → token |

| okx-agentic-wallet | Quote + calldata generation + signing & broadcasting via wallet contract-call | Wallet auth, balance, send, history | This skill orchestrates the full swap flow; wallet provides signing |

Rule of thumb: okx-dex-swap orchestrates the full swap flow — quote, approve, swap calldata generation, and signing/broadcasting via onchainos wallet contract-call. It does NOT query prices or search tokens.

Cross-Skill Workflows

This skill is the execution endpoint of most user trading flows. It almost always needs input from other skills first.

Workflow A: Full Swap by Token Name (most common)

User: "Swap 1 SOL for BONK on Solana"

1. okx-dex-token    onchainos token search --query BONK --chains solana               → get BONK tokenContractAddress
       ↓ tokenContractAddress
2. okx-dex-swap     onchainos swap quote \
                      --from 11111111111111111111111111111111 \
                      --to <BONK_address> --amount 1000000000 --chain solana → get quote
       ↓ user confirms
3. okx-dex-swap     onchainos swap swap \
                      --from 11111111111111111111111111111111 \
                      --to <BONK_address> --amount 1000000000 --chain solana \
                      --wallet <addr>                                        → get swap calldata
4. onchainos wallet contract-call --to <contract> --chain 501 --unsigned-tx <unsigned_tx>

Data handoff:

Workflow B: EVM Swap with Approve+Swap

User: "Swap 100 USDC for OKB on XLayer"

1. okx-dex-token    onchainos token search --query USDC --chains xlayer               → get USDC address
2. onchainos wallet status                                                             → check login + get wallet address
3. okx-dex-swap     onchainos swap quote --from <USDC> --to 0xeeee...eeee --amount 100000000 --chain xlayer
       ↓ check isHoneyPot, taxRate, priceImpactPercent + MEV assessment
4. okx-dex-swap     onchainos swap approve --token <USDC> --amount 100000000 --chain xlayer  → get approve calldata
5. onchainos wallet contract-call --to <token_contract_address> --chain 196 --input-data <approve_calldata>  → sign & broadcast approval
6. okx-dex-swap     onchainos swap swap --from <USDC> --to 0xeeee...eeee --amount 100000000 --chain xlayer --wallet <local_wallet_addr>
7. onchainos wallet contract-call --to <contract> --chain 196 --value <value_in_UI_units> --input-data <swap_calldata> \
     --aa-dex-token-addr <fromToken.tokenContractAddress> --aa-dex-token-amount <fromTokenAmount>

Unit conversion for --value: swap swap returns tx.value in minimal units (wei), but contract-call --value expects UI units. Convert: UI_value = tx.value / 10^nativeToken.decimal (e.g., 10000000000000000 wei ÷ 10^18 = 0.01 ETH). If tx.value is "0" or empty, use "0".

Key: EVM tokens (not native) require an approve step. Skip if selling native tokens.

Workflow C: Compare Quote Then Execute

1. onchainos swap quote --from ... --to ... --amount ... --chain ...  → get quote with route info
2. Display: expected output, gas, price impact, route, MEV risk assessment
3. If price impact > 5% → warn. If isHoneyPot = true → block (buy) / warn (sell).
4. User confirms → proceed to approve (if EVM) → swap → wallet contract-call

Swap Flow

EVM Chains — Approve+Swap

Flow: quote → approve (if non-native token) → contract-call approve → swap → contract-call swap.

--chain mapping: swap commands accept chain names (e.g., xlayer), but wallet contract-call requires realChainIndex (e.g., 196). See the mapping table in "Chain Name Support" above. Common: xlayer→196, ethereum→1, bsc→56, base→8453, arbitrum→42161, solana→501.

1. onchainos swap quote ...                 → Get price, route, and spender address
2. onchainos swap approve ...               → Get approval calldata (skip for native tokens)
3. onchainos wallet contract-call --to <token_contract_address> --chain <chainIndex> --input-data <approve_calldata>
4. onchainos swap swap ...                  → Get swap calldata
5. onchainos wallet contract-call --to <contract> --chain <chainIndex> --value <value_in_UI_units> --input-data <swap_calldata> \
     --aa-dex-token-addr <fromToken.tokenContractAddress> --aa-dex-token-amount <fromTokenAmount>

Solana

1. onchainos swap quote ...              → Get price and route
2. onchainos swap swap ...               → Get swap calldata
3. onchainos wallet contract-call --to <contract> --chain 501 --unsigned-tx <unsigned_tx>

Security Rules

These rules are mandatory. Do NOT skip or bypass them.

  1. User confirmation required before every transaction. Never execute an approval or swap without displaying the full details (token, amount, estimated output, gas, price impact) and receiving explicit user confirmation. Exception: silent/automated mode with explicit user authorization.
  2. Unlimited approvals by default. In the merged approve+swap flow, use unlimited approval to reduce future approval steps. If the user explicitly requests scoped (exact-amount) approval, respect their preference.
  3. Honeypot handling. If isHoneyPot = true for the buy token, BLOCK the trade. If selling a honeypot token, WARN but allow (user may need to exit a position).
  4. Price impact gates:
  1. Tax token disclosure. If taxRate is non-zero, display the tax rate before confirmation (e.g., "This token has a 5% sell tax"). Note: taxRate is separate from slippage.
  2. No silent retries on transaction failures. If a swap or approval call fails, report the error with diagnostic summary. Do not automatically retry.

Operation Flow

Step 1: Identify Intent

Supported Transaction Scenarios

| Scenario | Status | Notes |

|---|---|---|

| Manual single trade | Supported (default) | User initiates and confirms each swap |

| Agent auto-strategy | Supported (silent mode) | Requires explicit user authorization |

Step 2: Collect Parameters

Trading Parameter Presets

Use these reference presets to guide parameter selection based on token characteristics. Agent selects the most appropriate preset based on context without asking the user.

| # | Preset | Scenario | Slippage | Gas | MEV Protection |

|---|---|---|---|---|---|

| 1 | Meme/Low-cap | Meme coins, new tokens, low liquidity | autoSlippage (ref 5%-20%) | Recommend fast | Default MEV logic(in "### MEV Protection") |

| 2 | Mainstream | BTC/ETH/SOL/major tokens, high liquidity | autoSlippage (ref 0.5%-1%) | average | Default on |

| 3 | Stablecoin | USDC/USDT/DAI pairs | autoSlippage (ref 0.1%-0.3%) | average | Default on |

| 4 | Large Trade | priceImpact ≥ 10% AND fromToken value ≥ $1,000 AND pair liquidity ≥ $10,000 | autoSlippage | average | Default on |

Custom Preset (#5+): If the user wants a custom preset, guide them with ≤5 questions (e.g., token type, risk tolerance, speed preference). Agent fills remaining parameters with sensible defaults.

Preset detection: Use marketCap, liquidity, volume, isStablecoin, and priceImpact to determine which preset applies.

Repeated failures: If a swap fails repeatedly with the current preset, ask the user before adjusting configuration (e.g., increasing slippage or switching gas level).

Wallet Address Resolution

After quote completes, resolve the wallet address from the local Agentic Wallet:

  1. Run onchainos wallet status to check login state.
  2. Not logged in → run onchainos wallet login (without email parameter) for silent login. If silent login fails (e.g., no AK configured), ask the user to provide an email for OTP login (onchainos wallet login <email> → onchainos wallet verify <otp>). After login succeeds, continue with the user's original command — do not ask the user to repeat it.
  3. Logged in, local wallet exists:
  1. Logged in, no local wallet → suggest adding one (onchainos wallet add).

Step 3: Execute

Interactive Mode (Default)
Silent / Automated Mode

Enabled only when the user has explicitly authorized automated execution (e.g., "execute my strategy automatically", "auto-swap when price hits X"). Three mandatory rules:

  1. Explicit authorization: User must clearly opt in to silent mode. Never assume silent mode.
  2. Risk gate pause: Even in silent mode, BLOCK-level risk items (see Risk Controls) must halt execution and notify the user.
  3. Execution log: Log every silent transaction with: timestamp, token pair, amount, slippage used, txHash, success/fail status. Present the log to the user on request or at session end.

Step 3a: Transaction Signing & Broadcasting

After onchainos swap swap returns successfully, use onchainos wallet contract-call to sign and broadcast in one step. Note: wallet contract-call --chain requires realChainIndex (e.g., 196 for XLayer, 1 for Ethereum, 501 for Solana), not the chain name used in swap commands.

The contract-call command handles TEE signing and broadcasting internally — no separate broadcast step is needed.

--value unit conversion: swap swap returns tx.value in minimal units (wei/lamports), but contract-call --value expects UI units. Convert: value_in_UI_units = tx.value / 10^nativeToken.decimal (e.g., 18 for ETH, 9 for SOL). If tx.value is "0" or empty, use "0".

Step 3b: Result Messaging

Use business-level language for success messages:

Do NOT use chain/broadcast-level wording such as "Transaction confirmed on-chain", "Successfully broadcast", "On-chain success", etc. The user cares about the business outcome (approve / swap done), not the underlying broadcast mechanics.

Step 4: Suggest Next Steps

After displaying results, suggest 2-3 relevant follow-up actions:

| Just completed | Suggest |

|---|---|

| swap quote (not yet confirmed) | 1. View price chart before deciding → okx-dex-market 2. Proceed with swap → continue approve + swap (this skill) |

| Swap executed successfully | 1. View transaction details → provide explorer link (e.g. https://<explorer>/tx/<txHash>) 2. Check price of the token just received → okx-dex-market 3. Swap another token → new swap flow (this skill) |

| swap liquidity | 1. Get a swap quote → onchainos swap quote (this skill) |

Present conversationally, e.g.: "Swap complete! Would you like to check your updated balance?" — never expose skill names or endpoint paths to the user.

Additional Resources

For detailed parameter tables, return field schemas, and usage examples for all 5 commands, consult:

To search for specific command details: grep -n "onchainos swap <command>" references/cli-reference.md

Risk Controls

| Risk Item | Buy | Sell | Notes |

|---|---|---|---|

| Honeypot (isHoneyPot=true) | BLOCK | WARN (allow exit) | Selling allowed for stop-loss scenarios |

| High tax rate (>10%) | WARN | WARN | Display exact tax rate |

| Price impact >5% | WARN | WARN | Suggest splitting trade |

| Price impact >10% | BLOCK | WARN | Strongly discourage, allow sell for exit |

| No quote available | CANNOT | CANNOT | Token may be unlisted or zero liquidity |

| Black/flagged address | BLOCK | BLOCK | Address flagged by security services |

| New token (<24h) | WARN | PROCEED | Extra caution on buy side |

| Insufficient liquidity | CANNOT | CANNOT | Liquidity too low to execute trade |

| Token type not supported | CANNOT | CANNOT | Inform user, suggest alternative |

Legend: BLOCK = halt, require explicit override · WARN = display warning, ask confirmation · CANNOT = operation impossible · PROCEED = allow with info

MEV Protection

Two conditions (OR — either triggers enable):

Disable only when BOTH are below threshold.

If toTokenPrice or fromTokenPrice unavailable/0 → enable by default.

| Chain | MEV Protection | Threshold | Path |

|---|---|---|---|

| Ethereum | Yes | $2,000 | wallet contract-call --mev-protection |

| Solana | Yes | $1,000 | /swap with tips param (SOL, 0.0000000001–2) + computeUnitPrice=0 → response returns jitoCalldata → wallet contract-call --mev-protection --jito-unsigned-tx <jito_base58_tx> |

| BNB Chain | Yes | $200 | wallet contract-call --mev-protection |

| Base | Yes | $200 | wallet contract-call --mev-protection |

| Others | No | — | — |

MEV protection is handled directly by onchainos wallet contract-call --mev-protection.

Failure Diagnostics

When a swap transaction fails (broadcast error, on-chain revert, or timeout), generate a diagnostic summary before reporting to the user:

Diagnostic Summary:
  txHash:        <hash or "simulation failed">
  chain:         <chain name (chainIndex)>
  errorCode:     <API or on-chain error code>
  errorMessage:  <human-readable error>
  tokenPair:     <fromToken symbol> → <toToken symbol>
  amount:        <amount in UI units>
  slippage:      <value used, or "auto">
  mevProtection: <on|off>
  walletAddress: <address>
  timestamp:     <ISO 8601>
  cliVersion:    <onchainos --version>

This helps debug issues without requiring the user to gather info manually.

Edge Cases

Items covered by the Risk Controls table (honeypot, price impact, tax, new tokens, insufficient liquidity, no quote) are not repeated here. Refer to Risk Controls for action levels.

Amount Display Rules

Global Notes

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.