Skip to content

Commit

Permalink
[Fix] maker-minor funding allocation during socialization (#64)
Browse files Browse the repository at this point in the history
* add basic all positions tests

* position delta tests

* price delta tests

* add liquidation test

* partial socialization test

* add closed test

* shortfall test

* long liquidation tests

* fix test conflict

* fix rebase
  • Loading branch information
kbrizzle authored Aug 17, 2023
1 parent fd0c3c5 commit 28e34cc
Show file tree
Hide file tree
Showing 6 changed files with 3,159 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"hardhat-dependency-compiler": "1.1.3",
"hardhat-deploy": "0.11.31",
"hardhat-gas-reporter": "1.0.9",
"hardhat-tracer": "2.5.1",
"husky": ">=6",
"lerna": "^4.0.0",
"lint-staged": ">=10",
Expand Down
2 changes: 2 additions & 0 deletions packages/common/hardhat.default.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import '@nomicfoundation/hardhat-toolbox'
import 'hardhat-contract-sizer'
import 'hardhat-deploy'
import 'hardhat-dependency-compiler'
import 'hardhat-tracer'
import 'solidity-coverage'

import { getChainId, isArbitrum, isBase, isOptimism, SupportedChain } from './testutil/network'

import { utils } from 'ethers'
Expand Down
10 changes: 10 additions & 0 deletions packages/perennial/contracts/types/Position.sol
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,16 @@ library PositionLib {
return _skew(self, riskParameter.virtualTaker);
}

/// @notice Returns the skew of the position taking into account position socialization
/// @dev Used to calculate the portion of the position that is covered by the maker
/// @param self The position object to check
/// @return The socialized skew of the position
function socializedSkew(Position memory self) internal pure returns (UFixed6) {
return takerSocialized(self).isZero() ?
UFixed6Lib.ZERO :
takerSocialized(self).sub(minor(self)).div(takerSocialized(self));
}

/// @notice Helper function to return the skew of the position with an optional virtual taker
/// @param self The position object to check
/// @param virtualTaker The virtual taker to use in the calculation
Expand Down
6 changes: 3 additions & 3 deletions packages/perennial/contracts/types/Version.sol
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ library VersionLib {
);

// accumulate interest
(values.interestMaker,values.interestLong, values.interestShort, values.interestFee) =
(values.interestMaker, values.interestLong, values.interestShort, values.interestFee) =
_accumulateInterest(self, fromPosition, fromOracleVersion, toOracleVersion, marketParameter, riskParameter);

// accumulate P&L
Expand Down Expand Up @@ -207,11 +207,11 @@ library VersionLib {

// Redirect net portion of minor's side to maker
if (fromPosition.long.gt(fromPosition.short)) {
fundingValues.fundingMaker = fundingValues.fundingShort.mul(Fixed6Lib.from(fromPosition.skew().abs()));
fundingValues.fundingMaker = fundingValues.fundingShort.mul(Fixed6Lib.from(fromPosition.socializedSkew()));
fundingValues.fundingShort = fundingValues.fundingShort.sub(fundingValues.fundingMaker);
}
if (fromPosition.short.gt(fromPosition.long)) {
fundingValues.fundingMaker = fundingValues.fundingLong.mul(Fixed6Lib.from(fromPosition.skew().abs()));
fundingValues.fundingMaker = fundingValues.fundingLong.mul(Fixed6Lib.from(fromPosition.socializedSkew()));
fundingValues.fundingLong = fundingValues.fundingLong.sub(fundingValues.fundingMaker);
}

Expand Down
Loading

0 comments on commit 28e34cc

Please sign in to comment.