CONVEXSWAP

ConvexSwap started as a hackathon project addressing a simple problem: big trades on decentralized exchange pools like Uniswap move the price against you. A pool prices every swap off its own reserves, so the larger the order, the higher the slippage. ConvexSwap streams the trade out over time instead, in smaller chunks, letting other participants arbitrage the market price back to its starting point, saving the trader a part of their slippage costs.

uniswap/x·y=k — selling 40 ETH for USDC, same trade, same time
1 swapone swap, whole window0USDC received200k300k400k500k80100120140160ETH in pool (x)p₀ = y/x
4 chunkschunk 1 / 40USDC received200k300k400k500k80100120140160ETH in pool (x)p₀ = y/x
invariantx · y = k100 · 300,000 = 30,000,000
you receiveΔy = y · Δx / (x + Δx)85,714 USDC
spot pricep₀ = y / x3,000 USDC/ETH
execution pricep = Δy / Δx = y / (x + Δx)2,143 USDC/ETH
slippage1 − p / p₀ = Δx / (x + Δx)28.6%
in 4 chunksn · y · (Δx/n) / (x + Δx/n)109,091 USDC · 9.1%
Ideal fill at p₀ is 120,000 USDC. 4 chunks keep 23,377 USDC more than one swap, if arbitrage restores the price between them. Fees ignored.

The interesting part is deciding how many chunks to use. Splitting a trade to many chunks cuts slippage costs but increases gas costs and exposure to volatility. This forms a convex optimization problem. The ConvexSwap algorithm finds the number of chunks executed at a set interval that minimize trade costs. Inputs are gas cost estimates, market liquidity depth, and recent volatility.

convexswap/objective — what the chunk count minimizes
minimizeC(b) = I(b) + G(b) + V(b)over whole chunk counts, 1 to 50
price impactI(b) = b · [ k / (y − R/b) − k / y ]b swaps of R/b each; falls as b grows
gasG(b) = ḡ · bḡ is the mean cost of one swap over recent blocks
drift riskV(b) = ½ · b(b − 1) · (γyσy² + γkσk²)σ is the volatility of y and k while the trade waits; γ weights it
answerb* = argmin C(b)one chunk every 500 blocks
convexswap/cost — total cost against chunk count
02040601481216number of chunks the trade is split intocost (illustrative units)total costprice impactgas + drift riskcheapest: 4 chunks

Illustrative numbers on a toy pool; the shape is the one the optimizer searches.

> cd ..