Skip to content

Commit

Permalink
Merge branch '4626' of https://github.com/Gearbox-protocol/core-v2 in…
Browse files Browse the repository at this point in the history
…to 4626
  • Loading branch information
Van0k committed Feb 13, 2023
2 parents 4443f3d + 0d8d97d commit 54e4512
Show file tree
Hide file tree
Showing 41 changed files with 2,497 additions and 393 deletions.
7 changes: 7 additions & 0 deletions .env.fork
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

REACT_APP_BACKEND_ADDR=http://localhost:8000
REACT_APP_ADDRESS_PROVIDER=0xcF64698AFF7E5f27A11dff868AF228653ba53be0
REACT_APP_CHAIN_ID=1337
REACT_APP_PATHFINDER=0x67C9a1B633e47172Fa609DCAebafec3C72d09f7E
REACT_APP_TOKEN_DISTRIBUTOR=0xBF57539473913685688d224ad4E262684B23dD4c
REACT_APP_ACCOUNT_MINER=0x7B1AAF21AC0D420666B5966338FF9aEe763C29DF
4 changes: 4 additions & 0 deletions .env.goerli
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
REACT_APP_ADDRESS_PROVIDER=0x95f4cea53121b8A2Cb783C6BFB0915cEc44827D3
REACT_APP_CHAIN_ID=5
REACT_APP_DEGEN_NFT=0xc4cA5B61e58cDAa3cc283906b65aeFc8A80EA04A
REACT_APP_DEGEN_DISTRIBUTOR=0x75f74B4A665BFcc78df0Ff82c2eB677E610B7313
7 changes: 7 additions & 0 deletions .env.mainnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

REACT_APP_BACKEND_ADDR=http://localhost:8000
REACT_APP_ADDRESS_PROVIDER=0xcF64698AFF7E5f27A11dff868AF228653ba53be0
REACT_APP_CHAIN_ID=1
REACT_APP_PATHFINDER=0xBC0DE81339Da70e41897FB377b4D5C33A304f44f
REACT_APP_TOKEN_DISTRIBUTOR=0xBF57539473913685688d224ad4E262684B23dD4c
REACT_APP_ACCOUNT_MINER=0x7B1AAF21AC0D420666B5966338FF9aEe763C29DF
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[submodule "lib/forge-std"]
path = lib/forge-std
url = https://github.com/foundry-rs/forge-std
branch = v1.3.0
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[solidity]": {
"editor.defaultFormatter": "esbenp.forge"
"editor.defaultFormatter": "NomicFoundation.hardhat-solidity"
},

"editor.defaultFormatter": "esbenp.prettier-vscode",
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ This directory contains code used for third-party integration. Since this reposi
This directory contains protocol contracts related to access, contract discovery, data reporting, etc.

1. `AccountFactory.sol` is used to deploy Credit Accounts and managed the existing Credit Account queue. Credit Managers take accounts from the factory when a new account in Gearbox is opened and return them after the account is closed.
2. `ACL.sol` is the main access control contract in the system. Contracts that inherit `ACLTrait.sol` use `ACL.sol` to determine access to configurator-only functions.
2. `ACL.sol` is the main access control contract in the system. Contracts that inherit `ACLNonReentrantTrait.sol` use `ACL.sol` to determine access to configurator-only functions.
3. `AddressProvider.sol` is used by other contracts in the system to determine the up-to-date addresses of core contracts, such as `ACL`, `PriceOracle`, `GearToken`, etc.
4. `ContractsRegister.sol` contains a list of legitimate Gearbox Credit Managers and pools connected to the system.
5. `DataCompressor.sol` is used to retrieve detailed data on particular Credit Managers and Credit Accounts.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,39 @@ import { ZeroAddressException, CallerNotConfiguratorException, CallerNotPausable

/// @title ACL Trait
/// @notice Utility class for ACL consumers
abstract contract ACLTrait is Pausable {
abstract contract ACLNonReentrantTrait is Pausable {
uint8 private constant _NOT_ENTERED = 1;
uint8 private constant _ENTERED = 2;

// ACL contract to check rights
IACL public immutable _acl;

address public controller;
bool public externalController;

uint8 private _status = _NOT_ENTERED;

/**
* @dev Prevents a contract from calling itself, directly or indirectly.
* Calling a `nonReentrant` function from another `nonReentrant`
* function is not supported. It is possible to prevent this from happening
* by making the `nonReentrant` function external, and making it call a
* `private` function that does the actual work.
*/
modifier nonReentrant() {
// On the first call to nonReentrant, _notEntered will be true
require(_status != _ENTERED, "ReentrancyGuard: reentrant call");

// Any calls to nonReentrant after this point will fail
_status = _ENTERED;

_;

// By storing the original value once again, a refund is triggered (see
// https://eips.ethereum.org/EIPS/eip-2200)
_status = _NOT_ENTERED;
}

event NewController(address indexed newController);

/// @dev constructor
Expand Down
6 changes: 3 additions & 3 deletions contracts/core/AccountFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import { ICreditAccount } from "../interfaces/ICreditAccount.sol";
import { AddressProvider } from "./AddressProvider.sol";
import { ContractsRegister } from "./ContractsRegister.sol";
import { CreditAccount } from "../credit/CreditAccount.sol";
import { ACLTrait } from "./ACLTrait.sol";
import { ACLNonReentrantTrait } from "./ACLNonReentrantTrait.sol";
import { ReentrancyGuard } from "@openzeppelin/contracts/security/ReentrancyGuard.sol";

import { Errors } from "../libraries/Errors.sol";

/// @title Abstract reusable credit accounts factory
/// @notice Creates, holds & lends credit accounts to Credit Managers
contract AccountFactory is IAccountFactory, ACLTrait, ReentrancyGuard {
contract AccountFactory is IAccountFactory, ACLNonReentrantTrait {
using EnumerableSet for EnumerableSet.AddressSet;

//
Expand Down Expand Up @@ -77,7 +77,7 @@ contract AccountFactory is IAccountFactory, ACLTrait, ReentrancyGuard {
*
* @param addressProvider Address of address repository
*/
constructor(address addressProvider) ACLTrait(addressProvider) {
constructor(address addressProvider) ACLNonReentrantTrait(addressProvider) {
require(
addressProvider != address(0),
Errors.ZERO_ADDRESS_IS_NOT_ALLOWED
Expand Down
8 changes: 5 additions & 3 deletions contracts/core/ContractsRegister.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ pragma solidity ^0.8.10;

import { IContractsRegister } from "../interfaces/IContractsRegister.sol";
import { Errors } from "../libraries/Errors.sol";
import { ACLTrait } from "./ACLTrait.sol";
import { ACLNonReentrantTrait } from "./ACLNonReentrantTrait.sol";

/// @title Pool & Credit Manager registry
/// @notice Stores addresses of Pools and Credit Managers
contract ContractsRegister is IContractsRegister, ACLTrait {
contract ContractsRegister is IContractsRegister, ACLNonReentrantTrait {
/// @dev List of all registered pools
address[] public override pools;

Expand All @@ -25,7 +25,9 @@ contract ContractsRegister is IContractsRegister, ACLTrait {
/// @dev Contract version
uint256 public constant version = 1;

constructor(address addressProvider) ACLTrait(addressProvider) {}
constructor(address addressProvider)
ACLNonReentrantTrait(addressProvider)
{}

/// @dev Adds a pool to the list
/// @param newPoolAddress Address of the new pool
Expand Down
2 changes: 1 addition & 1 deletion contracts/core/DataCompressor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { ZeroAddressException } from "../interfaces/IErrors.sol";

/// @title Data compressor
/// @notice Collects data from various contracts for use in the dApp
/// Do not use for data from data compressor for state-changing functions
/// Do not use for data from any onchain activities
contract DataCompressor is IDataCompressor {
using PercentageMath for uint256;

Expand Down
21 changes: 8 additions & 13 deletions contracts/credit/CreditConfigurator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { WAD } from "../libraries/Constants.sol";
import { PERCENTAGE_FACTOR } from "../libraries/PercentageMath.sol";

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

Expand All @@ -24,7 +24,7 @@ import { IPoolService } from "../interfaces/IPoolService.sol";
import { IAddressProvider } from "../interfaces/IAddressProvider.sol";

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

/// @title CreditConfigurator
Expand All @@ -33,7 +33,7 @@ import { ICreditManagerV2, ICreditManagerV2Exceptions } from "../interfaces/ICre
/// @dev All functions can only by called by he Configurator as per ACL.
/// CreditManager blindly executes all requests from CreditConfigurator, so all sanity checks
/// are performed here.
contract CreditConfigurator is ICreditConfigurator, ACLTrait {
contract CreditConfigurator is ICreditConfigurator, ACLNonReentrantTrait {
using EnumerableSet for EnumerableSet.AddressSet;
using Address for address;

Expand Down Expand Up @@ -69,7 +69,7 @@ contract CreditConfigurator is ICreditConfigurator, ACLTrait {
CreditFacade _creditFacade,
CreditManagerOpts memory opts
)
ACLTrait(
ACLNonReentrantTrait(
address(
IPoolService(_creditManager.poolService()).addressProvider()
)
Expand Down Expand Up @@ -694,11 +694,9 @@ contract CreditConfigurator is ICreditConfigurator, ACLTrait {
/// In Credit Facade (and, consequently, the Credit Manager)
/// @param _mode Prohibits borrowing if true, and allows borrowing otherwise
function setIncreaseDebtForbidden(bool _mode) external {
if (_mode && !_acl.isPausableAdmin(msg.sender))
if (!_acl.isPausableAdmin(msg.sender)) {
revert CallerNotPausableAdminException();
else if (!_mode && !_acl.isUnpausableAdmin(msg.sender))
revert CallerNotUnPausableAdminException();

}
_setIncreaseDebtForbidden(_mode);
}

Expand Down Expand Up @@ -743,10 +741,7 @@ contract CreditConfigurator is ICreditConfigurator, ACLTrait {
/// @notice Upgradeable contracts are contracts with an upgradeable proxy
/// Or other practices and patterns potentially detrimental to security
/// Contracts from the list have certain restrictions applied to them
function addContractToUpgradeable(address addr)
external
controllerOnly // F:[CC-2] == TODO: Add contoller test
{
function addContractToUpgradeable(address addr) external configuratorOnly {
_addContractToUpgradeable(addr); // F: [CC-35]
}

Expand Down Expand Up @@ -798,7 +793,7 @@ contract CreditConfigurator is ICreditConfigurator, ACLTrait {
/// @notice See more at https://dev.gearbox.fi/docs/documentation/credit/liquidation#liquidating-accounts-by-expiration
function setExpirationDate(uint40 newExpirationDate)
external
controllerOnly // F:[CC-2] == TODO: Add contoller test
configuratorOnly // F: [CC-38]
{
_setExpirationDate(newExpirationDate); // F: [CC-34]
}
Expand Down
18 changes: 3 additions & 15 deletions contracts/credit/CreditManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import { SafeCast } from "@openzeppelin/contracts/utils/math/SafeCast.sol";

import { ACLTrait } from "../core/ACLTrait.sol";
import { ACLNonReentrantTrait } from "../core/ACLNonReentrantTrait.sol";

// INTERFACES
import { IAccountFactory } from "../interfaces/IAccountFactory.sol";
Expand Down Expand Up @@ -52,7 +52,7 @@ struct Slot1 {
/// @notice Encapsulates the business logic for managing Credit Accounts
///
/// More info: https://dev.gearbox.fi/developers/credit/credit_manager
contract CreditManager is ICreditManagerV2, ACLTrait {
contract CreditManager is ICreditManagerV2, ACLNonReentrantTrait {
using SafeERC20 for IERC20;
using Address for address payable;
using SafeCast for uint256;
Expand Down Expand Up @@ -149,18 +149,6 @@ contract CreditManager is ICreditManagerV2, ACLTrait {
// MODIFIERS
//

/// @dev Protects against reentrancy.
/// @notice Custom ReentrancyGuard implementation is used to optimize storage reads.
modifier nonReentrant() {
if (entered) {
revert ReentrancyLockException();
}

entered = true;
_;
entered = false;
}

/// @dev Restricts calls to Credit Facade or allowed adapters
modifier adaptersOrCreditFacadeOnly() {
if (
Expand Down Expand Up @@ -194,7 +182,7 @@ contract CreditManager is ICreditManagerV2, ACLTrait {
/// @dev Constructor
/// @param _pool Address of the pool to borrow funds from
constructor(address _pool)
ACLTrait(address(IPoolService(_pool).addressProvider()))
ACLNonReentrantTrait(address(IPoolService(_pool).addressProvider()))
{
IAddressProvider addressProvider = IPoolService(_pool)
.addressProvider();
Expand Down
7 changes: 6 additions & 1 deletion contracts/factories/PoolFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ struct PoolOpts {
address addressProvider; // address of addressProvider contract
address underlying; // address of underlying token for pool and creditManager
uint256 U_optimal; // linear interest model parameter
uint256 U_reserve; // linear interest model parameter
uint256 R_base; // linear interest model parameter
uint256 R_slope1; // linear interest model parameter
uint256 R_slope2; // linear interest model parameter
uint256 R_slope3; // linear interest model parameter
uint256 expectedLiquidityLimit; // linear interest model parameter
uint256 withdrawFee; // withdrawFee
}
Expand All @@ -30,9 +32,12 @@ contract PoolFactory is ContractUpgrader {
constructor(PoolOpts memory opts) ContractUpgrader(opts.addressProvider) {
LinearInterestRateModel linearModel = new LinearInterestRateModel(
opts.U_optimal,
opts.U_reserve,
opts.R_base,
opts.R_slope1,
opts.R_slope2
opts.R_slope2,
opts.R_slope3,
false
); // T:[PD-1]

pool = new PoolService(
Expand Down
26 changes: 26 additions & 0 deletions contracts/interfaces/IGauge.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// SPDX-License-Identifier: MIT
// Gearbox Protocol. Generalized leverage for DeFi protocols
// (c) Gearbox Holdings, 2022
pragma solidity ^0.8.10;

struct GaugeOpts {
address addressProvider;
address pool;
uint256 firstEpochTimestamp;
}

interface IGaugeExceptions {
error TokenQuotaIsAlreadyAdded();
}

interface IGaugeEvents {}

/// @title IGauge

interface IGauge is IGaugeEvents, IGaugeExceptions {
/// @dev Returns quota rate in PERCENTAGE FORMAT
function getQuotaRate(address) external view returns (uint256);

/// @dev Returns cumulative index in RAY for particular token. If token is not
function cumulativeIndex(address token) external view returns (uint256);
}
22 changes: 21 additions & 1 deletion contracts/interfaces/IInterestRateModel.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,32 @@ pragma solidity ^0.8.10;

import { IVersion } from "./IVersion.sol";

interface IInterestRateModel is IVersion {
interface IInterestRateModelExceptions {
error IncorrectParameterException();
error BorrowingMoreOptimalForbiddenException();
}

interface IInterestRateModel is IInterestRateModelExceptions, IVersion {
/// @dev Returns the borrow rate calculated based on expectedLiquidity and availableLiquidity
/// @param expectedLiquidity Expected liquidity in the pool
/// @param availableLiquidity Available liquidity in the pool
/// @notice In RAY format
function calcBorrowRate(
uint256 expectedLiquidity,
uint256 availableLiquidity
) external view returns (uint256);

/// @dev Returns the borrow rate calculated based on expectedLiquidity and availableLiquidity
/// @param expectedLiquidity Expected liquidity in the pool
/// @param availableLiquidity Available liquidity in the pool
/// @notice In RAY format
function calcBorrowRate(
uint256 expectedLiquidity,
uint256 availableLiquidity,
bool checkOptimalBorrowing
) external view returns (uint256);

function availableToBorrow(
uint256 expectedLiquidity,
uint256 availableLiquidity
) external view returns (uint256);
Expand Down
Loading

0 comments on commit 54e4512

Please sign in to comment.