Skip to content

Commit

Permalink
test: additional tests cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lekhovitsky committed Nov 30, 2023
1 parent 17e71a8 commit 224346e
Show file tree
Hide file tree
Showing 19 changed files with 73 additions and 147 deletions.
55 changes: 8 additions & 47 deletions contracts/test/unit/credit/CreditManagerV3.unit.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {AddressProviderV3ACLMock} from "../../mocks/core/AddressProviderV3ACLMoc
import {AccountFactoryMock} from "../../mocks/core/AccountFactoryMock.sol";

import {CreditManagerV3Harness} from "./CreditManagerV3Harness.sol";
import {CreditManagerV3Harness_USDT} from "./CreditManagerV3Harness_USDT.sol";
import "@gearbox-protocol/core-v2/contracts/libraries/Constants.sol";

// LIBS & TRAITS
Expand Down Expand Up @@ -139,9 +138,7 @@ contract CreditManagerV3UnitTest is TestHelper, ICreditManagerV3Events, BalanceH
poolQuotaKeeperMock = new PoolQuotaKeeperMock(address(poolMock), underlying);
poolMock.setPoolQuotaKeeper(address(poolQuotaKeeperMock));

creditManager = (isFeeToken)
? new CreditManagerV3Harness_USDT(address(addressProvider), address(poolMock), name)
: new CreditManagerV3Harness(address(addressProvider), address(poolMock), name);
creditManager = new CreditManagerV3Harness(address(addressProvider), address(poolMock), name, isFeeToken);
creditManager.setCreditFacade(address(this));

creditManager.setFees(
Expand Down Expand Up @@ -236,8 +233,9 @@ contract CreditManagerV3UnitTest is TestHelper, ICreditManagerV3Events, BalanceH

function _addTokensBatch(address creditAccount, uint8 numberOfTokens, uint256 balance) internal {
for (uint8 i = 0; i < numberOfTokens; ++i) {
ERC20Mock t =
new ERC20Mock(string.concat("new token ", Strings.toString(i+1)),string.concat("NT-", Strings.toString(i+1)), 18);
ERC20Mock t = new ERC20Mock(
string.concat("new token ", Strings.toString(i + 1)), string.concat("NT-", Strings.toString(i + 1)), 18
);

_addToken({token: address(t), lt: 80_00});

Expand Down Expand Up @@ -784,9 +782,9 @@ contract CreditManagerV3UnitTest is TestHelper, ICreditManagerV3Events, BalanceH
tokenTestSuite.mint(tokenTestSuite.addressOf(Tokens.LINK), creditAccount, _case.linkBalance);

vm.startPrank(CONFIGURATOR);
for (uint256 i; i < _case.quotedTokens.length; ++i) {
for (uint256 j; j < _case.quotedTokens.length; ++j) {
creditManager.setQuotedMask(
creditManager.quotedTokensMask() | creditManager.getTokenMaskOrRevert(_case.quotedTokens[i])
creditManager.quotedTokensMask() | creditManager.getTokenMaskOrRevert(_case.quotedTokens[j])
);
}

Expand Down Expand Up @@ -880,10 +878,10 @@ contract CreditManagerV3UnitTest is TestHelper, ICreditManagerV3Events, BalanceH
}

{
(uint256 debt,, uint128 cumulativeQuotaInterest, uint128 quotaFees,,,,) =
(uint256 accountDebt,, uint128 cumulativeQuotaInterest, uint128 quotaFees,,,,) =
creditManager.creditAccountInfo(creditAccount);

assertEq(debt, 0, _testCaseErr("Debt is not zero"));
assertEq(accountDebt, 0, _testCaseErr("Debt is not zero"));
assertEq(cumulativeQuotaInterest, 1, _testCaseErr("cumulativeQuotaInterest is not 1"));
assertEq(quotaFees, 0, _testCaseErr("quotaFees is not zero"));
}
Expand Down Expand Up @@ -2112,43 +2110,6 @@ contract CreditManagerV3UnitTest is TestHelper, ICreditManagerV3Events, BalanceH
}
}

/// @dev U:[CM-23]: calcDebtAndCollateral adds withrawal for particilar cases correctly
function test_U_CM_23_calcDebtAndCollateral_adds_withrawal_for_particilar_cases_correctly()
public
creditManagerTest
{
uint256 debt = DAI_ACCOUNT_AMOUNT;
uint256 amount1 = 10_000;
uint256 amount2 = 999;

_collateralTestSetup(debt);

address creditAccount = DUMB_ADDRESS;
tokenTestSuite.mint({token: underlying, to: creditAccount, amount: 10_000});

for (uint256 i = 0; i < 2; ++i) {
bool setFlag = i == 1;
caseName =
string.concat(caseName, "withdrawal computation. WITHDRAWAL FLAG is ", setFlag ? "true" : "flase");

creditManager.setCreditAccountInfoMap({
creditAccount: creditAccount,
debt: debt,
cumulativeIndexLastUpdate: vars.get("cumulativeIndexLastUpdate"),
cumulativeQuotaInterest: uint128(vars.get("INITIAL_INTEREST") + 1),
quotaFees: 0,
enabledTokensMask: UNDERLYING_TOKEN_MASK,
flags: 0,
borrower: USER
});

CollateralDebtData memory collateralDebtData = creditManager.calcDebtAndCollateral({
creditAccount: creditAccount,
task: CollateralCalcTask.DEBT_COLLATERAL
});
}
}

///
/// GET QUOTED TOKENS DATA
///
Expand Down
21 changes: 18 additions & 3 deletions contracts/test/unit/credit/CreditManagerV3Harness.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@
pragma solidity ^0.8.17;

import {CreditManagerV3, CreditAccountInfo} from "../../../credit/CreditManagerV3.sol";
import {USDT_Transfer} from "../../../traits/USDT_Transfer.sol";

import {CollateralDebtData, CollateralCalcTask, CollateralTokenData} from "../../../interfaces/ICreditManagerV3.sol";
import {IPoolV3} from "../../../interfaces/IPoolV3.sol";
import {EnumerableSet} from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
import {PERCENTAGE_FACTOR} from "@gearbox-protocol/core-v2/contracts/libraries/Constants.sol";

contract CreditManagerV3Harness is CreditManagerV3 {
contract CreditManagerV3Harness is CreditManagerV3, USDT_Transfer {
using EnumerableSet for EnumerableSet.AddressSet;

constructor(address _addressProvider, address _pool, string memory _name)
bool _enableTransferFee;

constructor(address _addressProvider, address _pool, string memory _name, bool enableTransferFee)
CreditManagerV3(_addressProvider, _pool, _name)
{}
USDT_Transfer(IPoolV3(_pool).underlyingToken())
{
_enableTransferFee = enableTransferFee;
}

function setReentrancy(uint8 _status) external {
_reentrancyStatus = _status;
Expand Down Expand Up @@ -122,4 +129,12 @@ contract CreditManagerV3Harness is CreditManagerV3 {
function setCollateralTokensCount(uint8 _collateralTokensCount) external {
collateralTokensCount = _collateralTokensCount;
}

function _amountWithFee(uint256 amount) internal view override returns (uint256) {
return _enableTransferFee ? _amountUSDTWithFee(amount) : amount;
}

function _amountMinusFee(uint256 amount) internal view override returns (uint256) {
return _enableTransferFee ? _amountUSDTMinusFee(amount) : amount;
}
}
21 changes: 0 additions & 21 deletions contracts/test/unit/credit/CreditManagerV3Harness_USDT.sol

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {AddressProviderV3ACLMock} from "../../mocks/core/AddressProviderV3ACLMoc
// EXCEPTIONS
import "../../../interfaces/IExceptions.sol";

contract ControllerTimelockTest is Test, IControllerTimelockV3Events {
contract ControllerTimelockV3UnitTest is Test, IControllerTimelockV3Events {
AddressProviderV3ACLMock public addressProvider;

ControllerTimelockV3 public controllerTimelock;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {TestHelper} from "../../lib/helper.sol";
// EXCEPTIONS
import "../../../interfaces/IExceptions.sol";

contract GauageTest is TestHelper, IGaugeV3Events {
contract GauageV3UnitTest is TestHelper, IGaugeV3Events {
address gearToken;
address underlying;

Expand Down Expand Up @@ -207,8 +207,7 @@ contract GauageTest is TestHelper, IGaugeV3Events {
vm.prank(CONFIGURATOR);
gauge.changeQuotaMinRate(token, minRate);

(uint16 _minRate, uint16 _maxRate, uint96 _totalVotesLpSide, uint96 _totalVotesCaSide) =
gauge.quotaRateParams(token);
(uint16 _minRate,,,) = gauge.quotaRateParams(token);

assertEq(_minRate, minRate, "Incorrect minRate");
}
Expand Down Expand Up @@ -242,8 +241,7 @@ contract GauageTest is TestHelper, IGaugeV3Events {
vm.prank(CONFIGURATOR);
gauge.changeQuotaMaxRate(token, maxRate);

(uint16 _minRate, uint16 _maxRate, uint96 _totalVotesLpSide, uint96 _totalVotesCaSide) =
gauge.quotaRateParams(token);
(, uint16 _maxRate,,) = gauge.quotaRateParams(token);

assertEq(_maxRate, maxRate, "Incorrect maxRate");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {Tokens} from "@gearbox-protocol/sdk-gov/contracts/Tokens.sol";
// EXCEPTIONS
import "../../../interfaces/IExceptions.sol";

contract GearStakingTest is Test, IGearStakingV3Events {
contract GearStakingV3UnitTest is Test, IGearStakingV3Events {
address gearToken;

AddressProviderV3ACLMock public addressProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
// (c) Gearbox Foundation, 2023.
pragma solidity ^0.8.17;

import {PolicyManagerV3, Policy} from "../../../governance/PolicyManagerV3.sol";
import {PolicyManagerInternal} from "../../mocks/governance/PolicyManagerInternal.sol";
import {PolicyManagerV3Harness, Policy} from "./PolicyManagerV3Harness.sol";
import {PERCENTAGE_FACTOR} from "@gearbox-protocol/core-v2/contracts/libraries/Constants.sol";

// TEST
Expand All @@ -16,10 +15,10 @@ import {AddressProviderV3ACLMock} from "../../mocks/core/AddressProviderV3ACLMoc
// EXCEPTIONS
import "../../../interfaces/IExceptions.sol";

contract PolicyManagerTest is Test {
contract PolicyManagerV3UnitTest is Test {
AddressProviderV3ACLMock public addressProvider;

PolicyManagerInternal public policyManager;
PolicyManagerV3Harness public policyManager;

event SetPolicy(bytes32 indexed policyHash, bool enabled);
event SetGroup(address indexed contractAddress, string indexed group);
Expand All @@ -28,7 +27,7 @@ contract PolicyManagerTest is Test {
vm.prank(CONFIGURATOR);
addressProvider = new AddressProviderV3ACLMock();

policyManager = new PolicyManagerInternal(address(addressProvider));
policyManager = new PolicyManagerV3Harness(address(addressProvider));
}

///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
// (c) Gearbox Foundation, 2023.
pragma solidity ^0.8.17;

import {PolicyManagerV3} from "../../../governance/PolicyManagerV3.sol";
import {PolicyManagerV3, Policy} from "../../../governance/PolicyManagerV3.sol";

contract PolicyManagerInternal is PolicyManagerV3 {
contract PolicyManagerV3Harness is PolicyManagerV3 {
constructor(address _addressProvider) PolicyManagerV3(_addressProvider) {}

function checkPolicy(address contractAddress, string memory paramName, uint256 oldValue, uint256 newValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {TestHelper} from "../../lib/helper.sol";

/// @title BitMask logic test
/// @notice U:[BM]: Unit tests for bit mask library
contract BitMaskTest is TestHelper {
contract BitMaskUnitTest is TestHelper {
using BitMask for uint256;

/// @notice U:[BM-1]: `calcIndex` reverts for zero value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import "../../lib/constants.sol";

/// @title CreditAccountHelper logic test
/// @notice U:[CAH]: Unit tests for credit account helper
contract CreditAccountHelperTest is TestHelper, BalanceHelper {
contract CreditAccountHelperUnitTest is TestHelper, BalanceHelper {
using CreditAccountHelper for ICreditAccountBase;

address creditAccount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import {GeneralMock} from "../../mocks/GeneralMock.sol";
import {PERCENTAGE_FACTOR} from "@gearbox-protocol/core-v2/contracts/libraries/Constants.sol";
import {RAY, WAD} from "@gearbox-protocol/core-v2/contracts/libraries/Constants.sol";

/// @title BitMask logic test
/// @notice [BM]: Unit tests for bit mask library
contract CreditLogicTest is TestHelper {
/// @title Credit logic unit test
/// @notice U:[BM]: Unit tests for CreditLogic library
contract CreditLogicUnitTest is TestHelper {
uint256 public constant TEST_FEE = 50;

address[8] tokens;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {TestHelper} from "../../lib/helper.sol";
import {RAY, WAD} from "@gearbox-protocol/core-v2/contracts/libraries/Constants.sol";

/// @title Quotas logic test
/// @notice [BM]: Unit tests for QuotasLogic library
contract QuotasLogicTest is TestHelper {
/// @notice U:[BM]: Unit tests for QuotasLogic library
contract QuotasLogicUnitTest is TestHelper {
TokenQuotaParams params;
AccountQuota accountQuota;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,13 @@ import "../../../interfaces/IExceptions.sol";

import {TestHelper} from "../../lib/helper.sol";

contract LinearInterestRateModelV3UniTest is TestHelper {
contract LinearInterestRateModelV3UnitTest is TestHelper {
using Math for uint256;

LinearInterestRateModelV3 irm;

function setUp() public {
irm = new LinearInterestRateModelV3(
80_00,
95_00,
10_00,
20_00,
30_00,
40_00,
true
);
irm = new LinearInterestRateModelV3(80_00, 95_00, 10_00, 20_00, 30_00, 40_00, true);
}

//
Expand Down Expand Up @@ -155,14 +147,14 @@ contract LinearInterestRateModelV3UniTest is TestHelper {

vm.expectRevert(IncorrectParameterException.selector);
irm = new LinearInterestRateModelV3(
testCase.U_1,
testCase.U_2,
testCase.R_base,
testCase.R_slope1,
testCase.R_slope2,
testCase.R_slope3,
false
);
testCase.U_1,
testCase.U_2,
testCase.R_base,
testCase.R_slope1,
testCase.R_slope2,
testCase.R_slope3,
false
);
}
}

Expand Down Expand Up @@ -439,14 +431,14 @@ contract LinearInterestRateModelV3UniTest is TestHelper {
LinearCalculationsCase memory testCase = cases[i];

irm = new LinearInterestRateModelV3(
testCase.U_1,
testCase.U_2,
testCase.R_base,
testCase.R_slope1,
testCase.R_slope2,
testCase.R_slope3,
testCase.isBorrowingMoreU2Forbidden
);
testCase.U_1,
testCase.U_2,
testCase.R_base,
testCase.R_slope1,
testCase.R_slope2,
testCase.R_slope3,
testCase.isBorrowingMoreU2Forbidden
);

if (testCase.expectedRevert) {
vm.expectRevert(BorrowingMoreThanU2ForbiddenException.selector);
Expand Down
Loading

0 comments on commit 224346e

Please sign in to comment.