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
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.”
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.
Reproduced exactly. Then stopped.
of the attacker's real steps computed exactly as Balancer's Vault did, by the Fe port, from the real chain state. Every end state matches the pool's.
USD for one cbETH at block 42,194,663: Moonwell's deployed oracle, then Dimension's, reading the same two Chainlink feeds.
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.
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.
Bind check
A contract's constructor checks each declared feed reports the declared decimals.
Read check
Positive, fresh, inside the declared band: or the read reverts with a typed error.
Runtime guard
An exact 512-bit check that the value behind each pool share did not fall.
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.
Scale that answers a question.
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.
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) }
}$ 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_balancerA 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.
[dependencies.dimension]
source = "https://github.com/Femtech-web/Dimension"
rev = "5a1aafe80e2f9034242fb6f5ada9b2495685d2ac" # v0.3.1
name = "dimension"
version = "0.3.1"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 baseEvery claim reproduces from one front door.
docker run --rm ghcr.io/femtech-web/dimension # the published image
docker build -t dimension . && docker run --rm dimension # or build it from a cloneAny other target runs the same way, such as docker run --rm ghcr.io/femtech-web/dimension make demo. Or with the toolchain:
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