Skip to content

Commit

Permalink
Merge pull request #12 from Gearbox-protocol/poolQuotaKeeper-improvem…
Browse files Browse the repository at this point in the history
…ents

feat: traits folder + pqk tests cntd
  • Loading branch information
0xmikko authored Apr 1, 2023
2 parents 95b124d + fcae127 commit 27fdfec
Show file tree
Hide file tree
Showing 54 changed files with 925 additions and 621 deletions.
8 changes: 4 additions & 4 deletions contracts/adapters/AbstractAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
// (c) Gearbox Holdings, 2023
pragma solidity ^0.8.17;

import {ACLNonReentrantTrait} from "../core/ACLNonReentrantTrait.sol";
import {IAdapter} from "../interfaces/adapters/IAdapter.sol";
import {ACLNonReentrantTrait} from "../traits/ACLNonReentrantTrait.sol";
import {IAdapter} from "../interfaces/IAdapter.sol";
import {IAddressProvider} from "@gearbox-protocol/core-v2/contracts/interfaces/IAddressProvider.sol";
import {ICreditManagerV2} from "../interfaces/ICreditManagerV2.sol";
import {IPool4626} from "../interfaces/IPool4626.sol";
import {ZeroAddressException} from "../interfaces/IErrors.sol";
import "../interfaces/IExceptions.sol";

/// @title Abstract adapter
/// @dev Inheriting adapters MUST use provided internal functions to perform all operations with credit accounts
Expand Down Expand Up @@ -40,7 +40,7 @@ abstract contract AbstractAdapter is IAdapter, ACLNonReentrantTrait {
/// of inheriting adapters that perform actions on account MUST have this modifier
modifier creditFacadeOnly() {
if (msg.sender != _creditFacade()) {
revert CreditFacadeOnlyException(); // F: [AA-5]
revert CallerNotCreditFacadeException(); // F: [AA-5]
}
_;
}
Expand Down
6 changes: 3 additions & 3 deletions contracts/adapters/UniversalAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {UNIVERSAL_CONTRACT} from "@gearbox-protocol/core-v2/contracts/libraries/Constants.sol";

import {AbstractAdapter} from "./AbstractAdapter.sol";
import {AdapterType} from "../interfaces/adapters/IAdapter.sol";
import {AdapterType} from "../interfaces/IAdapter.sol";
import {ICreditManagerV2} from "../interfaces/ICreditManagerV2.sol";
import {IUniversalAdapter, RevocationPair} from "../interfaces/adapters/IUniversalAdapter.sol";
import {ZeroAddressException} from "../interfaces/IErrors.sol";
import {IUniversalAdapter, RevocationPair} from "../interfaces/IUniversalAdapter.sol";
import "../interfaces/IExceptions.sol";

/// @title Universal adapter
/// @notice Implements the initial version of universal adapter, which handles allowance revocations
Expand Down
48 changes: 17 additions & 31 deletions contracts/core/DataCompressor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {PERCENTAGE_FACTOR} from "@gearbox-protocol/core-v2/contracts/libraries/PercentageMath.sol";

import {IDataCompressor} from "@gearbox-protocol/core-v2/contracts/interfaces/IDataCompressor.sol";
import {ICreditManager} from "@gearbox-protocol/core-v2/contracts/interfaces/V1/ICreditManager.sol";
import {ICreditManager as ICreditManagerV1} from "@gearbox-protocol/core-v2/contracts/interfaces/V1/ICreditManager.sol";
import {ICreditManagerV2} from "../interfaces/ICreditManagerV2.sol";
import {ICreditFacade} from "../interfaces/ICreditFacade.sol";
import {ICreditFilter} from "@gearbox-protocol/core-v2/contracts/interfaces/V1/ICreditFilter.sol";
Expand All @@ -21,7 +21,7 @@ import {IVersion} from "@gearbox-protocol/core-v2/contracts/interfaces/IVersion.

import {AddressProvider} from "@gearbox-protocol/core-v2/contracts/core/AddressProvider.sol";
import {ContractsRegister} from "@gearbox-protocol/core-v2/contracts/core/ContractsRegister.sol";

import {ContractsRegisterTrait} from "../traits/ContractsRegisterTrait.sol";
import {
CreditAccountData,
CreditManagerData,
Expand All @@ -32,12 +32,12 @@ import {
} from "@gearbox-protocol/core-v2/contracts/libraries/Types.sol";

// EXCEPTIONS
import {ZeroAddressException} from "../interfaces/IErrors.sol";
import {ZeroAddressException} from "../interfaces/IExceptions.sol";

/// @title Data compressor
/// @notice Collects data from various contracts for use in the dApp
/// Do not use for data from any onchain activities
contract DataCompressor is IDataCompressor {
contract DataCompressor is IDataCompressor, ContractsRegisterTrait {
/// @dev Address of the AddressProvider
AddressProvider public immutable addressProvider;

Expand All @@ -50,21 +50,7 @@ contract DataCompressor is IDataCompressor {
// Contract version
uint256 public constant version = 3_00;

/// @dev Prevents function usage for target contracts that are not Gearbox pools
modifier targetIsRegisteredPool(address pool) {
if (!contractsRegister.isPool(pool)) revert NotPoolException(); // T:[WG-1]
_;
}

/// @dev Prevents function usage for target contracts that are not Gearbox Credit Managers
modifier targetIsRegisteredCreditManager(address creditManager) {
if (!contractsRegister.isCreditManager(creditManager)) {
revert NotCreditManagerException();
} // T:[WG-3]
_;
}

constructor(address _addressProvider) {
constructor(address _addressProvider) ContractsRegisterTrait(_addressProvider) {
if (_addressProvider == address(0)) revert ZeroAddressException();

addressProvider = AddressProvider(_addressProvider);
Expand Down Expand Up @@ -113,7 +99,7 @@ contract DataCompressor is IDataCompressor {
function hasOpenedCreditAccount(address _creditManager, address borrower)
public
view
targetIsRegisteredCreditManager(_creditManager)
registeredCreditManagerOnly(_creditManager)
returns (bool)
{
return _hasOpenedCreditAccount(_creditManager, borrower);
Expand All @@ -129,7 +115,7 @@ contract DataCompressor is IDataCompressor {
{
(
uint8 ver,
ICreditManager creditManager,
ICreditManagerV1 creditManager,
ICreditFilter creditFilter,
ICreditManagerV2 creditManagerV2,
ICreditFacade creditFacade,
Expand All @@ -150,15 +136,15 @@ contract DataCompressor is IDataCompressor {
result.totalValue = creditFilter.calcTotalValue(creditAccount);
result.healthFactor = creditFilter.calcCreditAccountHealthFactor(creditAccount);

try ICreditManager(creditManager).calcRepayAmount(borrower, false) returns (uint256 value) {
try ICreditManagerV1(creditManager).calcRepayAmount(borrower, false) returns (uint256 value) {
result.repayAmount = value;
} catch {}

try ICreditManager(creditManager).calcRepayAmount(borrower, true) returns (uint256 value) {
try ICreditManagerV1(creditManager).calcRepayAmount(borrower, true) returns (uint256 value) {
result.liquidationAmount = value;
} catch {}

try ICreditManager(creditManager)._calcClosePayments(creditAccount, result.totalValue, false) returns (
try ICreditManagerV1(creditManager)._calcClosePayments(creditAccount, result.totalValue, false) returns (
uint256, uint256, uint256 remainingFunds, uint256, uint256
) {
result.canBeClosed = remainingFunds > 0;
Expand Down Expand Up @@ -231,7 +217,7 @@ contract DataCompressor is IDataCompressor {
function getCreditManagerData(address _creditManager) public view returns (CreditManagerData memory result) {
(
uint8 ver,
ICreditManager creditManager,
ICreditManagerV1 creditManager,
ICreditFilter creditFilter,
ICreditManagerV2 creditManagerV2,
ICreditFacade creditFacade,
Expand Down Expand Up @@ -311,7 +297,7 @@ contract DataCompressor is IDataCompressor {

if (ver == 1) {
// VERSION 1 SPECIFIC FIELDS
result.maxLeverageFactor = ICreditManager(creditManager).maxLeverageFactor();
result.maxLeverageFactor = ICreditManagerV1(creditManager).maxLeverageFactor();
result.maxEnabledTokensLength = 255;
result.feeInterest = uint16(creditManager.feeInterest());
result.feeLiquidation = uint16(creditManager.feeLiquidation());
Expand All @@ -338,7 +324,7 @@ contract DataCompressor is IDataCompressor {

/// @dev Returns PoolData for a particular pool
/// @param _pool Pool address
function getPoolData(address _pool) public view targetIsRegisteredPool(_pool) returns (PoolData memory result) {
function getPoolData(address _pool) public view registeredPoolOnly(_pool) returns (PoolData memory result) {
IPoolService pool = IPoolService(_pool);
result.version = uint8(pool.version());

Expand Down Expand Up @@ -390,7 +376,7 @@ contract DataCompressor is IDataCompressor {
function getAdapter(address _creditManager, address _allowedContract)
external
view
targetIsRegisteredCreditManager(_creditManager)
registeredCreditManagerOnly(_creditManager)
returns (address adapter)
{
(uint8 ver,, ICreditFilter creditFilter, ICreditManagerV2 creditManagerV2,,) =
Expand All @@ -410,10 +396,10 @@ contract DataCompressor is IDataCompressor {
function getCreditContracts(address _creditManager)
internal
view
targetIsRegisteredCreditManager(_creditManager)
registeredCreditManagerOnly(_creditManager)
returns (
uint8 ver,
ICreditManager creditManager,
ICreditManagerV1 creditManager,
ICreditFilter creditFilter,
ICreditManagerV2 creditManagerV2,
ICreditFacade creditFacade,
Expand All @@ -422,7 +408,7 @@ contract DataCompressor is IDataCompressor {
{
ver = uint8(IVersion(_creditManager).version());
if (ver == 1) {
creditManager = ICreditManager(_creditManager);
creditManager = ICreditManagerV1(_creditManager);
creditFilter = ICreditFilter(creditManager.creditFilter());
} else {
creditManagerV2 = ICreditManagerV2(_creditManager);
Expand Down
35 changes: 14 additions & 21 deletions contracts/core/WETHGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";

import {AddressProvider} from "@gearbox-protocol/core-v2/contracts/core/AddressProvider.sol";
import {ContractsRegister} from "@gearbox-protocol/core-v2/contracts/core/ContractsRegister.sol";
import {ContractsRegisterTrait} from "../traits/ContractsRegisterTrait.sol";

import {IPoolService} from "@gearbox-protocol/core-v2/contracts/interfaces/IPoolService.sol";
import {IPool4626} from "../interfaces/IPool4626.sol";
Expand All @@ -19,39 +19,33 @@ import {IWETH} from "@gearbox-protocol/core-v2/contracts/interfaces/external/IWE
import {IWETHGateway} from "../interfaces/IWETHGateway.sol";
import {Errors} from "@gearbox-protocol/core-v2/contracts/libraries/Errors.sol";

import {
RegisteredPoolOnlyException,
ZeroAddressException,
WethPoolsOnlyException,
ReceiveIsNotAllowedException
} from "../interfaces/IExceptions.sol";

/// @title WETHGateway
/// @notice Used for converting ETH <> WETH
contract WETHGateway is IWETHGateway, ReentrancyGuard {
contract WETHGateway is IWETHGateway, ReentrancyGuard, ContractsRegisterTrait {
using SafeERC20 for IERC20;
using Address for address payable;

error RegisteredPoolsOnlyException();
error WethPoolsOnlyException();
error RegisteredCreditManagersOnly();
error ReceiveIsNotAllowedException();

address public immutable weth;
ContractsRegister internal immutable cr;

mapping(address => uint256) public override balanceOf;
mapping(address => uint256) public override(IWETHGateway) balanceOf;

// Contract version
uint256 public constant version = 3_00;

/// @dev Checks that the pool is registered and the underlying token is WETH
modifier wethPoolOnly(address pool) {
if (!cr.isPool(pool)) revert RegisteredPoolsOnlyException(); // T:[WG-1]
if (!isRegisteredPool(pool)) revert RegisteredPoolOnlyException(); // T:[WG-1]
if (IPoolService(pool).underlyingToken() != weth) revert WethPoolsOnlyException(); // T:[WG-2]
_;
}

/// @dev Checks that credit manager is registered
modifier creditManagerOnly() {
if (!cr.isCreditManager(msg.sender)) revert RegisteredCreditManagersOnly(); // T:[WG-3]

_;
}

/// @dev Measures WETH balance before and after function call and transfers
/// difference to providced address
modifier unwrapAndTransferWethTo(address to) {
Expand All @@ -72,10 +66,9 @@ contract WETHGateway is IWETHGateway, ReentrancyGuard {

/// @dev Constructor
/// @param addressProvider Address Repository for upgradable contract model
constructor(address addressProvider) {
require(addressProvider != address(0), Errors.ZERO_ADDRESS_IS_NOT_ALLOWED);
constructor(address addressProvider) ContractsRegisterTrait(addressProvider) {
if (addressProvider == address(0)) revert ZeroAddressException();
weth = AddressProvider(addressProvider).getWethToken();
cr = ContractsRegister(AddressProvider(addressProvider).getContractsRegister());
}

/// FOR POOLS V3
Expand Down Expand Up @@ -142,7 +135,7 @@ contract WETHGateway is IWETHGateway, ReentrancyGuard {

// CREDIT MANAGERS

function depositFor(address to, uint256 amount) external override creditManagerOnly {
function depositFor(address to, uint256 amount) external override registeredCreditManagerOnly(msg.sender) {
balanceOf[to] += amount;
}

Expand Down
18 changes: 5 additions & 13 deletions contracts/credit/CreditConfigurator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
import {PERCENTAGE_FACTOR} from "@gearbox-protocol/core-v2/contracts/libraries/PercentageMath.sol";

// CONTRACTS
import {ACLNonReentrantTrait} from "../core/ACLNonReentrantTrait.sol";
import {ACLNonReentrantTrait} from "../traits/ACLNonReentrantTrait.sol";
import {CreditFacade} from "./CreditFacade.sol";
import {CreditManager} from "./CreditManager.sol";

Expand All @@ -34,14 +34,8 @@ import {IAddressProvider} from "@gearbox-protocol/core-v2/contracts/interfaces/I
import {IPoolQuotaKeeper} from "../interfaces/IPoolQuotaKeeper.sol";

// EXCEPTIONS
import {
AddressIsNotContractException,
IncorrectPriceFeedException,
IncorrectTokenContractException,
CallerNotPausableAdminException,
TokenNotAllowedException
} from "../interfaces/IErrors.sol";
import {ICreditManagerV2, ICreditManagerV2Exceptions} from "../interfaces/ICreditManagerV2.sol";
import "../interfaces/IExceptions.sol";
import {ICreditManagerV2} from "../interfaces/ICreditManagerV2.sol";

/// @title CreditConfigurator
/// @notice This contract is used to configure CreditManagers and is the only one with the priviledge
Expand Down Expand Up @@ -240,7 +234,7 @@ contract CreditConfigurator is ICreditConfigurator, ACLNonReentrantTrait {
creditManager.rampLiquidationThreshold(token, liquidationThresholdFinal, timestampRampStart, rampDuration);
emit TokenLiquidationThresholdRampScheduled(
token, currentLT, liquidationThresholdFinal, timestampRampStart, timestampRampStart + rampDuration
);
);
}
}

Expand Down Expand Up @@ -541,7 +535,7 @@ contract CreditConfigurator is ICreditConfigurator, ACLNonReentrantTrait {
PERCENTAGE_FACTOR - _liquidationDiscount,
_feeLiquidationExpired,
PERCENTAGE_FACTOR - _liquidationDiscountExpired
); // FT:[CC-1A,26]
); // FT:[CC-1A,26]
}
}

Expand Down Expand Up @@ -791,9 +785,7 @@ contract CreditConfigurator is ICreditConfigurator, ACLNonReentrantTrait {
_setBotList(botList);
}


function _setBotList(address botList) internal nonZeroAddress(botList) {

address currentBotList = creditFacade().botList();

if (botList != currentBotList) {
Expand Down
9 changes: 6 additions & 3 deletions contracts/credit/CreditFacade.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ pragma solidity ^0.8.10;
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {Address} from "@openzeppelin/contracts/utils/Address.sol";
import {EnumerableSet} from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";

import {ACLNonReentrantTrait} from "../traits/ACLNonReentrantTrait.sol";
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";
Expand All @@ -28,10 +29,12 @@ import {IBlacklistHelper} from "../interfaces/IBlacklistHelper.sol";
import {IBotList} from "../interfaces/IBotList.sol";

// CONSTANTS

import {LEVERAGE_DECIMALS} from "@gearbox-protocol/core-v2/contracts/libraries/Constants.sol";
import {PERCENTAGE_FACTOR} from "@gearbox-protocol/core-v2/contracts/libraries/PercentageMath.sol";

// EXCEPTIONS
import "../interfaces/IExceptions.sol";

struct Params {
/// @dev Maximal amount of new debt that can be taken per block
uint128 maxBorrowedAmountPerBlock;
Expand Down Expand Up @@ -118,7 +121,7 @@ contract CreditFacade is ICreditFacade, ACLNonReentrantTrait {
/// @dev Restricts actions for users with opened credit accounts only
modifier creditConfiguratorOnly() {
if (msg.sender != creditManager.creditConfigurator()) {
revert CreditConfiguratorOnlyException();
revert CallerNotConfiguratorException();
}

_;
Expand Down
Loading

0 comments on commit 27fdfec

Please sign in to comment.