-
Notifications
You must be signed in to change notification settings - Fork 117
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
[chore] Move perpetuals/keeper
helper functions to perpetuals/lib
#1678
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package lib | ||
|
||
import ( | ||
"math/big" | ||
|
||
"github.com/dydxprotocol/v4-chain/protocol/lib" | ||
"github.com/dydxprotocol/v4-chain/protocol/x/perpetuals/types" | ||
pricestypes "github.com/dydxprotocol/v4-chain/protocol/x/prices/types" | ||
) | ||
|
||
// GetSettlementPpm returns the net settlement amount ppm (in quote quantums) given | ||
// the perpetual and position size (in base quantums). | ||
func GetSettlementPpmWithPerpetual( | ||
perpetual types.Perpetual, | ||
quantums *big.Int, | ||
index *big.Int, | ||
) ( | ||
bigNetSettlementPpm *big.Int, | ||
newFundingIndex *big.Int, | ||
) { | ||
indexDelta := new(big.Int).Sub(perpetual.FundingIndex.BigInt(), index) | ||
|
||
// if indexDelta is zero, then net settlement is zero. | ||
if indexDelta.Sign() == 0 { | ||
return big.NewInt(0), perpetual.FundingIndex.BigInt() | ||
} | ||
|
||
bigNetSettlementPpm = new(big.Int).Mul(indexDelta, quantums) | ||
|
||
// `bigNetSettlementPpm` carries sign. `indexDelta` is the increase in `fundingIndex`, so if | ||
// the position is long (positive), the net settlement should be short (negative), and vice versa. | ||
// Thus, always negate `bigNetSettlementPpm` here. | ||
bigNetSettlementPpm = bigNetSettlementPpm.Neg(bigNetSettlementPpm) | ||
|
||
return bigNetSettlementPpm, perpetual.FundingIndex.BigInt() | ||
} | ||
|
||
// GetNetNotionalInQuoteQuantums returns the net notional in quote quantums, which can be | ||
// represented by the following equation: | ||
// | ||
// `quantums / 10^baseAtomicResolution * marketPrice * 10^marketExponent * 10^quoteAtomicResolution`. | ||
// Note that longs are positive, and shorts are negative. | ||
func GetNetNotionalInQuoteQuantums( | ||
perpetual types.Perpetual, | ||
marketPrice pricestypes.MarketPrice, | ||
bigQuantums *big.Int, | ||
) ( | ||
bigNetNotionalQuoteQuantums *big.Int, | ||
) { | ||
bigQuoteQuantums := lib.BaseToQuoteQuantums( | ||
bigQuantums, | ||
perpetual.Params.AtomicResolution, | ||
marketPrice.Price, | ||
marketPrice.Exponent, | ||
) | ||
|
||
return bigQuoteQuantums | ||
} | ||
|
||
// GetMarginRequirementsInQuoteQuantums returns initial and maintenance margin requirements | ||
// in quote quantums, given the position size in base quantums. | ||
func GetMarginRequirementsInQuoteQuantums( | ||
perpetual types.Perpetual, | ||
marketPrice pricestypes.MarketPrice, | ||
liquidityTier types.LiquidityTier, | ||
bigQuantums *big.Int, | ||
) ( | ||
bigInitialMarginQuoteQuantums *big.Int, | ||
bigMaintenanceMarginQuoteQuantums *big.Int, | ||
) { | ||
// Always consider the magnitude of the position regardless of whether it is long/short. | ||
bigAbsQuantums := new(big.Int).Abs(bigQuantums) | ||
|
||
// Calculate the notional value of the position in quote quantums. | ||
bigQuoteQuantums := lib.BaseToQuoteQuantums( | ||
bigAbsQuantums, | ||
perpetual.Params.AtomicResolution, | ||
marketPrice.Price, | ||
marketPrice.Exponent, | ||
) | ||
// Calculate the perpetual's open interest in quote quantums. | ||
openInterestQuoteQuantums := lib.BaseToQuoteQuantums( | ||
perpetual.OpenInterest.BigInt(), // OpenInterest is represented as base quantums. | ||
perpetual.Params.AtomicResolution, | ||
marketPrice.Price, | ||
marketPrice.Exponent, | ||
) | ||
|
||
// Initial margin requirement quote quantums = size in quote quantums * initial margin PPM. | ||
bigBaseInitialMarginQuoteQuantums := liquidityTier.GetInitialMarginQuoteQuantums( | ||
bigQuoteQuantums, | ||
big.NewInt(0), // pass in 0 as open interest to get base IMR. | ||
) | ||
// Maintenance margin requirement quote quantums = IM in quote quantums * maintenance fraction PPM. | ||
bigMaintenanceMarginQuoteQuantums = lib.BigMulPpm( | ||
bigBaseInitialMarginQuoteQuantums, | ||
lib.BigU(liquidityTier.MaintenanceFractionPpm), | ||
true, | ||
) | ||
|
||
bigInitialMarginQuoteQuantums = liquidityTier.GetInitialMarginQuoteQuantums( | ||
bigQuoteQuantums, | ||
openInterestQuoteQuantums, // pass in current OI to get scaled IMR. | ||
) | ||
return bigInitialMarginQuoteQuantums, bigMaintenanceMarginQuoteQuantums | ||
} | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The
GetMarginRequirementsInQuoteQuantums
function is comprehensive and handles both initial and maintenance margin calculations. It correctly considers the absolute value of quantums to ensure that the direction of the position does not affect the margin requirements. The use of helper functions likeBaseToQuoteQuantums
andBigMulPpm
helps maintain modularity and reusability. However, consider adding more comments to explain the logic, especially around the calculations involving open interest caps.Consider adding detailed comments to explain the logic behind the calculations, especially how the open interest affects the margin requirements.