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
ralr
on the price adjustment curve. When the oracle price remains unchanged, the trading price is entirely dependent on
ralr
- a higher
ralr
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
ralr
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
ralr
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:
-
A trader swaps
token0
for
token1
to move up
ralr.
-
The trader then allocates
token0
liquidity to or deallocates
token1
liquidity from the same VTP, driving
ralr
even higher.
-
The same trader swaps the same amount of
token1
he received in step 1 back to
token0.
Notice:
-
Without step 2, the trader should get in step 3 the exact amount of
token0
he spent in step 1 (See Staple's Pricing Mechanism).
-
In step 2, moving
ralr
upwards results in a lower trading price for
token0
to
token1
and, conversely, a higher trading price for the trading direction from
token1
to
token0. This increased price ratio of
token1
to
token0
enables the trader to swap back more
token0
than what was spent in step 1, leading to an arbitrage profit at the expense of the LP's assets.
With the same definition as in "Staple's Pricing Mechanism":
-
ALR0=L0A0
The asset liabity ratio of
token0;
-
ALR1=L1A1
The asset liabity ratio of
token1,
We clarify the four allocation and deallocation patterns that result in a higher
ralr, potentially creating arbitrage opportunities that can be detrimental to LP assets in the aforementioned step 2.
-
ALR0>1, Deallocate
token0
eg.
ALR0=1.1=220/200, deallocate 100
→
ALR0=1.2=120/100,
ALR0
goes up.
-
ALR0<1, Allocate
token0
eg.
ALR0=0.8=80/100, allocate 100
→
ALR0=0.9=180/200,
ALR0
goes up.
-
ALR1>1, Allocate
token1
eg.
ALR1=1.2=120/100, allocate 100
→
ALR1=1.1=220/200,
ALR1
goes down.
-
ALR1<1, Deallocate
token1
eg.
ALR1=0.9=180/200, deallocate 100
→
ALR1=0.8=80/100,
ALR1′
goes down.
To prevent the harmful arbitrages described above, fees should be applied to liquidity allocation and deallocation. These fees are charged on LPs when
ralr
is within a certain range (RRS), 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
token0
to
token1. In the opposite trading direction, there are equivalent allocation and deallocation patterns that will increase
ralr1, 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.
-
ALR1>1, Deallocate
token1
-
ALR1<1, Allocate
token1
-
ALR0>1, Allocate
token0
-
ALR0<1, Deallocate
token0
For the sake of simplicity in the following analysis, we will focus on the trading direction from
token0
to
token1.
2. Definition & Pre-calculation
-
L0
The amount of liability of
token0.
-
L1
The amount of liability of
token1.
-
A0
The amount of asset of
token0.
-
A1
The amount of asset of
token1.
-
ALR0=L0A0
The asset liabity ratio of
token0.
-
ALR1=L1A1
The asset liabity ratio of
token1.
-
OP0
The oracle price of
token0.
-
OP1
The oracle price of
token1.
-
ralr=ralr0=ALR1ALR0
The ratio of the asset
liability ratios:
token0
to
token1.
-
ralr1=ALR0ALR1=ralr1
The ratio of
the asset liability ratios:
token1
to
token0.
-
Pas=Pas0
The adjusted starting price from
token0
to
token1
before a transaction takes place - oracle price adjusted
by the current
ralr.
-
Pas1
The adjusted starting price from
token1
to
token0
before a transaction takes place - oracle price adjusted
by the current
ralr.
-
Pae=Pae0
The adjusted end price from
token0
to
token1
when a transaction is finished.
-
Pae1
The adjusted end price from
token1
to
token0
when a transaction is finished.
-
Pav=Pav0
The average trading price of this transaction
from
token0
to
token1
-
Pav0
- is the weighted average
between
Pas0
and
Pae0.
-
Pav1
The trading price of a transaction from
token1
to
token0.
-
′
an ' is superscripted to a parameter to denote the new status of this parameter following a transaction
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
token0
to
token1
|
(de)allocate |
swap
token1
to
token0
|
amountin
|
|
y
|
D
token0
or
D/Pas0
token1
|
y∗Pav0
|
Pas0
|
Pas0
|
Pas0′
|
Pas0′′
|
Pas0′′′
|
swap price |
|
Pav0=(Pas0∗Pas0′)21
|
|
Pav0′−1=(Pas0′′∗Pas0′′′)−21
|
amountout
|
|
y∗Pav0
|
|
y∗Pav0∗Pav0′−1
|
To symplify the calculation, we further define the parameter
M:
-
M=Pav0∗Pav1′=Pav0∗Pav0′−1
M
is the product of trading prices of step 1 and step 3 defined in the above table. (De)allocation fee applies only if
M>1, which indicates profit is gained from the above arbitrage.
-
AER
Arbitrage earning rate - the earning rate of (de)allocating
D
token0
or
D/Pas0
token1
during the above arbitrage, assuming a swap of
y
token0
was initiated.
A trader will benefit
y∗(M−1)
token0
from the above arbitrage. Therefore
AER={Dy∗(M−1),D/Pas0y∗(M−1),token0(de)allocatedtoken1(de)allocated
To discourage the harmful arbitrage, the (de)allocation fee should be charged in a way to cancel out
AER. Therefore, to calculate the (de)allocation fee, we need to express the maximal value of
AER
in known parameters. Since the starting point of (de)allocation in the above arbitrage is when a swap of
token0
is finished, the maximal value of
AER
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.
-
Liability:
L0=L0′,L1=L1′
-
Assets:
A0=A0′−y,A1=A1′+y∗Pav0
-
RAS:
RAS0=RAS0′,RAS1=RAS1′
(See Reasonable Swap Amount for details.)
Before we get into the calculation of (de)allocation fees, we express
M,
M2
and
M2n
in terms of ratio of asset-liability ratios to ease the subsequent calculation.
M=Pav0∗Pav0′−1=(Pas0′′∗Pas0′′′Pas0∗Pas0′)21
M2=Pas0′′∗Pas0′′′Pas0∗Pas0′=Pas0Pas0′′∗Pas0Pas0′′′1∗Pas0Pas0′=(ralr0ralr0′)−n1∗(ralr0ralr0′′)n1∗(ralr0ralr0′′′)n1
M2n=(ralr0ralr0′)∗(ralr0ralr0′′)∗(ralr0ralr0′′′)
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′+A0′−L0′)
Within the range of
RAS
(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
ALR
is to
RAS. 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′>1, Deallocate
token0
ralr0ralr0′=1−y∗Pav0/A11+A0y
ralr0ralr0′′=1−y∗Pav0/A11+A0y−A0D∗1−L0D1
ralr0ralr0′′′=11+A0y(1−M)−A0D∗1−L0D1
M2n=1+A0y1+A0y−A0D∗(1+A0y(1−M)−A0D)∗(1−L0D)21⩽(1−L0D1−1+A0yA0D)2
M⩽(1+1−L0DL0D−1+A0yA0D)n1⩽1+n1∗1−L0DL0D−1+A0yA0D
AER⩽n1∗y∗(A0′−L0′)/A0′/(L0′−D)
ARR⩽n1∗(RAS0′+A0′−L0′)∗(A0′−L0′)/A0′/(L0′−D)
∴Deallocation Charge=n1∗(RAS0′+A0′−L0′)∗(A0′−L0′)/A0′/(L0′−D),
If Deallocation Charge⩽0,Deallocation Charge=0.
3.2.
alr0′<1, Allocate
token0
ralr0ralr0′=1−y∗Pav0/A11+A0y
ralr0ralr0′′=1−y∗Pav0/A11+A0y+A0D∗1+L0D1
ralr0ralr0′′′=11+A0y(1−M)+A0D∗1+L0D1
M2n=1+A0y1+A0y+A0D∗(1+A0y(1−M)+A0D)∗(1+L0D)21⩽(1+L0D1+A0D)2
M⩽(1+L0D1+A0D)n1=(1+1+L0DA0D−L0D)n1⩽1+n1∗1+L0DA0D−L0D
AER⩽n1∗y∗(L0−A0)/A0/(L0+D)
AER⩽n1∗(RAS0′+A0′−L0′)∗RAS0′/(L0′−RAS0′)/(L0′+D)
∴Allocation Charge=n1∗(RAS0′+A0′−L0′)∗RAS0′/(L0′−RAS0′)/(L0′+D),
If Allocation Charge⩽0,Allocation Charge=0.
3.3.
ALR1′>1, allocate
token1
ralr0ralr0′=1−y∗Pav0/A11+A0y
ralr0ralr0′′=1−y∗Pav0/A1+A1D1+A0y∗(1+L1D)
ralr0ralr0′′′=1+A1D1+A0y(1−M)∗(1+L1D)
M2n=1−y∗Pav0/A1+A1D1−y∗Pav0/A1∗1+A1D1+A0y(1−M)∗(1+L1D)2⩽(1+A1D1+L1D)2
M⩽1+n1∗1+A1DL1D−A1D
AER⩽n1∗y∗(A1−L1′)/L1′/(A1+D)/Pas1′
∵A1⩽L1+RAS1=L1′+RAS1′
∴AER⩽n1∗(RAS0′+A0′−L0′)∗RAS1′/L1′/(L1′+RAS1′+D)/Pas1′
∴Allocation Charge=n1∗(RAS0′+A0′−L0′)∗RAS1′/L1′/(L1′+RAS1′+D)/Pas1′,
If Allocation Charge⩽0,Allocation Charge=0.
3.4.
ALR1′<1, deallocate
token1
ralr0ralr0′=1−y∗Pav0/A11+A0y
ralr0ralr0′′=1−y∗Pav0/A1−A1D1+A0y∗(1−L1D)
ralr0ralr0′′′=1−A1D1+A0y(1−M)∗(1−L1D)
M2n=1−y∗Pav0/A1−A1D1−y∗Pav0/A1∗1−A1D1+A0y(1−M)∗(1−L1D)2⩽(1−1−y∗Pav0/A1A1D1−L1D)2=(1−A1′D1−L1D)2
M⩽1+n1∗1−A1′DA1′D−L1D
AER⩽n1∗y∗(L1′−A1′)/L1′/(A1′−D)/Pas1′
∴AER⩽n1∗(RAS0′+A0′−L0′)∗(L1′−A1′)/L1′/(A1′−D)/Pas1′
∴Deallocation Charge=n1∗(RAS0′+A0′−L0′)∗(L1′−A1′)/L1′/(A1′−D)/Pas1′,
If Deallocation Charge⩽0,Deallocation Charge=0.