Skip to main content

MEV Tax Distribution

Angstrom L2 captures priority-fee MEV inside the Uniswap v4 hook and pays it out immediately. This note explains how the hook splits the tax between fee recipients and how liquidity providers receive their share. The first section summarizes the on-chain flow; the remainder reproduces the full compensation-price derivation used by the contracts.

How the Tax Flows to LPs

  1. Snapshot state: beforeSwap stores the pre-swap tick and liquidity so the hook knows exactly which ranges were crossed.
  2. Compute base deltas: TickIteratorLib walks the crossed ticks to derive each range's token deltas.
  3. Solve for a uniform price: CompensationPriceFinder chooses a compensation price p* so makers are paid as if the entire swap cleared at that level, using the formulas derived below.
  4. Accrue rewards: PoolRewardsLib updates growth accumulators (rewardGrowthOutsideX128, globalGrowthX128) that mirror Uniswap v3/v4 fee accounting.
  5. Pay on exit: When LPs remove liquidity, afterRemoveLiquidity settles their accumulated native-token rewards. Integrators can also query getPendingPositionRewards mid-position.

The hook shares the tax between the pool creator, protocol treasury, and LPs according to the fee configuration stored in the factory. LP payouts always stay in the rollup's native asset, matching the currency that sequencers prioritize.

Detailed Compensation Price Derivation

This section ports the formal derivation from the contracts repository. It defines the compensation price pp_\star that equalizes maker payoffs when a swap crosses multiple ticks.

Variables & Definitions

  • Li>0L_i > 0: virtual liquidity for range ii (Li=xiyiL_i = \sqrt{x_i y_i} where xi,yix_i, y_i are the virtual reserves for range ii)
  • pi>0p_{i} > 0 is the price of the lower boundary of range ii
  • p>0p_\star > 0: post-trade effective compensation price
  • B>0B > 0 is the bid/liquidity compensation amount.
  • Given some liquidity LiL_i and a price pp we can determine the virtual reserves in xx & yy at that price point with: x=Li1p,y=Lipx = L_i\cdot\frac{1}{\sqrt p}, y = L_i\cdot{\sqrt p}
  • To compute the net amount deltas required to cross a range ii: Δxi=Li(1pi1pi+1), Δyi=Li(pi+1pi)\Delta x_i =L_i(\frac{1}{\sqrt{p_i}}-\frac{1}{\sqrt{p_{i+1}}}),\ \Delta y_i = L_i(\sqrt{p_{i+1}}-\sqrt{p_i})
  • X^,Y^>0\hat X, \hat Y > 0: aggregated swap amount of whole ranges where the final p[ai,bi]p_\star \notin [a_i, b_i] is known to lie outside of (where ai=max{pi,pstart},bi=min{pi+1,pend}a_i = \max\{p_i, p_{\text{start}}\}, b_i = \min\{p_{i+1}, p_{\text{end}}\}, implicitly aibia_i \le b_i):

X^=iLi(1ai1bi),Y^=iLi(biai).\hat X =\sum_i L_i(\frac{1}{\sqrt{a_i}}-\frac{1}{\sqrt{b_i}}),\qquad \hat Y = \sum_i L_i(\sqrt{b_i}-\sqrt{a_i}).

Compensation Price definition

Assuming unsigned total deltas X=iΔxi,Y=iΔyiX = \sum_i {\Delta x_i}, Y=\sum_i \Delta y_i the final compensation price pp_\star is defined as:

Zero-for-One Swap: p=YX+Bp_\star = \frac{Y}{X + B} such that each range ii trades Δxi=Δyi(min{p,ΔyiΔxi})1\Delta x_i' = \Delta y_i \cdot (\min\{p_\star, \frac{\Delta y_i}{\Delta x_i}\})^{-1}

One-for-Zero Swap: p=YXBp_\star = \frac{Y}{X- B} such that each range ii trades Δxi=Δyi(max{p,ΔyiΔxi})1\Delta x_i' = \Delta y_i \cdot (\max\{p_\star, \frac{\Delta y_i}{\Delta x_i}\})^{-1}

Base Considered Swap Amount

Notice from the above definition that there will be a consecutive sub-set of ranges which will trade at (pΔyiΔxip_\star \le \frac{\Delta y_i}{\Delta x_i} for zero-for-one, pΔyiΔxip_\star \ge \frac{\Delta y_i}{\Delta x_i} for one-for-zero).

This range can be determined by walking from pstartpendp_{start} \rightarrow p_{end} and keeping track of the total sum so far X^=iΔxi,Y^=iΔyi\hat X = \sum_i {\Delta x_i},\hat Y=\sum_i \Delta y_i. At each step checking p~i=Y^X^+B/p~i=Y^X^B\tilde p_i = \frac{\hat Y}{\hat X + B}/\tilde p_i = \frac{\hat Y}{\hat X - B} depending on the swap direction. If p~i\tilde p_i is outside of the current range the current range will be part of the consecutive set.

If all ranges are depleted and p~end\tilde p_{end} lies beyond pendp_{end} then we can take p:=p~endp_\star := \tilde p_{end}.

Otherwise when a range is found such that p~i[pi,pi+1]\tilde p_i \in [p_i, p_{i+1}] we need to calculate the actual pp_\star that satisfies our original formula.

Zero-for-One (price decreasing)

Setup

For a given tick ii that is being swapped through, we have:

Δx=L(1p1pi+1),Δy=L(pi+1p),B=Y^+Δyp(X^+Δx).\Delta x = L\left(\frac{1}{\sqrt{p_\star}}-\frac{1}{\sqrt{p_{i+1}}}\right),\qquad \Delta y = L\cdot(\sqrt{p_{i+1}}-\sqrt{p_\star}),\qquad B = \frac{\hat Y+\Delta y}{p_\star} - (\hat X+\Delta x).

Quadratic in p\sqrt{p_\star}

Clearing denominators and simplifying yields:

A(p)2+2Lp(Y^+y)=0,A:=B+X^xA \cdot(\sqrt{p*\star})^2 + 2L\cdot\sqrt{p*\star} - (\hat Y+y) = 0,\qquad A := B + \hat X - x

Solutions:

p=L±L2+A(Y^+y)A=L±Y^(B+X^x)+y(B+X^)B+X^x.\begin{aligned} \sqrt{p_\star} &= \frac{-L \pm \sqrt{L^2 + A(\hat Y + y)}}{A} \\ &= \frac{-L \pm \sqrt{\hat Y (B + \hat X - x) + y (B + \hat X)}}{B + \hat X - x}. \end{aligned}

(The two radicands are equal because L2=xyL^2=xy.)

Existence & Uniqueness on (0,u](0,u] (where u=pi+1u=\sqrt{p_{i+1}})

The quadratic gives us two solutions, we now want to prove the theorem that:

There exists one unique solution that lies in the range (0,u](0, u] and that solution is given by: s+=L+L2+A(Y^+y)As_+ = \frac{-L + \sqrt{L^2 + A(\hat Y+y)}}{A}

Let's take the above quadratic as a function ϕ(s)\phi(s) s.t. ϕ(p)=0\phi(\sqrt{p_\star}) = 0:

ϕ(s)=As2+2Ls(Y^+y)\phi(s) =A s^2 + 2Ls - (\hat Y+y)

Lemma 1: Monotonicity of ϕ(s)\phi(s) over (0,u](0,u]

We prove that ϕ(s)\phi(s) monotonically increases for s(0,u]s \in (0, u] by showing that ϕ(s)0\phi'(s) \ge 0 in that range:

ϕ(s)=2As+2L\phi'(s) = 2As + 2L

ϕ(0)0\phi'(0) \ge 0: ϕ(0)=2A(0)+2L=2Lϕ(0)0\phi'(0) = 2A(0) + 2L = 2L \Rightarrow \phi'(0) \ge 0

ϕ(u)0\phi'(u) \ge 0:

  • Expand ϕ(u)\phi'(u): 2Au+2L02(B+X~x)u2L2Au + 2L \ge 0 \Leftrightarrow 2 (B + \tilde X - x)u \ge -2L
  • Prove tighter bound: B+X~xx2(x)u2L2Au2LB + \tilde X - x \ge -x \Rightarrow 2(-x)u \ge -2L \Rightarrow 2Au \ge -2L
  • Use u=yxu = \sqrt\frac{y}{x}: 2(x)yx2xyxyxxyx2yx=xy\quad 2 (-x)\sqrt{\frac{y}{x}}\ge -2\sqrt{xy}\Leftrightarrow x\sqrt{\frac{y}{x}}\le\sqrt{xy}\Leftrightarrow\sqrt{x^2\frac{y}{x}}=\sqrt{xy}

Lemma 2: Boundary signs ϕ(0)<0\phi(0) < 0 and ϕ(u)0\phi(u) \ge 0

ϕ(0)<0\phi(0) \lt 0: (Y~+y)<0- (\tilde Y + y) < 0 ϕ(u)0\phi(u) \ge 0:

  • Expand ϕ(u)\phi(u): Au2+2Lu(Y~+y)0Au^2+2Lu-(\tilde Y + y) \ge 0
  • Expand AA and simplify: (B+X^x)u2+2yY~y0(B+X~)u2Y~(B + \hat X - x)u^2+2y-\tilde Y - y \ge 0 \Leftrightarrow (B + \tilde X)u^2 \ge \tilde Y
  • Reorganize & expand u2u^2: pi+1Y~B+X~p_{i+1}\ge\frac{\tilde Y}{B + \tilde X}
  • Recognize that p~=Y~B+X~\tilde p = \frac{\tilde Y}{B + \tilde X} and that pi+1p~p_{i+1} \ge \tilde p is the precondition for beginning this calculation

Lemma 3: Unique Solution in (0,u](0,u]

Using Lemma 1 & 2 and the Intermediate value theorem we now know that there is exactly one s(0,u]s \in (0,u] s.t. ϕ(s)=0\phi(s) = 0 and therefore only one solution to the quadratic in that range.

Lemma 4: s+s_+ is that solution

s+=L+DA,D:=L2+A(Y^+y)s_+ = \frac{-L + \sqrt{D}}{A}, D:=L^2 + A(\hat Y+y) s=LDAs_- = \frac{-L - \sqrt{D}}{A}

A>0A > 0: trivially s<0s_- < 0, leaving s+s_+ as the only positive solution.

A<0A < 0:

When A<0A < 0 then D<L\sqrt D < L. This leads to both s+s_+ and ss_- being positive. However in this case we can show that:

s+<ss_+ < s_- L+DA<LDA\frac{-L + \sqrt{D}}{A} < \frac{-L - \sqrt{D}}{A} L+D>LD-L + \sqrt{D} > -L - \sqrt{D} D>D\sqrt{D} > - \sqrt{D}

All together

Together we've shown that there is always a positive solution for p\sqrt {p_\star} that lies between 00 and pi+1\sqrt{p_{i+1}} and that it's calculated via the positive solution from the quadratic.

One-for-Zero (price increasing)

For a given tick ii that is being swapped through, we have:

Δx=L(1pi1p),Δy=L(ppi),B=(X^+Δx)Y^+Δyp.\Delta x = L\left(\frac{1}{\sqrt{p_i}}-\frac{1}{\sqrt{p_\star}}\right),\qquad\Delta y = L\cdot(\sqrt{p_\star}-\sqrt{p_i}),\qquad B = (\hat X+\Delta x) - \frac{\hat Y+\Delta y}{p_\star}.

Quadratic in p\sqrt{p_\star}

Clearing denominators, substituting A:=X~+xBA := \tilde X + x - B and s:=ps := \sqrt{p_\star}, simplifying yields:

As22Ls(Y~y)=0As^2 - 2Ls - (\tilde Y - y) = 0

Solutions:

p=L±L2+A(Y^y)A=L±Y^(x+X^B)y(X^B)x+X^B.\begin{aligned} \sqrt{p_\star} &= \frac{L \pm \sqrt{L^2 + A(\hat Y - y)}}{A} \\ &= \frac{L \pm \sqrt{\hat Y (x + \hat X - B) - y (\hat X - B)}}{x + \hat X - B}. \end{aligned}

Lemma 1: Monotonicity in range [l,+)[l, +\infty) (where l=pil = \sqrt{p_i})

ϕ(s)=2As2Lϕ(s)0sLA\begin{aligned} \phi'(s) &= 2As - 2L \\ \phi'(s) \ge 0 &\Leftrightarrow s \ge \frac{L}{A} \end{aligned}

We'll call the point LA \frac L A at which ϕ(s)\phi(s) becomes monotonically increasing s0s_0 it may or may not lie in the range [l,+)[l, +\infty)

Lemma 2: Negative Lower Range Bound ϕ(s0)0\phi(s_0) \le 0

ϕ(s0)0A(LA)22L(LA)(Y~y)0L2A(Y~y)0L2AY~yL2(Y~y)AL2Y~Ay(X~B)yx0Y~Ay(X~B)y(X~B)Y~A\begin{aligned} \phi(s_0) &\le 0 \\ A\left(\frac{L}{A}\right)^2 - 2L\left(\frac{L}{A}\right) - (\tilde Y - y) &\le 0 \\ -\frac{L^2}{A} - (\tilde Y - y) &\le 0 \\ -\frac{L^2}{A} &\le \tilde Y - y \\ -L^2 &\le (\tilde Y - y) A \\ -L^2 &\le \tilde Y A - y (\tilde X - B) - yx \\ 0 &\le \tilde Y A - y (\tilde X - B) \\ y (\tilde X - B) &\le \tilde Y A \end{aligned}

X~B<0\tilde X - B < 0:

  • Reorganize into fractions: yAY~X~B\frac y A \ge \frac {\tilde Y}{\tilde X - B}
  • Trivially true because y>0,A>0,Y~>0y > 0, A > 0, \tilde Y > 0

X~B>0\tilde X - B > 0

  • Reorganize into fractions: yx+(X~B)Y~X~B\frac y {x + (\tilde X - B)} \le \frac {\tilde Y}{\tilde X - B}
  • Tighten inequality: yxY~X~B\frac y x \le \frac {\tilde Y}{\tilde X - B}
  • Trivially true as it's the pre-condition to the computation

Lemma 3: Unique solution in [s0,+)[s_0, +\infty)

Using the above lemmas together with a intermediate value theorem we now know that there is a unique solution in this range.

Lemma 4: s+[s0,+)s_+ \in [s_0, +\infty)

D:=L2+A(Y^y)s+=L+DAs=LDA\begin{aligned} D &:= L^2 + A(\hat Y - y) \\ s_+ &= \frac{L + \sqrt{D}}{A} \\ s_- &= \frac{L - \sqrt{D}}{A} \end{aligned}

When Y^y0\hat Y-y \ge 0:

  • A(Y~y)0DL2DLA(\tilde Y - y) \ge 0 \Rightarrow D \ge L^2 \Rightarrow \sqrt D \ge L
  • Trivially: s<0s_- < 0 leaving s+s_+ as the only positive solution.

When Y^y<0\hat Y-y \lt 0:

  • D<L\sqrt D \lt L following the above reasoning, meaning both s+s_+ and ss_- are positive
  • However we can trivially see that s+>ss_+ > s_-
  • Because exactly one solution must lie in [s0,+)[s_0, +\infty) it must be s+s_+ because if it were ss_- it would lead to a contradiction requiring s+>ss_+ > s_- but also s+[s0,+)s_+ \notin [s_0, +\infty).

Lemma 5: s+ls_+ \ge l

L+DAlL+Dl(x+X~B)L+DL+l(X~B)Dl(X~B)\begin{aligned} \frac{L + \sqrt{D}}{A} &\ge l \\ L + \sqrt{D} &\ge l (x + \tilde X - B) \\ L + \sqrt{D} &\ge L + l (\tilde X - B) \\ \sqrt{D} &\ge l (\tilde X - B) \end{aligned}

When X~B<0\tilde X - B < 0: Inequality true because left side is positive and left side negative.

When X~B0\tilde X -B \ge 0:

Dpi(X~B)2L2+A(Y~y)pi(X~B)2L2+AY~yxy(X~B)pi(X~B)2AY~y(X~B)pi(X~B)2AY~pi(X~B)2+y(X~B)AY~pi(X~B)2+pix(X~B)AY~pi(X~B)((X~B)+x)Y~X~Bpip~pi\begin{aligned} D &\ge p_i (\tilde X - B)^2 \\ L^2 + A (\tilde Y - y) &\ge p_i (\tilde X - B)^2 \\ L^2 + A \tilde Y - yx - y(\tilde X - B) &\ge p_i (\tilde X - B)^2 \\ A \tilde Y - y(\tilde X - B) &\ge p_i (\tilde X - B)^2 \\ A \tilde Y &\ge p_i (\tilde X - B)^2 + y(\tilde X - B) \\ A \tilde Y &\ge p_i (\tilde X - B)^2 + p_i x(\tilde X - B) \\ A \tilde Y &\ge p_i (\tilde X - B)\big((\tilde X - B) + x\big) \\ \frac{\tilde Y}{\tilde X - B} &\ge p_i \\ \tilde p &\ge p_i \end{aligned}

This is the precondition for the calculation which makes our lemma s+ls_+ \ge l true!

Final Proposition

Thanks to lemmas 3-5 we have now proven the required facts to know that s+s_+ gives us the unique p\sqrt{p_\star}.