Skip to content

Commit

Permalink
Merge pull request #13 from Gearbox-protocol/security-improvements
Browse files Browse the repository at this point in the history
feat: credit facade gas tests
  • Loading branch information
0xmikko authored Mar 31, 2023
2 parents 69d7028 + 6c2ecde commit 95b124d
Show file tree
Hide file tree
Showing 6 changed files with 1,021 additions and 13 deletions.
3 changes: 1 addition & 2 deletions contracts/credit/CreditFacade.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {EnumerableSet} from "@openzeppelin/contracts/utils/structs/EnumerableSet
import {SafeCast} from "@openzeppelin/contracts/utils/math/SafeCast.sol";
import {ACLNonReentrantTrait} from "../core/ACLNonReentrantTrait.sol";


// DATA
import {MultiCall} from "@gearbox-protocol/core-v2/contracts/libraries/MultiCall.sol";
import {Balance, BalanceOps} from "@gearbox-protocol/core-v2/contracts/libraries/Balances.sol";
Expand All @@ -20,8 +19,8 @@ import {ICreditFacade, ICreditFacadeExtended, FullCheckParams} from "../interfac
import {ICreditManagerV2, ClosureAction} from "../interfaces/ICreditManagerV2.sol";
import {IPriceOracleV2} from "@gearbox-protocol/core-v2/contracts/interfaces/IPriceOracle.sol";


import {IPool4626} from "../interfaces/IPool4626.sol";
import {TokenLT} from "../interfaces/IPoolQuotaKeeper.sol";
import {IDegenNFT} from "@gearbox-protocol/core-v2/contracts/interfaces/IDegenNFT.sol";
import {IWETH} from "@gearbox-protocol/core-v2/contracts/interfaces/external/IWETH.sol";
import {IWETHGateway} from "../interfaces/IWETHGateway.sol";
Expand Down
5 changes: 2 additions & 3 deletions contracts/credit/CreditManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,8 @@ contract CreditManager is ICreditManagerV2, ACLNonReentrantTrait {
if (supportsQuotas) {
tokens = getLimitedTokens(creditAccount);


// TODO: Check that it never breaks
quotaInterest = cumulativeQuotaInterest[creditAccount] - 1;
uint256 quotaInterest = cumulativeQuotaInterest[creditAccount] - 1;

if (tokens.length > 0) {
quotaInterest += poolQuotaKeeper().closeCreditAccount(creditAccount, tokens); // F: [CMQ-6]
Expand Down Expand Up @@ -1343,7 +1342,7 @@ contract CreditManager is ICreditManagerV2, ACLNonReentrantTrait {
if (supportsQuotas) {
TokenLT[] memory tokens = getLimitedTokens(creditAccount);

quotaInterest = cumulativeQuotaInterest[creditAccount];
quotaInterest = cumulativeQuotaInterest[creditAccount] - 1;

if (tokens.length > 0) {
quotaInterest += poolQuotaKeeper().outstandingQuotaInterest(address(this), creditAccount, tokens); // F: [CMQ-10]
Expand Down
2 changes: 1 addition & 1 deletion contracts/pool/PoolQuotaKeeper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ contract PoolQuotaKeeper is IPoolQuotaKeeper, ACLNonReentrantTrait {

while (i < len && tokensLT[i].token != address(0)) {
address token = tokensLT[i].token;
totalQuotas[token].limit = 1; // F: [CMQ-12]
totalQuotaParams[token].limit = 1; // F: [CMQ-12]

unchecked {
++i;
Expand Down
3 changes: 2 additions & 1 deletion contracts/test/credit/CreditManager_Quotas.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,7 @@ contract CreditManagerQuotasTest is

/// @dev [CMQ-12]: Credit Manager zeroes limits on quoted tokens upon incurring a loss
function test_CMQ_12_creditManager_triggers_limit_zeroing_on_loss() public {
_makeTokenLimited(tokenTestSuite.addressOf(Tokens.LINK), 10_00, uint96(1_000_000 * WAD));
_makeTokenLimited(tokenTestSuite.addressOf(Tokens.USDT), 500, uint96(1_000_000 * WAD));

(,,, address creditAccount) = _openCreditAccount();
Expand Down Expand Up @@ -656,7 +657,7 @@ contract CreditManagerQuotasTest is
for (uint256 i = 0; i < quotedTokens.length; ++i) {
if (quotedTokens[i].token == address(0)) continue;

(, uint96 limit,,) = poolQuotaKeeper.totalQuotas(quotedTokens[i].token);
(, uint96 limit,,) = poolQuotaKeeper.totalQuotaParams(quotedTokens[i].token);

assertEq(limit, 1, "Limit was not zeroed");
}
Expand Down
Loading

0 comments on commit 95b124d

Please sign in to comment.