Using Dimension
Dimension is for engineers writing DeFi contracts in Fe, and for teams porting Solidity protocols to Fe. It is a Fe library ingot, Fe's equivalent of a Rust crate. Your contracts depend on it, its rules are checked when they compile, and the types themselves cost nothing at run time.
This guide covers three things:
- getting a project running with it, in five minutes;
- each concept, linked to the tested code in this repository it draws on (two snippets are marked as sketches of a pattern);
- a checklist for taking a protocol built on it to production.
Requirements
- Fe 26.6.0 or 26.7.0. CI pins 26.6.0, and also runs every ingot, test
suite and compile-fail case on 26.7.0 (
make compat). Install Fe with its installer:curl -fsSL https://raw.githubusercontent.com/argotorg/fe/master/feup/feup.sh | bash -s -- --version v26.6.0 - Targets. Everything compiles for the EVM. Everything except
value::oracle, which calls other contracts, also compiles natively. Fe 26.6.0 builds native executables on Linux x86-64 and Apple silicon.
Getting started
With the command line (pipx install "git+https://github.com/Femtech-web/Dimension#subdirectory=cli", or python3 cli/dimension.py from a clone),
dimension new ../my_protocol does steps 1 to 3: it creates the quickstart
project with Dimension pinned to the latest release. By hand:
1. Create a project.
fe new my_protocol && cd my_protocol
2. Add Dimension to fe.toml, pinned to a release commit:
[dependencies.dimension]
source = "https://github.com/Femtech-web/Dimension"
rev = "5a1aafe80e2f9034242fb6f5ada9b2495685d2ac" # v0.3.1
name = "dimension"
version = "0.3.1"
- Fe resolves
revas a full commit hash. A tag name is rejected, so take the commit listed for the release in the changelog. nameselects thedimensioningot inside the repository's workspace.- For a local checkout, use a path relative to your project; Fe rejects
absolute paths:
dimension = { path = "../Dimension/ingots/dimension" }.
3. Write typed code. Copy the quickstart's
src/lib.fe and
src/tests.fe into your project. They
are a deposit desk contract and its tests, and their core is this:
use dimension::{Asset, In, Out, Quantity, Settled, admit, given, owed, scaling_factor, settle}
pub struct USDC {}
impl Asset for USDC {
const DECIMALS: u256 = 6
}
/// Internal units credited for a deposit of `raw` USDC. Value arriving is counted down.
pub fn credit(_ raw: u256) -> u256 {
admit(given<USDC, In>(raw), scaling_factor<USDC>()).value()
}
/// USDC paid out for `internal` units owed. Value leaving is paid down.
pub fn payout(_ internal: u256) -> Quantity<USDC, Out, Settled> {
settle(owed<USDC, Out>(internal), scaling_factor<USDC>())
}
4. Check, test and build.
fe check # every Dimension rule, at compile time
fe test # the 3 tests, on the EVM
fe test --backend native # and natively
fe build # the Desk contract's bytecode and ABI, in out/
5. See a rule hold. Change payout to return
Quantity<USDC, In, Settled>, a payout treated as a charge. fe check rejects
it: expected Quantity<USDC, In, Settled>, but Quantity<USDC, Out, Settled> is given.
All of this is checked on every CI run (make consumer): dimension new
creates the project, which takes Dimension by git and must pass its tests, and
a misuse must be rejected with Dimension's type error.
For the full API, every item with its doc comment, see the reference generated
by fe doc: /api on the project site, or make apidoc locally.
Declare your assets
One type per token, with its decimals. The type is how every amount, price and value names its asset.
use dimension::Asset
pub struct USDC {}
impl Asset for USDC {
const DECIMALS: u256 = 6
}
scaling_factor<USDC>() requires T::DECIMALS <= 18 at compile time.
Move amounts in and out
The pattern from the Balancer kernel
(pool/typed.fe) has five steps:
- Label what the caller names with its direction.
- Bring it into internal units with
admit. - Let your math return an owed result.
- Leave through
settle. - Keep what you hold as a
Held<T>and what you issue as aSupply<T>, and move them withcreditanddebit, orissueandretire.
The direction, In or Out of the protocol, decides every rounding. A sketch:
// 1. An amount the caller wants out of the protocol.
let wanted = given<WSTETH, Out>(17)
// 2. Admission rounds Out up: 17 wstETH at rate 1.058 counts as 18 internal units.
let scaled = admit(wanted, factor)
// 3. Your math takes admitted amounts and returns owed ones.
let charge: Quantity<WSTETH, In, Owed> = owed<WSTETH, In>(compute(scaled.value()))
// 4. Settlement rounds In up: the caller is charged at least what is owed.
let to_pay = settle(charge, factor)
| Flow | admit |
settle |
|---|---|---|
In: value arriving |
rounds down | rounds up |
Out: value leaving |
rounds up | rounds down |
Make your math's signatures demand the stages, as
typed/math.fe does:
pub fn bpt_in_given_exact_token_out<T, const N: usize>(
_ pool: Pool<N>,
_ balances: [u256; N],
index: usize,
amount_out: Quantity<T, Out, Admitted>,
invariant: u256,
) -> Quantity<Bpt, In, Owed>
Then an amount that skipped admit cannot reach the math. Putting Balancer's
_upscale back is a compile error.
Step 5 is what makes a label more than a promise. A Held<T> is credited only
with an amount of T moving In, and debited only with one moving Out, both
in token units (Given or Settled); a Supply<T> is issued only Out and
retired only In. So an amount labelled with the wrong direction, or of the
wrong token, does not compile where it changes a balance; a supply cannot be
credited as if held; and internal 18-decimal units never reach either. The
vault (vault.fe) keeps its assets as a
Held<A> and its shares as a Supply<Shares<A>>:
pub fn withdraw<A>(_ v: mut Vault<A>, _ assets: Quantity<A, Out, Given>) -> Quantity<Shares<A>, In, Settled> {
let shares = preview_withdraw(v, assets)
v.total_assets = v.total_assets.debit(assets) // assets leave
v.total_shares = v.total_shares.retire(shares) // shares come back
shares
}
Write v.total_assets.credit(assets) there, and the withdrawal's own label
refuses it: expected Quantity<A, In, _>, but Quantity<A, Out, Given> is given. A deposit labelled, consistently, as leaving, which would round its
shares up, is refused the same way
(vault_deposit_labeled_as_leaving).
Rates come in through ScalingFactor<T>::with_rate, and a Rate<T> exists
only from a checked read (below). For a fee added to a charge or taken from a
payout, Quantity<T, F, Settled>::scaled_by(numerator, denominator) keeps the
direction's rounding.
Read prices and rates at a verified boundary
Declare each feed once, with its pair as types and the bounds a sane answer
must meet. feed is a const fn, so a registry is a set of constants
(moonwell/src/registry.fe):
use dimension::value::oracle::{Feed, feed}
pub const ETH_USD: Feed<ETH, USD> = feed(
Address::new(0x71041dddad3595F9CEd3DcCFBe3D1F4b0a16Bb70),
decimals: 8,
max_age: 1_800,
min_wad: 100 * WAD,
max_wad: 100_000 * WAD,
)
The feed is checked twice:
- Bind it in your constructor.
verify(ETH_USD)checks the decimals the feed reports. - Read it with the effects it needs.
read(ETH_USD)requiresuses (call: mut Call, ctx: Ctx)and checks that the answer is positive, not in the future, no older thanmax_age, and inside the band. Otherwise it reverts with a typed error.
Prices compose only through a shared asset
(moonwell/src/pricing.fe):
let cbeth_usd: Price<CBETH, USD> = oracle::read(CBETH_ETH) * oracle::read(ETH_USD)
Rate providers work the same way: declare rate_source<T>(addr, min_wad:, max_wad:) and read it with read_rate, which gives a Rate<T>.
Value positions and check policies
value_of rounds by side: collateral down, debt up
(tests/core/src/value.fe).
let collateral: Value<USD, Collateral> = value_of(Amount<ETH>::new(WAD), price)
let debt: Value<USD, Debt> = value_of(Amount<ETH>::new(WAD * 85 / 100), price)
A collateral value and a debt value cannot be added, and a value in USD cannot be compared with one in ETH.
Risk parameters are constants, and the functions that use them state when
they are coherent (tests/core/src/policy.fe):
pub struct Conservative {}
impl RiskPolicy for Conservative {
const LTV_BPS: u256 = 8_000
const LIQUIDATION_THRESHOLD_BPS: u256 = 8_500
const LIQUIDATION_BONUS_BPS: u256 = 500
}
let healthy = is_healthy<USD, Conservative>(collateral, debt)
A policy whose loan-to-value reaches its liquidation threshold, or whose bonus would create bad debt at the threshold, fails to compile wherever it is used.
Guard what types cannot see
Some losses come from precision inside your own math. Compare the value behind each share before and after an operation. A sketch:
let before = Backing { value: invariant_before, shares: supply_before }
// ... the operation ...
let after = Backing { value: invariant_after, shares: supply_after }
assert!(preserves_share_value(before, after), "share value decreased")
The comparison is an exact 512-bit cross-multiplication. In the Balancer kernel it rejects exactly the operations that would lose value, at +7% to +8% gas on exits.
Testing
Tests need prices and rates without a live feed.
dimension::value::testing::{assume_price, assume_rate} provide them, as
unsafe fn. Only a root ingot that sets allow_unsafe = true on its
dimension dependency can call them, as
tests/core/fe.toml does. Production ingots cannot.
Keep a check like make unsafe-policy in your CI so that stays true.
Porting an existing protocol
The method used for Balancer, in order:
- Port faithfully first. Keep the original's rounding and checks, with
#![arithmetic(unchecked)]where the original wraps. - Prove the port. Compare it with the original on identical calldata, and with the deployed contract on a fork.
- Type the boundary. Change only where amounts enter and leave, and make the math's signatures demand admitted amounts.
- Measure the difference. Run both versions over the same states and attacks, and measure gas.
Before production
A checklist for a team shipping a protocol built on Dimension.
dimension check <project> --network base runs items 1, 2 and
5, finds any unsafe code, and confirms the project compiles. It also lists
every label the compiler finds, each place a raw number gets its meaning: read
each one. Items 3, 4, 6 and 7 need your judgment.
A check that cannot run, because fe is missing or the project does not
compile, is a warning, not a failure. In CI, add --strict, so that any
warning fails the build too.
- Pin a release commit, and read the changelog before moving to another.
- Never set
allow_unsafeon Dimension in an ingot you deploy. Keepassume_priceandassume_ratein test roots.dimension checkfails a project that sets it, or that containsunsafecode. - Make your math's signatures demand admitted amounts, as
typed/math.fedoes. A rawu256can then never reach it unrounded. And keep every balance as aHeld<T>and every supply as aSupply<T>, so each label is checked against the money it moves. - Declare each feed once. Set
max_agefrom the feed's measured heartbeat plus a margin, and a band no honest answer leaves. Callverifyin the constructor. - Check every registry entry before deploying, with
feed_check.pyfor Chainlink feeds. It catches a wrong address that every on-chain check accepts. For other feeds, review address, pair and decimals against the operator's own record. - Decide where the runtime guard is worth its gas. It is one invariant solve on a swap, and nearly free on joins and exits.
- Audit as usual. Dimension is not audited. Its guarantees and limits are in guarantees.md.
API at a glance
| Item | Kind | Purpose |
|---|---|---|
Asset |
trait | DECIMALS per token type |
In, Out |
flows | which way value moves; picks admission and settlement rounding |
Given, Admitted, Owed, Settled |
stages | where an amount is in its life |
Quantity<T, F, S> |
type | an amount of T moving in F at stage S |
given, owed |
functions | label a raw number |
Held<T>, held, credit, debit |
type, function, methods | a balance of T the protocol holds: credited only In, debited only Out, by amounts of T in token units |
Supply<T>, supply_of, issue, retire |
type, function, methods | a total of T the protocol issues: issued only Out, retired only In |
TokenUnits |
sealed trait | the stages in token units, Given and Settled |
admit, settle |
functions | the only ways to reach Admitted and Settled |
ScalingFactor<T>, scaling_factor, with_rate |
type, functions | decimals and rate scaling |
Price<B, Q> |
type | B priced in Q; * composes through a shared asset |
Rate<T> |
type | a token's exchange rate, from read_rate only |
Amount<T>, Value<Q, V>, value_of, haircut, covers |
types, functions | positions valued by side (Collateral, Debt) |
RiskPolicy, borrow_capacity, is_healthy |
trait, functions | risk parameters checked at compile time |
Backing, preserves_share_value |
type, function | the runtime share-value guard |
oracle::{feed, verify, read, rate_source, read_rate} |
EVM functions | the verified boundary |
testing::{assume_price, assume_rate} |
unsafe functions | test constructors |
Source: docs/guide.md