8000 feat: add tradingLimit decimal tracking by philbow61 · Pull Request #573 · mento-protocol/mento-core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

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
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions contracts/interfaces/ITradingLimits.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ interface ITradingLimits {
* @param netflow0 The current netflow of the asset for limit0.
* @param netflow1 The current netflow of the asset for limit1.
* @param netflowGlobal The current netflow of the asset for limitGlobal.
* @param netflowDecimals The current decimal part of all netflows.
*/
struct State {
uint32 lastUpdated0;
uint32 lastUpdated1;
int48 netflow0;
int48 netflow1;
int48 netflowGlobal;
int48 netflowDecimals;
}

/**
Expand Down
82 changes: 69 additions & 13 deletions contracts/libraries/TradingLimits.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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) {
Copy link
Contributor

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)

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");
}
}
}

Expand Down Expand Up @@ -110,6 +123,9 @@ library TradingLimits {
if (config.flags & LG == 0) {
self.netflowGlobal = 0;
}
if (config.flags & (L0 | LG) == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
}

Expand All @@ -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) {
Expand All @@ -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.
Expand Down
11 changes: 8 additions & 3 deletions test/fork/GoodDollar/GoodDollarBaseForkTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,14 @@ contract GoodDollarBaseForkTest is BaseForkTest {
function getTradingLimitsState(address tokenAddress) public view returns (ITradingLimits.State memory state) {
bytes32 limitId = getTradingLimitId(tokenAddress);
ITradingLimits.State memory _state;
(_state.lastUpdated0, _state.lastUpdated1, _state.netflow0, _state.netflow1, _state.netflowGlobal) = Broker(
address(broker)
).tradingLimitsState(limitId);
(
_state.lastUpdated0,
_state.lastUpdated1,
_state.netflow0,
_state.netflow1,
_state.netflowGlobal,
_state.netflowDecimals
) = Broker(address(broker)).tradingLimitsState(limitId);

return _state;
}
Expand Down
6 changes: 4 additions & 2 deletions test/fork/helpers/TradingLimitHelpers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ library TradingLimitHelpers {
limitState.lastUpdated1,
limitState.netflow0,
limitState.netflow1,
limitState.netflowGlobal
limitState.netflowGlobal,
limitState.netflowDecimals
) = Broker(address(ctx.broker())).tradingLimitsState(limitId);
return limitState;
}
Expand Down Expand Up @@ -81,7 +82,8 @@ library TradingLimitHelpers {
limitState.lastUpdated1,
limitState.netflow0,
limitState.netflow1,
limitState.netflowGlobal
limitState.netflowGlobal,
limitState.netflowDecimals
) = Broker(address(ctx.broker())).tradingLimitsState(limitId);
return limitState;
}
Expand Down
Loading
Loading
0