https://github.com/pvnotpv/bonding-curve-plotter
A tool to plot the Uniswap V3 bonding curve programmatically, add liquidity, change price ranges and swap between ranges.
https://github.com/pvnotpv/bonding-curve-plotter
Last synced: about 2 months ago
JSON representation
A tool to plot the Uniswap V3 bonding curve programmatically, add liquidity, change price ranges and swap between ranges.
- Host: GitHub
- URL: https://github.com/pvnotpv/bonding-curve-plotter
- Owner: pvnotpv
- Created: 2025-01-26T19:48:52.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2025-01-27T11:35:04.000Z (4 months ago)
- Last Synced: 2025-02-03T06:41:59.141Z (4 months ago)
- Language: Python
- Homepage:
- Size: 582 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
### Uniswap V3 concentrated liquidity implementation (only the mathematical part)
> Understanding Uniswap v3 is kind of hard compared to v1 and v2; there's like a lot of magical black box formulas that do "stuff." I kind of made this after trying to figure out how Uniswap v3 works under the hood after
reading the Uniswap v3 book and other resources. Note that if there are any issues or something is not mathematically correct, please issue a pull request; I'm still learning on the learning curve... so yk...- Initializing a new curve in tests.py with:
```python
curve = curve.BondingCurve(50000, 50000, 1, 7, 0.5)
```#### BondingCurve(amountTokenX, amountTokenY, currentPrice, upperPrice, lowerPrice)
As we can see that the above curve is not so capital efficient due to the far price limits...
---
A simple swap of 1522 token x for token y:
![]()
The plotted graph for above swap:
![]()
---
Files:
```curve.py``` - The main curve class which defines all the parameters and methods.
```graph.py``` - Used to plot the curve.
```tests.py``` - For swapping tokens and saving the state to state.json
```swap.py``` - Does the actual swapping part.
---
The "tests.py" file accepts two arguments (xOry, amount):
xOry = 1, when swapping token y for token x
xOry = 0, when swapping token x for token y
---
- tests.py dumps the current state of the curve to a json file.- Running ```python3 graph.py scalefactor``` will take the current state from state.json and plots the graph.
- The graph.py accepts a scalefactor parameter that is used to scale the graph... Since there can be a lot of scenarios... I really have no idea how to do it programmatically, but this way gives you a lot of control of the plotted graph; you can make it look beautiful, tbh.
Use cases and different scenarios explained in my blog post, also the scaling part(btw you may not get the curve plotted, if the scale variable is incorrent): https://pvnotpv.github.io/posts/bondingcurve-plotter/
## Warning: The graph script isn't optimised for cases where the liquidity is too high! It will consume a huge amount of RAM to plot the graph!