This project demonstrates a token sale mechanism using a polynomial bonding curve for automatic price discovery.
- Bonding Curve Visualization Script: A Python script that generates visualizations of token price curves with different exponents.
- Interactive Web App: A Next.js application that allows users to simulate buying and selling tokens on the bonding curve.
The bonding curve uses a polynomial formula (x^n) where:
nis the exponent parameter (typically between 1 and 3)- The price of tokens increases as more tokens are purchased (convex curve)
- The price decreases as tokens are sold back to the contract
To generate the bonding curve visualization:
-
Set up a Python virtual environment:
python3 -m venv venv source venv/bin/activate pip install -r requirements.txt -
Run the visualization script:
python scripts/bonding_curve_visualization.py
This will generate a bonding_curve.png file showing different curves with various exponents.
The web app provides an interactive simulation of the bonding curve:
-
Navigate to the app directory:
cd app -
Install dependencies:
npm install
-
Run the development server:
npm run dev
-
Visit http://localhost:3000 in your browser and click on "Try Bonding Curve Simulator"
- Curve Visualization: Interactive chart showing the bonding curve with adjustable exponents
- Buy/Sell Simulation: Test how buying and selling affects price and token supply
- Price Calculations: See how different transaction amounts impact price slippage
- Wallet Simulation: Track your simulated token and ETH balances
-
Buying Tokens:
- User sends ETH to buy tokens
- Price is calculated based on the current supply and exponent
- Tokens are added to the user's balance
- Supply increases
-
Selling Tokens:
- User sells tokens back to the contract
- Price is calculated based on the new (reduced) supply
- ETH is added to the user's balance
- Supply decreases
- Token Price:
price = (supply / max_supply)^n * max_price - Buy Return (simplified):
tokens = eth_amount / current_price - Sell Return (simplified):
eth = token_amount * current_price
In a production implementation, integral calculus would be used to accurately calculate the area under the curve for precise token/ETH amounts.