The Maximal Extractible Value (MEV) is a central notion in smart contracts for decentralized finance. When an adversary proposes a new block of transactions, they can choose what to include in the block, and in which order. This privilege can be sometimes exploited to obtain a cryptocurrency gain: the MEV.
We formalize MEV in Lean 4. We provide a general framework providing the main definitions and related theorems, which can be applied to arbitrary smart contracts. We then put our tools to use, and establish the MEV of a few use cases.
This helper library defines a type for positive reals and related results. It is akin to the NNReal library in Mathlib.
Most results in this library are taken from this repository.
-
Contains coercions to
NNRealandReal, and related properties. -
Contains basic propositions about inequalities of
PRealvalues. -
Defines the square root of a
PRealvalue, and a few related propositions. -
Contains ad hoc lemmas that simplify MEV calculations for AMM contracts.
-
Defines subtraction for
PRealvalues, and related propositions.
The core library.
-
Defines a generic smart contract system, as well as MEV. Also contains our main theorem,
MEV.characterizationwhich allows us to establish the MEV of a contract. -
Contains the simplest example of application of our theory, an Airdrop contract.
-
Contains an implementation of a coin pushing game, and establishes its MEV when the blockchain mempool is empty, or when it has a single transaction.
-
Defines our main example contract, a constant product Automated Market Maker.
-
Establishes the MEV of an AMM with an empty mempool.
-
Establishes the MEV of an AMM whose mempool has a single transaction.