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

[Fix] maker-minor funding allocation during socialization #64

Merged
merged 10 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()));
Copy link
Collaborator

Choose a reason for hiding this comment

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

This doesn't need an abs anymore?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's already abs inside the function. This is probably not the ideal name for the method, but I did include a dev comment on what it's doing.

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