Negative cycle detection is arbitrage detection. The algorithm predates DeFi by forty years. Bellman-Ford was published in 1958. DeFi launched in 2020. The connection is direct: a negative cycle in a currency exchange graph is a guaranteed profit opportunity. The algorithm finds it. The smart contract executes it.
The insight is simple: if you can exchange A for B, B for C, and C for A, and end up with more A than you started with, you have found a negative cycle. The "negative" refers to the log of the exchange rates — a profitable cycle has a negative total log-weight.
THE DEEP DIVE
The Currency Graph
Each token is a node. Each exchange rate is a directed edge. The edge weight is -log(rate), so that the sum of weights around a profitable cycle is negative. The Bellman-Ford algorithm detects these negative cycles in O(V*E) time.
// Build the currency graph from AMM pools
function buildGraph(pools: Pool[]): Edge[] {
const edges: Edge[] = [];
for (const pool of pools) {
// Forward edge: tokenA -> tokenB
edges.push({
from: pool.tokenA,
to: pool.tokenB,
weight: -Math.log(pool.rateAtoB * (1 - pool.fee))
});
// Backward edge: tokenB -> tokenA
edges.push({
from: pool.tokenB,
to: pool.tokenA,
weight: -Math.log(pool.rateBtoA * (1 - pool.fee))
});
}
return edges;
}
// Detect negative cycles
function detectArbitrage(nodes: string[], edges: Edge[]): Cycle[] {
const dist = new Map(nodes.map(n => [n, 0]));
const prev = new Map(nodes.map(n => [n, ""]));
const cycles: Cycle[] = [];
for (let i = 0; i < nodes.length - 1; i++) {
for (const e of edges) {
const newDist = dist.get(e.from)! + e.weight;
if (newDist < dist.get(e.to)!) {
dist.set(e.to, newDist);
prev.set(e.to, e.from);
}
}
}
// Find negative cycles
for (const e of edges) {
if (dist.get(e.from)! + e.weight < dist.get(e.to)!) {
cycles.push(traceCycle(e, prev));
}
}
return cycles;
}
PRINCIPLES
- The graph is weighted by -log(rate). Positive exchange rates become additive weights. Profitable cycles have negative total weight.
- Fee structure matters. A cycle that is profitable without fees may be unprofitable with them. Include fees in the edge weights.
- The algorithm is O(V*E). For most DeFi graphs, that is sub-millisecond. The bottleneck is data freshness, not computation.
- Multi-hop arbitrage (3+ tokens) is common. The algorithm naturally handles any cycle length.
- The smart contract execution must be atomic. If the cycle closes between steps, the transaction must revert.
IN PRACTICE
Three-Token Arbitrage
ETH -> USDC -> DAI -> ETH. The cycle has three edges. Bellman-Ford finds it by relaxing all edges V-1 times, then checking for further relaxation. If any edge can still be relaxed, a negative cycle exists.
Flash Loan Arbitrage
Borrow ETH via flash loan, execute the three-token arbitrage, repay the loan, keep the profit. The entire sequence is atomic. If the profit is negative, the transaction reverts. Zero capital required, zero risk of loss.
LIVE SIGNALS
These items surfaced from the intelligence pipeline at generation time.
- Ethena (ENA) trending — Trending asset in the crypto market feed. (COINGECKO)
- GRVT Token (GRVT) trending — Trending asset in the crypto market feed. (COINGECKO)
- Ethereum (ETH) trending — Trending asset in the crypto market feed. (COINGECKO)
- COTI (COTI) trending — Trending asset in the crypto market feed. (COINGECKO)
- Pudgy Penguins (PENGU) trending — Trending asset in the crypto market feed. (COINGECKO)
ANTIPATTERNS
- Ignoring gas costs. A cycle that yields 0.01% profit but costs 0.05% in gas is a loss.
- Using Bellman-Ford on the full token graph. Pre-filter to tokens with active pools.
- Not accounting for slippage. Large trades move the price along the cycle.
- Running the detection on-chain. The graph is too large for gas. Detect off-chain, execute on-chain.
CHECKLIST
- Graph includes all active pools with correct exchange rates
- Edge weights include protocol fees
- Gas cost is subtracted from cycle profit
- Execution is atomic (flash loan or smart contract)
- The cycle is verified on-chain before execution
YOUR MOVE
Export the top 100 Uniswap V3 pools. Build the adjacency list. Run Bellman-Ford. List every negative cycle with its profit after fees.