Skip to main content

Overview

DEX (Decentralized Exchange) swaps execute on-chain through smart contracts. Unlike CEX swaps that use deposit addresses, DEX swaps require users to connect their wallet, sign transactions, and broadcast them directly to the blockchain.
Best For: Users who want true decentralized swaps, keep custody of their funds, and interact directly with on-chain liquidity sources like Uniswap, Cowswap, and 1inch.

Supported Networks

DEX swaps are currently supported on the following networks:
  • EVM (Ethereum, BSC, Polygon, etc.)
  • Solana
  • SUI
  • TRON
  • TON
  • Stellar
Stellar Trustline Requirement: For swaps involving Stellar assets, the integrator is responsible for ensuring that the destination account has the required trustlines established before initiating the swap. Houdini does not handle trustline creation automatically. If the trustline is missing, the swap will fail.

Key Characteristics

How It Works

DEX swaps follow this flow:

Technical Flow Diagram

This diagram shows the complete integration flow with conditional logic:

Integration Steps

Step 1: Get Supported Assets

Discover which tokens and networks are available for DEX swaps. Learn more about DEX tokens and network identifiers.
Performance Note: The /tokens endpoint can take several seconds to minute to respond due to the large token list.
  • Save tokens in your backend database
  • Load tokens on server startup or via scheduled job
  • Serve token list from your database to frontend
  • Refresh cache periodically (e.g., every 24 hours)

Step 2: Get DEX Quote

Request a quote for the DEX swap using token IDs from /dexTokens:
Quote Parameters:
  • tokenIdFrom: Source token _id from /dexTokens endpoint
  • tokenIdTo: Destination token _id from /dexTokens endpoint
  • amount: Amount to swap (float: 1 = 1 token)
  • slippage (optional): Slippage percentage (e.g., 0.5 for 0.5%)
  • fromAddress (optional): Specific source address for the swap
  • toAddress (optional): Specific destination address for receiving tokens
Remember to use the _id field from DEX tokens, not the id field. Learn more about token identifiers.

Quote Response

The response is an array of quote options from different DEX aggregators, sorted by best rate (highest output first). Each quote object contains:
Filtering Routes: When filtered: true, the route does not support different sender and receiver addresses for same-chain swaps. You must either:
  • Filter out these routes from the UI when addressFrom !== addressTo
  • Disable the route option and show a message explaining it’s not available for this configuration

Step 3: Check Approvals and Signatures

Before executing a swap, check what’s needed from the user:
Response Contains:
  • approvals: Array of on-chain approval transactions (may be empty)
  • signatures: Array of signatures needed (may be empty)
Both Arrays Can Exist: You may receive both approvals AND signatures. Handle approvals first, then signatures.

Understanding Approvals

If the approvals array is not empty, the user must approve the DEX to spend their tokens. See more in Step 4.

Understanding Signatures

The signatures array can contain two types: 1. SINGLE Type - Simple one-time signature: Action: User signs once, add to results, done. 2. CHAINED Type - Multi-step signature (currently only for Cowswap): Action: User signs, call /chainSignatures, repeat until isComplete: true. See more in Step 5.

Step 4: Send Approval Transactions (if needed)

If the approvals array is not empty, broadcast approval transactions:
Skip This Step If: No approvals were required (empty approvals array in Step 3).

Step 5: Process Signatures (if needed)

Handle signature requests from Step 3:
Key Points:
  • SINGLE: User signs once, done
  • CHAINED: User signs → API call → User signs again → Repeat until complete
  • Only keep the final signature from CHAINED sequences
Skip This Step If: No signatures were required (empty signatures array in Step 3).

Step 6: Check Allowance (if approvals were sent)

If you sent approval transactions in Step 4, verify they are confirmed on-chain:
Skip This Step If: No approvals were sent in Step 4.

Step 7: Execute the Swap

Now execute the swap with any collected signatures:
Request Parameters:
  • tokenIdFrom: Source token ID
  • tokenIdTo: Destination token ID
  • amount: Amount to swap (float)
  • addressFrom: User’s wallet address (source)
  • addressTo: Destination address for receiving tokens
  • route: Complete route object from quote response (quote.raw)
  • swap: DEX identifier from quote (e.g., “zx” for 0x, “cs” for Cowswap)
  • quoteId: Quote ID from quote response
  • signatures: Array of signature objects from Step 5 (empty if none required)
  • destinationTag: Memo/tag for chains that require it (empty string if not needed)
  • deviceInfo: Device type - “web”, “ios”, “android”, or custom identifier
  • isMobile: Boolean indicating if request is from mobile device
  • walletInfo: Name of wallet being used (e.g., “MetaMask”, “Rabby Wallet”)
  • slippage: Custom slippage tolerance (null to use default from quote)
Response Contains:
  • order.houdiniId: Unique swap identifier for tracking
  • order.metadata.offChain: Boolean indicating if user transaction is needed
  • order.metadata.to: DEX router address (if offChain: false)
  • order.metadata.data: Encoded swap call (if offChain: false)
  • order.metadata.value: ETH value for native swaps (if offChain: false)

Broadcast Transaction and Confirm

After receiving the swap order, you need to:
  1. Have the user broadcast the transaction (if offChain: false)
  2. Call /dexConfirmTx to notify Houdini of the transaction hash
User must broadcast the transaction, then confirm with Houdini:
Critical Step: You MUST call /dexConfirmTx after creating the swap order:
  • On-chain swaps: Pass the transaction hash after user broadcasts
  • Off-chain swaps: Pass txHash: undefined to start backend processing
Without this call, the swap will not be processed and status tracking will not work.

Step 8: Track Swap Status

Monitor the swap progress:
Status Codes:
  • 0 = WAITING - Awaiting transaction
  • 1 = CONFIRMING - Transaction submitted, waiting for confirmations
  • 2 = EXCHANGING - Processing swap
  • 4 = COMPLETED - Swap complete ✅
  • 6 = FAILED - Swap failed ❌
Polling: Check status every 30 seconds for on-chain swaps. They typically complete in seconds to a few minutes depending on network congestion.

Complete Example

For a complete, runnable Node.js example:

DEX Swap Example

Complete DEX swap script with approval handling and wallet simulation

Next Steps

Private Swaps

Learn about CEX-based private swaps

Standard Swaps

Integrate fast single-hop CEX swaps

Order Lifecycle

Understand swap status progression

Error Handling

Handle errors and edge cases