Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate own step payout function #2352

Merged
merged 4 commits into from
Apr 7, 2024
Merged

Generate own step payout function #2352

merged 4 commits into from
Apr 7, 2024

Commits on Apr 5, 2024

  1. fix(payout_curve): Fix payout_price_range_is_below_max_price test

    `NumericalDescriptor::get_range_payouts` should be called with the
    _total_ collateral, including position margin and the collateral
    reserves.
    luckysori committed Apr 5, 2024
    Configuration menu
    Copy the full SHA
    0a9e881 View commit details
    Browse the repository at this point in the history
  2. fix(payout_curve): Ensure that liquidation payouts are sane

    We were allowing the short liquidation payout to go under the short
    party's collateral reserve, which was obviously wrong.
    
    The payout curve is still a bit rough around the edges (literally).
    This is because we have to make relatively big payout jumps to connect
    the liquidation intervals with the adjacent intervals. I'm not sure
    there is a good solution for this other than going back to the
    ItchySats payout curve.
    luckysori committed Apr 5, 2024
    Configuration menu
    Copy the full SHA
    f7bde96 View commit details
    Browse the repository at this point in the history
  3. chore(coordinator): Reproduce payout curve limitation

    This is the (probably) last thing that prevents us from being able to
    rely on the payout curve liquidation intervals for things like the
    available balance and, relatedly, position resizing.
    luckysori committed Apr 5, 2024
    Configuration menu
    Copy the full SHA
    88bcf77 View commit details
    Browse the repository at this point in the history
  4. feat: Generate own step payout function

    Before this patch, we generated a polynomial payout function which
    would then be transformed by `rust-dlc` into a step function. This
    approach was problematic because we could not trust `rust-dlc` to not
    mess with the intervals excessively.
    
    Specifically, any changes to the liquidation intervals could cause
    problems because they represent the collateral reserves, which are
    used to indicate available balances and are circumstantially needed to
    complete features such as position resizing.
    
    Now we take full control of the payout function generation, directly
    generating a step function that can be consumed by `rust-dlc`
    _without_ rounding. The payout is calculated by taking the PNL at the
    half-way point of the interval.
    
    It is important to emphasise that we must opt out
    of rounding by using a `RoundingIntervals` with a single element of
    the form:
    
    ```rust
    RoundingInterval {
        begin_interval: 0,
        rounding_mod: 1,
    }
    ```
    
    Overall, I think the code is considerably simpler and there are fewer
    edge cases to handle.
    
    One important thing to consider is that we can now directly control
    the number of payouts we generate, which has an effect on the number
    of CETs that will be created. This is currently set by the constant
    `PAYOUT_CURVE_DISCRETIZATION_INTERVALS`, but in the future we could
    make it dynamic.
    
    Additionally, at the moment the majority of the intervals are of equal
    length in terms of price. Eventually we might want to generate more,
    smaller intervals around the starting price, and make the less likely
    intervals bigger as we approach the liquidation zones.
    
    Other notable changes
    ---------------------
    
    Since we had to replace a snapshot, we've introduced a dev-dependency
    to `insta`[^1], a library that makes it easy to write and maintain
    snapshot tests. There is a companion tool, `cargo-insta`[^2], which
    can help with this process.
    
    [^1]: https://github.com/mitsuhiko/insta.
    [^2]: https://github.com/mitsuhiko/insta/tree/master/cargo-insta.
    luckysori committed Apr 5, 2024
    Configuration menu
    Copy the full SHA
    10e0cee View commit details
    Browse the repository at this point in the history