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

Adds mint and burn functions #1220

Merged
merged 34 commits into from
Jan 23, 2025
Merged

Adds mint and burn functions #1220

merged 34 commits into from
Jan 23, 2025

Conversation

jalextowle
Copy link
Contributor

@jalextowle jalextowle commented Nov 20, 2024

Description

This PRs adds two functions that together allow traders to completely bypass Hyperdrive's AMM system while still allowing them to open long and short positions. In order to bypass the AMM and avoid the use of LP funds, mint and burn provide the low-level plumbing required for traders to be matched directly against each other. These functions provide a way to directly mint longs and shorts equal in value to a given amount of base and directly burn longs and shorts for the amount of value underlying the bonds.

Mint

The operation of minting takes in a share deposit $\Delta z$ and outputs an amount of bonds $\Delta y$ that were minted. The bonds are split between long and short tokens that are both minted with amounts equal to $\Delta y$.

One of the goals for the mint function is to interoperate with the existing accounting mechanisms. Long and short tokens that are created through calling the mint function must be able to be closed by calling closeLong or closeShort without negatively impacting the system invariants like solvency. Since offsetting positions are created, the price shouldn't change when positions are minted.

To mint matched long and short positions, a few things must be paid for:

  • Underlying value of the bond in shares, equal to $\tfrac{\Delta y}{c}$
  • Pre-paid variable interest in shares, equal to $\tfrac{\max(c, c_0) - c_0}{c_0 \cdot c} \cdot \Delta y$ (the max component ensures that this is never negative)
  • Prepaid flat fee, equal to $\tfrac{\phi_f}{c} \cdot \Delta y$
  • Governance fees, by convention equal to $2 \cdot \tfrac{\phi_g \cdot \phi_f}{c} \cdot \Delta y$

Given all of this information, we can set up the equality relating the shares deposited to the bond amount:

$$ \begin{aligned} \Delta z & = \tfrac{\Delta y}{c} + \tfrac{\max(c, c_0) - c_0}{c_0 \cdot c} \cdot \Delta y + \tfrac{\phi_f}{c} \cdot \Delta y + 2 \cdot \tfrac{\phi_g \cdot \phi_f}{c} \cdot \Delta y \\ & \implies \\ \Delta y & = \frac{\Delta z}{\tfrac{1}{c} + \tfrac{\max(c, c_0) - c_0}{c_0 \cdot c} + \tfrac{\phi_f}{c} + 2 \cdot \tfrac{\phi_g \cdot \phi_f}{c}} \\ & \implies \\ \Delta y & = \frac{c \cdot \Delta z}{\tfrac{\max(c, c_0)}{c_0} + \phi_f + 2 \cdot \phi_g \cdot \phi_f} \end{aligned} $$

Since the trade isn't happening on the curve and conceptually the long and short trades are perfectly matched, the only accounting updates the need to be made is to update the average maturity times, the longs outstanding, the shorts outstanding, and the amount of governance fees that have accrued.

Burn

burn does the opposite of mint. It takes in $\Delta y$ long and short positions with the same maturity and redeems them for the amount of capital underlying the position. Similar to the mint function, burn should be able to interoperate seamlessly with positions opened by openLong and openShort.

When positions are burned, several things must be accounted for:

  1. The underlying value of the bonds in shares, equal to $\tfrac{c_1}{c_0 \cdot c} \cdot \Delta y$
  2. The flat fee pre-paid by the short, equal to $\tfrac{\phi_f}{c} \cdot \Delta y$.
  3. The flat fees owed to the LPs. This shouldn't be required, but it's a hard component to get around because of the way that checkpointing automatically closes positions and applies the flat fee. This is equal to $2 \cdot \tfrac{\phi_f \cdot t_m}{c}$.
  4. The governance fees paid when burning the positions, equal by convention to $2 \cdot \tfrac{\phi_g \cdot \phi_f}{c} \cdot \Delta y$.

The proceeds in shares is given by:

$$ \Delta z = \tfrac{c_1}{c_0 \cdot c} \cdot \Delta y + \tfrac{\phi_f}{c} \cdot \Delta y - 2 \cdot \tfrac{\phi_f \cdot t_m}{c} - 2 \cdot \tfrac{\phi_g \cdot \phi_f}{c} \cdot \Delta y $$

The share and bond reserves aren't updated since the long and short trades perfectly cancel out. The only state changes that must be made are updating the average maturity times, the longs outstanding, the shorts outstanding, and the amount of governance fees accrued.

Todos

  • mint and burn
    • Write the mint function.
    • Write unit tests for mint.
    • Write integration tests for mint.
    • Write the burn function.
    • Write unit tests for burn.
    • Write integration tests for burn.
    • Ensure that negative interest and zombie interest are properly
      • Add a negative interest test for mint.
      • Zombie interest
    • Add tests in the remaining major test suites.
      • Look into why the LP share tolerance needs to be 1e10 in the
        test_lp_withdrawal_pair_redemption.
    • Add mint and burn to the instance test framework.
    • Fix the code size.
    • Wire this up with the agent0 fuzzing framework

@jalextowle jalextowle changed the title Adds an openPair function WIP: Adds an openPair function Nov 20, 2024
@jalextowle jalextowle changed the title WIP: Adds an openPair function WIP: Adds a mint function Nov 27, 2024
test/utils/InstanceTest.sol Outdated Show resolved Hide resolved
Copy link
Contributor

@jrhea jrhea left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥

@jalextowle jalextowle enabled auto-merge January 21, 2025 19:21
@jalextowle jalextowle added this pull request to the merge queue Jan 22, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to no response for status checks Jan 22, 2025
@jalextowle jalextowle enabled auto-merge January 23, 2025 00:07
@jalextowle jalextowle added this pull request to the merge queue Jan 23, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to no response for status checks Jan 23, 2025
@jalextowle jalextowle added this pull request to the merge queue Jan 23, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to no response for status checks Jan 23, 2025
@jalextowle jalextowle added this pull request to the merge queue Jan 23, 2025
Merged via the queue into main with commit ba47f16 Jan 23, 2025
35 checks passed
@jalextowle jalextowle deleted the jalextowle/feature/mint branch January 23, 2025 22:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants