Reproduced to the wei. Then made unwritable.
The attacker drained the osETH/wETH pool in three transactions. Dimension's Fe port of Balancer runs every one of their 360 steps from the real chain state and computes exactly what Balancer's Vault computed. The same steps on Dimension's typed kernel pay the pool, until the attacker's batch reverts.
Every step of the real exploit, on both arithmetics.
The same values as a table
| Transaction | Steps | Balancer: value per share before → after | Typed: reverts at step | Typed: value per share before it stops |
|---|---|---|---|---|
| 1 · block 23,717,397 | 121 | 1.0273 → 0.0202 | 25 (BAL#001) | 1.1856 |
| 2 · block 23,717,398 | 121 | 0.0202 → 4.12×10⁻⁴ | 25 (BAL#001) | 0.0233 |
| 3 · block 23,717,399 | 118 | 4.12×10⁻⁴ → 9.19×10⁻⁶ | 22 (BAL#001) | 4.76×10⁻⁴ |
Fixtures are the swaps Balancer's Vault executed on this pool, read from each transaction's events. Each transaction is replayed on a mainnet fork at that transaction. The faithful kernel must compute every amount Balancer computed, and must end in the pool's real state once the real transaction runs on the same fork. The typed kernel charges more once balances reach dust, and the attacker's plan stops fitting the pool: step 25 (counting from 0) fails Balancer's own BAL#001 check.
The attacker's own sequence cannot run against the typed kernels. An attacker who knew the pool rounds correctly would search for another, so the replay runs one. From the dust state the attacker's own exits leave, it re-plans each of 90 swaps, trying about 95 amounts in both directions and keeping the one that leaves the value per share lowest.
| Re-planning attacker | Start | Lowest | After 90 swaps |
|---|---|---|---|
| Balancer (faithful) | 1.0293 | 0.5771 (-43.9%) | 0.5784 (-43.8%) |
| Typed | 1.0293 | 1.0293 (the start) | 1.1640 (+13.1%) |
| Guarded | 1.0293 | 1.0293 (the start) | 1.1640 (+13.1%) |
Value per share: the invariant over the BPT supply. Evidence, not proof: the candidates are a fixed set and each choice is greedy. The exhaustive search below covers every single operation, on smaller pools.
- ✓holdsDoes the Fe port compute every step of the real exploit exactly as Balancer's Vault did?360 of 360 steps across the three attack transactions (121, 121, 118): every amount equal
- ✓holdsAfter each attack transaction, does the faithful kernel hold exactly what the real pool held?WETH and osETH balances and BPT supply equal after each transaction; value per BPT 1.02735 -> 0.02019 -> 0.000412136 -> 9.19223e-06
- ✓holdsDo the typed and guarded kernels stop the attacker's own sequence?yes: transaction 1 reverts at step 25 with BAL#001; transaction 2 reverts at step 25 with BAL#001; transaction 3 reverts at step 22 with BAL#001. Before that, value per BPT on the typed kernel rose from 1.0273 to 1.1856
- ✓holdsFrom the real dust state, does an attacker who re-plans every swap find a loss on Balancer's arithmetic, and none on Dimension's?on Balancer's arithmetic: -43.9% in 90 swaps. On the typed and guarded kernels, never below where it started; after 90 swaps it ends +13.1%
Three independent proofs, before any type is applied.
The port covers FixedPoint, Math and StableMath line by line, the composable stable pool's swap, join and exit paths, and the protocol fees it mints on every join and exit. Without the fees the replay could not match: the pool minted fee shares 33 times in the first attack transaction alone.
Written down, reviewed, shipped.
function _upscale(uint256 amount, uint256 scalingFactor) pure returns (uint256) {
// Upscale rounding wouldn't necessarily always go in the same direction: in a swap for example the balance of
// token in should be rounded up, and that of token out rounded down. This is the only place where we round in
// the same direction for all amounts, as the impact of this rounding is expected to be minimal.
return FixedPoint.mulDown(amount, scalingFactor);
}$ make show CASE=typed_pool_with_balancer_upscale
the bug: rejected by the compiler, error[8-0000]: type mismatch
expected `Quantity<_, Out, Admitted>`, but `u256` is given
the fix: - let scaled_out = upscale(amount_out.value(), factor.value())
+ let scaled_out = admit(amount_out, factor)
fixed: compiles; PASS charges_what_the_typed_pool_charges_and_more_than_balancerThe typed kernel changes Balancer in exactly the two places it rounded an outgoing amount down. Everything else is identical, every StableMath call included, and the typed kernel reaches StableMath only through signatures that demand admitted amounts.
4,608 pool states, every operation, three kernels.
Natively, in exact integers: 3.14 million operations per kernel. Each cell counts operations that lowered the value behind each pool share, and operations that stored an invariant its balances do not support.
| Operation | Balancer (faithful) | Typed | Typed + guard |
|---|---|---|---|
| swapGivenOut | 48 / 0 | 0 / 0 | 0 / 0 |
| exitGivenOut | 57,990 / 108,976 | 8 / 16 | 0 / 0 |
| joinGivenIn | 9 / 14 | 9 / 14 | 0 / 0 |
| joinGivenOut | 0 / 57 | 0 / 57 | 0 / 0 |
The types remove the losses at Balancer's two rounding sites. What remains comes from StableMath's own precision, identical with and without the types, and the guard rejects exactly those operations.
| Greedy attacker, repeated exact-out exits | Balancer | Typed | Guarded |
|---|---|---|---|
| 300 / 300 wei, rate 1.058, amp 50 | -0.743 | -95.011 | -95.011 |
| 8 / 8 wei, rate 1.150, amp 50 | +1.386 | -2.493 | -2.493 |
| 1000 / 1000 wei, rate 1.210, amp 200 | +20.923 | -151.416 | -151.416 |
The attacker's gain, in invariant units. Positive only on Balancer's arithmetic.
The three kernels as deployed contracts.
- ✓holdsDeployed on an EVM, does the faithful pool let a repeated-exit attacker take value from LPs?value per BPT -3.38% over 51 exits
- ✓holdsDo the typed and guarded pools keep the value behind each BPT under the same attack?typed +19.51%, guarded +19.51%
- ✓holdsDoes every optimization level compute the same attack, to the wei?the attack's exits and value per BPT on each kernel are identical at -O0, -O1, -O2 and -Os
- ✓holdsBuilt on Dimension itself, does typed code return exactly what the same code written by hand on raw u256 returns, at every optimization level?4,096 calls per level over 8 functions (the four roundings, an ERC-4626 vault's four previews), reverts included: all equal at -O0, -O1, -O2 and -Os
- ✓holdsAbove -O0, is Dimension's typing free, gas for gas, against the same code written by hand?at -O1, -O2 and -Os the roundings compile to identical bytes, and the vault to the same size with identical gas on every call. At -O0, which no one deploys, typed costs rounding +7.8%, vault +15.0% per call
| Gas, the pool's real parameters | Balancer (faithful) | Typed | Guarded |
|---|---|---|---|
| swapGivenIn | 15,164 | 16,104 (+6.2%) | 22,644 (+49.3%) |
| swapGivenOut | 15,981 | 17,000 (+6.4%) | 23,553 (+47.4%) |
| exitGivenIn | 23,240 | 24,189 (+4.1%) | 24,995 (+7.6%) |
| exitGivenOut | 24,493 | 25,424 (+3.8%) | 26,230 (+7.1%) |
| joinGivenIn | 25,585 | 26,472 (+3.5%) | 27,278 (+6.6%) |
| joinGivenOut | 23,453 | 24,452 (+4.3%) | 25,258 (+7.7%) |
| mean gas, -O0 | 29,850 | 32,779 (+9.8%) | 36,056 (+20.8%) |
| mean gas, -O1 | 21,319 | 22,274 (+4.5%) | 24,993 (+17.2%) |
| mean gas, -O2 | 21,303 | 22,258 (+4.5%) | 24,978 (+17.3%) |
| mean gas, -Os | 22,744 | 23,783 (+4.6%) | 26,587 (+16.9%) |
| bytecode, -O0 | 19,423 B | 29,161 B | 29,167 B |
| bytecode, -O1 | 8,014 B | 9,412 B | 9,867 B |
| bytecode, -O2 | 7,992 B | 9,400 B | 9,855 B |
| bytecode, -Os | 9,139 B | 10,299 B | 10,685 B |
The type machinery itself compiles away. The typed contract's near-constant extra cost is real work: reading its rate through the banded boundary and checking its factors on every operation. The guard costs one invariant solve on swaps. Starting that solve from the invariant before the swap would be cheaper, but at dust balances Newton's integer iteration then settles on a different value: tried in the search, it let 13 losing operations through, so the guard keeps Balancer's own solve. Every optimization level computes the same attack to the wei; -Os costs more gas than -O2 as well as more bytes. Value per share under the EVM attack: -3.38% on Balancer's arithmetic, +19.51% typed, starting from 1.00.
The same contracts on Base Sepolia, attacked on chain →
The full account, with every limit →