Skip to content

A tool to plot the Uniswap V3 bonding curve programmatically, add liquidity, change price ranges and swap between ranges.

Notifications You must be signed in to change notification settings

pvnotpv/bonding-curve-plotter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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:
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!

About

A tool to plot the Uniswap V3 bonding curve programmatically, add liquidity, change price ranges and swap between ranges.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages