← All posts
· Updated on

DeFiTraderBot: one bot against chains of bots on BNB Chain

From an arbitrage POC to watching an ecosystem of automated programs: latency, local copies of the chain and operator tracing.

DeFiTraderBot started as an arbitrage proof of concept on BNB Chain, a public blockchain: buy a token in one place, sell it for more in another. It became an observation post on a chain where almost everything that moves is a program.

The outcome fits in one sentence: it is not profitable on either of the two grounds explored. So this article is not about returns. It is about the technical challenge: understanding what the other bots do, and how they are organised.

A few words of vocabulary

  • A pool is a reserve of two tokens deposited in a program on the chain. Anyone can swap one for the other there; the price depends on the quantities left.
  • Liquidity is the content of that reserve. Whoever deposited it can often take it back.
  • A liquidity withdrawal (a “rug pull”): the creator of a token takes the reserve back after buyers have brought their money. Their tokens are then worth nothing.
  • A node is a server that gives access to the chain. An archive node also keeps the whole history.
  • Gas is the fee paid for each operation. Paying more lets you go ahead of the others.

First ground: arbitrage

Arbitrage means taking advantage of a price gap between two pools for the same token. You first need to know all the pools. The service keeps an inventory in a local database: 3.7 million pairs of one type, 91,000 pools of another, 3.6 million tokens.

Three technical difficulties:

  • Public, free nodes, and therefore limited ones. Each caps the number of questions per second, and some refuse certain operations. The service uses 21 of them through a shared scheduler: a quota per node, two priority levels, a preference for the fastest and most reliable. Each error is classified, to decide whether to retry elsewhere or wait.
  • A history that is almost out of reach. Only one of these nodes agreed to serve history in large slices. To find recent pools, the service therefore queries the programs that create them directly, while the reading of history moves slowly in the background.
  • A picture taken at a single instant. The chain moves forward in blocks, about two per second. Comparing two prices read at two different blocks shows gaps that do not exist. All the reads of one comparison are therefore grouped into a single question (a “Multicall”), which answers for one precise block.

Out of one million comparisons, about 39,000 showed a positive gap by calculation. The best 195 were replayed in real conditions: none was validated. The gaps came from tokens that take a tax on each swap, that block selling, or that reject certain buyers. A visible price gap is most often a trap for programs that calculate without checking.

Checking on a local copy of the chain

A displayed price says nothing about what a token’s code does. The only way to know is to execute the swap. A tool called anvil makes that possible without risk: it creates on the PC a copy of the chain as it is right now (a “fork”), on which anything can be tried with no real money and without sending anything to the real chain.

On that copy, the service:

  • buys then sells the token: the difference between the amount received and the amount announced gives the real tax;
  • transfers the tokens to a second wallet, which sells them: if that fails, the token keeps a blacklist;
  • attempts a buy five times larger, to detect a per-operation limit;
  • reverts everything after each measurement, so that the next one starts from the same state.

Second ground: newly launched tokens

Every day, hundreds of new tokens are launched with their pool. The second module follows them and simulates buys, without buying anything. The first finding sets the scene: 97% of these pools lose their liquidity within 24 hours, half of them in under 12 minutes. Here, the difficulty is time: everything is decided in the first minute.

The first real-time run gave results very far from the simulation on history, because of reaction time. Spotting a new pool took about 5 seconds; it now takes 0.3 to 0.4. The causes were ordinary:

  • the PC clock was 2.55 seconds behind, which skewed every measurement;
  • the program only looked for new items every 5 seconds;
  • the permanent connection that announces new blocks was sometimes dropped by the provider, with no error message;
  • two parts of the program were waiting on each other;
  • the database saved rows one by one, instead of grouping them.

On the other side: other bots

Seventeen days of history give a portrait of those who buy 15 to 90 seconds after a launch:

  • 83% of buys come from the same wallets, a few hundred of them, active day and night on about fifty pools a day: these are automated programs;
  • none pays more gas to get ahead of the others: the race is not run there;
  • most go through their own program rather than the standard interface;
  • some buy and sell at a loss, in a loop: this looks like activity manufactured to make a pool attractive;
  • out of 62 wallets followed in detail, only one makes money, and nearly one position in two is never sold.

That last point changes the reading. These bots are not really competing with each other: they are the ones other programs come looking for.

Chains of bots

One night, 35 pools are drained in 45 minutes, by 35 different creators. It looks like 35 independent authors. On a public chain, every transfer of money is visible, so the origin of each creator’s money can be traced. That tracing shows a single operator, or a small group:

  • Single-use wallets, funded in a chain. The creator of a drained pool sends the money to the next creator, with amounts identical to the hundredth.
  • Launch “kits”. The same starting amounts come back thousands of times: it is the same tool, used in parallel by several chains.
  • Timers. Withdrawals happen on schedule, around 10 minutes for two kits, and earlier when the pool does not attract enough buyers. One chain is regular to within 14 seconds: that is a program, not a person.
  • Few destinations. The money of dozens of creators ends up on two or three addresses.

Over one day, 60 chains of this kind cover 89% of the fast withdrawals whose creator could be identified.

The trap itself is the most instructive part. Addresses linked to the creator buy in the first seconds and sell with a small gain. They produce exactly what buying bots check before entering: activity, and sells that work. The bots enter, the liquidity is withdrawn less than a minute after launch, and the address that collects the money immediately launches a new pool. A tiny real trial, with a fixed stake and strict caps, fell into it twice in two minutes.

Recognising an operator

The simplest ideas do not work:

  • Remembering creators already seen. Each creator is used almost only once: only 16 out of 3,299 launched more than one pool.
  • Comparing the code, the name or the starting amount. They point to the launch tool, not its user: 95% of tokens have a twin within half an hour, including tokens with no problem.
  • Watching the market with statistical detectors, which flag an abnormal rise in withdrawals. They react 3 to 14 minutes after a wave begins, while the first losses arrive within the first 2 to 6 minutes.

The only signal that stays valid on new data is where the money comes from. At each observed withdrawal, a list receives the creator, those who sent it money, those who sent money to them, and the addresses where the money went afterwards. A new pool is refused if its creator or one of its sources is on the list. This list recognises 30 to 80% of an attack’s withdrawals before the buy, and wrongly refuses about 9% of pools with no problem.

The operator adapts: from one attack to the next, it changes some of its sources, and recognition drops to about 40%. Tracing the origin of the money also takes 30 to 50 questions to an archive node per level, which is too slow for a decision to be made within seconds. Hence a local database of all transfers, fed continuously, which brings that search down to a few milliseconds.

Checking the measurements

A flattering simulation is easy to produce. Several precautions limit that risk:

  • Replaying without knowing what comes next. Each rule is tested with only the information available at decision time, then checked on a period that was not used to tune it.
  • Comparing simulation with execution. Each simulated buy is replayed on the local copy with the real execution code, to measure the gap between the two.
  • Framing real execution. A separate switch, mandatory limits (the service refuses to start if one is missing) and two emergency stops: one blocks buys but still allows selling, the other blocks everything.
  • Testing the code. More than 1,100 automated tests, none of which touches the network.

Outcome: profitable on neither ground

  • Arbitrage: no executable opportunity was demonstrated. The visible gaps almost all came from tokens that were taxed, blocked or impossible to sell.
  • Launches: some settings looked like winners in simulation. They do not hold up against organised withdrawals: the operators manufacture precisely the signals a buying bot looks for, and change method once they are recognised.

The value of the project lies elsewhere: in what had to be built to find that out.

The stack

  • .NET 10 and C#, as a Windows service (Microsoft.Extensions.Hosting)
  • Nethereum for calls to the nodes, Multicall3 for grouped reads, WebSocket to follow new blocks
  • PancakeSwap and Uniswap, V2 and V3, including mixed and triangular routes
  • SQLite, with separate databases so that the different modules do not block each other
  • Solidity for the execution contract, anvil for local copies of the chain
  • Node.js for study scripts
  • xUnit for tests

The method: Codex, then Claude Code

The project started with Codex and continued with Claude Code. The AI writes most of the code and the studies; the work is framing, reviewing, testing and deciding.

Two habits mattered. A reference document describes every rule of the bot in plain language, and the assistant reads it at every session. And each report separates what is measured from what is deduced, lists its limits and notes the measurement errors corrected along the way.

The clearest thing the project showed is not a strategy. On this market, the opponent is not a price: it is a program watching the same blocks, and knowing what the other programs will look at.

Lire en français →