How routing works

How Ghostdex turns an intention into a transaction: where quotes come from, how minimums are set, and every reason an order can fail.

2 min read

Two routers

Ghostdex uses two routing paths, each where it is strongest. Single swaps and bridges go through LI.FI, which aggregates many exchanges and bridges and is often the best price for one pair. Baskets and launched tokens go through Uniswap's Universal Router at 0x8876789976dEcBfCbBbe364623C63652db8C0904, because it can run several swaps inside one transaction, which is what a basket needs.

Routing is the same whoever signs. From a ghost, quotes are requested for the ghost's address, and the outputs go back to the ghost.

Finding the pool for a basket leg

Ghostdex keeps a snapshot of every Uniswap v3 and v4 pool that holds a stock token against USDG or ETH: 223 pools across 72 stocks at the last probe. Baskets use the 48 stocks whose deepest USDG pool fills a $250 buy within 3% of the on-chain mid price. For each leg of a basket, the app quotes every candidate pool on the chain with Uniswap's own quoters and keeps the one that returns the most. A pool that has emptied since the snapshot simply drops out.

  1. 1
    Split the amount

    Each leg gets amount x weight. Weights that do not add up to 100% are scaled in proportion.

  2. 2
    Quote every pool

    The V4 quoter and V3 QuoterV2 are called for each candidate pool. The best quote wins.

  3. 3
    Set the minimum

    Each leg's minimum is quote x (1 - slippage). The router enforces it on chain.

  4. 4
    Build one call

    The legs become commands of one execute(commands, inputs, deadline) call: wrap ETH if needed, swap, take the outputs to the signing address, sweep any leftover back to it.

A three-stock basket paid in ETH
WRAP_ETH            ETH -> WETH, held by the router for this call
V3_SWAP_EXACT_IN    WETH -> USDG
V4_SWAP             SETTLE USDG
                    SWAP_EXACT_IN_SINGLE  USDG -> NVDA   minOut set
                    SWAP_EXACT_IN_SINGLE  USDG -> AAPL   minOut set
                    SWAP_EXACT_IN_SINGLE  USDG -> META   minOut set
                    TAKE  NVDA, AAPL, META -> signing address
SWEEP               leftover USDG -> signing address
// measured: about 539,000 gas paid in ETH, 427,000 paid in USDG

What can make an order fail

ReasonWhat you seeWhat to do
Price moved past your limitThe price moved past your slippage limit.Refresh the quote, or allow more slippage.
Quote expiredThe quote expired before the transaction was mined.Sign again; quotes carry a deadline.
Token paused by the issuerThis token is paused right now.Wait for the issuer to unpause it.
Address blocked by the issuerThis wallet cannot send or receive this stock.Nothing an interface can change.
Missing approvalThe router is not allowed to spend this token yet.Approve, then retry.
Not enough balance or gasNot enough balance for this trade plus gas.Lower the amount, or add ETH for gas to the address you act as.

In every case the chain reverts the whole transaction. Nothing is half done, and only the gas used up to the failure is spent.