# How to calculate the continuous price of a bonding curve token

I am trying to write a solidity smart contract implementing a power bonding cure like y=mxn

where `y`=token price, `m`=slope parameter, `n`=exponential parameter, and `x`=token supply.

Assuming `m` would be 0.003, reserve ratio `r` is 1/3, and `n` is 2: How do I get the price of tokens when a user wants to buy or sell `k` amount of tokens?

Here is what I came up with but it does not come close to solving the problem

``````function calculatePurchaseReturn(
uint256 _totalSupply,
uint256 _depositAmount
) public pure returns (uint256) {
uint256 newPrice = (30 * ((newTotal * newTotal) / DECIMALS)) / 10000;

return newPrice;
}
``````

Edit

I have looked at the bancor bonding curve but I don't want to use their power function. I wan't to be able to do the calculation on my own so I can also do the same calculation outside of solidity just incase I need to. My issue right now is how to implement the continuous token price and how to get the price at any given time.

Thanks

• Does this help yos.io/2018/11/10/bonding-curves ? Commented Aug 11, 2022 at 13:25
• I have looked at that article, but I don't want to use the power function. I am trying to implement it on my own so I understand what happens behind the scene Commented Aug 11, 2022 at 18:46

By assumption, given the total supply of token `x`, the price `y` is given by:

with `n`, `m` constants.

Now, the cost `dp` to buy `dx` tokens is `y*dx`. So, if we want to buy `k` tokens starting from a total supply of `x`, the total cost is:

Inverting the formula we get `k` as a function of the cost `p`:

with Rb = m*x^(n+1)/(n+1) "reserve balance", and r = 1/(n+1) "reserve ratio".

This is what we are interested in, basically how many tokens we buy by paying `p`, i.e. the "purchase return".

Using `n = 2` we can rewrite it easily like this:

and in solidity:

``````    function calculatePurchaseReturn(
uint256 _totalSupply,
uint256 _depositAmount
) public pure returns (uint256) {
uint256 temp = 1000*_depositAmount + _totalSupply*_totalSupply*_totalSupply;
temp = powerOneThird(temp); // temp^(1/3)

return temp - _totalSupply;
}
``````

You can't escape doing the `1/3` exponentiation. The Bancor's formula is a good general one, and it helps preventing overflows.

• thank you for your response. Why did you multiply `_depositAmount` by 10000? In my code my calculation was done in bps that was why I divided by 10000, what does 10000 represent? Commented Aug 12, 2022 at 9:59
• @Starbody my bad, it's a `1000` since `m=3/1000` Commented Aug 12, 2022 at 10:43
• What does 1000 represent? Commented Aug 12, 2022 at 10:49
• @Starbody Just a factor, `m=0.003=3/1000` so `3*p/m = 1000*p` Commented Aug 12, 2022 at 10:51
• @Jose4Linux There aren't many articles about this. Just by googling I found these which may be ok: phemex.com/academy/what-is-bonding-curve linumlabs.com/articles/… Commented Sep 22, 2022 at 17:59