← Notes

Testing a liquidator against a copy of mainnet

Liquidations are rare, adversarial and expensive to get wrong. So we crash prices on a local copy of Solana and let the real system react.

A liquidation engine has an awkward testing problem. The code paths that matter only run when a borrower’s position goes underwater, which on a calm day might not happen at all. When it does happen, it happens on mainnet, with real money, against other bots that are faster than your test suite. Unit tests catch arithmetic mistakes, but they don’t tell you whether a lending program will actually accept the transaction you built.

So before any change reaches real capital, we run it against a copy of mainnet where we control the prices.

A fork that fills itself in

The core is a local Solana simulator that forks mainnet lazily: the first time anything reads an account, it is fetched from a mainnet RPC and kept locally. Every lending market, oracle, token account and DEX pool the system touches is the real one, as it was at that moment. After that the local copy is ours to change.

On top of it we attach the same Geyser plugin that streaming providers run in production. Our system subscribes to it exactly as it does on mainnet, so the code under test is not a mock of the system. It is the system, with its endpoints pointed at the fork.

Crashing a price on purpose

The simulator lets us overwrite any account. That is the whole trick: we rewrite an oracle’s price feed, say SOL down 10% in two steps, and keep it fresh, because frozen oracles go stale and lending programs rightly refuse stale prices. Borrowers priced off that feed become liquidatable, the change arrives over the stream like any real price update, and the engine has to:

  • notice the account is underwater,
  • size the liquidation the program will actually allow,
  • build a transaction the lending program accepts,
  • sell the seized collateral inside the same transaction,
  • and land it, or decide not to.

Afterwards we read the wallet’s actual balance changes from each landed transaction and price them. A liquidation that “worked” but cost more in fees than it earned is a failure, and the harness makes that visible.

What it caught

Most of the value is in what goes wrong. Running real programs against real account state surfaces problems no unit test would: a program that needs every price tick above a threshold passed up front, a vault balance check that only fails when accounts were copied at different moments, a threshold computed a few ticks off because a price needed scaling by an exchange rate we had skipped. Each of these became a fixture pinned to real mainnet data, so it can’t come back quietly.

What a fork can’t tell you

A frozen copy of the chain has honest limits, and we keep them in mind when reading results:

  • No competitors. Nobody else is racing for the same liquidation, so every opportunity looks winnable.
  • No market reaction. DEX pools don’t follow the crashed price, so sale prices, and therefore profits, look better than they would.
  • No real network. Bundles land instantly and request limits aren’t enforced. We once shipped a change that fetched more accounts per call than production RPCs allow. The fork happily served it; production didn’t. Since then a separate check loads everything from the real endpoints before every deploy.

So the harness answers one question very well: if prices move, does the system do the right thing? How often prices move, and how many races we win, only mainnet can tell us.