-
Identify the basic trading price through high frequency Oracle feed;
-
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 and , and define the following parameters:
-
Liability of a liquidity token
The net supply of the token in the liquidity pool.
-
Asset of a liquidity token
The reserve of the token in the liquidity pool following sequences of transactions.
-
The amount of liability of .
-
The amount of liability of .
-
The amount of asset of .
-
The amount of asset of .
-
The asset liability ratio of .
-
The asset liability ratio of .
-
The oracle price of .
-
The oracle price of .
With the parameters defined above, the objective of the price adjustment curve is to maintain both and close to or above 1. Consequently, the curve should be designed in such a way that any negative deviation from or results in a price deviation from the oracle. This creates an arbitrage opportunity that incentivizes traders to trade towards the direction of and .
To account for both and in the price adjustment curve, we define the following parameters:
-
The ratio of the asset liability ratios: to .
-
The ratio of the asset liability ratios: to .
And design the curve to encourage traders to trade towards the direction of . Now, the issue arises when is approaching 1: will and also approach 1 or remain above 1? There are three potential scenarios when is approaching 1:
-
- When and are on different sides of 1.
-
- When both and are .
-
- When both and are .
Case 1
When is approaching 1, both and must be approaching 1 as well, but from different sides of 1.
Case 2
Both and have more assets than liabilities in the pool, ensuring liquidity for withdrawals. As approaches 1, the two tokens become more balanced in terms of the asset-liability ratio.
Case 3
When approaches 1, it suggests a balanced asset-liability ratio between and , but it does not necessarily bring and back to 1. Case 3 indicates that LPs' assets for both and have decreased following a series of transactions.
The only transaction pattern that could potentially result in a drop in the 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 into the liquidity pool and withdraw a larger amount of compared to the scenario where the trading rate was higher. Consequently, the increase in cannot compensate for the decrease in . When this transaction pattern occurs frequently for both tokens, it could lead to a drop in the for both tokens.
This could be understood with an example. Suppose we have and in the liquidity pool and their starting trading rate is . In the first transaction, a trader trades for , bringing the pool to a new status: and . In the next transaction, a trader wants to trade for . At the same time, the trading rate of to drops to . Then with , the trader could receive , leading the pool to and , where for both tokens drops below .
The above analysis demonstrates that pushing toward 1 serves the purpose of moving and 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:
-
Theoretically, there is only a 33% chance that the assets to be purchased happen to experience a price drop, the impact of which on and could be canceled out by transactions where the purchased token increases in price.
-
Any transaction that moves away from the center (1) will immediately create an arbitrage opportunity through the price adjustment curve. Most likely, arbitrage bots will trade back to 1 before the oracle price is refreshed, which, once again, cancels out the drop resulting from the transaction coinciding with a price drop in the purchased token.
-
Depending on the size of a transaction, the price adjustment curve imposes varying levels of price impact or slippage on the trader. All these price impacts and slippages contribute to the LP assets, subsequently affecting the of the token. This further reduces the likelihood that for both tokens will remain below 1.
From the above, we can see that in most cases, serves the purpose of keeping and close to or above 1. Therefore, we use as the axis of the price adjustment curve and the price adjustment factor (for the oracle price) as the 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 ( axis) and the imbalance in the asset-liability ratio of the two liquidity tokens ( axis), motivating traders to trade toward . To be specific, given an oracle price, the trading rate of a transaction is derived as follows:
-
Acquire oracle rate for this transaction;
-
Calculate the current adjustment factor through the price adjustment
curve based on current ; -
Calculate starting trading rate = oracle rate * adjustment factor;
-
Determine where the intended transaction volume will shift on the adjustment curve to calculate the end trading rate;
-
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:
-
The price calculated from oracle prices: to .
-
The price calculated from oracle prices: to .
-
The adjusted starting price from to before a transaction takes place.
Oracle price adjusted by the current .
-
The adjusted starting price from to before a transaction takes place.
Oracle price adjusted by the current . -
The adjusted end price from to when a transaction is finished.
Before the transaction, we are at a point on the axis of the price adjustment curve represented by . The transaction's volume and direction shift the to a new point that corresponds to a new price adjustment factor on the price adjustment curve. This new price adjustment factor drives the trading price from to .
-
The adjusted end price from to when a transaction is finished.
-
is not the trading price for this transaction; it represents the adjusted end trading price of this transaction and is also the adjusted starting trading price for the next transaction, assuming that the oracle price remains unchanged for the next transaction.
The actual trading price of this transaction from to , , is the weighted average between and .
-
The trading price of a transaction from to .
-
The price adjustment function generates the price adjustment factor with the ratio of asset liability ratios as the input.
With the above definition, we could easily arrive at the following equations:
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 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:
If we replace and in equation with equation and equation , respectively, we get the following equation:
The equation indicates that the price adjustment curve must meet the following requirement: When two s are reciprocals, their corresponding s must also be reciprocals.
In summary, 5 conditions must be satisfied for to operate as a proper price adjustment function:
-
-
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;
-
To allow the low slippage for the trading, must have an extremely low negative slope near the starting point where is in the close proximity of 1;
-
To create a large price adjustment factor that encourage the traders to push back to the proximity of 1, must have a steep negative slope where is away from 1;
-
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 , where
-
The first segment aims at low slippage trading and should meet the condition 1, 2, 3, 5 in the above list and;
-
The second and third segment are intended to keep in the close proximity of 1 from 2 directions, respectively and should both meet the condition 1, 2, 4, 5 in the above list.
4. The 3-segment Price Adjustment Curve
4.1 First Segment
The first segment of is defined as follows:
The here is a creator-defined parameter, representing 's sensitivity to the difference between the market price and the oracle price, assuming that the trading price reflects the market price.
Therefore, the larger is, the more sensitive is.
Function is visualized as the dotted blue curve in the graph, where 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 while also encouraging 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 to , and depict the price adjustment factor for trading in that direction.
The curve satisfies:
-
Proof:
-
Monotonically decreasing.
As seen in the graph, always decreases when increases. -
Low slope near .
-
could be approximated with polynomials, whose computational complexity is within the capacity of EVM.
4.2 Second Segment
The 2nd segment of is designed to push the back to the first segment if the 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 is the X axis and is the Y axis.
where , and p is a user (trading pair creator) defined parameter of the curve that denotes the threshoof penalty ld of 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:
,where , the same as they are defined in segment 2.
In this case, if 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:
-
When :
-
users swap around , no extra price should of penalty be imposed;
-
function generates a rather flat price adjustment curve, resulting in a swap price with limited slippage
-
-
When :
always increases during a swap.
Therefore, when a transaction pushed out of the acceptable range, the VTP is moved to a unhealthy condition. Extra price penalty need to be charged.
The amount of penalty is (see equation 32).
-
When :
It means that the starting condition of the pool is very bad and this swap is pushing the to recover.User will receive incentive for doing this swap. And the amount is see equation 33.
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 approaches the 2nd/3rd segment of the price adjustment curve, the could rise to the point where (de)allocation fee becomes unacceptably high for LPs.
To remedy this, we define a new parameter "reasonable shift" (), which indicates the healthy range in which transactions are encouraged to take place. Within this range, (de)allocation fees are charged to LPs. Once transactions shift beyond , 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 is to be defined. The typical range of the 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 as the reasonable asset shift of a token in a trading pair from the starting point where . of a token is its maximal asset shift needed to reach the in a trading pair.
is derived from as follows:
Assume a trader swaps amount of to . is the maximal needed to reach the in a trading pair. Therefore, to express in terms of , we need to express in . To that end, we use the swap price , which is a function of .
Combining equation (8) and equation (9), we get:
Equation (10) can be transformed to the expression below:
Since is the maximal needed for to reach , we can arrive at the following equation, which expresses in .
5.3 Price Penalty When Moves Beyond RAS
In graph 5.1, we explained that price penalty is imposed to replace (de)allocation fees when is not within , 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 to :
-
, deallocate
-
, allocate
-
, allocate
-
, deallocate
Following a swap from to , a detrimental arbitrage could be realized by (de)allocating either or . If both tokens are (de)allocated in 2 consecutive transactions and both contribute to the growth of , then the arbitrage profits are gained from both (de)allocation actions. Therefore, the price penalty for a transaction that ended up with beyond should encompass the potential arbitrage profits from the (de)allocation of both tokens. Depending on the of and , 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. (for potential deallocation)
5.3.2. (for potential allocation)
5.3.3. (for potential allocation)
5.3.4. (for potential deallocation)
where (see Fees for Liquidity Allocation & Deallocation), is the amount gained from the swap after the price penalty has been imposed on the .
The price penalty of a transaction consists of the combination of the penalty for and the penalty for , each from 1 of the 2 cases set forth above.
5.4 Visualization of Price Penalty When Moves Beyond RAS
The price penalty for the 4 (de)allocaiton patterns when is out of the 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:
-
Params
-
a user (trading pair creator) defined parameter of the curve that denotes the threshold of where segment 1 and segment 2 interfaces.
-
Transaction fee for swap in.
-
Transaction fee for swap out.
-
-
axis
, where is the amount of to sell.
-
axis
The price penalty of the transaction.
-
Lines
-
Red
The red line of this graph shows the price impacts of swaps in a certain condition.
-
Purple
The results without any penalty and only with and .
-
Blue
The results with penalty of swapping exceed , but without penalty of letting exceed .
-
Orange
The results with penalty of swapping exceed only during swap in.
-
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.
-
At any point of time, the status of a VTP is defined by a set of parameters e.g. , , , , , , , , , . 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.
-
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.
-
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 range post the transaction.
-
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.
-
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 . In section 6.2, 6.3 and 6.4, we express the equation (5) (6) (7) with known parameters without further calculation.
-
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.
-
Calculate the transaction fee for the token to buy with the outcome from step 6 and the determined percentage allocation of transaction fees.
-
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 range post the transaction.
-
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).
-
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
Formula
With the definition of above symbols and price adjustment curve, we are now set to decide . Assume a swap from to with the starting adjusted price and the end adjusted price . We define the average swap price of this transaction to be
A swap of for should return to the trader.
Reasonablity
The price function defined above should satisfy the following requirement:
If a trader swaps to , and swaps these right back, he should get exactly amount of when fees are not considered.
Proof
In the forward transaction: , where
In the reverse transaction: , where
Define ,
If , left side of the equation < 0, the equation does not hold;
If , left side of the equation > 0, the equation does not hold;
So a trader who swaps to will get exactly through a reverse swap of s received from the previous transaction.
6.2.2 Calculate
The above was calculated based on the known . With the oracle input, the transaction input eg. volume and direction and the defined above, the end adjusted price could indeed be calculated. However, the complexity of this numerical calculation will lead to huge gas cost. An accurate approximation is needed to derive .
Calculation
To derive the formula that approximates , we define:
.
So .
With the above definition, we can express with the following formula:
Now we try to express it with known parameters. From equation (18), we could get the following equation:
By substituting in equation (21) with the definition of , we get the following equation:
could be approximated with the following approximate equation to reduce the computational complexity:
From equation (23) and (24), we can arrive at:
where
With equation (26) (27), the equation (21) for approximating can be expressed with known parameters.
Validity of the Approximation
In this section, we prove that the 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 satisfies (see equaiton (25)). Based on the analysis in the previous section, we see that is an approximated value of . To prove the approximated is smaller than the exact value, we prove that (see equation (21)).
From above, we know
So we could express as follows:
The expression is further simplified to:
By substituting equation (28) into equation (30), the following equation is gained:
Following equation (27),
If ,
Now we have proven that the approximated 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 penalty, we could now derive the amount of token to be gained from the transaction () 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 , assuming the swap direction to .
6.4 Price Calculation for the Third Segment
As the third segment is the mirror of the second segment around (see graph), the is approaching 1 during a swap from to . Therefore, rather than price penalty, this transaction creates a price reward as follows: