Skip to content

Commit

Permalink
feat: additional validation on tradingLimits (#257)
Browse files Browse the repository at this point in the history
  • Loading branch information
philbow61 committed Aug 8, 2023
1 parent 9d9a4aa commit a4b01b0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
6 changes: 6 additions & 0 deletions contracts/libraries/TradingLimits.sol
Expand Up @@ -87,6 +87,12 @@ library TradingLimits {
require(self.flags & L1 == 0 || self.flags & L0 != 0, "L1 without L0 not allowed");
require(self.flags & L0 == 0 || self.timestep0 > 0, "timestep0 can't be zero if active");
require(self.flags & L1 == 0 || self.timestep1 > 0, "timestep1 can't be zero if active");
require(self.flags & L0 == 0 || self.limit0 > 0, "limit0 can't be zero if active");
require(self.flags & L1 == 0 || self.limit1 > 0, "limit1 can't be zero if active");
require(self.flags & LG == 0 || self.limitGlobal > 0, "limitGlobal can't be zero if active");
require(self.flags & (L0 | L1) != 3 || self.limit0 < self.limit1, "limit1 must be greater than limit0");
require(self.flags & (L1 | LG) != 6 || self.limit1 < self.limitGlobal, "limitGlobal must be greater than limit1");
require(self.flags & (L0 | LG) != 5 || self.limit0 < self.limitGlobal, "limitGlobal must be greater than limit0");
}
/**
Expand Down
2 changes: 1 addition & 1 deletion slither.db.json

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions test/libraries/TradingLimits.t.sol
Expand Up @@ -101,17 +101,53 @@ contract TradingLimitsTest is Test {
config.validate();
}
function test_validate_withL0_withoutLimit0_isNotValid() public {
TradingLimits.Config memory config = configL0(100, 0);
vm.expectRevert(bytes("limit0 can't be zero if active"));
config.validate();
}

function test_validate_withL0L1_isValid() public pure {
TradingLimits.Config memory config = configL0L1(100, 1000, 1000, 10000);
config.validate();
}

function test_validate_withL1_withoutLimit1_isNotValid() public {
TradingLimits.Config memory config = configL0L1(100, 1000, 1000, 0);
vm.expectRevert(bytes("limit1 can't be zero if active"));
config.validate();
}
function test_validate_withL0L1_withoutTimestape_isNotValid() public {
TradingLimits.Config memory config = configL0L1(0, 1000, 1000, 10000);
vm.expectRevert(bytes("timestep0 can't be zero if active"));
config.validate();
}

function test_validate_withL0L1_withLimit0LargerLimit1_isNotValid() public {
TradingLimits.Config memory config = configL0L1(10000, 10000, 1000, 1000);
vm.expectRevert(bytes("limit1 must be greater than limit0"));
config.validate();
}

function test_validate_withLG_withoutLimitGlobal_isNotValid() public {
TradingLimits.Config memory config = configL0L1LG(100, 1000, 1000, 10000, 0);
vm.expectRevert(bytes("limitGlobal can't be zero if active"));
config.validate();
}
function test_validate_withL0LG_withLimit0LargerLimitGlobal_isNotValid() public {
TradingLimits.Config memory config = configL0LG(10000, 10000, 1000);
vm.expectRevert(bytes("limitGlobal must be greater than limit0"));
config.validate();
}
function test_validate_withL1LG_withLimit1LargerLimitGlobal_isNotValid() public {
TradingLimits.Config memory config = configL0L1LG(100, 1000, 10000, 10000, 1000);
vm.expectRevert(bytes("limitGlobal must be greater than limit1"));
config.validate();
}
function test_validate_withL0L1LG_isValid() public pure {
TradingLimits.Config memory config = configL0L1LG(100, 1000, 1000, 10000, 100000);
config.validate();
Expand Down

0 comments on commit a4b01b0

Please sign in to comment.