A Fe library for financial arithmetic

Types that know which way value moves.

In Dimension, the arithmetic behind two of DeFi's costliest recent failures does not compile. Each claim below is checked against the original code and the real chains, and every result on this site comes from a run you can repeat.

220,000
vectors against Balancer's Solidity, 0 mismatches
360/360
real attack steps reproduced exactly
22/22
planted bugs caught
17 wstETH leave the pool, at a rate of 1.0581
171817.988 units left the poolDimension counts 18Balancer V2 counted 17: 0.988 of a unit uncounted
An amount leaving the pool, scaled into internal units. Balancer V2 rounded every amount down, so the pool counted less than left it, on every exact-out swap and exit. In Dimension the direction comes from the type: Out rounds up, and the pool keeps the difference.

“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.”

Balancer V2, _upscale. In November 2025 that rounding drained its composable stable pools: 95 to 128 million USD by various estimates.

The decision was written down, reviewed and shipped, because nothing could enforce it: to the compiler, every amount was a u256. Three months later a Moonwell governance proposal priced cbETH with its ETH exchange rate, in a feed named cbETHETH_ORACLE, and the market valued it at 1.12 USD for eight days. Dimension puts that meaning into the types.

How it works

Four layers. Each catches what the one before cannot.

Planting a bug in any of them is caught by the check meant for it. Breaking the type rule on purpose shows the layers are independent: the typed kernel then loses exactly as Balancer did, and the guard still rejects every loss.

01 Compile time

Financial types

Amounts carry token, direction and stage; balances carry their token and role, and move only the way an amount is labelled; prices carry their pair; policies carry predicates.

stopsBalancer's _upscale in the typed pool. MIP-X43's feed in a USD slot. A deposit labelled as leaving, or credited to another token.
02 Deployment

Bind check

A contract's constructor checks each declared feed reports the declared decimals.

stopsA registry that declares cbETH/ETH as an 8-decimal USD feed.
03 Every read

Read check

Positive, fresh, inside the declared band: or the read reverts with a typed error.

stopsA registry that declares cbETH/ETH as cbETH/USD with its real decimals.
04 Every operation

Runtime guard

An exact 512-bit check that the value behind each pool share did not fall.

stopsLosses from StableMath's own precision: 17 operations in 3.14 million.
Before deployment

Feed identity

No check on chain can prove an address is the feed it is declared to be. make feeds checks each declaration against Chainlink's own directory, so the one trust left is in that directory.

Checked and attacked

Scale that answers a question.

220,000
vectors against Balancer's own Solidity, reverts included: 0 mismatches
$ make differential
669
quotes against the deployed pool, the block before the attack: 0 mismatches
$ make fidelity
360/360
of the real exploit's steps reproduced exactly; the typed kernels revert each batch
$ make replay
3.14M
operations per kernel over 4,608 pool states, exact integers, both backends
$ make explore
22/22
planted bugs caught by the check meant for each, across 7 layers
$ make mutants
23
constructions the compiler must reject, each with a corrected twin that passes
$ make compile-fail
40,000
ERC-4626 vectors equal to OpenZeppelin, no rounding named
$ make vault
14
Base blocks across MIP-X43: correct at every one
$ make moonwell
Fe highlight

Rounding derived from meaning, at zero runtime cost.

A direction is a sealed trait whose associated types name its roundings. Only admit produces an admitted amount, and it takes its rounding from the direction. A call site never names one, so it cannot name the wrong one. And the direction an amount is labelled with is checked against the money: a balance is credited only with value arriving, debited only with value leaving, and only ever by its own token.

The direction is resolved statically: on the library itself, the typed code compiles to the same bytes as hand-written mul_div calls at -O1, -O2 and -Os. Effects make a feed read declare its calls and clock, and make failure a capability: one kernel reverts with Balancer's codes on the EVM and records failures in a native search.

ingots/dimension/src/flow.fe, value.fefe
pub trait Flow: Sealed {
    /// Rounding for bringing a given amount into internal accounting.
    type Admission: Rounding
    /// Rounding for turning an internal result into an amount to transfer.
    type Obligation: Rounding
    /// What the protocol gives in exchange moves the other way.
    type Opposite: Flow
}

/// Value the protocol pays out: count at least what leaves, pay no more than owed.
impl Flow for Out {
    type Admission = Ceil
    type Obligation = Floor
    type Opposite = In
}

pub fn admit<T, F: Flow>(_ q: Quantity<T, F, Given>, _ factor: ScalingFactor<T>) -> Quantity<
    T,
    F,
    Admitted,
> {
    Quantity { v: F::Admission::mul_div(q.v, factor.v, WAD) }
}
Balancer's line, put back into the typed poolshell
$ 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_balancer
Use it in your protocol

A Fe library. Five minutes to a typed contract.

Dimension is an ingot, Fe's equivalent of a crate. dimension new creates a project from the quickstart, pinned to a release, and your contracts' amounts, prices and policies are checked whenever they compile. dimension check runs the checklist before deploying: no allow_unsafe, no unsafe code, and every Chainlink feed the feed it claims to be.

CI runs both on every change: the new project must pass its tests, a misuse must be rejected, and the check must fail the project once it can forge what Dimension trusts.

fe.tomltoml
[dependencies.dimension]
source = "https://github.com/Femtech-web/Dimension"
rev = "5a1aafe80e2f9034242fb6f5ada9b2495685d2ac"   # v0.3.1
name = "dimension"
version = "0.3.1"
with the command lineshell
pipx install "git+https://github.com/Femtech-web/Dimension#subdirectory=cli"
dimension new my-protocol
(cd my-protocol && fe test)        # 3 tests, on the EVM
dimension check my-protocol --network base
Don't take our word for it

Every claim reproduces from one front door.

with only Docker: every offline check, no networkshell
docker run --rm ghcr.io/femtech-web/dimension              # the published image
docker build -t dimension . && docker run --rm dimension   # or build it from a clone

Any other target runs the same way, such as docker run --rm ghcr.io/femtech-web/dimension make demo. Or with the toolchain:

a clean cloneshell
make toolchain   # Fe 26.6.0, downloaded and checksum-verified
make demo        # both incidents: the mistake, the verdict, the fix
make ci          # every offline check, about two and a half minutes
make replay      # the attacker's 360 steps, on mainnet forks
make mutants     # plant bugs; each must be caught