{
  "schemaVersion": "1.0",
  "item": {
    "slug": "easy-swap",
    "name": "DEX Swap & Broadcast",
    "source": "tencent",
    "type": "skill",
    "category": "开发工具",
    "sourceUrl": "https://clawhub.ai/AaronLLee/easy-swap",
    "canonicalUrl": "https://clawhub.ai/AaronLLee/easy-swap",
    "targetPlatform": "OpenClaw"
  },
  "install": {
    "downloadMode": "redirect",
    "downloadUrl": "/downloads/easy-swap",
    "sourceDownloadUrl": "https://wry-manatee-359.convex.site/api/v1/download?slug=easy-swap",
    "sourcePlatform": "tencent",
    "targetPlatform": "OpenClaw",
    "installMethod": "Manual import",
    "extraction": "Extract archive",
    "prerequisites": [
      "OpenClaw"
    ],
    "packageFormat": "ZIP package",
    "includedAssets": [
      "SKILL.md",
      "scripts/easy_swap.py"
    ],
    "primaryDoc": "SKILL.md",
    "quickSetup": [
      "Download the package from Yavira.",
      "Extract the archive and review SKILL.md first.",
      "Import or place the package into your OpenClaw setup."
    ],
    "agentAssist": {
      "summary": "Hand the extracted package to your coding agent with a concrete install brief instead of figuring it out manually.",
      "steps": [
        "Download the package from Yavira.",
        "Extract it into a folder your agent can access.",
        "Paste one of the prompts below and point your agent at the extracted folder."
      ],
      "prompts": [
        {
          "label": "New install",
          "body": "I downloaded a skill package from Yavira. Read SKILL.md from the extracted folder and install it by following the included instructions. Tell me what you changed and call out any manual steps you could not complete."
        },
        {
          "label": "Upgrade existing",
          "body": "I downloaded an updated skill package from Yavira. Read SKILL.md from the extracted folder, compare it with my current installation, and upgrade it while preserving any custom configuration unless the package docs explicitly say otherwise. Summarize what changed and any follow-up checks I should run."
        }
      ]
    },
    "sourceHealth": {
      "source": "tencent",
      "status": "healthy",
      "reason": "direct_download_ok",
      "recommendedAction": "download",
      "checkedAt": "2026-04-30T16:55:25.780Z",
      "expiresAt": "2026-05-07T16:55:25.780Z",
      "httpStatus": 200,
      "finalUrl": "https://wry-manatee-359.convex.site/api/v1/download?slug=network",
      "contentType": "application/zip",
      "probeMethod": "head",
      "details": {
        "probeUrl": "https://wry-manatee-359.convex.site/api/v1/download?slug=network",
        "contentDisposition": "attachment; filename=\"network-1.0.0.zip\"",
        "redirectLocation": null,
        "bodySnippet": null
      },
      "scope": "source",
      "summary": "Source download looks usable.",
      "detail": "Yavira can redirect you to the upstream package for this source.",
      "primaryActionLabel": "Download for OpenClaw",
      "primaryActionHref": "/downloads/easy-swap"
    },
    "validation": {
      "installChecklist": [
        "Use the Yavira download entry.",
        "Review SKILL.md after the package is downloaded.",
        "Confirm the extracted package contains the expected setup assets."
      ],
      "postInstallChecks": [
        "Confirm the extracted package includes the expected docs or setup files.",
        "Validate the skill or prompts are available in your target agent workspace.",
        "Capture any manual follow-up steps the agent could not complete."
      ]
    },
    "downloadPageUrl": "https://openagent3.xyz/downloads/easy-swap",
    "agentPageUrl": "https://openagent3.xyz/skills/easy-swap/agent",
    "manifestUrl": "https://openagent3.xyz/skills/easy-swap/agent.json",
    "briefUrl": "https://openagent3.xyz/skills/easy-swap/agent.md"
  },
  "agentAssist": {
    "summary": "Hand the extracted package to your coding agent with a concrete install brief instead of figuring it out manually.",
    "steps": [
      "Download the package from Yavira.",
      "Extract it into a folder your agent can access.",
      "Paste one of the prompts below and point your agent at the extracted folder."
    ],
    "prompts": [
      {
        "label": "New install",
        "body": "I downloaded a skill package from Yavira. Read SKILL.md from the extracted folder and install it by following the included instructions. Tell me what you changed and call out any manual steps you could not complete."
      },
      {
        "label": "Upgrade existing",
        "body": "I downloaded an updated skill package from Yavira. Read SKILL.md from the extracted folder, compare it with my current installation, and upgrade it while preserving any custom configuration unless the package docs explicitly say otherwise. Summarize what changed and any follow-up checks I should run."
      }
    ]
  },
  "documentation": {
    "source": "clawhub",
    "primaryDoc": "SKILL.md",
    "sections": [
      {
        "title": "Overview",
        "body": "This skill generates production-ready code for the complete on-chain swap flow using the OKX DEX Aggregator:\n\n┌─────────┐     ┌──────────┐     ┌──────────┐     ┌───────────┐\n│  Quote   │ ──▶ │   Swap   │ ──▶ │   Sign   │ ──▶ │ Broadcast │\n│ (optional│     │ API Call │     │   Tx     │     │  to Chain │\n│  preview)│     │          │     │          │     │           │\n└─────────┘     └──────────┘     └──────────┘     └───────────┘\n\nTwo API endpoints involved:\n\nStepEndpointMethodPurposeSwap/api/v6/dex/aggregator/swapGETGet transaction calldata for the swapBroadcast/api/v6/dex/pre-transaction/broadcast-transactionPOSTSubmit signed transaction to the chain\n\nKey features:\n\nFull swap lifecycle with transaction signing\nAuto-slippage calculation based on market conditions\nMEV (sandwich attack) protection on ETH, BSC, SOL, BASE\nJito tips for Solana priority transactions\nToken approval handling (ERC-20 approve)\nCommission/referral fee splitting\nPrice impact protection with configurable thresholds"
      },
      {
        "title": "Required Credentials",
        "body": "OKX_ACCESS_KEY — API key\nOKX_SECRET_KEY — Secret key for HMAC signing\nOKX_PASSPHRASE — Account passphrase"
      },
      {
        "title": "Wallet Requirements",
        "body": "EVM chains: Private key or a signer (e.g., web3.py Account, ethers.js Wallet)\nSolana: Keypair for transaction signing\nThe wallet must have sufficient balance of the fromToken and native token for gas"
      },
      {
        "title": "Environment",
        "body": "Python: requests, web3 (for EVM signing), solders / solana-py (for Solana)\nNode.js: axios, ethers (for EVM signing), @solana/web3.js (for Solana)"
      },
      {
        "title": "Step 1: Call the Swap API",
        "body": "Endpoint:\n\nGET https://web3.okx.com/api/v6/dex/aggregator/swap\n\nRequired parameters:\n\nParameterTypeRequiredDescriptionchainIndexStringYesChain ID (e.g., 1 = Ethereum, 501 = Solana)amountStringYesAmount in raw units with decimals (e.g., 1000000 for 1 USDT)swapModeStringYesexactIn (default) or exactOutfromTokenAddressStringYesSell token contract addresstoTokenAddressStringYesBuy token contract addressslippagePercentStringYesSlippage tolerance (e.g., 0.5 = 0.5%). EVM: 0-100, Solana: 0 to <100userWalletAddressStringYesUser's wallet address that will sign and send the tx\n\nImportant optional parameters:\n\nParameterTypeDescriptionapproveTransactionBooleanSet true to get ERC-20 approval calldata in responseapproveAmountStringCustom approval amount (raw units). If omitted, approves exact swap amountswapReceiverAddressStringRecipient address if different from senderfeePercentStringCommission fee %. Max 3% (EVM) / 10% (Solana)fromTokenReferrerWalletAddressStringWallet to receive fromToken commissiontoTokenReferrerWalletAddressStringWallet to receive toToken commissionautoSlippageBooleanAuto-calculate optimal slippage (overrides slippagePercent)maxAutoslippagePercentStringCap for auto-slippagepriceImpactProtectionPercentStringMax price impact allowed (0-100, default 90)gasLevelStringslow, average (default), or fastgaslimitStringCustom gas limit in wei (EVM only)dexIdsStringRestrict to specific DEX IDs (comma-separated)excludeDexIdsStringExclude specific DEX IDs (comma-separated)directRouteBooleanSingle-pool routing only (Solana only)disableRFQStringDisable time-sensitive RFQ liquidity sourcescallDataMemoStringCustom 64-byte hex data to include on-chain\n\nSolana-specific parameters:\n\nParameterTypeDescriptioncomputeUnitPriceStringPriority fee (like gasPrice on EVM)computeUnitLimitStringCompute budget (like gasLimit on EVM)tipsStringJito tips in SOL (min 0.0000000001, max 2). Set computeUnitPrice=0 when using tips\n\nSwap API response structure:\n\n{\n  \"code\": \"0\",\n  \"data\": [{\n    \"routerResult\": {\n      \"chainIndex\": \"1\",\n      \"fromToken\": { \"tokenSymbol\": \"USDC\", \"decimal\": \"6\", ... },\n      \"toToken\": { \"tokenSymbol\": \"WBTC\", \"decimal\": \"8\", ... },\n      \"fromTokenAmount\": \"100000000000\",\n      \"toTokenAmount\": \"90281915\",\n      \"tradeFee\": \"1.35\",\n      \"estimateGasFee\": \"1248837\",\n      \"priceImpactPercent\": \"0.07\",\n      \"dexRouterList\": [...]\n    },\n    \"tx\": {\n      \"from\": \"0x77660f...\",\n      \"to\": \"0x5E1f62...\",\n      \"value\": \"0\",\n      \"data\": \"0xf2c42696...\",\n      \"gas\": \"1248837\",\n      \"gasPrice\": \"557703374\",\n      \"maxPriorityFeePerGas\": \"500000000\",\n      \"minReceiveAmount\": \"90191633\",\n      \"slippagePercent\": \"0.1\",\n      \"signatureData\": [...]\n    }\n  }],\n  \"msg\": \"\"\n}\n\nKey fields in tx object:\n\nFieldDescriptionfromSender wallet addresstoOKX DEX router contract addressdataTransaction calldata (the swap instruction)valueNative token amount to send (in wei). \"0\" for ERC-20 swapsgasEstimated gas limit (already padded +50%)gasPriceGas price in weimaxPriorityFeePerGasEIP-1559 priority feeminReceiveAmountMinimum output at max slippagesignatureDataApproval calldata (if approveTransaction=true) or Jito tips calldata"
      },
      {
        "title": "Step 2: Handle Token Approval (EVM Only)",
        "body": "For ERC-20 tokens (not native ETH/BNB), you need to approve the DEX router to spend your tokens BEFORE the swap.\n\nWhen approveTransaction=true in the request:\nThe response tx.signatureData contains the approval info:\n\n{\n  \"approveContract\": \"0x40aA958dd87FC8305b97f2BA922CDdCa374bcD7f\",\n  \"approveTxCalldata\": \"0x095ea7b3...\"\n}\n\nApproval flow:\n\nParse signatureData to get approveContract and approveTxCalldata\nSend an approval transaction: to=approveContract, data=approveTxCalldata\nWait for approval tx to be confirmed\nThen send the swap transaction\n\nIMPORTANT: For native tokens (ETH, BNB, etc. using 0xeeee...eeee), no approval is needed."
      },
      {
        "title": "Step 3: Sign the Transaction",
        "body": "EVM chains (Python / web3.py):\n\ntx_params = {\n    \"from\": tx_data[\"from\"],\n    \"to\": tx_data[\"to\"],\n    \"value\": int(tx_data[\"value\"]),\n    \"data\": tx_data[\"data\"],\n    \"gas\": int(tx_data[\"gas\"]),\n    \"gasPrice\": int(tx_data[\"gasPrice\"]),\n    \"nonce\": w3.eth.get_transaction_count(wallet_address),\n    \"chainId\": chain_id,\n}\nsigned = w3.eth.account.sign_transaction(tx_params, private_key)\nsigned_tx_hex = signed.raw_transaction.hex()\n\nEVM chains (Node.js / ethers.js):\n\nconst tx = {\n  from: txData.from,\n  to: txData.to,\n  value: txData.value,\n  data: txData.data,\n  gasLimit: txData.gas,\n  gasPrice: txData.gasPrice,\n  nonce: await provider.getTransactionCount(walletAddress),\n  chainId: chainId,\n};\nconst signedTx = await wallet.signTransaction(tx);\n\nSolana:\nUse solders or @solana/web3.js to deserialize, sign, and serialize the transaction from tx.data."
      },
      {
        "title": "Step 4: Broadcast the Signed Transaction",
        "body": "Endpoint:\n\nPOST https://web3.okx.com/api/v6/dex/pre-transaction/broadcast-transaction\n\nIMPORTANT: This is a POST request with a JSON body (unlike the GET-based swap/quote endpoints).\n\nRequest body:\n\nParameterTypeRequiredDescriptionsignedTxStringYesThe hex-encoded signed transaction stringchainIndexStringYesChain ID (e.g., \"1\" for Ethereum)addressStringYesSender wallet addressextraDataStringNoJSON string with extra options (see below)\n\nextraData options (JSON string):\n\nFieldTypeDescriptionenableMevProtectionBooleanEnable MEV (sandwich) protection. Supported: ETH, BSC, SOL, BASEjitoSignedTxStringBase58-encoded signed Jito transaction (Solana only). Required when tips > 0\n\nSigning algorithm for POST requests:\n\nprehash = timestamp + \"POST\" + request_path + json_body\nsignature = Base64(HMAC-SHA256(secret_key, prehash))\n\nNote: For POST, the request_path has NO query string. The JSON body is appended directly to the prehash string.\n\nBroadcast response:\n\n{\n  \"code\": \"0\",\n  \"data\": [{\n    \"orderId\": \"0e1d79837afce1e149b6ab54b6e2edce8130c3f8\",\n    \"txHash\": \"0xd394f356a16b618ed839c66c935c9cccc5dde0af832ff9b468677eea38759db5\"\n  }],\n  \"msg\": \"\"\n}\n\nFieldDescriptionorderIdOKX internal order tracking IDtxHashOn-chain transaction hash. Use this to check status on block explorer"
      },
      {
        "title": "Step 5: Verify Transaction",
        "body": "After broadcasting, verify the transaction was confirmed:\n\nEVM: Check txHash on Etherscan / block explorer, or use web3.eth.wait_for_transaction_receipt()\nSolana: Check on Solscan, or use connection.confirmTransaction()"
      },
      {
        "title": "Security",
        "body": "NEVER hardcode private keys in code. Use environment variables, keystore files, or hardware wallets.\nNEVER log private keys, signed transactions, or secret keys in production.\nAlways validate token addresses and amounts before sending.\nCheck isHoneyPot on both tokens before proceeding.\nUse priceImpactProtectionPercent (recommend setting to 10 or lower for safety)."
      },
      {
        "title": "Slippage Configuration",
        "body": "Stable pairs (USDC/USDT): 0.1% - 0.5%\nMajor pairs (ETH/USDC): 0.5% - 1%\nVolatile/low-liquidity tokens: 1% - 5%\nMeme coins: 5% - 15% (or higher, but be cautious)\nUse autoSlippage=true with maxAutoslippagePercent for optimal auto-calculation."
      },
      {
        "title": "MEV Protection",
        "body": "Enable enableMevProtection: true for large trades on ETH, BSC, SOL, BASE.\nOn Solana, combine with Jito tips for priority + MEV protection.\nWhen using Jito tips, set computeUnitPrice=0 to avoid wasting fees."
      },
      {
        "title": "Token Approval Strategy",
        "body": "For one-time swaps: approve exact amount (approveAmount = swap amount).\nFor repeated swaps: consider a larger approval (e.g., max uint256) to save gas on future swaps, but understand the security trade-off.\nAlways check existing allowance before sending a new approval."
      },
      {
        "title": "Uni V3 Liquidity Edge Case",
        "body": "When swapping through Uniswap V3 pools, if the liquidity for the pair is drained mid-swap, the router will only consume part of your input tokens. The OKX DEX Router smart contract will automatically refund the remainder. Ensure your integration contract supports receiving token refunds."
      },
      {
        "title": "Amount Handling",
        "body": "Always use strings for amounts to avoid floating-point precision loss.\nPython: int() for calculations, never float().\nJavaScript: BigInt or ethers.parseUnits() for amounts.\nFormula: raw_amount = human_amount * 10^decimals"
      },
      {
        "title": "Error Handling",
        "body": "Check response[\"code\"] == \"0\" before processing.\nIf broadcast fails, do NOT automatically retry without checking nonce — you may double-spend.\nCommon errors:\n\n401 = Signature mismatch (check POST signing includes body)\n429 = Rate limited\nSwap returns error = insufficient balance, invalid params, or liquidity issue\nBroadcast returns error = already executed, nonce too low, insufficient gas"
      },
      {
        "title": "Commission/Referral Fees",
        "body": "Only ONE of fromTokenReferrerWalletAddress or toTokenReferrerWalletAddress per tx.\nSolana: referrer wallet must have SOL deposited for activation.\nTON: limited DEX support (Stonfi V2, Dedust).\nBSC: no commission through Four.meme swaps."
      },
      {
        "title": "Example 1: Python — Complete EVM Swap Flow",
        "body": "import os, hmac, hashlib, base64, json, requests\nfrom datetime import datetime, timezone\nfrom urllib.parse import urlencode\nfrom web3 import Web3\n\n# === Configuration ===\nAPI_KEY = os.environ[\"OKX_ACCESS_KEY\"]\nSECRET_KEY = os.environ[\"OKX_SECRET_KEY\"]\nPASSPHRASE = os.environ[\"OKX_PASSPHRASE\"]\nPRIVATE_KEY = os.environ[\"WALLET_PRIVATE_KEY\"]\n\nBASE_URL = \"https://web3.okx.com\"\nCHAIN_INDEX = \"1\"  # Ethereum\nCHAIN_ID = 1\nRPC_URL = \"https://eth.llamarpc.com\"\n\nw3 = Web3(Web3.HTTPProvider(RPC_URL))\naccount = w3.eth.account.from_key(PRIVATE_KEY)\nWALLET = account.address\n\n\ndef _sign_request(timestamp, method, request_path, body=\"\"):\n    prehash = timestamp + method + request_path + body\n    mac = hmac.new(SECRET_KEY.encode(), prehash.encode(), hashlib.sha256)\n    return base64.b64encode(mac.digest()).decode()\n\n\ndef _headers(method, request_path, body=\"\"):\n    timestamp = datetime.now(timezone.utc).strftime(\"%Y-%m-%dT%H:%M:%S.%f\")[:-3] + \"Z\"\n    sig = _sign_request(timestamp, method, request_path, body)\n    return {\n        \"OK-ACCESS-KEY\": API_KEY,\n        \"OK-ACCESS-SIGN\": sig,\n        \"OK-ACCESS-PASSPHRASE\": PASSPHRASE,\n        \"OK-ACCESS-TIMESTAMP\": timestamp,\n        \"Content-Type\": \"application/json\",\n    }\n\n\n# --- Step 1: Get swap calldata ---\ndef get_swap_data(from_token, to_token, amount, slippage=\"0.5\"):\n    params = {\n        \"chainIndex\": CHAIN_INDEX,\n        \"fromTokenAddress\": from_token,\n        \"toTokenAddress\": to_token,\n        \"amount\": amount,\n        \"swapMode\": \"exactIn\",\n        \"slippagePercent\": slippage,\n        \"userWalletAddress\": WALLET,\n        \"approveTransaction\": \"true\",\n    }\n    query = urlencode(params)\n    path = f\"/api/v6/dex/aggregator/swap?{query}\"\n    headers = _headers(\"GET\", path)\n\n    resp = requests.get(BASE_URL + path, headers=headers, timeout=30)\n    resp.raise_for_status()\n    data = resp.json()\n\n    if data[\"code\"] != \"0\":\n        raise Exception(f\"Swap API error: {data['msg']}\")\n    return data[\"data\"][0]\n\n\n# --- Step 2: Handle approval (if needed) ---\ndef send_approval_if_needed(swap_result):\n    sig_data = swap_result[\"tx\"].get(\"signatureData\", [])\n    if not sig_data:\n        return None\n\n    for item in sig_data:\n        parsed = json.loads(item) if isinstance(item, str) else item\n        if \"approveContract\" in parsed:\n            approve_tx = {\n                \"from\": WALLET,\n                \"to\": Web3.to_checksum_address(parsed[\"approveContract\"]),\n                \"data\": parsed[\"approveTxCalldata\"],\n                \"gas\": 60000,\n                \"gasPrice\": w3.eth.gas_price,\n                \"nonce\": w3.eth.get_transaction_count(WALLET),\n                \"chainId\": CHAIN_ID,\n            }\n            signed = w3.eth.account.sign_transaction(approve_tx, PRIVATE_KEY)\n            tx_hash = w3.eth.send_raw_transaction(signed.raw_transaction)\n            receipt = w3.eth.wait_for_transaction_receipt(tx_hash, timeout=120)\n            print(f\"Approval confirmed: {tx_hash.hex()}\")\n            return receipt\n    return None\n\n\n# --- Step 3: Sign the swap transaction ---\ndef sign_swap_tx(swap_result):\n    tx_data = swap_result[\"tx\"]\n    tx_params = {\n        \"from\": Web3.to_checksum_address(tx_data[\"from\"]),\n        \"to\": Web3.to_checksum_address(tx_data[\"to\"]),\n        \"value\": int(tx_data[\"value\"]),\n        \"data\": tx_data[\"data\"],\n        \"gas\": int(tx_data[\"gas\"]),\n        \"gasPrice\": int(tx_data[\"gasPrice\"]),\n        \"nonce\": w3.eth.get_transaction_count(WALLET),\n        \"chainId\": CHAIN_ID,\n    }\n    signed = w3.eth.account.sign_transaction(tx_params, PRIVATE_KEY)\n    return signed.raw_transaction.hex()\n\n\n# --- Step 4: Broadcast via OKX ---\ndef broadcast_tx(signed_tx_hex, enable_mev=True):\n    path = \"/api/v6/dex/pre-transaction/broadcast-transaction\"\n    body_dict = {\n        \"chainIndex\": CHAIN_INDEX,\n        \"address\": WALLET,\n        \"signedTx\": signed_tx_hex,\n    }\n    if enable_mev:\n        body_dict[\"extraData\"] = json.dumps({\"enableMevProtection\": True})\n\n    body_str = json.dumps(body_dict)\n    headers = _headers(\"POST\", path, body_str)\n\n    resp = requests.post(BASE_URL + path, headers=headers, data=body_str, timeout=30)\n    resp.raise_for_status()\n    data = resp.json()\n\n    if data[\"code\"] != \"0\":\n        raise Exception(f\"Broadcast error: {data['msg']}\")\n    return data[\"data\"][0]\n\n\n# === Execute full swap ===\nif __name__ == \"__main__\":\n    ETH = \"0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee\"\n    USDC = \"0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48\"\n    amount = str(10 ** 17)  # 0.1 ETH\n\n    print(\"1. Getting swap data...\")\n    swap = get_swap_data(ETH, USDC, amount, slippage=\"0.5\")\n\n    router = swap[\"routerResult\"]\n    to_dec = int(router[\"toToken\"][\"decimal\"])\n    out_human = int(router[\"toTokenAmount\"]) / (10 ** to_dec)\n    print(f\"   Expected output: {out_human:,.2f} {router['toToken']['tokenSymbol']}\")\n    print(f\"   Price impact: {router.get('priceImpactPercent', 'N/A')}%\")\n    print(f\"   Min receive: {int(swap['tx']['minReceiveAmount']) / (10 ** to_dec):,.2f}\")\n\n    # Honeypot check\n    if router[\"toToken\"].get(\"isHoneyPot\"):\n        print(\"   HONEYPOT DETECTED — aborting!\")\n        exit(1)\n\n    print(\"2. Handling approval...\")\n    send_approval_if_needed(swap)\n\n    print(\"3. Signing swap transaction...\")\n    signed_hex = sign_swap_tx(swap)\n\n    print(\"4. Broadcasting with MEV protection...\")\n    result = broadcast_tx(signed_hex, enable_mev=True)\n    print(f\"   Order ID: {result['orderId']}\")\n    print(f\"   Tx Hash: {result['txHash']}\")\n    print(f\"   View: https://etherscan.io/tx/{result['txHash']}\")"
      },
      {
        "title": "Example 2: Node.js — Complete EVM Swap Flow",
        "body": "const crypto = require(\"crypto\");\nconst https = require(\"https\");\nconst { ethers } = require(\"ethers\");\n\nconst API_KEY = process.env.OKX_ACCESS_KEY;\nconst SECRET_KEY = process.env.OKX_SECRET_KEY;\nconst PASSPHRASE = process.env.OKX_PASSPHRASE;\nconst PRIVATE_KEY = process.env.WALLET_PRIVATE_KEY;\n\nconst BASE_URL = \"https://web3.okx.com\";\nconst CHAIN_INDEX = \"1\";\nconst CHAIN_ID = 1;\n\nconst provider = new ethers.JsonRpcProvider(\"https://eth.llamarpc.com\");\nconst wallet = new ethers.Wallet(PRIVATE_KEY, provider);\n\nfunction sign(timestamp, method, path, body = \"\") {\n  return crypto\n    .createHmac(\"sha256\", SECRET_KEY)\n    .update(timestamp + method + path + body)\n    .digest(\"base64\");\n}\n\nfunction headers(method, path, body = \"\") {\n  const ts = new Date().toISOString();\n  return {\n    \"OK-ACCESS-KEY\": API_KEY,\n    \"OK-ACCESS-SIGN\": sign(ts, method, path, body),\n    \"OK-ACCESS-PASSPHRASE\": PASSPHRASE,\n    \"OK-ACCESS-TIMESTAMP\": ts,\n    \"Content-Type\": \"application/json\",\n  };\n}\n\nasync function getSwapData(fromToken, toToken, amount, slippage = \"0.5\") {\n  const params = new URLSearchParams({\n    chainIndex: CHAIN_INDEX,\n    fromTokenAddress: fromToken,\n    toTokenAddress: toToken,\n    amount,\n    swapMode: \"exactIn\",\n    slippagePercent: slippage,\n    userWalletAddress: wallet.address,\n    approveTransaction: \"true\",\n  });\n  const path = `/api/v6/dex/aggregator/swap?${params}`;\n  const h = headers(\"GET\", path);\n\n  const resp = await fetch(`${BASE_URL}${path}`, { headers: h });\n  const data = await resp.json();\n  if (data.code !== \"0\") throw new Error(`Swap error: ${data.msg}`);\n  return data.data[0];\n}\n\nasync function broadcastTx(signedTx, enableMev = true) {\n  const path = \"/api/v6/dex/pre-transaction/broadcast-transaction\";\n  const bodyObj = {\n    chainIndex: CHAIN_INDEX,\n    address: wallet.address,\n    signedTx,\n    ...(enableMev && {\n      extraData: JSON.stringify({ enableMevProtection: true }),\n    }),\n  };\n  const bodyStr = JSON.stringify(bodyObj);\n  const h = headers(\"POST\", path, bodyStr);\n\n  const resp = await fetch(`${BASE_URL}${path}`, {\n    method: \"POST\",\n    headers: h,\n    body: bodyStr,\n  });\n  const data = await resp.json();\n  if (data.code !== \"0\") throw new Error(`Broadcast error: ${data.msg}`);\n  return data.data[0];\n}\n\nasync function main() {\n  const ETH = \"0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee\";\n  const USDC = \"0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48\";\n  const amount = (10n ** 17n).toString(); // 0.1 ETH\n\n  console.log(\"1. Getting swap data...\");\n  const swap = await getSwapData(ETH, USDC, amount);\n\n  const txData = swap.tx;\n  console.log(`   Min receive: ${txData.minReceiveAmount}`);\n\n  console.log(\"2. Signing transaction...\");\n  const tx = {\n    from: txData.from,\n    to: txData.to,\n    value: txData.value,\n    data: txData.data,\n    gasLimit: txData.gas,\n    gasPrice: txData.gasPrice,\n    nonce: await provider.getTransactionCount(wallet.address),\n    chainId: CHAIN_ID,\n  };\n  const signedTx = await wallet.signTransaction(tx);\n\n  console.log(\"3. Broadcasting with MEV protection...\");\n  const result = await broadcastTx(signedTx);\n  console.log(`   Tx Hash: ${result.txHash}`);\n}\n\nmain().catch(console.error);"
      },
      {
        "title": "Example 3: Solana Swap with Jito Tips",
        "body": "When using Jito tips on Solana:\n\nSet tips parameter (e.g., \"0.001\" SOL) in the swap request\nSet computeUnitPrice=0 (avoid double-paying for priority)\nThe signatureData in response contains the Jito tips calldata\nSign BOTH the main transaction AND the Jito transaction\nBroadcast with both signedTx and jitoSignedTx in extraData\n\n# Solana-specific broadcast with Jito\nbody = {\n    \"chainIndex\": \"501\",\n    \"address\": solana_wallet_pubkey,\n    \"signedTx\": base58_signed_main_tx,\n    \"extraData\": json.dumps({\n        \"enableMevProtection\": True,\n        \"jitoSignedTx\": base58_signed_jito_tx\n    })\n}\n\nIMPORTANT: For Solana, signedTx and jitoSignedTx must BOTH be provided when using tips."
      },
      {
        "title": "Troubleshooting",
        "body": "ProblemCauseSolution401 Unauthorized on broadcastPOST signing errorEnsure prehash = timestamp + \"POST\" + path + jsonBody. The body must be the exact JSON string.401 Unauthorized on swapGET signing errorEnsure prehash = timestamp + \"GET\" + path_with_query_string.Approval tx failsInsufficient ETH for gasEnsure wallet has native token for gas fees.Swap tx revertsSlippage exceededIncrease slippagePercent or use autoSlippage=true.minReceiveAmount is 0Extreme slippage or bad paramsCheck token addresses and amounts. Reduce trade size.Broadcast returns no txHashMEV protection routing delayWait and check orderId status. MEV-protected txs may take longer.\"Nonce too low\" on broadcastTx already sent or nonce reusedFetch fresh nonce before signing. Never reuse nonces.Solana broadcast failsMissing jitoSignedTxWhen tips > 0, you must provide both signedTx and jitoSignedTx.Token refund not receivedUni V3 liquidity drainedEnsure your contract supports receiving token refunds from the router.Commission not working on BSCFour.meme restrictionCommission is not supported for swaps through Four.meme on BSC.priceImpactPercent very negativeLow liquidityReduce amount or split into multiple smaller swaps."
      },
      {
        "title": "Reference: POST vs GET Signing",
        "body": "The OKX API uses different signing for GET and POST:\n\nMethodPrehash FormatGETtimestamp + \"GET\" + path_with_queryPOSTtimestamp + \"POST\" + path + json_body\n\nThe Swap endpoint is GET, the Broadcast endpoint is POST. Getting this wrong is the #1 cause of 401 errors."
      },
      {
        "title": "Reference: MEV Protection Support",
        "body": "ChainenableMevProtectionJito TipsEthereumYesNoBSCYesNoSolanaYesYesBaseYesNoOthersNot yetNot yet"
      },
      {
        "title": "Reference: Common Chain IDs",
        "body": "ChainchainIndexChain ID (for tx signing)Ethereum11BSC5656Polygon137137Arbitrum4216142161Optimism1010Base84538453Avalanche4311443114Solana501N/AUnichain130130"
      }
    ],
    "body": "OKX DEX Swap & Broadcast Skill\nOverview\n\nThis skill generates production-ready code for the complete on-chain swap flow using the OKX DEX Aggregator:\n\n┌─────────┐     ┌──────────┐     ┌──────────┐     ┌───────────┐\n│  Quote   │ ──▶ │   Swap   │ ──▶ │   Sign   │ ──▶ │ Broadcast │\n│ (optional│     │ API Call │     │   Tx     │     │  to Chain │\n│  preview)│     │          │     │          │     │           │\n└─────────┘     └──────────┘     └──────────┘     └───────────┘\n\n\nTwo API endpoints involved:\n\nStep\tEndpoint\tMethod\tPurpose\nSwap\t/api/v6/dex/aggregator/swap\tGET\tGet transaction calldata for the swap\nBroadcast\t/api/v6/dex/pre-transaction/broadcast-transaction\tPOST\tSubmit signed transaction to the chain\n\nKey features:\n\nFull swap lifecycle with transaction signing\nAuto-slippage calculation based on market conditions\nMEV (sandwich attack) protection on ETH, BSC, SOL, BASE\nJito tips for Solana priority transactions\nToken approval handling (ERC-20 approve)\nCommission/referral fee splitting\nPrice impact protection with configurable thresholds\nPrerequisites\nRequired Credentials\nOKX_ACCESS_KEY — API key\nOKX_SECRET_KEY — Secret key for HMAC signing\nOKX_PASSPHRASE — Account passphrase\nWallet Requirements\nEVM chains: Private key or a signer (e.g., web3.py Account, ethers.js Wallet)\nSolana: Keypair for transaction signing\nThe wallet must have sufficient balance of the fromToken and native token for gas\nEnvironment\nPython: requests, web3 (for EVM signing), solders / solana-py (for Solana)\nNode.js: axios, ethers (for EVM signing), @solana/web3.js (for Solana)\nWorkflow\nStep 1: Call the Swap API\n\nEndpoint:\n\nGET https://web3.okx.com/api/v6/dex/aggregator/swap\n\n\nRequired parameters:\n\nParameter\tType\tRequired\tDescription\nchainIndex\tString\tYes\tChain ID (e.g., 1 = Ethereum, 501 = Solana)\namount\tString\tYes\tAmount in raw units with decimals (e.g., 1000000 for 1 USDT)\nswapMode\tString\tYes\texactIn (default) or exactOut\nfromTokenAddress\tString\tYes\tSell token contract address\ntoTokenAddress\tString\tYes\tBuy token contract address\nslippagePercent\tString\tYes\tSlippage tolerance (e.g., 0.5 = 0.5%). EVM: 0-100, Solana: 0 to <100\nuserWalletAddress\tString\tYes\tUser's wallet address that will sign and send the tx\n\nImportant optional parameters:\n\nParameter\tType\tDescription\napproveTransaction\tBoolean\tSet true to get ERC-20 approval calldata in response\napproveAmount\tString\tCustom approval amount (raw units). If omitted, approves exact swap amount\nswapReceiverAddress\tString\tRecipient address if different from sender\nfeePercent\tString\tCommission fee %. Max 3% (EVM) / 10% (Solana)\nfromTokenReferrerWalletAddress\tString\tWallet to receive fromToken commission\ntoTokenReferrerWalletAddress\tString\tWallet to receive toToken commission\nautoSlippage\tBoolean\tAuto-calculate optimal slippage (overrides slippagePercent)\nmaxAutoslippagePercent\tString\tCap for auto-slippage\npriceImpactProtectionPercent\tString\tMax price impact allowed (0-100, default 90)\ngasLevel\tString\tslow, average (default), or fast\ngaslimit\tString\tCustom gas limit in wei (EVM only)\ndexIds\tString\tRestrict to specific DEX IDs (comma-separated)\nexcludeDexIds\tString\tExclude specific DEX IDs (comma-separated)\ndirectRoute\tBoolean\tSingle-pool routing only (Solana only)\ndisableRFQ\tString\tDisable time-sensitive RFQ liquidity sources\ncallDataMemo\tString\tCustom 64-byte hex data to include on-chain\n\nSolana-specific parameters:\n\nParameter\tType\tDescription\ncomputeUnitPrice\tString\tPriority fee (like gasPrice on EVM)\ncomputeUnitLimit\tString\tCompute budget (like gasLimit on EVM)\ntips\tString\tJito tips in SOL (min 0.0000000001, max 2). Set computeUnitPrice=0 when using tips\n\nSwap API response structure:\n\n{\n  \"code\": \"0\",\n  \"data\": [{\n    \"routerResult\": {\n      \"chainIndex\": \"1\",\n      \"fromToken\": { \"tokenSymbol\": \"USDC\", \"decimal\": \"6\", ... },\n      \"toToken\": { \"tokenSymbol\": \"WBTC\", \"decimal\": \"8\", ... },\n      \"fromTokenAmount\": \"100000000000\",\n      \"toTokenAmount\": \"90281915\",\n      \"tradeFee\": \"1.35\",\n      \"estimateGasFee\": \"1248837\",\n      \"priceImpactPercent\": \"0.07\",\n      \"dexRouterList\": [...]\n    },\n    \"tx\": {\n      \"from\": \"0x77660f...\",\n      \"to\": \"0x5E1f62...\",\n      \"value\": \"0\",\n      \"data\": \"0xf2c42696...\",\n      \"gas\": \"1248837\",\n      \"gasPrice\": \"557703374\",\n      \"maxPriorityFeePerGas\": \"500000000\",\n      \"minReceiveAmount\": \"90191633\",\n      \"slippagePercent\": \"0.1\",\n      \"signatureData\": [...]\n    }\n  }],\n  \"msg\": \"\"\n}\n\n\nKey fields in tx object:\n\nField\tDescription\nfrom\tSender wallet address\nto\tOKX DEX router contract address\ndata\tTransaction calldata (the swap instruction)\nvalue\tNative token amount to send (in wei). \"0\" for ERC-20 swaps\ngas\tEstimated gas limit (already padded +50%)\ngasPrice\tGas price in wei\nmaxPriorityFeePerGas\tEIP-1559 priority fee\nminReceiveAmount\tMinimum output at max slippage\nsignatureData\tApproval calldata (if approveTransaction=true) or Jito tips calldata\nStep 2: Handle Token Approval (EVM Only)\n\nFor ERC-20 tokens (not native ETH/BNB), you need to approve the DEX router to spend your tokens BEFORE the swap.\n\nWhen approveTransaction=true in the request: The response tx.signatureData contains the approval info:\n\n{\n  \"approveContract\": \"0x40aA958dd87FC8305b97f2BA922CDdCa374bcD7f\",\n  \"approveTxCalldata\": \"0x095ea7b3...\"\n}\n\n\nApproval flow:\n\nParse signatureData to get approveContract and approveTxCalldata\nSend an approval transaction: to=approveContract, data=approveTxCalldata\nWait for approval tx to be confirmed\nThen send the swap transaction\n\nIMPORTANT: For native tokens (ETH, BNB, etc. using 0xeeee...eeee), no approval is needed.\n\nStep 3: Sign the Transaction\n\nEVM chains (Python / web3.py):\n\ntx_params = {\n    \"from\": tx_data[\"from\"],\n    \"to\": tx_data[\"to\"],\n    \"value\": int(tx_data[\"value\"]),\n    \"data\": tx_data[\"data\"],\n    \"gas\": int(tx_data[\"gas\"]),\n    \"gasPrice\": int(tx_data[\"gasPrice\"]),\n    \"nonce\": w3.eth.get_transaction_count(wallet_address),\n    \"chainId\": chain_id,\n}\nsigned = w3.eth.account.sign_transaction(tx_params, private_key)\nsigned_tx_hex = signed.raw_transaction.hex()\n\n\nEVM chains (Node.js / ethers.js):\n\nconst tx = {\n  from: txData.from,\n  to: txData.to,\n  value: txData.value,\n  data: txData.data,\n  gasLimit: txData.gas,\n  gasPrice: txData.gasPrice,\n  nonce: await provider.getTransactionCount(walletAddress),\n  chainId: chainId,\n};\nconst signedTx = await wallet.signTransaction(tx);\n\n\nSolana: Use solders or @solana/web3.js to deserialize, sign, and serialize the transaction from tx.data.\n\nStep 4: Broadcast the Signed Transaction\n\nEndpoint:\n\nPOST https://web3.okx.com/api/v6/dex/pre-transaction/broadcast-transaction\n\n\nIMPORTANT: This is a POST request with a JSON body (unlike the GET-based swap/quote endpoints).\n\nRequest body:\n\nParameter\tType\tRequired\tDescription\nsignedTx\tString\tYes\tThe hex-encoded signed transaction string\nchainIndex\tString\tYes\tChain ID (e.g., \"1\" for Ethereum)\naddress\tString\tYes\tSender wallet address\nextraData\tString\tNo\tJSON string with extra options (see below)\n\nextraData options (JSON string):\n\nField\tType\tDescription\nenableMevProtection\tBoolean\tEnable MEV (sandwich) protection. Supported: ETH, BSC, SOL, BASE\njitoSignedTx\tString\tBase58-encoded signed Jito transaction (Solana only). Required when tips > 0\n\nSigning algorithm for POST requests:\n\nprehash = timestamp + \"POST\" + request_path + json_body\nsignature = Base64(HMAC-SHA256(secret_key, prehash))\n\n\nNote: For POST, the request_path has NO query string. The JSON body is appended directly to the prehash string.\n\nBroadcast response:\n\n{\n  \"code\": \"0\",\n  \"data\": [{\n    \"orderId\": \"0e1d79837afce1e149b6ab54b6e2edce8130c3f8\",\n    \"txHash\": \"0xd394f356a16b618ed839c66c935c9cccc5dde0af832ff9b468677eea38759db5\"\n  }],\n  \"msg\": \"\"\n}\n\nField\tDescription\norderId\tOKX internal order tracking ID\ntxHash\tOn-chain transaction hash. Use this to check status on block explorer\nStep 5: Verify Transaction\n\nAfter broadcasting, verify the transaction was confirmed:\n\nEVM: Check txHash on Etherscan / block explorer, or use web3.eth.wait_for_transaction_receipt()\nSolana: Check on Solscan, or use connection.confirmTransaction()\nBest Practices\nSecurity\nNEVER hardcode private keys in code. Use environment variables, keystore files, or hardware wallets.\nNEVER log private keys, signed transactions, or secret keys in production.\nAlways validate token addresses and amounts before sending.\nCheck isHoneyPot on both tokens before proceeding.\nUse priceImpactProtectionPercent (recommend setting to 10 or lower for safety).\nSlippage Configuration\nStable pairs (USDC/USDT): 0.1% - 0.5%\nMajor pairs (ETH/USDC): 0.5% - 1%\nVolatile/low-liquidity tokens: 1% - 5%\nMeme coins: 5% - 15% (or higher, but be cautious)\nUse autoSlippage=true with maxAutoslippagePercent for optimal auto-calculation.\nMEV Protection\nEnable enableMevProtection: true for large trades on ETH, BSC, SOL, BASE.\nOn Solana, combine with Jito tips for priority + MEV protection.\nWhen using Jito tips, set computeUnitPrice=0 to avoid wasting fees.\nToken Approval Strategy\nFor one-time swaps: approve exact amount (approveAmount = swap amount).\nFor repeated swaps: consider a larger approval (e.g., max uint256) to save gas on future swaps, but understand the security trade-off.\nAlways check existing allowance before sending a new approval.\nUni V3 Liquidity Edge Case\n\nWhen swapping through Uniswap V3 pools, if the liquidity for the pair is drained mid-swap, the router will only consume part of your input tokens. The OKX DEX Router smart contract will automatically refund the remainder. Ensure your integration contract supports receiving token refunds.\n\nAmount Handling\nAlways use strings for amounts to avoid floating-point precision loss.\nPython: int() for calculations, never float().\nJavaScript: BigInt or ethers.parseUnits() for amounts.\nFormula: raw_amount = human_amount * 10^decimals\nError Handling\nCheck response[\"code\"] == \"0\" before processing.\nIf broadcast fails, do NOT automatically retry without checking nonce — you may double-spend.\nCommon errors:\n401 = Signature mismatch (check POST signing includes body)\n429 = Rate limited\nSwap returns error = insufficient balance, invalid params, or liquidity issue\nBroadcast returns error = already executed, nonce too low, insufficient gas\nCommission/Referral Fees\nOnly ONE of fromTokenReferrerWalletAddress or toTokenReferrerWalletAddress per tx.\nSolana: referrer wallet must have SOL deposited for activation.\nTON: limited DEX support (Stonfi V2, Dedust).\nBSC: no commission through Four.meme swaps.\nExamples\nExample 1: Python — Complete EVM Swap Flow\nimport os, hmac, hashlib, base64, json, requests\nfrom datetime import datetime, timezone\nfrom urllib.parse import urlencode\nfrom web3 import Web3\n\n# === Configuration ===\nAPI_KEY = os.environ[\"OKX_ACCESS_KEY\"]\nSECRET_KEY = os.environ[\"OKX_SECRET_KEY\"]\nPASSPHRASE = os.environ[\"OKX_PASSPHRASE\"]\nPRIVATE_KEY = os.environ[\"WALLET_PRIVATE_KEY\"]\n\nBASE_URL = \"https://web3.okx.com\"\nCHAIN_INDEX = \"1\"  # Ethereum\nCHAIN_ID = 1\nRPC_URL = \"https://eth.llamarpc.com\"\n\nw3 = Web3(Web3.HTTPProvider(RPC_URL))\naccount = w3.eth.account.from_key(PRIVATE_KEY)\nWALLET = account.address\n\n\ndef _sign_request(timestamp, method, request_path, body=\"\"):\n    prehash = timestamp + method + request_path + body\n    mac = hmac.new(SECRET_KEY.encode(), prehash.encode(), hashlib.sha256)\n    return base64.b64encode(mac.digest()).decode()\n\n\ndef _headers(method, request_path, body=\"\"):\n    timestamp = datetime.now(timezone.utc).strftime(\"%Y-%m-%dT%H:%M:%S.%f\")[:-3] + \"Z\"\n    sig = _sign_request(timestamp, method, request_path, body)\n    return {\n        \"OK-ACCESS-KEY\": API_KEY,\n        \"OK-ACCESS-SIGN\": sig,\n        \"OK-ACCESS-PASSPHRASE\": PASSPHRASE,\n        \"OK-ACCESS-TIMESTAMP\": timestamp,\n        \"Content-Type\": \"application/json\",\n    }\n\n\n# --- Step 1: Get swap calldata ---\ndef get_swap_data(from_token, to_token, amount, slippage=\"0.5\"):\n    params = {\n        \"chainIndex\": CHAIN_INDEX,\n        \"fromTokenAddress\": from_token,\n        \"toTokenAddress\": to_token,\n        \"amount\": amount,\n        \"swapMode\": \"exactIn\",\n        \"slippagePercent\": slippage,\n        \"userWalletAddress\": WALLET,\n        \"approveTransaction\": \"true\",\n    }\n    query = urlencode(params)\n    path = f\"/api/v6/dex/aggregator/swap?{query}\"\n    headers = _headers(\"GET\", path)\n\n    resp = requests.get(BASE_URL + path, headers=headers, timeout=30)\n    resp.raise_for_status()\n    data = resp.json()\n\n    if data[\"code\"] != \"0\":\n        raise Exception(f\"Swap API error: {data['msg']}\")\n    return data[\"data\"][0]\n\n\n# --- Step 2: Handle approval (if needed) ---\ndef send_approval_if_needed(swap_result):\n    sig_data = swap_result[\"tx\"].get(\"signatureData\", [])\n    if not sig_data:\n        return None\n\n    for item in sig_data:\n        parsed = json.loads(item) if isinstance(item, str) else item\n        if \"approveContract\" in parsed:\n            approve_tx = {\n                \"from\": WALLET,\n                \"to\": Web3.to_checksum_address(parsed[\"approveContract\"]),\n                \"data\": parsed[\"approveTxCalldata\"],\n                \"gas\": 60000,\n                \"gasPrice\": w3.eth.gas_price,\n                \"nonce\": w3.eth.get_transaction_count(WALLET),\n                \"chainId\": CHAIN_ID,\n            }\n            signed = w3.eth.account.sign_transaction(approve_tx, PRIVATE_KEY)\n            tx_hash = w3.eth.send_raw_transaction(signed.raw_transaction)\n            receipt = w3.eth.wait_for_transaction_receipt(tx_hash, timeout=120)\n            print(f\"Approval confirmed: {tx_hash.hex()}\")\n            return receipt\n    return None\n\n\n# --- Step 3: Sign the swap transaction ---\ndef sign_swap_tx(swap_result):\n    tx_data = swap_result[\"tx\"]\n    tx_params = {\n        \"from\": Web3.to_checksum_address(tx_data[\"from\"]),\n        \"to\": Web3.to_checksum_address(tx_data[\"to\"]),\n        \"value\": int(tx_data[\"value\"]),\n        \"data\": tx_data[\"data\"],\n        \"gas\": int(tx_data[\"gas\"]),\n        \"gasPrice\": int(tx_data[\"gasPrice\"]),\n        \"nonce\": w3.eth.get_transaction_count(WALLET),\n        \"chainId\": CHAIN_ID,\n    }\n    signed = w3.eth.account.sign_transaction(tx_params, PRIVATE_KEY)\n    return signed.raw_transaction.hex()\n\n\n# --- Step 4: Broadcast via OKX ---\ndef broadcast_tx(signed_tx_hex, enable_mev=True):\n    path = \"/api/v6/dex/pre-transaction/broadcast-transaction\"\n    body_dict = {\n        \"chainIndex\": CHAIN_INDEX,\n        \"address\": WALLET,\n        \"signedTx\": signed_tx_hex,\n    }\n    if enable_mev:\n        body_dict[\"extraData\"] = json.dumps({\"enableMevProtection\": True})\n\n    body_str = json.dumps(body_dict)\n    headers = _headers(\"POST\", path, body_str)\n\n    resp = requests.post(BASE_URL + path, headers=headers, data=body_str, timeout=30)\n    resp.raise_for_status()\n    data = resp.json()\n\n    if data[\"code\"] != \"0\":\n        raise Exception(f\"Broadcast error: {data['msg']}\")\n    return data[\"data\"][0]\n\n\n# === Execute full swap ===\nif __name__ == \"__main__\":\n    ETH = \"0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee\"\n    USDC = \"0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48\"\n    amount = str(10 ** 17)  # 0.1 ETH\n\n    print(\"1. Getting swap data...\")\n    swap = get_swap_data(ETH, USDC, amount, slippage=\"0.5\")\n\n    router = swap[\"routerResult\"]\n    to_dec = int(router[\"toToken\"][\"decimal\"])\n    out_human = int(router[\"toTokenAmount\"]) / (10 ** to_dec)\n    print(f\"   Expected output: {out_human:,.2f} {router['toToken']['tokenSymbol']}\")\n    print(f\"   Price impact: {router.get('priceImpactPercent', 'N/A')}%\")\n    print(f\"   Min receive: {int(swap['tx']['minReceiveAmount']) / (10 ** to_dec):,.2f}\")\n\n    # Honeypot check\n    if router[\"toToken\"].get(\"isHoneyPot\"):\n        print(\"   HONEYPOT DETECTED — aborting!\")\n        exit(1)\n\n    print(\"2. Handling approval...\")\n    send_approval_if_needed(swap)\n\n    print(\"3. Signing swap transaction...\")\n    signed_hex = sign_swap_tx(swap)\n\n    print(\"4. Broadcasting with MEV protection...\")\n    result = broadcast_tx(signed_hex, enable_mev=True)\n    print(f\"   Order ID: {result['orderId']}\")\n    print(f\"   Tx Hash: {result['txHash']}\")\n    print(f\"   View: https://etherscan.io/tx/{result['txHash']}\")\n\nExample 2: Node.js — Complete EVM Swap Flow\nconst crypto = require(\"crypto\");\nconst https = require(\"https\");\nconst { ethers } = require(\"ethers\");\n\nconst API_KEY = process.env.OKX_ACCESS_KEY;\nconst SECRET_KEY = process.env.OKX_SECRET_KEY;\nconst PASSPHRASE = process.env.OKX_PASSPHRASE;\nconst PRIVATE_KEY = process.env.WALLET_PRIVATE_KEY;\n\nconst BASE_URL = \"https://web3.okx.com\";\nconst CHAIN_INDEX = \"1\";\nconst CHAIN_ID = 1;\n\nconst provider = new ethers.JsonRpcProvider(\"https://eth.llamarpc.com\");\nconst wallet = new ethers.Wallet(PRIVATE_KEY, provider);\n\nfunction sign(timestamp, method, path, body = \"\") {\n  return crypto\n    .createHmac(\"sha256\", SECRET_KEY)\n    .update(timestamp + method + path + body)\n    .digest(\"base64\");\n}\n\nfunction headers(method, path, body = \"\") {\n  const ts = new Date().toISOString();\n  return {\n    \"OK-ACCESS-KEY\": API_KEY,\n    \"OK-ACCESS-SIGN\": sign(ts, method, path, body),\n    \"OK-ACCESS-PASSPHRASE\": PASSPHRASE,\n    \"OK-ACCESS-TIMESTAMP\": ts,\n    \"Content-Type\": \"application/json\",\n  };\n}\n\nasync function getSwapData(fromToken, toToken, amount, slippage = \"0.5\") {\n  const params = new URLSearchParams({\n    chainIndex: CHAIN_INDEX,\n    fromTokenAddress: fromToken,\n    toTokenAddress: toToken,\n    amount,\n    swapMode: \"exactIn\",\n    slippagePercent: slippage,\n    userWalletAddress: wallet.address,\n    approveTransaction: \"true\",\n  });\n  const path = `/api/v6/dex/aggregator/swap?${params}`;\n  const h = headers(\"GET\", path);\n\n  const resp = await fetch(`${BASE_URL}${path}`, { headers: h });\n  const data = await resp.json();\n  if (data.code !== \"0\") throw new Error(`Swap error: ${data.msg}`);\n  return data.data[0];\n}\n\nasync function broadcastTx(signedTx, enableMev = true) {\n  const path = \"/api/v6/dex/pre-transaction/broadcast-transaction\";\n  const bodyObj = {\n    chainIndex: CHAIN_INDEX,\n    address: wallet.address,\n    signedTx,\n    ...(enableMev && {\n      extraData: JSON.stringify({ enableMevProtection: true }),\n    }),\n  };\n  const bodyStr = JSON.stringify(bodyObj);\n  const h = headers(\"POST\", path, bodyStr);\n\n  const resp = await fetch(`${BASE_URL}${path}`, {\n    method: \"POST\",\n    headers: h,\n    body: bodyStr,\n  });\n  const data = await resp.json();\n  if (data.code !== \"0\") throw new Error(`Broadcast error: ${data.msg}`);\n  return data.data[0];\n}\n\nasync function main() {\n  const ETH = \"0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee\";\n  const USDC = \"0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48\";\n  const amount = (10n ** 17n).toString(); // 0.1 ETH\n\n  console.log(\"1. Getting swap data...\");\n  const swap = await getSwapData(ETH, USDC, amount);\n\n  const txData = swap.tx;\n  console.log(`   Min receive: ${txData.minReceiveAmount}`);\n\n  console.log(\"2. Signing transaction...\");\n  const tx = {\n    from: txData.from,\n    to: txData.to,\n    value: txData.value,\n    data: txData.data,\n    gasLimit: txData.gas,\n    gasPrice: txData.gasPrice,\n    nonce: await provider.getTransactionCount(wallet.address),\n    chainId: CHAIN_ID,\n  };\n  const signedTx = await wallet.signTransaction(tx);\n\n  console.log(\"3. Broadcasting with MEV protection...\");\n  const result = await broadcastTx(signedTx);\n  console.log(`   Tx Hash: ${result.txHash}`);\n}\n\nmain().catch(console.error);\n\nExample 3: Solana Swap with Jito Tips\n\nWhen using Jito tips on Solana:\n\nSet tips parameter (e.g., \"0.001\" SOL) in the swap request\nSet computeUnitPrice=0 (avoid double-paying for priority)\nThe signatureData in response contains the Jito tips calldata\nSign BOTH the main transaction AND the Jito transaction\nBroadcast with both signedTx and jitoSignedTx in extraData\n# Solana-specific broadcast with Jito\nbody = {\n    \"chainIndex\": \"501\",\n    \"address\": solana_wallet_pubkey,\n    \"signedTx\": base58_signed_main_tx,\n    \"extraData\": json.dumps({\n        \"enableMevProtection\": True,\n        \"jitoSignedTx\": base58_signed_jito_tx\n    })\n}\n\n\nIMPORTANT: For Solana, signedTx and jitoSignedTx must BOTH be provided when using tips.\n\nTroubleshooting\nProblem\tCause\tSolution\n401 Unauthorized on broadcast\tPOST signing error\tEnsure prehash = timestamp + \"POST\" + path + jsonBody. The body must be the exact JSON string.\n401 Unauthorized on swap\tGET signing error\tEnsure prehash = timestamp + \"GET\" + path_with_query_string.\nApproval tx fails\tInsufficient ETH for gas\tEnsure wallet has native token for gas fees.\nSwap tx reverts\tSlippage exceeded\tIncrease slippagePercent or use autoSlippage=true.\nminReceiveAmount is 0\tExtreme slippage or bad params\tCheck token addresses and amounts. Reduce trade size.\nBroadcast returns no txHash\tMEV protection routing delay\tWait and check orderId status. MEV-protected txs may take longer.\n\"Nonce too low\" on broadcast\tTx already sent or nonce reused\tFetch fresh nonce before signing. Never reuse nonces.\nSolana broadcast fails\tMissing jitoSignedTx\tWhen tips > 0, you must provide both signedTx and jitoSignedTx.\nToken refund not received\tUni V3 liquidity drained\tEnsure your contract supports receiving token refunds from the router.\nCommission not working on BSC\tFour.meme restriction\tCommission is not supported for swaps through Four.meme on BSC.\npriceImpactPercent very negative\tLow liquidity\tReduce amount or split into multiple smaller swaps.\nReference: POST vs GET Signing\n\nThe OKX API uses different signing for GET and POST:\n\nMethod\tPrehash Format\nGET\ttimestamp + \"GET\" + path_with_query\nPOST\ttimestamp + \"POST\" + path + json_body\n\nThe Swap endpoint is GET, the Broadcast endpoint is POST. Getting this wrong is the #1 cause of 401 errors.\n\nReference: MEV Protection Support\nChain\tenableMevProtection\tJito Tips\nEthereum\tYes\tNo\nBSC\tYes\tNo\nSolana\tYes\tYes\nBase\tYes\tNo\nOthers\tNot yet\tNot yet\nReference: Common Chain IDs\nChain\tchainIndex\tChain ID (for tx signing)\nEthereum\t1\t1\nBSC\t56\t56\nPolygon\t137\t137\nArbitrum\t42161\t42161\nOptimism\t10\t10\nBase\t8453\t8453\nAvalanche\t43114\t43114\nSolana\t501\tN/A\nUnichain\t130\t130"
  },
  "trust": {
    "sourceLabel": "tencent",
    "provenanceUrl": "https://clawhub.ai/AaronLLee/easy-swap",
    "publisherUrl": "https://clawhub.ai/AaronLLee/easy-swap",
    "owner": "AaronLLee",
    "version": "1.0.0",
    "license": null,
    "verificationStatus": "Indexed source record"
  },
  "links": {
    "detailUrl": "https://openagent3.xyz/skills/easy-swap",
    "downloadUrl": "https://openagent3.xyz/downloads/easy-swap",
    "agentUrl": "https://openagent3.xyz/skills/easy-swap/agent",
    "manifestUrl": "https://openagent3.xyz/skills/easy-swap/agent.json",
    "briefUrl": "https://openagent3.xyz/skills/easy-swap/agent.md"
  }
}