This repository contains a formalization of Automated Market Makers in the Lean 4 Theorem Prover. The formalization is based on the work of Bartoletti, Chiang and Lluch-Lafuente in A theory of Automated Market Makers in DeFi.
In AMMLib/State we give the fundamental definitions to model the state of a system of AMMs as well as to model several properties of a state, such as the price and the supply of minted tokens.
Token.lean contains definitions for the two most basic underlying types: Account and Token. Both are defined as wrappers to the natural numbers, which renders equality decidable.
Wallets are split into two types.
- AtomicWall.lean contains definitions for wallets of atomic token types
W₀. They are modeled as finitely supported functions fromTokento non-negative reals. - MintedWall.lean contains definitions for wallets of minted token types
W₁. They are modeled as constrained finitely supported functions from pairs ofTokento non-negative reals. Then, AtomicWallSet.lean and MintedWallSet.lean define sets of atomic token walletsS₀and sets of minted token walletsS₁as finitely supported functions fromAccounttoW₀and fromAccounttoW₁respectively.
We do not define a type for a singular AMM, instead we directly define sets of AMMs AMMs in AMMs.lean. Similarly to minted token type wallets, they are modeled as constrained finitely supported functions from pairs of Token to non-negative reals. The constraints on a set f of AMMs are:
f t0 t1 ≠ 0 ↔ f t1 t0 ≠ 0f t t = 0The intuition is thatf t0 t1andf t1 t0will be the amount of tokent0and of tokent1respectively in the liquidity pool of thet0-t1decentralized exchange.
In State.lean we define the state type Γ as a triple of sets of atomic type wallets, sets of minted type wallets and sets of AMMs. In Networth.lean we define the price of a unit of a minted token type and the wealth of a user. In Supply.lean we define the supply of tokens in a state.
In AMMLib/Transaction we define the various types of transactions and prove some of their effects on the state and on the users' wealth, giving particular attention to the swap transaction, to which we dedicate multiple files in the Swap directory. There we define several properties a swap rate function may have and their consequences on the results of the swap transactions. In Constprod.lean we define the constant product swap rate function and prove some of its properties and economic features.
In Trace.lean we define valid sequences of transactions starting from a state, which we use to define reachable states as valid sequences of transactions starting from the initial state. We then prove that the existence of an AMM Implies the circulation of its associated minted token type.