Fees Charged for Liquidity Allocation & Deallocation



Luffy, Mihawk, Hajrudin, Clover, Rayleigh


October 31st, 2023


1. Background

In Staple's pricing mechanism, the oracle price serves as the basis for determining the trading rate of a token pair, which is then adjusted by the parameter ralrr_{alr} on the price adjustment curve. When the oracle price remains unchanged, the trading price is entirely dependent on ralrr_{alr} - a higher ralrr_{alr} leads to a lower trading price.

However, any of the following actions: liquidity allocation, liquidity deallocation, and swapping in a Virtual Trading Pair (VTP), can alter the ralrr_{alr} value of the VTP (as described in Towards Capital Efficiency - Staple's LP Architecture and Mechanism), thereby affecting the swap price for subsequent transactions. As a result, through actions like liquidity allocation or deallocation, a trader can to some extent control the ralrr_{alr} parameter of a VTP, creating opportunities for arbitrage profit by exploiting these price variations.

In specific, the following operations could give rise to an arbitrage opportunity that is detrimental to LP's assets:

  1. A trader swaps token0token_0 for token1token_1 to move up ralrr_{alr}.

  2. The trader then allocates token0token_0 liquidity to or deallocates token1token_1 liquidity from the same VTP, driving ralrr_{alr} even higher.

  3. The same trader swaps the same amount of token1token_1 he received in step 1 back to token0token_0.

Notice:

With the same definition as in "Staple's Pricing Mechanism":

We clarify the four allocation and deallocation patterns that result in a higher ralrr_{alr}, potentially creating arbitrage opportunities that can be detrimental to LP assets in the aforementioned step 2.

To prevent the harmful arbitrages described above, fees should be applied to liquidity allocation and deallocation. These fees are charged on LPs when ralrr_{alr} is within a certain range (RRSRRS), beyond which LPs are exempt from the fees. However, transactions conducted outside this range will incur an additional price penalty.

It's important to note that the example provided above only considers the trading direction from token0token_0 to token1token_1. In the opposite trading direction, there are equivalent allocation and deallocation patterns that will increase ralr1r_{alr_1}, and fees will also be charged for these actions. As a result, fees are applied to any allocation or deallocation action, regardless of the trading direction.

For the sake of simplicity in the following analysis, we will focus on the trading direction from token0token_0 to token1token_1.

2. Definition & Pre-calculation

In line with the price calculation derived in Staple's Pricing Mechanism, the table below demonstrates the arbitrage process:

start step 1 step 2 step 3
swap token0token_0 to token1token_1 (de)allocate swap token1token_1 to token0token_0
amountinamount_{in} yy DD token0token_0 or D/Pas0D/P_{as_0} token1token_1 yPav0y*P_{av_0}
Pas0P_{as_0} Pas0P_{as_0} Pas0P_{as_0}' Pas0P_{as_0}'' Pas0P_{as_0}'''
swap price Pav0=(Pas0Pas0)12P_{av_0} =(P_{as_0}*P_{as_0}')^{\frac{1}{2}} Pav01=(Pas0Pas0)12P_{av_0}'^{-1}=({P_{as_0}''}*{P_{as_0}'''})^{-\frac{1}{2}}
amountoutamount_{out} yPav0y*P_{av_0} yPav0Pav01y*P_{av_0}*P_{av_0}'^{-1}

To symplify the calculation, we further define the parameter MM:

A trader will benefit y(M1)y*(M-1) token0token_0 from the above arbitrage. Therefore

AER={y(M1)D,token0(de)allocatedy(M1)D/Pas0,token1(de)allocatedAER = \begin{cases} \frac{y*(M-1)}{D}, & token_0 (de)allocated \\ \frac{y*(M-1)}{D/P_{as_0}}, & token_1 (de)allocated \\ \end{cases}

To discourage the harmful arbitrage, the (de)allocation fee should be charged in a way to cancel out AERAER. Therefore, to calculate the (de)allocation fee, we need to express the maximal value of AERAER in known parameters. Since the starting point of (de)allocation in the above arbitrage is when a swap of token0token_0 is finished, the maximal value of AERAER should be expressed with the known parameters at the point the swap is finished. Below we list the relation for some parameters before and after the swap to ease the calculation of (de)allocation fees.

Before we get into the calculation of (de)allocation fees, we express MM, M2M^{2} and M2nM^{2n} in terms of ratio of asset-liability ratios to ease the subsequent calculation.

M=Pav0Pav01=(Pas0Pas0Pas0Pas0)12M = P_{av_0}*P_{av_0}'^{-1} = (\frac{P_{as_0}*P_{as_0}'}{P_{as_0}''*P_{as_0}'''})^{\frac{1}{2}}

M2=Pas0Pas0Pas0Pas0=1Pas0Pas0Pas0Pas0Pas0Pas0=(ralr0ralr0)1n(ralr0ralr0)1n(ralr0ralr0)1nM^{2} = \frac{P_{as_0}*P_{as_0}'}{P_{as_0}''*P_{as_0}'''} = \frac{1*\frac{P_{as_0}'}{P_{as_0}}}{\frac{P_{as_0}''}{P_{as_0}}*\frac{P_{as_0}'''}{P_{as_0}}} = (\frac{r_{alr_0}'}{r_{alr_0}})^{-\frac{1}{n}} * (\frac{r_{alr_0}''}{r_{alr_0}})^{\frac{1}{n}} * (\frac{r_{alr_0}'''}{r_{alr_0}})^{\frac{1}{n}}

M2n=(ralr0ralr0)(ralr0ralr0)(ralr0ralr0)M^{2n} = (\frac{r_{alr_0}'}{r_{alr_0}}) * (\frac{r_{alr_0}''}{r_{alr_0}}) * (\frac{r_{alr_0}'''}{r_{alr_0}})

3. Fee Calculation for the 4 (de)allocation patterns

The fees are only needed when the transactions take place within the first segment of the price adjustment curve, beyond which price penalty will turn the arbitrage invalid. Therefore the following condition holds:

y(RAS0+A0L0)y \leqslant (RAS_0'+ A_0'-L_0')

Within the range of RASRAS (see Reasonable Swap Amount for details), the (de)allocation fee is typically in the range of 0.0001% - 0.02% of the amount of token to sell, depending on how close the current ALRALR is to RASRAS. Note that in the following calculation, all the (de)allocation charge is expressed in the amount of the token to be (de)allocated.

3.1 alr0>1alr_0' > 1, Deallocate token0token_0

ralr0ralr0=1+yA01yPav0/A1\frac{r_{alr_0}'}{r_{alr_0}} = \frac{1+\frac{y}{A_0}}{1-y*P_{av_0}/A_1}

ralr0ralr0=1+yA0DA01yPav0/A111DL0\frac{r_{alr_0}''}{r_{alr_0}} = \frac{1+\frac{y}{A_0}-\frac{D}{A_0}}{1-y*P_{av_0}/A_1}*\frac{1}{1-\frac{D}{L_0}}

ralr0ralr0=1+yA0(1M)DA0111DL0\frac{r_{alr_0}'''}{r_{alr_0}} = \frac{1+\frac{y}{A_0}(1-M)-\frac{D}{A_0}}{1}*\frac{1}{1-\frac{D}{L_0}}

M2n=1+yA0DA01+yA0(1+yA0(1M)DA0)1(1DL0)2(1DA01+yA01DL0)2M^{2n} = \frac{1+\frac{y}{A_0}-\frac{D}{A_0}}{1+\frac{y}{A_0}}*(1+\frac{y}{A_0}(1-M)-\frac{D}{A_0})*\frac{1}{(1-\frac{D}{L_0})^2}\leqslant (\frac{1-\frac{\frac{D}{A_0}}{1+\frac{y}{A_0}}}{1-\frac{D}{L_0}})^2

M(1+DL0DA01+yA01DL0)1n1+1nDL0DA01+yA01DL0M\leqslant (1+\frac{\frac{D}{L_0}-\frac{\frac{D}{A_0}}{1+\frac{y}{A_0}}}{1-\frac{D}{L_0}})^{\frac{1}{n}}\leqslant1+\frac{1}{n}*\frac{\frac{D}{L_0}-\frac{\frac{D}{A_0}}{1+\frac{y}{A_0}}}{1-\frac{D}{L_0}}

AER1ny(A0L0)/A0/(L0D)AER \leqslant \frac{1}{n}*y*(A_0'-L_0')/A_0'/(L_0'-D)

ARR1n(RAS0+A0L0)(A0L0)/A0/(L0D)ARR \leqslant \frac{1}{n}* (RAS_0'+ A_0'-L_0')*(A_0'-L_0')/A_0'/(L_0'-D)

Deallocation Charge=1n(RAS0+A0L0)(A0L0)/A0/(L0D),\therefore Deallocation \ Charge = \frac{1}{n}* (RAS_0'+ A_0'-L_0')*(A_0'-L_0')/A_0'/(L_0'-D),

If Deallocation Charge0,Deallocation Charge=0.If \ Deallocation \ Charge \leqslant 0, Deallocation \ Charge = 0.

3.2. alr0<1alr_0' < 1, Allocate token0token_0

ralr0ralr0=1+yA01yPav0/A1\frac{r_{alr_0}'}{r_{alr_0}} = \frac{1+\frac{y}{A_0}}{1-y*P_{av_0}/A_1}

ralr0ralr0=1+yA0+DA01yPav0/A111+DL0\frac{r_{alr_0}''}{r_{alr_0}} = \frac{1+\frac{y}{A_0}+\frac{D}{A_0}}{1-y*P_{av_0}/A_1}*\frac{1}{1+\frac{D}{L_0}}

ralr0ralr0=1+yA0(1M)+DA0111+DL0\frac{r_{alr_0}'''}{r_{alr_0}} = \frac{1+\frac{y}{A_0}(1-M)+\frac{D}{A_0}}{1}*\frac{1}{1+\frac{D}{L_0}}

M2n=1+yA0+DA01+yA0(1+yA0(1M)+DA0)1(1+DL0)2(1+DA01+DL0)2M^{2n} = \frac{1+\frac{y}{A_0}+\frac{D}{A_0}}{1+\frac{y}{A_0}}*(1+\frac{y}{A_0}(1-M)+\frac{D}{A_0})*\frac{1}{(1+\frac{D}{L_0})^2} \leqslant (\frac{1+\frac{D}{A_0}}{1+\frac{D}{L_0}})^2

M(1+DA01+DL0)1n=(1+DA0DL01+DL0)1n1+1nDA0DL01+DL0M \leqslant (\frac{1+\frac{D}{A_0}}{1+\frac{D}{L_0}})^{\frac{1}{n}} = (1+\frac{\frac{D}{A_0}-\frac{D}{L_0}}{1+\frac{D}{L_0}})^{\frac{1}{n}} \leqslant 1+\frac{1}{n}*\frac{\frac{D}{A_0}-\frac{D}{L_0}}{1+\frac{D}{L_0}}

AER1ny(L0A0)/A0/(L0+D)AER \leqslant \frac{1}{n}*y*(L_0-A_0)/A_0/(L_0+D)

AER1n(RAS0+A0L0)RAS0/(L0RAS0)/(L0+D)AER \leqslant \frac{1}{n}* (RAS_0'+ A_0'-L_0')*RAS_0'/(L_0'-RAS_0')/(L_0'+D)

Allocation Charge=1n(RAS0+A0L0)RAS0/(L0RAS0)/(L0+D),\therefore Allocation \ Charge = \frac{1}{n}* (RAS_0'+ A_0'-L_0')*RAS_0'/(L_0'-RAS_0')/(L_0'+D),

If Allocation Charge0,Allocation Charge=0.If \ Allocation \ Charge \leqslant 0, Allocation \ Charge = 0.

3.3. ALR1>1ALR_1' > 1, allocate token1token_1

ralr0ralr0=1+yA01yPav0/A1\frac{r_{alr_0}'}{r_{alr_0}} = \frac{1+\frac{y}{A_0}}{1-y*P_{av_0}/A_1}

ralr0ralr0=1+yA01yPav0/A1+DA1(1+DL1)\frac{r_{alr_0}''}{r_{alr_0}} = \frac{1+\frac{y}{A_0}}{1-y*P_{av_0}/A_1+\frac{D}{A_1}}*(1+\frac{D}{L_1})

ralr0ralr0=1+yA0(1M)1+DA1(1+DL1)\frac{r_{alr_0}'''}{r_{alr_0}} = \frac{1+\frac{y}{A_0}(1-M)}{1+\frac{D}{A_1}}*(1+\frac{D}{L_1})

M2n=1yPav0/A11yPav0/A1+DA11+yA0(1M)1+DA1(1+DL1)2(1+DL11+DA1)2M^{2n} = \frac{1-y*P_{av_0}/A_1}{1-y*P_{av_0}/A_1+\frac{D}{A_1}} *\frac{1+\frac{y}{A_0}(1-M)}{1+\frac{D}{A_1}}*(1+\frac{D}{L_1})^2 \leqslant (\frac{1+\frac{D}{L_1}}{1+\frac{D}{A_1}})^2

M1+1nDL1DA11+DA1M\leqslant 1+\frac{1}{n}*\frac{\frac{D}{L_1}-\frac{D}{A_1}}{1+\frac{D}{A_1}}

AER1ny(A1L1)/L1/(A1+D)/Pas1AER \leqslant \frac{1}{n}*y*(A_1-L_1')/L_1'/(A_1+D)/P_{as_1}'

A1L1+RAS1=L1+RAS1\because A_1\leqslant L_1+RAS_1= L_1'+RAS_1'

AER1n(RAS0+A0L0)RAS1/L1/(L1+RAS1+D)/Pas1\therefore AER \leqslant \frac{1}{n}* (RAS_0'+ A_0'-L_0')*RAS_1'/L_1'/(L_1'+RAS_1'+D)/P_{as_1}'

Allocation Charge=1n(RAS0+A0L0)RAS1/L1/(L1+RAS1+D)/Pas1,\therefore Allocation \ Charge = \frac{1}{n}* (RAS_0'+ A_0'-L_0')*RAS_1'/L_1'/(L_1'+RAS_1'+D)/P_{as_1}',

If Allocation Charge0,Allocation Charge=0.If \ Allocation \ Charge \leqslant 0, Allocation \ Charge = 0.

3.4. ALR1<1ALR_1' < 1, deallocate token1token_1

ralr0ralr0=1+yA01yPav0/A1\frac{r_{alr_0}'}{r_{alr_0}} = \frac{1+\frac{y}{A_0}}{1-y*P_{av_0}/A_1}

ralr0ralr0=1+yA01yPav0/A1DA1(1DL1)\frac{r_{alr_0}''}{r_{alr_0}} = \frac{1+\frac{y}{A_0}}{1-y*P_{av_0}/A_1-\frac{D}{A_1}}*(1-\frac{D}{L_1})

ralr0ralr0=1+yA0(1M)1DA1(1DL1)\frac{r_{alr_0}'''}{r_{alr_0}} = \frac{1+\frac{y}{A_0}(1-M)}{1-\frac{D}{A_1}}*(1-\frac{D}{L_1})

M2n=1yPav0/A11yPav0/A1DA11+yA0(1M)1DA1(1DL1)2(1DL11DA11yPav0/A1)2=(1DL11DA1)2M^{2n} = \frac{1-y*P_{av_0}/A_1}{1-y*P_{av_0}/A_1-\frac{D}{A_1}} * \frac{1+\frac{y}{A_0}(1-M)}{1-\frac{D}{A_1}}*(1-\frac{D}{L_1})^2 \leqslant (\frac{1-\frac{D}{L_1}}{1-\frac{\frac{D}{A_1}}{1-y*P_{av_0}/A_1}})^2=(\frac{1-\frac{D}{L_1}}{1-\frac{D}{A_1'}})^2

M1+1nDA1DL11DA1M\leqslant 1+\frac{1}{n}*\frac{\frac{D}{A_1'}-\frac{D}{L_1}}{1-\frac{D}{A_1'}}

AER1ny(L1A1)/L1/(A1D)/Pas1AER \leqslant \frac{1}{n}*y*(L_1'-A_1')/L_1'/(A_1'-D)/P_{as_1}'

AER1n(RAS0+A0L0)(L1A1)/L1/(A1D)/Pas1\therefore AER \leqslant \frac{1}{n}* (RAS_0'+ A_0'-L_0')*(L_1'-A_1')/L_1'/(A_1'-D)/P_{as_1}'

Deallocation Charge=1n(RAS0+A0L0)(L1A1)/L1/(A1D)/Pas1,\therefore Deallocation \ Charge = \frac{1}{n}* (RAS_0'+ A_0'-L_0')*(L_1'-A_1')/L_1'/(A_1'-D)/P_{as_1}',

If Deallocation Charge0,Deallocation Charge=0.If \ Deallocation \ Charge \leqslant 0, Deallocation \ Charge = 0.