Try   HackMD

Penumbra DEX Engine Design

Overview

We follow the notation in Improved Price Oracles: Constant Function Market Makers.

Penumbra uses a hybrid, order-book-like AMM with automatic routing. Liquidity on Penumbra is recorded as many individual concentrated liquidity positions, akin to an order book. Each liquidity position is its own AMM, with its own fee tier, and that AMM has the simplest possible form, a constant-sum (fixed-price) market maker. These component AMMs are synthesized into a global AMM by the DEX engine, which optimally routes trades across the entire liquidity graph. Because each component AMM is of the simplest possible form, this optimization problem is easy to solve: it's a graph traversal.

Execution Phases

Penumbra has no intra-block trade ordering, so DEX execution operates at the end of the block in four phases:

  1. All newly opened liquidity positions are added to the active set.
  2. Trades are batched by liquidity pair and executed (in what order? random?).
  3. The chain arbitrages all active positions using an infinite-sized flash loan and burns the arbitrage profits.
  4. All newly closed positions are removed from to the active set.

Because execution is phased, market-makers can create fill-or-kill positions with prices valid for exactly one block without having to compete for ordering within that block, by opening and then closing a liquidity position in the same transaction.

Trading Functions

Each position's trading function is of the form

φ(R)=p1R1+p2R2

with a fee parameter

γ.

A trade with input amounts

Δ=(Δ1,Δ2) and output amounts
Λ=(Λ1,Λ2)
is accepted if and only if
φ(R)=φ(R+γΔΛ).

The new reserves from this trade are

R=R+γΔΛ. Conservation of value is recorded by the equation
R+Δ=(R+γΔΛ)+Λ+(1γ)Δ.

On the left-hand side, we have the initial reserves

R (held by the AMM) and the trade input
Δ
(held by the trader); on the right-hand side, we have the updated reserves
R=R+γΔΛ
(held by the AMM), the trade output
Λ
(held by the trader), and the fee
(1γ)Δ
.

The fee is recorded by being accumulated into the position's reserves, which are updated as

R=R+(1γ)Δ.

Explicit Formulas

In practice,

Δ1Δ2=0, i.e., one or the other input type is zero.

When

Δ2=0,
Λ1=0
and the trader is trading
Δ1
for
Λ2
. The formulas become
φ(R)=φ(R+γΔΛ)p1R1+p2R2=p1(R1+γΔ1)+p2(R2Λ)

so we can compute

Λ2 as
p2Λ2=p1(R1+γΔ1)p1R1+p2R2p2R2Λ2=p1p2γΔ1

Similarly, when trading

Δ2 for
Λ1
, we have
Λ1=p2p1γΔ2.

These formulas clarify the role of

p1 and
p2
as controlling the price, and
γ
as controlling the spread.

Composition of AMMs

Given two AMMs,

φ(R1,R2)=p1R1+p2R2 with fee
γ
trading between assets
1
and
2
and
ψ(S2,S3)=q2S2+q3S3
with fee
δ
trading between assets
2
and
3
, we can compose
φ
and
ψ
to obtain a synthetic position
χ
trading between assets
1
and
3
that first trades along
φ
and then
ψ
(or along
ψ
and then
φ
).

We want to write the trading function of this AMM as

χ(T1,T3)=r1T1+r3T3 with fee
ε
, prices
r1,r2
, and reserves
T1,T2
.

First, write the trade inputs and outputs for each AMM as

Δχ=(Δ1χ,Δ3χ),
Λχ=(Λ1χ,Λ3χ)
,
Δφ=(Δ1φ,Δ2φ)
,
Λφ=(Λ1φ,Λ2φ)
,
Δψ=(Δ2ψ,Δ3ψ)
,
Λψ=(Λ2ψ,Λ3ψ)
, where the subscripts index the asset type and the superscripts index the AMM. We want
Δχ=Δφ+Δψ
and
Λχ=Λφ+Λψ
, meaning that
(Δ1χ,Δ3χ)=(Δ1φ,Δ3ψ),(Λ1χ,Λ3χ)=(Λ1φ,Λ3ψ),(Δ2φ,Δ2ψ)=(Λ2ψ,Λ2φ).

The reserves

T1 are precisely the maximum possible output
Λ1χ
. On the one hand, we have
Λ1χ=Λ1φR1
, since we cannot obtain more output from
φ
than its available reserves. On the other hand, we also have
Λ1χ=Λ1φ=p2p1γΔ2φ=p2p1γΛ2ψp2p1γS2,

since we cannot input more into

φ than we can obtain as output from
ψ
. This means we have
T1=max{R1,p2p1γS2}T3=max{S3,q2q3δR2},

using similar reasoning for

T3 as for
T1
.

On input

Δ1χ, the output
Λ3χ
is
Λ3χ=Λ3ψ=q2q3δΔ2ψ=q2q3δΛ2φ=q2p1q3p2δγΔ1φ=q2p1q3p2δγΔ1χ,

and similarly on input

Δ3χ, the output
Λ1χ
is
Λ1χ=Λ1φ=p2p1γΔ2φ=p2p1γΛ2ψ=p2q3p1q2γδΔ1ψ=p2q3p1q2γδΔ1χ,

so we can write the trading function

χ of the composition as
χ(T1,T3)=r1T1+r3T3

with

r1=p1q2,
r3=p2q3
, fee
ε=γδ
, and reserves
T1
,
T3
.

Liquidity Positions

A liquidity position consists of:

  • A trading pair
    (a1,a2)
    recording the asset IDs of the assets in the pair. The asset IDs are
    Fq
    elements, and the pair is made order-independent by requiring that
    a1<a2
    .
  • A trading function
    φ
    , specified by
    p1,p2,γ
    .
  • A random, globally-unique 32-byte nonce
    n
    .

This data is hashed to form the position ID, which uniquely identifies the position. The position nonce ensures that it is not possible to create two positions with colliding position IDs.

The reserves are pointed to by the position ID and recorded separately, as they change over time as trades are executed against the position. One way to think of this is to think of the position ID as an ephemeral account content-addressed by the trading function whose assets are the reserves and which is controlled by bearer NFTs recorded in the shielded pool.

Positions have four position states, and can only progress through them in sequence:

  • an opened position has reserves and can be traded against;
  • a closed position has been deactivated and cannot be traded against, but still has reserves;
  • a withdrawn position has had reserves withdrawn;
  • a claimed position has had any applicable liquidity incentives claimed.

Control over a position is tracked by a liquidity position NFT (LPNFT) that records both the position ID and the position state. Having the LPNFT record both the position state and ID means that the transaction value balance mechanism can be used to enforce state transitions:

  • the PositionOpen action debits the initial reserves and credits an opened position NFT;
  • the PositionClose action debits an opened position NFT and credits a closed position NFT;
  • the PositionWithdraw action debits a closed position NFT and credits a withdrawn position NFT and the final reserves;
  • the PositionRewardClaim action debits a withdrawn position NFT and credits a claimed position NFT and any liquidity incentives.

Separating closed and withdrawn states is necessary because phased execution means that the exact state of the final reserves may not be known until the closure is processed position is removed from the active set.

However, having to wait for the next block to withdraw funds does not necessarily cause a gap in available capital: a marketmaker wishing to update prices block-by-block can stack the PositionWithdraw for the last block's position with a PositionOpen for their new prices and a PositionClose that expires the new position at the end of the next block.

Separating withdrawn and claimed states allows retroactive liquidity incentives (e.g.,

X rewards over some time window, allocated pro rata to liquidity provided, etc). As yet there are no concrete plans for liquidity incentives, but it seems desirable to build a hook for them, and to allow them to be funded permissionlessly (e.g., so some entity can decide to subsidize liquidity on X pair of their own accord).

Routing

To route a trade, perform a graph traversal from the source asset

s to the target asset
t
, building up a path that tracks its component positions as well as the trading function of the whole path. Select the path with the lowest price, and fill as much of the trade as possible. If not all of the trade can be filled, repeat the process with the remaining quantity.

To perform the graph traversal, use a variant of Bellman-Ford. Maintain a mapping BTreeMap<asset::Id, Path> recording the optimal route from

s to
a
for each intermediate asset
a
we've considered. Starting at
s
, iterate over all neighboring assets
a

  • Fill in description

Each Path contains:

  • a "whole-path" trading function and synthetic reserves
  • data implying the list of positions we would execute against if we were to use the path
    • do we want to track a list of positions here, or do we want to track just the list of intermediate assets?

The Path API would primarily be:

  • Path::extend(&self, edge: TradingFunction) -> Result<Path> extends self along edge or errors if the asset types don't line up
  • impl PartialOrd for Path where comparison is None if the start and end assets aren't equal, and otherwise compares by price

Data Types

  • TradingPair: a pair of assets, made logically order-independent by always choosing a canonical ordering on asset IDs
  • BatchSwapOutputData: describes the results of a batched swap
  • TradingFunction: describes the AMM, consisting of
    • TradingPair declaring the trading pair
    • BareTradingFunction with
      R1
      ,
      R2
      ,
      p1
      ,
      p2
  • Position: contains a TradingFunction, and a nonce that should be globally unique (so that we can treat positions NFTs as bearer assets).
  • Reserves: a pair of Amounts

DEX State

Consensus State

We need to record:

  • A global, append-only set of nonces used by existing positions
  • The set of positions for each trading pair (grouped by position state)
  • The current reserves for each position (only needed for open and closed positions)
  • The volume executed against each position (can be deleted at some point?)

Scratch:

  • dex/position_nonce/{nonce}
    • Records nonces used by existing liquidity positions
    • On PositionOpen, check that the nonce is not present, then add it
  • dex/positions/{trading_pair}/opened/{position_id}
    • Records opened positions for the given trading_pair

Nonconsensus State

  • Additional indices on position data (what would be useful?)
  • Cached routing data?
  • Pre-compute some common routes on liquidity position creation? i.e. to penumbra, atom, btc, usdc etc.
  • Store effective prices per block height and pair (tricky because they can change, is this even useful?)

scratch (the following is partly wrong)

Informally but slightly more precisely, a path consists of a list of liquidity positions, a fillable quantity

Δ, and a price
q
.

To route an input trade

Δa from
a
to
b
, initialize an empty path with
Δ=Δa
,
p=1
, and an empty list of positions.

To extend a path along the liquidity graph (say via asset

c), construct (or query) the list of all liquidity positions with trading pair
(a,c)
with nonzero reserves
Ra
of asset
a
, sorted by the effective price
γp/q
.[1] Select the position with minimum price, push it to the list, and update
Δmin{Δ,Ra}
,
rrγp/q
.

  • How do we formulate this in terms of exact integer arithmetic?

Exact version of one hop:

  • pair
    (a,c)
    with positions
    {(φ(0),R(0)),(φ(1),R(1)),}
    sorted by effective price
    γ(i)p(i)/q(i)
    with
    Rc(i)0
    • how do we construct byte strings such that the lex order on those byte strings is the numeric order on
      γ(i)p(i)/q(i)
      ?
    • we have an ordered k/v store for nonconsensus indexes, and we want the key to be some byte string with the correct ordering and the value to be data we use for working with the position
    • big-endian integer encodings have the property that the ordering on encodings is the ordering on integers, but we have a fraction
    • set the key as big-endian repr of
      2128γ(i)p(i)/q(i)
      ?
    • set the value as
      (φ(i),R(i))
      ? or as the position ID? if position data, we have what we need immediately, if position ID, we have to query again, but we can then name the position when we want to execute against it later. or, we could save both
  • Best price will be
    (φ(0),R(0))
    , reserves are
    R(0)=(Ra(0),Rc(0))
  • We want to fill as much of the trade as possible against the reserves. There are two cases: either the reserves of the best position are enough to execute the entire trade, or they aren't, in which case we want to consume the entire available reserves, setting
    Rc=0
    exactly rather than a dust amount so that we'll exclude it from the list in future iterations.
  • The maximal output occurs when
    Λ=Rc
    . Since (with exact arithmetic)
    Λ=γqpΔ
    , we have
    Δ=pq1γΛ
    , and we can write the input amount that causes the maximal output as
    Δmax=pq1γRc
    . (Here, the correct way to ensure conservation of value is to round up, so that we consume more of the input amount).
    • If
      ΔmaxΔ
      , the maximal output is less than the desired output, so we should consume the entire reserves, setting
      ΛRc
      and
      ΔΔmax
    • If
      Δmax>Δ
      , the maximal output is greater than the desired output, so we should
  • Suppose we do the following:
    • Set
      Δ=min{Δ,Rc(0)}
    • Compute
      Λ=round(γqpΔ)
      ("correct" defn of round tbd)
    • Update reserves
      RR+(Δ,0)(0,Λ)
      • this will accumulate fees into position
      • common case is that
        Δ>Δ
        , in this case we want to execute so that
        Rc(0)
        is set to
        0
        exactly (rather than a dust amount), so that in the future, we exclude it from the list
      • can we get that by working "forwards" from
        Δ
        to
        Λ
        or do we need to work "backwards"?
  • tmp

  1. If the canonical ordering of the pair is

    (c,a), this would instead be sorting by
    γq/p
    , but we ignore this case for simplicity of presentation. ↩︎