-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
95e4f73
commit c17e30c
Showing
12 changed files
with
1,840 additions
and
363 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[alias] | ||
wasm = "build --release --target wasm32-unknown-unknown" | ||
wasm-debug = "build --target wasm32-unknown-unknown" | ||
unit-test = "test --lib" | ||
integration-test = "test --test integration" | ||
schema = "run --example schema" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
[package] | ||
name = "cw-abc" | ||
version = "0.0.1" | ||
authors = ["Ethan Frey <[email protected]>", "Jake Hartnell"] | ||
edition = { workspace = true } | ||
description = "Implements an Augmented Bonding Curve" | ||
license = "Apache-2.0" | ||
repository = { workspace = true } | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[lib] | ||
crate-type = ["cdylib", "rlib"] | ||
|
||
[features] | ||
backtraces = ["cosmwasm-std/backtraces"] | ||
# use library feature to disable all instantiate/execute/query exports | ||
library = [] | ||
|
||
[dependencies] | ||
cw-utils = { workspace = true } | ||
cw2 = { workspace = true } | ||
cw-storage-plus = { workspace = true } | ||
cosmwasm-std = { workspace = true } | ||
cosmwasm-schema = { workspace = true } | ||
thiserror = { workspace = true } | ||
rust_decimal = "1.14.3" | ||
integer-sqrt = "0.1.5" | ||
integer-cbrt = "0.1.2" | ||
# TODO publish this | ||
token-bindings = { git = "https://github.com/CosmosContracts/token-bindings", rev = "1412b94" } | ||
|
||
[dev-dependencies] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
CW20-Bonding: Bonding Curve to release CW20 token | ||
Copyright 2020-21 Ethan Frey <[email protected]> | ||
Copyright 2021-22 Confio GmbH | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# cw-abc | ||
|
||
Implments an augmented bonding curve. | ||
|
||
Forked from and heavily inspired by the work on [cw20-bonding](https://github.com/cosmwasm/cw-tokens/tree/main/contracts/cw20-bonding). | ||
|
||
## Extended Reading | ||
|
||
- https://medium.com/commonsstack/deep-dive-augmented-bonding-curves-b5ca4fad4436 | ||
- https://tokeneconomy.co/token-bonding-curves-in-practice-3eb904720cb8 | ||
|
||
## TODO | ||
|
||
Taking inspiration from [this article](https://medium.com/commonsstack/deep-dive-augmented-bonding-curves-b5ca4fad4436) on augmented bonding curves: | ||
|
||
- [ ] Implement Hatch Phase to allow projects to raise funds | ||
- [ ] Implement optional Exit Tax | ||
- [ ] Optionally vest tokens during the hatch phase | ||
- [ ] Update `cw-vesting` to allow for partcipating in DAOs? | ||
|
||
## Design | ||
|
||
There are two variants: | ||
|
||
Minting: When the input is sent to the contract via `ExecuteMsg::Buy{}` | ||
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: `handle` specifies a bonding function, which is sent to parameterize | ||
`handle_fn` (which does all the work). The curve is set when compiling | ||
the contract. In fact many contracts can just wrap `cw-abc` and | ||
specify the custom curve parameter. | ||
|
||
Read more about [bonding curve math here](https://yos.io/2018/11/10/bonding-curves/) | ||
|
||
Note: the first version only accepts native tokens as the | ||
|
||
### Math | ||
|
||
Given a price curve `f(x)` = price of the `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 `F(x)` be the integral of `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 `S` | ||
tokens in exchange for `N = F(S)` input tokens. If someone sends us `x` tokens, | ||
how much will we issue? | ||
|
||
`F^-1(N+x) - F^-1(N)` = `F^-1(N+x) - S` | ||
|
||
And if we sell `x` tokens, how much we will get out: | ||
|
||
`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 `F(S)` when using `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](https://doc.rust-lang.org/std/primitive.i128.html#method.checked_pow). | ||
There is also a crate to [provide nth-root of for all integers](https://docs.rs/num-integer/0.1.43/num_integer/trait.Roots.html). | ||
With these two, we can handle most math except for logs/exponents. | ||
|
||
Compare this to [writing it all in solidity](https://github.com/OpenZeppelin/openzeppelin-contracts/blob/7b7ff729b82ea73ea168e495d9c94cb901ae95ce/contracts/math/Power.sol) | ||
|
||
Examples: | ||
|
||
Price Constant: `f(x) = k` and `F(x) = kx` and `F^-1(x) = x/k` | ||
|
||
Price Linear: `f(x) = kx` and `F(x) = kx^2/2` and `F^-1(x) = (2x/k)^(0.5)` | ||
|
||
Price Square Root: `f(x) = x^0.5` and `F(x) = x^1.5/1.5` and `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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use cosmwasm_schema::write_api; | ||
|
||
use cw_abc::msg::{ExecuteMsg, InstantiateMsg, QueryMsg}; | ||
|
||
fn main() { | ||
write_api! { | ||
instantiate: InstantiateMsg, | ||
query: QueryMsg, | ||
execute: ExecuteMsg, | ||
} | ||
} |
Oops, something went wrong.