Altcoins Talks - Cryptocurrency Forum

Cryptocurrency Ecosystem => DeFi tokens => Topic started by: z3r0 on October 28, 2020, 06:53:39 AM

Title: Bonding Curve for Swapping Stablecoins — A Theory Behind Smoothy.finance
Post by: z3r0 on October 28, 2020, 06:53:39 AM
Short Introduction to Bonding Curve
A bonding curve defines a mathematical relationship between the price of a token and other token parameters, mostly total supply. The basic idea is to define such a function so that as the more the token is supplied, the higher price of the token should be (otherwise there is no incentive to buy the token). An example of the bonding curve is as below (Bancor):

(https://miro.medium.com/max/700/1*OOj-7f55HGmaurOAADtTXg.png)

where ReserveTokenBalance is the number of reserve token in the pool, and ReserveRatio is a pre-defined constant.

Note that the function is a continuous price function, while to obtain the actual price to buy/sell some amount of the token, we need to take the integral of the price from pre-tokenSupply to post-tokenSupply after a trade.

(https://miro.medium.com/max/700/1*vLVH3_MCG6VJ72rY1QPLjA.png)

Here is the short introduction, which is sufficient to describe the rest of the article, but if you are interested in more details in bonding curves, you could find more details online.

Bonding Curve for Swapping Stablecoins
When comes to swapping stablecoins, e.g., tokens USDC and USDT, we have prior information that both tokens should have the same intrinsic value and most of the trades should be 1:1. However, the actual price varies around 1:1, and sometimes due to some extreme events, the price may fluctuate a lot. The question now is that could we also use bonding curves to price stablecoins so that most of the time the trades are 1:1, while the price may vary if a large number of such token are sold/bought in the market?

To design such a curve, first of all, we need to determine the token parameter of the price function. Clearly, using total supply is a bad choice — total supply does not reflect the intrinsic value of the token. Instead, we use a pooled method and price a token using its percentage in the pool as the pricing parameter:

Setup
Assuming we have a list of stablecoins with amounts [x_1, x_2, … , x_N] in the pool, so that S = sum(x_i). Furthermore, the liquidity providers that supply the tokens have S amount of sUSD — the LP token to buy/sell the stablecoins in the pool:

Buy LP tokens: A user can sell a stablecoin for sUSD by calling mint(), which will deposit x’ amount of a stablecoin k and return s’ sUSD back. s’ must <= x’, and If s’ < x’, x’ — s’ sUSD will be evenly distributed to existing LPs.

Sell LP tokens: A user can buy a stablecoin k with s amount of sUSD by calling redeem(), which will deposit s’ sUSD and return x’ amount of stablecoin k. x’ must <= s’, and if x’ < s’, s’ — x’ sUSD will be evenly distributed to existing LPs.

Bonding Curve for Buying LP Tokens
Similar to Bancor, let us define the curve as follows:

ContinuousLPTokenPrice = 1 if ContinousTokenPercentage_k < Weight_k or 1 + (ContinousTokenPercentage_k — Weight_k) / (1 — ContinousTokenPercentage_k)

where ContinousTokenPercentage_k = x_k / S, and Weight_k is the pre-defined threshold so that the swap between LP token (sUSD) and the stablecoin is 1:1, or if the threshold is exceeded, the LP token will be more expensive and the price grows to infinite if the percentage approaches 1.

(https://miro.medium.com/max/700/1*QRLiwhpxi6sMU295dMvuJQ.png)

The above graph shows the bonding curve with Weight_k = 0.5. To obtain the price of a swap, similar to Bancor, we could apply integral (the red part) to the bonding curve to calculate the area as the price of swap.

Bonding Curve for Selling LP Tokens
Selling LP tokens can be formulated as if a user wants to retrieve back x’ amount of stablecoin k, how much LP token the user should offer? This is a bit more complicated as the percentage of stablecoin k decreases, while all other stablecoins’ percentage increases, where the curve should account for. Let us define the curve as follows:

ContinunousLPTokenPrice = 1 + sum_{j \neq k} 0 if ContinousTokenPercentage_j < Weight_j or (ContinousTokenPercentage_j — Weight_j) / (1 — ContinousTokenPercentage_ — j)

The curve is very similar to the mint one, except that it uses all stablecoins’ percentages other than percentage kth stablecoin as a pricing parameter.

Special Case for Swapping a Stablecoin to Another
If a user wants to swap a stablecoin A to another B directly, one way is to use A to buy LP tokens and then sell the LP tokens for B sequentially, which will need all percentages of the stablecoins as pricing parameter. One observation is that, based on the model, the output amount of B must <= the input amount of A — this means that all tokens’ percentages will be decreased except A. As a result, we could only perform the bonding curve using stablecoin A’s percentage as pricing parameter, which can save a lot of gas cost (calculation and storage read).

The Experiment
We have implemented the idea of the bonding curve for swapping stablecoins and launch a testnet product at beta.smoothy.finance. Besides the bonding curve, smoothy.finance has the following unique features:

If you are interested in it, you can find more details about the project at smoothy.finance/#/. Welcome to join and provide valuable feedback.

About Smoothy:
Twitter: @smoothswap
Telegram: t.me/Smoothy_finance