-
Notifications
You must be signed in to change notification settings - Fork 58
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
Market debt #1700
Market debt #1700
Conversation
Codecov Report
@@ Coverage Diff @@
## main #1700 +/- ##
=======================================
Coverage 72.77% 72.77%
=======================================
Files 57 57
Lines 720 720
Branches 236 236
=======================================
Hits 524 524
Misses 167 167
Partials 29 29
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
} | ||
|
||
// TODO Should revert if totalDebt is positive and larger than totalCollateralValue??? | ||
return MathUtil.abs(totalCollateralValue.toInt() - totalDebt); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
absolute value here is wrong. lets return 0 if totalDebt is larger than the total collateral value.
this is super edge case where liquidations aren't happening properly and the market continues accruing debt. regardless, if this does happen, the debt is just 0; the LPs could continue accruing value but there's no real way to pay that out since we already have all of trader's collateral.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also shouldn't it be totalCollateralValue + totalDebt
?
example 1:
total collateral: $1000
total markets debt: $500
reported Debt == $1500
example 2:
total collateral value = $1000
total market debt = -$800
reported debt = $200
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to double check together with the +
or -
on oldPositionPnl
since it depends on how marketDebt is seen (if positive or negative)
|
||
self.debtCorrectionAccumulator += | ||
currentPrice.toInt() * | ||
(newPosition.size - oldPositionSize) - |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like this is +
oldPositionPnl in the sheet? i could be wrong but just double check
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some comments but lgtm!
@@ -36,7 +36,7 @@ contract LiquidationModule is ILiquidationModule { | |||
.load() | |||
.liquidatableAccounts; | |||
|
|||
for (uint i = 0; i < liquidatableAccounts.length(); i++) { | |||
for (uint i = 1; i <= liquidatableAccounts.length(); i++) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed this in my PR; we actually can't use liquidateAccounts
directly because if there's a full account liquidation, it gets removed and we get an error. instead i move the liquidate accounts values into an array in memory which solves the issue 👍🏽
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
feel free to keep this; and i'll fix merge conflict or vice versa depending on which PR goes in first
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll keep it otherwise tests will fail, but will fix if your PR comes first
markets/perps-market/contracts/modules/PerpsMarketFactoryModule.sol
Outdated
Show resolved
Hide resolved
} | ||
} | ||
|
||
// TODO Should revert if perpsMarketId is not correct??? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
0 is fine. we probably should never revert on reported debt since it could brick the core system
uint currentPrice = newPosition.latestInteractionPrice; | ||
(int oldPositionPnl, , , ) = oldPosition.getPnl(currentPrice); | ||
|
||
self.debtCorrectionAccumulator += | ||
currentPrice.toInt().mulDecimal(newPosition.size - oldPositionSize) + | ||
oldPositionPnl; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need to do anything with accumulator on liquidiations? i dont think so since it becomes realized right? i'll think through this a bit more
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
most likely yes. Maybe the numbers get balanced together, but not completely sure on some corner cases
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hey, suggested some changes. verified on the google sheet, but this makes the most sense to me. let me know your thoughts
uint currentPrice = newPosition.latestInteractionPrice; | ||
(int oldPositionPnl, , , ) = oldPosition.getPnl(currentPrice); | ||
|
||
self.debtCorrectionAccumulator += | ||
currentPrice.toInt().mulDecimal(newPosition.size - oldPositionSize) + | ||
oldPositionPnl; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
uint currentPrice = newPosition.latestInteractionPrice; | |
(int oldPositionPnl, , , ) = oldPosition.getPnl(currentPrice); | |
self.debtCorrectionAccumulator += | |
currentPrice.toInt().mulDecimal(newPosition.size - oldPositionSize) + | |
oldPositionPnl; | |
uint currentPrice = newPosition.latestInteractionPrice; | |
(int oldPositionPnl, int accruedFunding, , ) = oldPosition.getPnl(currentPrice); | |
self.debtCorrectionAccumulator += | |
currentPrice.toInt().mulDecimal(newPosition.size - oldPositionSize) + | |
oldPositionPnl - accruedFunding; // don't include accrued funding in this |
function marketDebt(Data storage self, uint price) internal view returns (int) { | ||
// all positions sizes multiplied by the price is equivalent to skew times price | ||
// and the debt correction accumulator is the sum of all positions pnl | ||
|
||
return self.skew.mulDecimal(price.toInt()) - self.debtCorrectionAccumulator; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
function marketDebt(Data storage self, uint price) internal view returns (int) { | |
// all positions sizes multiplied by the price is equivalent to skew times price | |
// and the debt correction accumulator is the sum of all positions pnl | |
return self.skew.mulDecimal(price.toInt()) - self.debtCorrectionAccumulator; | |
} | |
function marketDebt(Data storage self, uint price) internal view returns (int) { | |
// all positions sizes multiplied by the price is equivalent to skew times price | |
// and the debt correction accumulator is the sum of all positions pnl | |
int traderUnrealizedPnl = self.skew.mulDecimal(price.toInt()) - self.debtCorrectionAccumulator; | |
int totalAccruedFunding = self.skew.mulDecimal(price.toInt()).mulDecimal(currentFundingRate(); | |
return traderUnrealizedPnl + totalAccruedFunding; | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this will take care of unrealized funding. if we subtract it out of the accumulator then it would be counted double, so we only use trader pnl in the accumulator.
e7b110b
to
d1258b9
Compare
…e.sol Co-authored-by: Sunny Vempati <[email protected]>
d1258b9
to
25d2cb9
Compare
No description provided.