Staple's Pricing Mechanism



Luffy, Mihawk, Hajrudin, Clover, Rayleigh


October 31st, 2023


In order to eliminate the impermanent loss, Staple replaces the LP reserve ratio based price discovery process with an oracle based two-step process:
  1. Identify the basic trading price through high frequency Oracle feed;

  2. Based on the oracle price, adjust the trading price to keep LP's reserve in the close proximity to its original status.

1. Basic Price Identification

Staple uses Redstone's on-demand oracle price feed as the basic price indicator for a token.

Aside from its decentralized and robust price feeding infrastructure, Redstone's high price refresh rate (10 sec) ensures that only a rather limited gap, if any, needs to be closed between the oracle price and the market price.

The arbitraging activities from the oracle price to the market price might lead to small loss in LP's assets. Hence, by limiting the arbitrage space through high frequency oracle price feeding, Staple mitigates the impact on LP's profit.

2. Logic Behind the Design of the Curve

To prevent impermanent loss, we have replaced the LP reserve ratio with a high-frequency oracle for determining the trading price. However, any transactions executed in a liquidity pool can still result in a deviation of the LP token's reserve in the pool from its original status, potentially causing a loss to the overall assets in the liquidity pool.

To maintain assets in a liquidity pool as close as possible to their original quantities after a series of transactions, we have defined a price adjustment curve that introduces a price deviation in relation to the token reserve's deviation in the pool from its initial state.

The price deviation encourages arbitrageurs to trade in a way that brings the LP tokens in the pool back to their original status.

To elaborate on the price adjustment curve, we assume a two-token liquidity pool with token0token_0 and token1token_1, and define the following parameters:

With the parameters defined above, the objective of the price adjustment curve is to maintain both ALR0ALR_0 and ALR1ALR_1 close to or above 1. Consequently, the curve should be designed in such a way that any negative deviation from ALR0=1ALR_0 = 1 or ALR1=1ALR_1 = 1 results in a price deviation from the oracle. This creates an arbitrage opportunity that incentivizes traders to trade towards the direction of ALR0=1ALR_0 = 1 and ALR1=1ALR_1 = 1.

To account for both ALR0ALR_0 and ALR1ALR_1 in the price adjustment curve, we define the following parameters:

And design the curve to encourage traders to trade towards the direction of ralr=1r_{alr} = 1. Now, the issue arises when ralrr_{alr} is approaching 1: will ALR0ALR_0 and ALR1ALR_1 also approach 1 or remain above 1? There are three potential scenarios when ralrr_{alr} is approaching 1:

Case 1

When ralrr_{alr} is approaching 1, both ALR0ALR_0 and ALR1ALR_1 must be approaching 1 as well, but from different sides of 1.

Case 2

Both token0token_0 and token1token_1 have more assets than liabilities in the pool, ensuring liquidity for withdrawals. As ralrr_{alr} approaches 1, the two tokens become more balanced in terms of the asset-liability ratio.

Case 3

When ralrr_{alr} approaches 1, it suggests a balanced asset-liability ratio between ALR0ALR_0 and ALR1ALR_1, but it does not necessarily bring ALR0ALR_0 and ALR1ALR_1 back to 1. Case 3 indicates that LPs' assets for both token0token_0 and token1token_1 have decreased following a series of transactions.

The only transaction pattern that could potentially result in a drop in the ALRALR for both tokens is when the transactions in the pool consistently coincide with a decrease in the price of the purchased token (compared to the previous transaction).

In such situations, a trader needs to deposit less token0token_0 into the liquidity pool and withdraw a larger amount of token1token_1 compared to the scenario where the trading rate was higher. Consequently, the increase in ALR0ALR_0 cannot compensate for the decrease in ALR1ALR_1. When this transaction pattern occurs frequently for both tokens, it could lead to a drop in the ALRALR for both tokens.

This could be understood with an example. Suppose we have 10000 token010000 \ token_0 and 10000 token110000 \ token_1 in the liquidity pool and their starting trading rate is 1:11:1. In the first transaction, a trader trades 1000 token01000 \ token_0 for 1000 token11000 \ token_1, bringing the pool to a new status: 11000 token011000 \ token_0 and 9000 token19000 \ token_1. In the next transaction, a trader wants to trade 600 token1600 \ token_1 for token0token_0. At the same time, the trading rate of token0token_0 to token1token_1 drops to 2:12:1. Then with 600 token1600 \ token_1, the trader could receive 1200 token01200 \ token_0, leading the pool to 9800 token09800 \ token_0 and 9600 token19600 \ token_1, where ALRALR for both tokens drops below 11.

The above analysis demonstrates that pushing ralrr_{alr} toward 1 serves the purpose of moving ALR0ALR_0 and ALR1ALR_1 toward 1 or keeping both above 1 in the first two scenarios, but fails to achieve the same objective in case 3. However, it is unlikely that a liquidity pool in Staple will remain in the state of case 3 for an extended period for the following reasons:

From the above, we can see that in most cases, ralr=1r_{alr} = 1 serves the purpose of keeping ALR0ALR_0 and ALR1ALR_1 close to or above 1. Therefore, we use ralrr_{alr} as the XX axis of the price adjustment curve and the price adjustment factor (for the oracle price) as the YY axis.

3. Requirements for the Price Adjustment Curve

Now we understand that the curve is designed to establish a relationship between the price adjustment factor on the oracle price (YY axis) and the imbalance in the asset-liability ratio of the two liquidity tokens ralrr_{alr} (XX axis), motivating traders to trade toward ralr=1r_{alr} = 1. To be specific, given an oracle price, the trading rate of a transaction is derived as follows:

  1. Acquire oracle rate for this transaction;

  2. Calculate the current adjustment factor through the price adjustment
    curve based on current ralrr_{alr};

  3. Calculate starting trading rate = oracle rate * adjustment factor;

  4. Determine where the intended transaction volume will shift ralrr_{alr} on the adjustment curve to calculate the end trading rate;

  5. Calculate the average trading price for this transaction based on
    the starting rate and end rate.

To simplify the mathematical expression for this, let's define the following symbols:

With the above definition, we could easily arrive at the following equations:

Pas=Pas0=PoG(ralr)(1)P_{ as } = P_{ as_0 } = P_o * G( r_{alr} ) \tag{1}

Pas1=Po1G(ralr1)(2)P_{ as_1 } = P_{ o_1 } * G(r_{ alr_1 }) \tag{2}

The equations simply state that the adjusted starting price of a transaction is the product of the oracle price and the price adjustment function. This relationship holds for any point on the G(ralr)G(r_{alr}) curve and for both trading directions. If we disregard transaction fees, which is necessary for mathematical rigor when selecting the price adjustment curve, the following requirement must be satisfied for any point on the price adjustment curve:

PasPas1=1(3)P_{ as } * P_{ as_1 } = 1 \tag{3}

If we replace PasP_{ as } and Pas1P_{ as_1 } in equation (3)(3) with equation (1)(1) and equation (2)(2), respectively, we get the following equation:

G(ralr)G(ralr1)=1(4)G( r_{alr} ) * G( {r_{alr} } ^ {-1} ) = 1 \tag{4}

The equation indicates that the price adjustment curve must meet the following requirement: When two ralrr_{alr}s are reciprocals, their corresponding G(ralr)G( r_{alr} )s must also be reciprocals.

In summary, 5 conditions must be satisfied for G(ralr)G( r_{alr} ) to operate as a proper price adjustment function:

  1. G(ralr)G(ralr1)=1G( r_{ alr } ) * G( { r_{alr} } ^ {-1}) = 1

  2. G(ralr)G( r_{ alr } ) must be monotonically decreasing to ensure the price adjustment and the ratio of asset-liability deviation for the trading direction always grows in the opposite direction;

  3. To allow the low slippage for the trading, G(ralr)G(r_{ alr }) must have an extremely low negative slope near the starting point where ralrr_{ alr } is in the close proximity of 1;

  4. To create a large price adjustment factor that encourage the traders to push ralrr_{ alr } back to the proximity of 1, G(ralr)G(r_{ alr }) must have a steep negative slope where ralrr_{ alr } is away from 1;

  5. G(ralr)G(r_{ alr }) has the computational complexity that an EVM could handle or can be approximated by simpler calculations that an EVM could handle with sufficient accuracy.

To that end, we designed a 3-segment curve for Staple's G(ralr)G( r_{ alr } ), where

4. The 3-segment Price Adjustment Curve

4.1 First Segment

The first segment of G(ralr)G(r_{alr}) is defined as follows:

G(ralr)=ralr1n(5)G( r_{alr} ) = r_{ alr } ^ { - \frac{1}{n}} \tag{5}

The nn here is a creator-defined parameter, representing ralrr_{alr}'s sensitivity to the difference between the market price and the oracle price, assuming that the trading price reflects the market price.

G(ralr)=ralr1n=(1+(ralr1))1n11n(ralr1)G( r_{alr} ) = r_{ alr }^{-\frac{1}{n}} = (1+ (r_{ alr } - 1)) ^ { - \frac{1}{n}} \approx 1 - \frac{1}{n} * (r_{ alr } - 1)

ralr1+n(1G(ralr))r_{ alr } \approx 1 + n * (1 - G( r_{alr} ))

Therefore, the larger nn is, the more sensitive ralrr_{ alr } is.

Function (5)(5) is visualized as the dotted blue curve in the graph, where ralrr_{ alr } is the X axis and G is the Y axis. The black curve in this graph is the price adjustment curve, whose 1st segment that overlaps with the blue curve is a smooth monotonically decreasing curve that facilitates low-slippage trading in the proximity of ralr=1r_{ alr } = 1 while also encouraging ralrr_{ alr } to stay close to 1. The other 2 segments of the curve are explained in the next section.

Note that in the black price adjustment curve, we assume a transaction from token0token_0 to token1token_1, and depict the price adjustment factor for trading in that direction.

The curve satisfies:

4.2 Second Segment

The 2nd segment of G(ralr)G(r_{alr}) is designed to push the ralrr_{alr} back to the first segment if the ralrr_{alr} moves beyond a certain range to the right of 1. In this case, a large price should of penalty be generated, e.g. price adjustment factor << 1, that discourages the trade in this direction. The function below describes this segment of the curve. See the black steep curve to the right of the black smooth curve in the graph, where ralrr_{ alr } is the X axis and G(ralr)G(r_{alr}) is the Y axis.

G(ralr)=ralr1n[11+ralrmmralr]2(6)G(r_{ alr }) = r_{ alr } ^ { - \frac{1}{n}} * [\frac{1}{ 1 + \frac{r_{ alr }}{m}- \frac{m}{ r_{alr}}}]^2 \tag{6}

where m=1+pm = 1 + p, and p is a user (trading pair creator) defined parameter of the curve that denotes the threshoof penalty ld of ralrr_{alr} where segment 1 and segment 2 interfaces.

4.3 Third Segment

The third segment is the mirror of segment 2 to the left of segment 1. Its mathematical expression is also identical except for the change of direction and the shift in X:

G(ralr)=ralr1n[211+1mralrmralr]2(7)G(r_{ alr }) = r_{ alr } ^ { - \frac{1}{n}} * [2 - \frac{1}{1 + \frac{1}{m * r_{ alr } } - m* r_{alr}}] ^ 2 \tag{7}

,where m=1+pm = 1 + p, the same as they are defined in segment 2.

In this case, if ralrr_{alr} is significantly smaller than 1, then the 3rd segment of the curve should generate a large transaction reward in price, e.g., price adjustment factor >> 1, that encourages the trade in this direction. See the black steep curve to the left of black smooth curve in the graph.

4.4 The Complete Price Adjustment Curve:

y={x1n[211+1xmxm]2,x<1mx1n,1mxmx1n[11+xmmx]2,x>my = \begin{cases} x ^ { - \frac{1}{n}} * [2 - \frac{1}{1+\frac{1}{xm} - xm}] ^ {2}, & x < \frac{1}{m} \\ x ^ { - \frac{1}{n}}, & \frac{1}{m} \leqslant x \leqslant m \\ x ^ { - \frac{1}{n}} * [ \frac{1}{1 + \frac{x}{m} - \frac{m}{x}}] ^ 2, & x > m \\ \end{cases}

5. RRS and RAS

5.1 RRS

It is explained in Fees for Liquidity Allocation & Deallocation that any (de)allocation action is charged for small fees to prevent the arbitrage that impacts LP's assets. However, when ralrr_{alr} approaches the 2nd/3rd segment of the price adjustment curve, the G(ralr)G(r_{alr}) could rise to the point where (de)allocation fee becomes unacceptably high for LPs.

To remedy this, we define a new parameter "reasonable ralrr_{alr} shift" (RRSRRS), which indicates the healthy ralrr_{alr} range in which transactions are encouraged to take place. Within this range, (de)allocation fees are charged to LPs. Once transactions shift ralrr_{alr} beyond RRSRRS, a price penalty is imposed on the transactions, and LPs are freed from (de)allocation fees.

This way we keep the (de)allocation fee within the reasonable range while restraining the harmful arbitrage as described in Fees for Liquidity Allocation & Deallocation. A balance should be found between the pool liquidity utilization and the limitation in the LP's (de)allocation fee when the value of RRSRRS is to be defined. The typical range of the RRSRRS is 6% - 9%.

The price penalty is imposed on top of the 3-segment price adjustment curve, thus adding extra logic to the price calculation.

5.2 RAS

To ease the calculation of fees and price penalties, we define another parameter RASRAS as the reasonable asset shift of a token in a trading pair from the starting point where ralr=1r_{alr} = 1. RASRAS of a token is its maximal asset shift needed to reach the RRSRRS in a trading pair.

RASRAS is derived from RRSRRS as follows:

Assume a trader swaps DD amount of token0token_0 to token1token_1. RASRAS is the maximal DD needed to reach the RRSRRS in a trading pair. Therefore, to express RASRAS in terms of RRSRRS, we need to express max{D}max\{D\} in RRSRRS. To that end, we use the swap price Pav0(D)P_{ av_0 }(D), which is a function of DD.

ralr0ralr0=1+D/L01DPav0(D)/L11+D/L0+DPav0(D)/L1(8)\frac{r_{ alr_0 }'}{r_{ alr_0 }} = \frac{1 + D / L_0}{ 1 - D * P_{ av_0}(D) / L_1} \geqslant 1+D / L_0 + D * P_{ av_0 }(D) / L_1 \tag{8}

RRSralr0ralr01(9)RRS \geqslant \frac{r_{ alr_0 }'}{r_{ alr_0 }}-1 \tag{9}

Combining equation (8) and equation (9), we get:

RRSD/L0+DPav0(D)/L1(10)RRS \geqslant D / L_0 + D * P_{ av_0 }(D) / L_1 \tag{10}

Equation (10) can be transformed to the expression below:

DRRS1L0+Pav0(D)L1=RRS1L0+Pa(PaPa)12L1RRS1L0+Pa(1RRS/(2n))L1D \leqslant \frac{RRS}{\frac{1}{L_0} + \frac{ P_{ av_0 }(D)}{ L_1 }} = \frac{RRS}{ \frac{1}{ L_0 } + \frac{ P_a * (\frac{ P_a' }{ P_a }) ^ {\frac{1}{2}}}{L_1}} \leqslant \frac{RRS}{ \frac{1}{L_0} + \frac{ P_a * (1 - RRS / (2n))}{L_1}}

Since RASRAS is the maximal DD needed for ralrr_{alr} to reach RRSRRS, we can arrive at the following equation, which expresses RASRAS in RRSRRS.

RAS=RAS0=RRS1L0+Pa(1RRS/(2n))L1(11)RAS = RAS_0 = \frac{RRS}{\frac{1}{ L_0 } + \frac{ P_a * ( 1 - RRS / (2n))}{ L_1 }} \tag{11}

5.3 Price Penalty When ralrr_{alr} Moves Beyond RAS

In graph 5.1, we explained that price penalty is imposed to replace (de)allocation fees when ralrr_{alr} is not within RRSRRS, in order to discourage the detrimental arbitrages discussed in Fees for Liquidity Allocation & Deallocation. It's easy to understand that the price penalty should be set to the maximal arbitrage profit.

Therefore, the same calculation for the (de)allocation fees in Fees for Liquidity Allocation & Deallocation applies here for the price penalty.

As seen in Fees for Liquidity Allocation & Deallocation, there are 4 harmful (de)allocation patterns after a swap from token0token_0 to token1token_1:

  1. ALR0>1ALR_0 > 1, deallocate token0token_0

  2. ALR0<1ALR_0 < 1, allocate token0token_0

  3. ALR1>1ALR_1 > 1, allocate token1token_1

  4. ALR1<1ALR_1 < 1, deallocate token1token_1

Following a swap from token0token_0 to token1token_1, a detrimental arbitrage could be realized by (de)allocating either token0token_0 or token1token_1. If both tokens are (de)allocated in 2 consecutive transactions and both contribute to the growth of ralrr_{alr}, then the arbitrage profits are gained from both (de)allocation actions. Therefore, the price penalty for a transaction that ended up with ralrr_{alr} beyond RASRAS should encompass the potential arbitrage profits from the (de)allocation of both tokens. Depending on the ALRALR of token0token_0 and token1token_1, the price penalty of each token is expressed as follows.(Note that all the price penalty is expressed in the amount of token pertinent to the calculation):

5.3.1. ALR0>1ALR_0' > 1 (for potential token0token_0 deallocation)

M11nD(A0L0)/A0/(L0D)M-1 \leqslant \frac{1}{n} * D * ( A_0' - L_0' ) / A_0' / ( L_0' - D )

M11n(A0+yL0)/(A0+y)M-1 \leqslant \frac{1}{n} * (A_0 + y - L_0 ) / ( A_0 + y )

Penalty=y1n(A0+yL0)/(A0+y)(12)Penalty = y * \frac{1}{n} * ( A_0 + y - L_0 ) / ( A_0 + y ) \tag{12}

5.3.2. ALR0<1ALR_0' < 1 (for potential token0token_0 allocation)

M11nD(L0A0)/A0/(L0+D)M-1 \leqslant \frac{1}{n} * D * ( L_0 - A_0 ) / A_0 / ( L_0 + D )

M11n(L0A0)/A0M-1 \leqslant \frac{1}{n} * ( L_0 - A_0 ) / A_0

Penalty=y1n(L0A0)/A0(13)Penalty = y * \frac{1}{n} * ( L_0 - A_0 ) / A_0 \tag{13}

5.3.3. ALR1>1ALR_1' > 1 (for potential token1token_1 allocation)

M11nD(A1L1)/L1/(A1+D)M-1 \leqslant \frac{1}{n} * D * ( A_1 - L_1 ) / L_1 / ( A_1 + D )

M11n(A1L1)/L1M-1 \leqslant \frac{1}{n} * ( A_1 - L_1 ) / L_1

Penalty=y1n(A1L1)/L1(14)Penalty = y' * \frac{1}{n} * ( A_1 - L_1 ) / L_1 \tag{14}

5.3.4. ALR1<1ALR_1' < 1 (for potential token1token_1 deallocation)

M11nD(L1A1)/L1/(A1D)M-1 \leqslant \frac{1}{n} * D * ( L_1' - A_1' ) / L_1' / ( A_1' - D )

M11n(L1A1+yPav0)/L1M-1 \leqslant \frac{1}{n} * ( L_1 - A_1 + y * P_{ av_0 } ) / L_1

Penalty=y1n(L1A1+y)/L1(15)Penalty = y' * \frac{1}{n} * ( L_1 - A_1 + y' ) / L_1 \tag{15}

where M=Pav0Pav1=Pav0Pav01M = P_{ av_0 } * P_{ av_1 }' = P_{ av_0 } * P_{ av_0 }'^{ -1 } (see Fees for Liquidity Allocation & Deallocation), yy' is the amount token1token_1 gained from the swap after the price penalty has been imposed on the token0token_0.

The price penalty of a transaction consists of the combination of the penalty for token0token_0 and the penalty for token1token_1, each from 1 of the 2 ALRALR cases set forth above.

5.4 Visualization of Price Penalty When ralrr_{alr} Moves Beyond RAS

The price penalty for the 4 (de)allocaiton patterns when ralrr_{alr} is out of the RASRAS are visualized in graph. On top of the parameters defined above, the following parameters are defined to complete the expression of the price penalty curves:

6. Swap Price Calculation

6.1 Logic for price calculation

Before we enter into the calculation of trading prices, we first sort out the steps of price calculation (including transaction fees). One thing to note is that the imposition of transaction fees is quite flexible in Staple - at the time a virtual trading pair is being created, the creator could decide the percentage of transaction fees imposed on each of the 2 tokens in a VTP.

  1. At any point of time, the status of a VTP is defined by a set of parameters e.g. L0L_0, L1L_1, A0A_0, A1A_1, ALR0ALR_0, ALR1ALR_1, OP0OP_0, OP1OP_1, pp, RASRAS. While these parameters are determined by the status of liquidity provision and the history of transactions of a VTP in the real life, they could be manually set when the price calculation is simulated with graph1 and graph2.

  2. Based on the amount of the token to sell and the percentage allocation of transaction fees, calculate the transaction fee for the token to sell.

  3. Calculate the amount of penalty imposed on the token to sell with one applicable formula from equation (12), (13), (14), (15), if the asset liability ratio of this token in the VTP stays out of the RASRAS range post the transaction.

  4. Calculate the net amount of token to sell by subtracting transaction fees and price penalty, both expressed in the amount of token, from the amount of token to sell in step 1.

  5. Calculate the estimated swap price with a known oracle price and the formula selected from equation (5) (6) (7) based on the relative position of ralrr_{ alr }. In section 6.2, 6.3 and 6.4, we express the equation (5) (6) (7) with known parameters without further calculation.

  6. Multiply the net amount of token to sell from step 4 with the estimated swap price from step 5 to roughly estimate the amount of token to buy that will be gained from the transaction.

  7. Calculate the transaction fee for the token to buy with the outcome from step 6 and the determined percentage allocation of transaction fees.

  8. Calculate the amount of penalty imposed on the token to buy with 1 applicable formula from equation (12) (13) (14) (15), if the asset liability ratio of the token to sell in the VTP stays out of the RASRAS range post the transaction.

  9. Calculate the net amount of token that will be received by subtracting transaction fees and price penalty, both expressed in the amount of token to buy, from the estimated amount of token to receive (see step 6).

  10. The trader's cost in this transaction is the difference in the value of token to sell and the value of received token to buy. The price impact is the ratio of this cost and the value of token to sell.

6.2 Price Calculation for the First Segment

6.2.1 Calculate PavP_{av}

Formula

With the definition of above symbols and price adjustment curve, we are now set to decide PavP_{av}. Assume a swap from token0token_0 to token1token_1 with the starting adjusted price PasP_{as} and the end adjusted price Pas=PaeP_{as}' = P_{ae}. We define the average swap price of this transaction to be

Pav=PasPae(16)P_{av} =\sqrt{ P_{ as } * P_{ ae } } \tag{16}

A swap of DD token0token_0 for token1token_1 should return DPavD*P_{av} token1token_1 to the trader.

Reasonablity

The price function defined above should satisfy the following requirement:

If a trader swaps DD token0token_0 to DPavD*P_{av} token1token_1, and swaps these token1token_1 right back, he should get exactly DD amount of token0token_0 when fees are not considered.

Proof

In the forward transaction: D token0DPav0 token1D \ token_0 \rightarrow D * P_{ av_0 } \ token_1, where Pav0=(Pas0Pas0)12P_{ av_0 } = (P_{ as_0 } * P_{ as_0 }')^{ \frac{1}{2} }

In the reverse transaction: DPav0 token1DPav0Pav1 token0D * P_{ av_0 } \ token_1 \rightarrow D * P_{ av_0 } * P_{ av_1 }' \ token_0, where Pav1=(Pas1Pas1)12P_{ av_1 }' = (P_{ as_1 }' * P_{ as_1 }'')^{ \frac{1}{2}}

Define M=Pav0Pav1M = P_{ av_0 } * P_{ av_1 }',
M2=Pas0Pas0Pas1Pas1=Pas0Pas0=(ralr0ralr0)1n(17)M^{2} = P_{ as_0 } * P_{ as_0 }' * P_{ as_1 }' * P_{ as_1 }''= \frac{P_{ as_0 }}{P_{ as_0 }''} = (\frac{ r_{ alr_0 }}{ r_{ alr_0 }''})^{ - \frac{1}{n} } \tag{17}

M2n=ralr0ralr0=A0+DDMA0(18)M^{2n} = \frac{r_{ alr_0 }''}{r_{ alr_0 }} = \frac{ A_0 + D - D * M }{ A_0 } \tag{18}

(1M2n)+D/A0(1M)=0(19)(1 - M^{ 2n }) + D / A_0 * ( 1 - M ) = 0 \tag{19}

If M>1M>1, left side of the equation < 0, the equation does not hold;

If M<1M<1, left side of the equation > 0, the equation does not hold;

M=1\therefore M = 1
Pav0Pav1=1(20)\therefore P_{ av_0 } * P_{ av_1 }' = 1 \tag{20}

So a trader who swaps DD token0token_0 to DPavD*P_{av} token1token_1 will get exactly DD token0token_0 through a reverse swap of token1token_1s received from the previous transaction.

6.2.2 Calculate PaeP_{ae}

The PavP_{av} above was calculated based on the known PaeP_{ae}. With the oracle input, the transaction input eg. volume and direction and the G(ralr)G(r_{alr}) defined above, the end adjusted price PaeP_{ae} could indeed be calculated. However, the complexity of this numerical calculation will lead to huge gas cost. An accurate approximation is needed to derive PaeP_{ae}.

Calculation

To derive the formula that approximates PaeP_{ae}, we define:

x=(Pas0Pas0)12=1t (t>0)x = (\frac{P_{ as_0 }'}{P_{ as_0 }})^{\frac{1}{2}} = 1 - t \ (t>0).

So Pav0=(Pas0Pas0)12=xPas0P_{ av_0 } = (P_{ as_0 } * P_{as_0}')^{\frac{1}{2}} = x * P_{ as_0 }.

With the above definition, we can express PaeP_{ae} with the following formula:

Pae=Pas0=(1t)2Pas0(21)\therefore P_{ae} = P_{ as_0 }' = ( 1 - t )^2 * P_{ as_0 } \tag{21}

Now we try to express it with known parameters. From equation (18), we could get the following equation:

Pas0Pas0=(1+D/A01DPav0/A1)1n(22)\frac{P_{ as_0 }'}{P_{ as_0 }} = (\frac{1 + D / A_0 }{1- D * P_{ av_0 } / A_1})^{ - \frac{1}{n}} \tag{22}

By substituting Pas0Pas0\frac{P_{ as_0 }'}{P_{ as_0 }} in equation (21) with the definition of xx, we get the following equation:

(1t)2nDPas0/A11+D/A0t+DPas0/A111+D/A0=0(23)( 1 - t )^{ 2n } - \frac{ D * P_{ as_0 } / A_1}{ 1 + D / A_0} * t + \frac{D * P_{ as_0 } / A_1 - 1 }{1 + D / A_0} = 0 \tag{23}

(1t)2n(1-t)^{2n} could be approximated with the following approximate equation to reduce the computational complexity:

(1t)2n12nt+n(2n1)t2(24)(1-t)^{2n} \approx 1- 2n * t + n * (2n-1) * t^2 \tag{24}

From equation (23) and (24), we can arrive at:

t2at+b=0(25)t^2-at+b = 0 \tag{25}

where a=DPas0/A11+D/A0+2nn(2n1), b=1+DPas0/A111+D/A0n(2n1)(26)a = \frac{\frac{D * P_{ as_0 } / A_1}{ 1 + D / A_0} + 2n}{n * (2n - 1)}, \ b = \frac{1 + \frac{D * P_{ as_0 } / A_1 - 1}{ 1 + D / A_0}}{n * (2n - 1)} \tag{26}

t=aa24b2(27)\therefore t=\frac{a - \sqrt{{a}^2 - 4b}}{2} \tag{27}

With equation (26) (27), the equation (21) for approximating PaeP_{ae} can be expressed with known parameters.

Validity of the Approximation

In this section, we prove that the PaeP_{ae} approximated with the above numerical solution is smaller than the exact outcome of the loop calculation, meaning a trader will receive slightly less tokens than what he was supposed to.

Suppose t0t_0 satisfies t02at0+b=0t_0^2-at_0+b = 0 (see equaiton (25)). Based on the analysis in the previous section, we see that t0t_0 is an approximated value of PaeP_{ae}. To prove the approximated PaeP_{ae} is smaller than the exact value, we prove that 1t>1t01-t>1-t_0 (see equation (21)).

From above, we know

b=(t02at0)(28)b =-(t_0^2-at_0) \tag{28}

(1t)2n12nt+n(2n1)t2\because (1-t)^{2n} \leqslant 1-2n * t + n * (2n-1) * t^2

So we could express (1t)2n(1-t)^{2n} as follows:

(1t)2n=12nt+n(2n1)t2n(2n1)λ,λ0(29)(1-t)^{2n}= 1-2n * t + n * (2n - 1) * t^2 - n * (2n - 1)*\lambda, \lambda \geqslant 0 \tag{29}

The expression is further simplified to:

t2at+bλ=0(30)t^2 - at + b - \lambda = 0 \tag{30}

By substituting equation (28) into equation (30), the following equation is gained:

(tt0)(t+t0a)=λ(31)(t - t_0)( t + t_0 - a) = \lambda \tag{31}

Following equation (27),

t=aa24b2\because t = \frac{a - \sqrt{{a}^2 - 4b}}{2}

a>2t\therefore a > 2t

If t>t0t > t_0, 0λ=(tt0)(t+t0a)<00 \leqslant \lambda = (t - t_0 )( t + t_0 - a) < 0

t<t0\therefore t < t_0

1t>1t0\therefore 1-t > 1 - t_0

Now we have proven that the approximated PaeP_{ae} is smaller than the exact value. However, without providing the proof here, the gas saved in the calculation outweighs the loss from the approximation inaccuracy. Therefore the above numerical solution a valid approach.

6.3 Price Calculation for the Second Segment

With the known amount of token to sell, the equations from Section 6.2, the known percentage allocation of transaction fees in this VTP and equation (12), (13), (14), (15) for RASRAS penalty, we could now derive the amount of token to be gained from the transaction (DoutD_{out}) before the price penalty is imposed from the 2nd segment of the price adjustment curve. Without further proof, the following equation expresses the price penalty from the 2nd segment in terms of the amount of token that should be taken off from DoutD_{out}, assuming the swap direction token0token_0 to token1token_1.

Price Penalty=Dout[111+ralr0mmralr0](32)Price \ Penalty = D_{ out } * [1 - \frac{1}{1 + \frac{r_{ alr_0 }'}{m} - \frac{m}{ r_{alr_0}'}}] \tag{32}

6.4 Price Calculation for the Third Segment

As the third segment is the mirror of the second segment around ralr0=1r_{alr_0} = 1 (see graph), the ralr0r_{alr_0} is approaching 1 during a swap from token0token_0 to token1token_1. Therefore, rather than price penalty, this transaction creates a price reward as follows:

Price Reward=Dout[111+1mralr0mralr0](33)Price \ Reward = D_{out} * [1 - \frac{1}{1 + \frac{1}{m * r_{ alr_0 }'} - m * r_{ alr_0 }'}] \tag{33}

span class="vlist" style="height:0.143em;">1](33)