header-langage
简体中文
繁體中文
English
Tiếng Việt
Scan to Download the APP
Partnered With:

How to secure LP Token value

Read this article in 6 Minutes
LP mortgage has become an urgent need, and there is no better way to do it. Alpha Finance's approach can be said to be a safer way to implement it, making attacks on quantity control unfeasible or very expensive.
Source: Slow Fog Technology
Introduction: A Secure Approach to Acquiring LP Prices


At present, there is a growing demand for borrowing with LP tokens as collateral, but there is no perfect method for securely obtaining the price of LP tokens on the market. In the process of analyzing the acquisition methods of LP Token prices, the Slow Fog Security team paid attention to the methods of secure acquisition of LP prices by the Alpha Finance team. After reading carefully, I will share my thoughts with you.


LP Token price acquisition analysis


Currently, the common ways to obtain LP Token prices are as follows:

引介:一种安全的 LP 价格的获取方法

Where, R0, R1 represent   Uniswap  The stock of two tokens in a trading pair, price0 and price1, represent the price of tokens corresponding to R0 and R1, respectively. The above formula is simply to calculate the sum of the total value of the two tokens in the trading pair and then divide by the total number of LP tokens to get the value of a single LP.


At first glance, this formula doesn't seem to be a problem; in general, price0 and price1 take the delayed price provided by Uniswap itself. But there is a risk of flash lending. While price0 and price1 are not manageable, R0 and R1 are. By manipulating the values of R0 and R1, the whole formula can be manipulated. For details, please refer to the article "Slow fog".The use of delayed feeding price is also black? Warp Finance hacked in detail"


So is there a way to get a secure LP Token price so that the stock of tokens can't be manipulated? The Alpha Finance team offers an idea:

引介:一种安全的 LP 价格的获取方法


According to Alpha Finance's analysis, the whole process is divided into three steps:


The first step is to obtain the number of corresponding tokens in the trading pair through the GetReserves interface of Uniswap and calculate K

Step 2 is to get the corresponding price of each token in the transaction pair, and then calculate the ratio P of the price of the token

Step 3 is to invert the real token stock through the relationship between K and P.


After completing the above 3 steps, the final price acquisition formula for LP Token will look like the following:


引介:一种安全的 LP 价格的获取方法


It's a little bit confusing, but it's not a problem, so let's do it one by one.


First of all, we know that Uniswap uses a constant product algorithm. In short,   x * y = K  In other words, the value of K will not change before and after the transaction. The K value is not supposed to change without discussing the handling fee. Let's keep that premise in mind. Then, get the respective price of each token in the trading pair, for example, p  USDT  The price. Here to & have spent ETH-BTC  For example, if the price of ETH is 650 USDT and the price of BTC is 22,000 USDT, then the price ratio P of ETH/BTC is 0.03. After obtaining the price ratio P, calculate K/P and K*P directly using the K obtained in step 1 to get a normal number of trade pairs. Step 3, which is getting the normal amount, is described accordingly.


Formula thought explanation


Now begin the instructions to step 3 above. Hold on to your seat


As mentioned earlier, the formula for constant product is:

引介:一种安全的 LP 价格的获取方法

So you can actually figure out x and y in terms of K. And then from step 2 in the last video, we got the ratio of the price of x to the price of y, P. Since Uniswap itself determines the corresponding price based on the proportion of tokens in the pool, the ratio P is itself a ratio of the price of x/y. Then, because   K is equal to x times y, and P is the ratio given by the correct price, so we can actually invert the real x and y with this real K and x over y.


The calculation is as follows:


First of all, according to the ratio of P to R0 and R1, we can get the following formula:

引介:一种安全的 LP 价格的获取方法

Then, based on P, the real R0, R1 can be inferred backwards, as follows:


引介:一种安全的 LP 价格的获取方法


Then, with the correct ratio of x and y, the price of LP would be the following formula:

引介:一种安全的 LP 价格的获取方法

Then convert it to the following:

引介:一种安全的 LP 价格的获取方法

Possibility of attack


After formula analysis, it is not difficult for us to know that as long as there is a correct price ratio P, real R0 and R1 can be inferred backwards according to this ratio, and finally the formula can be obtained:


 LP Token 质押量激增,如何安全获取其价格?

So, can this formula be attacked? As can be seen from the formula, the price0 and price1 of the formula are the correct prices obtained by the trusted source, and this value cannot be manipulated. Then comes TotalSupply. Although this value can be manipulated, changing TotalSupply in the process of controlling the LP price to attack can only change your mortgage quantity, which is useless for the time being. So all we have left to manipulate is the values of r0 and r1. How do I change the value of r0 and r1? The following two approaches are provided for analysis:


Idea 1: Carry out token exchange directly


As we know, in the token pool, no matter what algorithm is used for calculation, the number of tokens will inevitably change in the process of token exchange in the token pool, so can this change ultimately manipulate the formula? Well, you can't. We know that in the constant product model, x * y = K  In other words, no matter how the exchange of tokens occurs in the transaction process, the value of K is always the same (the handling fee is not taken into account here), and the formula uses R0 and R1 to multiply, so it is actually not feasible to manipulate the formula by using the exchange of tokens.


Idea 2: Put the token directly into the token pool


It's a crude way of thinking about it. You can manipulate the multiplication of r0 and r1 by ignoring the value of K, but after doing my calculations, it looks like it works, but it doesn't. Although the purpose of manipulation has been achieved, the formula itself adopts the model of square root when obtaining the final price, so the final profit obtained is the profit after the square root. For example, paying a cost of 10,000 can only get a maximum profit of 100, which is obviously not cost-effective. So that's not going to work.


Scope of application


The scope of application of this algorithm is limited to obtaining the LP price of the token pool applicable to the AMM model, because the whole derivation process is based on the basic property of K in the constant product formula. The acquired LP itself belongs to a transaction that is not feasible for the algorithm without using AMM model, because in this case, all the previous assumptions are not valid, so the calculation of the corresponding formula is also not valid.


conclusion


LP mortgage has become an urgent demand. In the absence of a better way (such as Chainlink's LP feed, Uniswap's delayed LP interface, etc.), Alpha Finance's way can be said to be a relatively safe implementation method, which makes attacks on quantity control impossible or very costly. Of course, with the emergence of more and more scenes, this algorithm is not necessarily omnipotent, the project side needs to combine their own scenes, reasonable use of the algorithm, to achieve good results. In addition, it should be noted that, although the ultimate form of the formula uses the square root of R0, R1 and price0,price1, the actual implementation needs to deduce the specific values of R0 and R1 according to K, otherwise there will be some errors.


Reference link:

Alpha Finance on Getting Fair & NBSP; LP price method


Welcome to join the official BlockBeats community:

Telegram Subscription Group: https://t.me/theblockbeats

Telegram Discussion Group: https://t.me/BlockBeats_App

Official Twitter Account: https://twitter.com/BlockBeatsAsia

举报 Correction/Report
Choose Library
Add Library
Cancel
Finish
Add Library
Visible to myself only
Public
Save
Correction/Report
Submit