Killua
← All writing MEV · Uniswap

How I built the Uniswap TokenJar MEV bot

Killua · 7 min read

When Uniswap turned on protocol fees, it created a new kind of race. Fees from V2, V3 and V4 pools flow into a contract called the TokenJar. Anyone can empty the jar, but only by burning a fixed amount of UNI into the Firepit. That burn releases a basket of the jar’s tokens to whoever paid for it.

So every few blocks there’s a simple question: is the basket worth more than the UNI it costs to open? Whoever answers first and lands their transaction first wins. Everyone else gets nothing.

I spent the summer building a bot to compete in that race. This post covers how it works and the design decisions that shaped it. I won’t share code, numbers or anything that would make the bot easy to copy. The ideas are the interesting part anyway.

The opportunity in one paragraph

The jar fills up slowly as fees are collected from thousands of pools. Opening it costs a fixed burn of UNI. The jar releases a limited set of tokens per opening, so you pick the most valuable ones. If the tokens you pick, sold back to WETH, are worth more than the UNI plus gas plus what you pay the block builder, you profit. Usually they aren’t, and the job is to watch the gap close and act the moment it flips.

One transaction, four steps

Everything happens atomically, inside a single transaction:

  1. Fill. Collect the fees that pools owe the jar but haven’t sent yet. V3 pools and the V4 PoolManager hold fees until someone collects them. V2 pairs only mint their fee share when liquidity changes, so the bot nudges the pairs that are worth nudging.
  2. Drain. Flash-borrow WETH, buy the UNI, and burn it through the Firepit to release the chosen tokens.
  3. Sell. Turn every released token back into WETH across V2, V3, V4 and multi-hop routes. Some released tokens are V2 LP tokens, so those get unwrapped into their two underlying tokens first.
  4. Guard. Check that the transaction made money. If it didn’t, revert everything.

Because the flash loan, the burn and every sale sit in one transaction, the bot never holds inventory and never takes price risk between steps. The worst case is a reverted transaction, and inside a private bundle even that usually costs nothing.

Fill, drain, sell and guard all run inside one transaction. Every other run here loses money, so the guard reverts it.

The brain decides, the contract guards

The most important architecture decision was a strict split of responsibilities.

The off-chain brain (TypeScript and viem) does all the thinking. It tracks which pools owe fees, values the jar, picks the basket, routes every sale, simulates the whole plan and builds the bundle.

The on-chain contract (Solidity, built and tested with Foundry) does almost no thinking. It receives a packed execution plan and follows it step by step. Its one real job is to refuse to finish unless the transaction’s own WETH balance went up by enough.

I check the profit as a change within this transaction, not as an account balance. If I compared absolute balances, leftover funds sitting in the contract could hide a losing run. Measuring only what this transaction produced means the guard can’t be fooled by money that was already there.

This split keeps the contract small and easy to audit. All the clever, fast-changing logic lives off-chain, where I can change it without redeploying anything. The contract is the safety net, so the brain is allowed to be wrong.

The brain plans everything off-chain. The contract follows the plan and checks only one thing: did this transaction make enough WETH?

Valuing a jar full of junk

The jar holds a huge number of tokens, and most of them are spam. Generic “get all token balances” APIs hit their limits and return results in address order. That means you read thousands of worthless tokens and can miss the ones that matter.

So I stopped asking what’s in the jar and started asking about only the tokens that could matter. The bot builds a candidate set from the pools it already tracks, plus the major tokens. It reads all their balances in one batched call. A token that isn’t in any pool worth selling into can’t be worth a slot, so ignoring it is free.

Three speeds of reading the chain

Not all data changes at the same speed, so the bot doesn’t read it all at the same speed:

Matching each read to how fast its data really changes kept the per-block work small enough to fit inside a 12-second block. I wrote each of these choices down as a decision record, with the reasoning and what I measured, so I could revisit them later without guessing.

Each kind of data is read at the speed it changes. Only the top lane runs every block.

Learning which tokens lie

Some tokens look valuable but can’t actually be sold. They might block transfers, tax every transfer, or only work in one direction. Early on, the only way I found out was by losing a race to one. The bot would put a poisoned token in the basket, the sale would fail, and the whole transaction would revert.

Worse, a bad token takes a slot that a good token could have used. So the cost is that token’s apparent value plus the value of the token it pushed out.

The fix was to probe before trusting. Before a token can earn a slot, the bot checks that it can really be moved and sold. Instead of keeping a hand-written deny list and updating it after each loss, the bot finds out for itself.

Plan one block ahead

This was the biggest lesson, and it came from timing, not code.

Block builders stop accepting bundles well before the next block lands. By the time the bot has seen a block, read everything, solved the basket and routed the sales, a lot of that window is already gone. Trying to squeeze the full pipeline into the time left is building on sand.

So I flipped it around. The expensive planning happens during the previous block. When a new block arrives, the bot only runs a quick pre-flight, a simulation of the plan it already has against the latest state. If the pre-flight says the plan is profitable, the bot submits it. The hot path went from “do everything” to “check one thing”.

The pre-flight runs the real contract against real state, so it measures the actual result instead of predicting it. That means the bot submits on a measurement, not a forecast.

Planning after the block lands runs past the builder cutoff. Planning during the previous block leaves only the pre-flight.

Bundles, builders and rivals

The bot sends private bundles to several block builders at once. It pays part of the profit to the builder, because that payment is what wins the auction against other searchers.

Rival searchers are invisible until they win, because their bundles are private too. So every time a rival opens the jar, the bot runs a post-mortem. It compares the rival’s transaction with the plan it held at the previous block and asks what it would have needed to win. Most of my design changes came out of those comparisons.

A dashboard that can’t slow the bot down

I wanted a live view of what the bot is thinking: how close it is to profitable, why it skipped a block, and what a rival did.

The rule for the dashboard was one-way isolation. The dashboard is a separate process that only reads the files the bot already writes. There’s no command channel and no socket into the bot. Killing the dashboard is invisible to the bot. The bot races a 12-second deadline, so the monitoring must never cost it time.

The dashboard itself is React, Vite and Tailwind, streaming over a WebSocket. It’s designed for three situations: glanced at for hours on a second monitor, watched closely when the jar is close to profitable, and checked on a phone from across the room.

Data flows one way. Closing the dashboard changes nothing for the bot.

What I’d tell someone building their first searcher

Building this taught me more about Uniswap internals, block building and latency than any audit has. It also made me a better auditor, because now I read every protocol with one question in mind: who can race this, and what do they win?