### 29 releases (7 breaking)

0.11.1 | Dec 28, 2021 |
---|---|

0.10.3 | Nov 16, 2021 |

0.8.0-rc1 | Jul 29, 2021 |

0.6.0-alpha1 | Mar 12, 2021 |

0.4.0 | Dec 22, 2020 |

#**13** in #cw20

**90** downloads per month

**Apache-2.0**and GPL-3.0-or-later

215KB

4.5K
SLoC

# CW20 Bonding curve

This builds on the Basic CW20 interface
as implemented in

.`cw20-base`

This serves three purposes:

- A usable and extensible contract for arbitrary bonding curves
- A demonstration of how to extend

to add extra functionality`cw20-base` - A demonstration of the [Receiver interface](Basic CW20 interface)

## Design

There are two variants - accepting native tokens and accepting cw20 tokens
as the *reserve* token (this is the token that is input to the bonding curve).

Minting: When the input is sent to the contract (either via

with native tokens, or via `ExecuteMsg ::`Buy

`{`

`}``ExecuteMsg``::`Receive`{``}`

with cw20 tokens),
those tokens remain on the contract and it issues it's own token to the
sender's account (known as *supply*token).

Burning: We override the burn function to not only burn the requested tokens, but also release a proper number of the input tokens to the account that burnt the custom token

Curves:

specifies a bonding function, which is sent to parameterize
`handle`

(which does all the work). The curve is set when compiling
the contract. In fact many contracts can just wrap `handle_fn`

and
specify the custom curve parameter.`cw20-bonding`

Read more about bonding curve math here

Note: the first version only accepts native tokens as the

### Math

Given a price curve

= price of the `f``(`x`)`

th token, we want to figure out
how to buy into and sell from the bonding curve. In fact we can look at
the total supply issued. let `x`

be the integral of `F (x)`

`f``(`x`)`

. We have issued
`x`

tokens for `F``(`x`)`

sent to the contract. Or, in reverse, if we send
`x`

tokens to the contract, it will mint `F``^``-``1``(`x`)`

tokens.From this we can create some formulas. Assume we currently have issued

tokens in exchange for `S`

input tokens. If someone sends us `N = F(S)`

`x`

tokens,
how much will we issue?

= `F ^-1(N+x) - F^-1(N)`

`F``^``-``1``(`N`+`x`)` `-` S

And if we sell

tokens, how much we will get out:`x`

= `F (S) - F(S-x)`

`N ``-` F`(`S`-`x`)`

Just one calculation each side. To be safe, make sure to round down and
always check against

when using `F (S)`

`F``^``-``1``(`S`)`

to estimate how much
should be issued. This will also safely give us how many tokens to return.There is built in support for safely raising i128 to an integer power. There is also a crate to provide nth-root of for all integers. With these two, we can handle most math except for logs/exponents.

Compare this to writing it all in solidity

Examples:

Price Constant:

and `f``(`x`)` `=` k

and `F (x) = kx`

`F``^``-``1``(`x`)` `=` x`/`k

Price Linear:

and `f``(`x`)` `=` kx

and `F (x) = kx^2/2`

`F``^``-``1``(`x`)` `=` `(`2x`/`k`)``^``(``0.``5``)`

Price Square Root:

and `f``(`x`)` `=` x`^``0.``5`

and `F (x) = x^1.5/1.5`

`F``^``-``1``(`x`)` `=` `(``1.``5``*`x`)``^``(``2``/``3``)`

We will only implement these curves to start with, and leave it to others to import this with more complex curves, such as logarithms.

#### Dependencies

~4.5–6MB

~131K SLoC