@orca-so/wavebreak
Version:
The wavebreak JS client for interacting with the wavebreak program.
91 lines (69 loc) • 3.53 kB
Markdown
# Orca Wavebreak Math library
This internal crate is used by other packages in this repo and serves as the codebase that hosts the main business-logic of the wavebreak program. This code is used downstream by:
* The solana program itself
* The rust client
* The js client (through wasm)
## Technical Details
The price curve implementation provides a flexible way to model price transitions using Bézier curves, discretized into bins for efficient computation.
### Curve definition
A cubic Bézier curve implementation is chosen because of the flexibility. This system supports various curve types including linear, exponential, sigmoid, and much more.
A curve is defined by the following parameters:
* The start and end price of the curve.
* Two points, (x1, y1) and (x2, y1), define the shape of the Bézier curve.
Bézier curves are generally calculated using floating point values between 0 and 1. Since floating point math is not precise this implementation uses an integer scale from 0 to 10000.
This implementation uses a simplified version of the standard cubic Bézier formulas because the first point is always (0, 0) and the last point is always (1, 1).
```
x(t) = 3 * (1-t)^2 * t * x1 + 3 * (1-t) * t^2 * x2 + t^3
y(t) = 3 * (1-t)^2 * t * y1 + 3 * (1-t) * t^2 * y2 + t^3
```
These two functions are used to calculate the x and y value of each bin. Here x represents the progress through the total quote amount and y represents the progress through the sqrt_price range. To get the actual quote token amount and sqrt_price of each bin the x and y values are scaled according to the start and end sqrt_price, and the graduation target.
### Price Calculations
Bins are discrete segments that divide the curve into parts where the sqrt_price is constant. Each bin contains a certain amount of base and quote token. The ratio of the tokens is determined by the sqrt_price of the bin.
We can calculate the delta quote or base amount between two points on the curve. This is done by finding the starting bin, and then iterating through each subsequent bin until the target point is reached.
### Different curve shapes
Because cubic Bézier curves are very flexible we can create almost any curve shape by manipulating the control points.
If both control points are on the (0, 0) -> (1, 1) line we get a linear curve. If that is then discretized into bins you get a curve like this:
```
__|
__|
__|
__|
__|
__|
__|
__|
__|
__|
```
If both control points are below the (0, 0) -> (1, 1) line we get an exponential curve. Discretized into bins that will look like this:
```
|
|
|
|
_|
_|
__|
__|
___|
___|
```
If the first control point is below the the (0, 0) -> (1, 1) line and the other one is above you get something that resembles a sigmoidial curve. A binned version looks something like this:
```
___|
__|
_|
|
|
|
|
_|
__|
___|
```
### Parameters of the curve
Since the curve is only defined by the control points, the start and end sqrt_price, and the graduation target. Other parameters of the curve can be derived from this:
* Total supply of the base token
* Fully diluted value at graduation
* Graduation quote and base amounts
* Etc.