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

Fixed the issue with open short #1199

Merged
merged 3 commits into from
Oct 24, 2024
Merged

Conversation

jalextowle
Copy link
Contributor

@jalextowle jalextowle commented Oct 24, 2024

Description

This PR fixes a minor issue with openShort that ensures that the amount of shares owned by the pool are always greater than the expected amount. This ensures that the system is always completely solvent. Despite the old pool design making it possible to have small amounts of insolvency, in practice this wasn't an issue because the magnitudes were so small. The following is an analysis of why this wasn't an issue.

Insolvency Writeup

Sheng surfaced a failing fuzz test that indicates insolvency on the Morpho Blue wstETH/USDA Hyperdrive pool. After investigating this issue, I noticed that this issue will occur whenever shorts are opened because of the imprecision in converting from shares to base using the vault share price.

Since this pool already has a fairly large amount of liquidity in it ($10k), it would be annoying for all involved to have to redeploy the pool. DELV has already deployed ~$80 of USDA to the pool, so depending on the amount of insolvency, this will be covered by DELV's initial deposit.

Issue Analysis

To get a better understanding of the insolvency issue surfaced yesterday, it's useful to understand the magnitude of the insolvency. If the insolvency is small enough, it can be covered by the initial liquidity supplied to the pool.

On a short of 156.468005765553258496 bonds, we run into a small amount of insolvency immediately. Looking at the expected balance of vault shares versus the actual balance of vault shares measured in vault shares and base, we can see that the discrepancy is quite small:

# Insolvency after shorting 156.46 bonds
expected - actual = 0.000002382523430226 (vault shares)
expected - actual = 0.000000000002426696 (base)

If the short amount is increased to the max short of 3231.05668405864919373 bonds, the insolvency amounts are:

# Insolvency after shorting 3,231.05 bonds
expected - actual = 0.000124150061041186 (vault shares)
expected - actual = 0.000000000126451877 (base)

To get a better sense of what this looks like in an extreme case, we'll add lots of liquidity before opening the max short. When we add 6_000_000 USDA of liquidity and open a max short of 2_011_395.096051101066013435, the insolvency amounts are:

# Insolvency after shorting 2,011,395.09 bonds
expected - actual = 0.073716526583006505 (vault shares)
expected - actual = 0.000000075083275057 (base)

Plotting an additional point on this line, we can see that insolvency scales with bonds with a slope of approximately 4e-14 base of insolvency per bond. Using this linear regression model, we can solve for the short volume that will result in an insolvency of $80:

80 = 4e-14 * volume 

        => 
        
volume = 80 / 4e-14 = ~1.98 quadrillion bonds of volume

To make sure that this analysis isn't leaving anything off, I tried to see what the impact of closing shorts immediately would have on solvency. In the three examples we have the following results:

# Insolvency after shorting 156.46 bonds
expected - actual = 0.000000046582281669 (vault shares)
expected - actual = 0.000000000000047445 (base)

# Insolvency after shorting 3,231.05 bonds
expected - actual = 0.000006210721733739 (vault shares)
expected - actual = 0.000000000006325872 (base)

# Insolvency after shorting 2,011,395.09 bonds
expected - actual = 0.003687738504722341 (vault shares)
expected - actual = 0.000000003756111381 (base)

Doing the same linear regression on these points, the insolvency per bond that is bought and immediately sold is ~2e-15. Solving for the amount of this volume that is required to achieve insolvency of $80 we get:

80 = 2e-15 * volume 

        => 
        
volume = 80 / 2e-15 = ~40 quadrillion bonds of volume

Next, I'll plot similar points if we close the longs at maturity with the variable rate staying constant for the full term. As we can see from the points below, closing at maturity results in the pool becoming more solvent, so we can support an infinite amount of volume in this direction.

# Insolvency after shorting 156.46 bonds
expected - actual = -655.548941700312485531    (vault shares)
expected - actual = -0.000679298418179787      (base)

# Insolvency after shorting 3,231.05 bonds
expected - actual = -14182.720602672148916913  (vault shares)
expected - actual = -0.014696361667379226      (base)

# Insolvency after shorting 2,011,395.09 bonds
expected - actual = -392669.718584225548928331 (vault shares)
expected - actual = -0.400268410847870286      (base)

Verdict

Given the extremely small monetary value of the insolvency accrued on the short, the pool can support more volume than we'll ever need it to support. This pool would require more volume than is processed in mature treasury markets in a decade to be more than $80 insolvent, and the pool's solvency is improved when shorts are held to maturity.

Copy link
Contributor

@sentilesdal sentilesdal left a comment

Choose a reason for hiding this comment

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

Nice writeup, thanks

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.

great work on this! love the writeup. one thing, it might be good to copy the writeup to the PR directly for posterity. also, is there a new test that you can write that demonstrates that this issue is fixed? otherwise, lgtm

@jalextowle jalextowle added this pull request to the merge queue Oct 24, 2024
Merged via the queue into main with commit a78e12c Oct 24, 2024
32 of 34 checks passed
@jalextowle jalextowle deleted the jalextowle/fix/open-short-fix branch October 24, 2024 21:37
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.

3 participants