-
Notifications
You must be signed in to change notification settings - Fork 3
feat: add tradingLimit decimal tracking #573
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
Draft
philbow61
wants to merge
2
commits into
develop
Choose a base branch
from
feat/add-decimaltracking-for-TradingLimits
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -46,6 +46,8 @@ library TradingLimits { | |
uint8 private constant LG = 4; // 0b100 LimitGlobal | ||
int48 private constant MAX_INT48 = type(int48).max; | ||
int48 private constant MIN_INT48 = type(int48).min; | ||
uint8 private constant DECIMAL_PRECISION = 14; | ||
int256 private constant DECIMAL_ONE = 1e14; | ||
|
||
/** | ||
* @notice Validate a trading limit configuration. | ||
|
@@ -71,17 +73,28 @@ library TradingLimits { | |
* @param config the trading limit Config to check against. | ||
*/ | ||
function verify(ITradingLimits.State memory self, ITradingLimits.Config memory config) internal pure { | ||
if ((config.flags & L0) > 0 && (-1 * config.limit0 > self.netflow0 || self.netflow0 > config.limit0)) { | ||
revert("L0 Exceeded"); | ||
if ((config.flags & L0) > 0) { | ||
int256 netflow0WithDecimals = int256(self.netflow0) * DECIMAL_ONE + int256(self.netflowDecimals); | ||
int256 limit0WithDecimals = int256(config.limit0) * DECIMAL_ONE; | ||
if (-1 * limit0WithDecimals > netflow0WithDecimals || netflow0WithDecimals > limit0WithDecimals) { | ||
revert("L0 Exceeded"); | ||
} | ||
} | ||
if ((config.flags & L1) > 0 && (-1 * config.limit1 > self.netflow1 || self.netflow1 > config.limit1)) { | ||
revert("L1 Exceeded"); | ||
if ((config.flags & L1) > 0) { | ||
int256 netflow1WithDecimals = int256(self.netflow1) * DECIMAL_ONE + int256(self.netflowDecimals); | ||
int256 limit1WithDecimals = int256(config.limit1) * DECIMAL_ONE; | ||
if (-1 * limit1WithDecimals > netflow1WithDecimals || netflow1WithDecimals > limit1WithDecimals) { | ||
revert("L1 Exceeded"); | ||
} | ||
} | ||
if ( | ||
(config.flags & LG) > 0 && | ||
(-1 * config.limitGlobal > self.netflowGlobal || self.netflowGlobal > config.limitGlobal) | ||
) { | ||
revert("LG Exceeded"); | ||
if (config.flags & LG > 0) { | ||
int256 netflowGlobalWithDecimals = int256(self.netflowGlobal) * DECIMAL_ONE + int256(self.netflowDecimals); | ||
int256 limitGlobalWithDecimals = int256(config.limitGlobal) * DECIMAL_ONE; | ||
if ( | ||
-1 * limitGlobalWithDecimals > netflowGlobalWithDecimals || netflowGlobalWithDecimals > limitGlobalWithDecimals | ||
) { | ||
revert("LG Exceeded"); | ||
} | ||
} | ||
} | ||
|
||
|
@@ -110,6 +123,9 @@ library TradingLimits { | |
if (config.flags & LG == 0) { | ||
self.netflowGlobal = 0; | ||
} | ||
if (config.flags & (L0 | LG) == 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would make more sense to reset the decimals for every reset for simplicity, considering it will be called infrequently and only downside in losing some decimals. wdyt? |
||
self.netflowDecimals = 0; | ||
} | ||
return self; | ||
} | ||
|
||
|
@@ -132,14 +148,13 @@ library TradingLimits { | |
return self; | ||
} | ||
|
||
int256 _deltaFlowUnits = _deltaFlow / int256((10 ** uint256(decimals))); | ||
(int256 _deltaFlowUnits, int48 newNetflowDecimals) = calculateUnitsAndDecimals(_deltaFlow, decimals, self, config); | ||
self.netflowDecimals = newNetflowDecimals; | ||
|
||
require(_deltaFlowUnits <= MAX_INT48, "dFlow too large"); | ||
require(_deltaFlowUnits >= MIN_INT48, "dFlow too small"); | ||
|
||
int48 deltaFlowUnits = int48(_deltaFlowUnits); | ||
if (deltaFlowUnits == 0) { | ||
deltaFlowUnits = _deltaFlow > 0 ? int48(1) : int48(-1); | ||
} | ||
|
||
if (config.flags & L0 > 0) { | ||
if (block.timestamp > self.lastUpdated0 + config.timestep0) { | ||
|
@@ -163,6 +178,47 @@ library TradingLimits { | |
return self; | ||
} | ||
|
||
/** | ||
* @notice Calculate the delta flow units and new decimals of the netflows. | ||
* @dev The decimals are scaled to 14 decimals, so the decimals are truncated/scaled | ||
* to fit in the int48 used to store the netflow decimals. | ||
* @param _deltaFlow the delta flow to calculate. | ||
* @param decimals the number of decimals the _deltaFlow is denominated in. | ||
* @param self the trading limit State to update. | ||
* @param config the trading limit Config for the provided State. | ||
* @return deltaFlowUnits the units of the delta flow. | ||
* @return newNetflowDecimals the new decimals of the netflows. | ||
*/ | ||
function calculateUnitsAndDecimals( | ||
int256 _deltaFlow, | ||
uint8 decimals, | ||
ITradingLimits.State memory self, | ||
ITradingLimits.Config memory config | ||
) internal view returns (int256 deltaFlowUnits, int48 newNetflowDecimals) { | ||
if (config.flags & L0 > 0 && self.lastUpdated0 + config.timestep0 < block.timestamp) { | ||
self.netflowDecimals = 0; | ||
} | ||
|
||
deltaFlowUnits = _deltaFlow / int256((10 ** uint256(decimals))); | ||
int256 deltaFlowDecimals = _deltaFlow % int256((10 ** uint256(decimals))); | ||
|
||
if (decimals > DECIMAL_PRECISION) { | ||
deltaFlowDecimals = deltaFlowDecimals / int256((10 ** uint256(decimals - DECIMAL_PRECISION))); | ||
} else { | ||
deltaFlowDecimals = deltaFlowDecimals * int256((10 ** uint256(DECIMAL_PRECISION - decimals))); | ||
} | ||
|
||
int256 decimalSum = int256(self.netflowDecimals) + deltaFlowDecimals; | ||
|
||
int256 carryOver = decimalSum / DECIMAL_ONE; | ||
deltaFlowUnits = deltaFlowUnits + carryOver; | ||
|
||
int256 normalizedDecimals = decimalSum % DECIMAL_ONE; | ||
newNetflowDecimals = int48(normalizedDecimals); | ||
|
||
return (deltaFlowUnits, newNetflowDecimals); | ||
} | ||
|
||
/** | ||
* @notice Safe add two int48s. | ||
* @dev Reverts if addition causes over/underflow. | ||
|
This file contains hidden or 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 hidden or 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
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.
Nit, I would prefer the order to be not changed for readability:
if (netflow0WithDecimals < -1 * limit0WithDecimals || netflow0WithDecimals > limit0WithDecimals)