Skip to content

Commit

Permalink
fix: quotas moved to IQoutaKeeper
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmikko committed Feb 16, 2023
1 parent 64bf20b commit 4286d45
Show file tree
Hide file tree
Showing 9 changed files with 1,726 additions and 612 deletions.
506 changes: 382 additions & 124 deletions contracts/credit/CreditFacade.sol

Large diffs are not rendered by default.

521 changes: 380 additions & 141 deletions contracts/credit/CreditManager.sol

Large diffs are not rendered by default.

724 changes: 530 additions & 194 deletions contracts/credit/CreditManager_V.sol

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions contracts/interfaces/ICreditFacade.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pragma solidity ^0.8.10;
import { Balance } from "../libraries/Balances.sol";
import { MultiCall } from "../libraries/MultiCall.sol";
import { ICreditManagerV2, ICreditManagerV2Exceptions } from "./ICreditManagerV2.sol";
import { QuotaUpdate } from "./IPoolQuotaKeeper.sol";
import { IVersion } from "./IVersion.sol";

interface ICreditFacadeExtended {
Expand Down Expand Up @@ -53,6 +54,12 @@ interface ICreditFacadeExtended {
///
/// @param amount Amount to increase borrowed amount
function decreaseDebt(uint256 amount) external;

// TODO: Add description
function updateQuota(address token, int96 quotaChange) external;

/// TODO: add description
function updateQuotas(QuotaUpdate[] memory quotaUpdates) external;
}

interface ICreditFacadeEvents {
Expand Down
108 changes: 86 additions & 22 deletions contracts/interfaces/ICreditManagerV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
// (c) Gearbox Holdings, 2022
pragma solidity ^0.8.10;

import {IPriceOracleV2} from "./IPriceOracle.sol";
import {IVersion} from "./IVersion.sol";
import { IPriceOracleV2 } from "./IPriceOracle.sol";
import { QuotaUpdate } from "./IPoolQuotaKeeper.sol";
import { IVersion } from "./IVersion.sol";

enum ClosureAction {
CLOSE_ACCOUNT,
Expand Down Expand Up @@ -74,7 +75,11 @@ interface ICreditManagerV2Exceptions {
/// @notice All Credit Manager functions are access-restricted and can only be called
/// by the Credit Facade or allowed adapters. Users are not allowed to
/// interact with the Credit Manager directly
interface ICreditManagerV2 is ICreditManagerV2Events, ICreditManagerV2Exceptions, IVersion {
interface ICreditManagerV2 is
ICreditManagerV2Events,
ICreditManagerV2Exceptions,
IVersion
{
//
// CREDIT ACCOUNT MANAGEMENT
//
Expand All @@ -85,7 +90,9 @@ interface ICreditManagerV2 is ICreditManagerV2Events, ICreditManagerV2Exceptions
///
/// @param borrowedAmount Amount to be borrowed by the Credit Account
/// @param onBehalfOf The owner of the newly opened Credit Account
function openCreditAccount(uint256 borrowedAmount, address onBehalfOf) external returns (address);
function openCreditAccount(uint256 borrowedAmount, address onBehalfOf)
external
returns (address);

/// @dev Closes a Credit Account - covers both normal closure and liquidation
/// - Checks whether the contract is paused, and, if so, if the payer is an emergency liquidator.
Expand Down Expand Up @@ -139,16 +146,23 @@ interface ICreditManagerV2 is ICreditManagerV2Events, ICreditManagerV2Exceptions
/// @param amount Amount to increase / decrease the principal by
/// @param increase True to increase principal, false to decrease
/// @return newBorrowedAmount The new debt principal
function manageDebt(address creditAccount, uint256 amount, bool increase)
external
returns (uint256 newBorrowedAmount);
function manageDebt(
address creditAccount,
uint256 amount,
bool increase
) external returns (uint256 newBorrowedAmount);

/// @dev Adds collateral to borrower's credit account
/// @param payer Address of the account which will be charged to provide additional collateral
/// @param creditAccount Address of the Credit Account
/// @param token Collateral token to add
/// @param amount Amount to add
function addCollateral(address payer, address creditAccount, address token, uint256 amount) external;
function addCollateral(
address payer,
address creditAccount,
address token,
uint256 amount
) external;

/// @dev Transfers Credit Account ownership to another address
/// @param from Address of previous owner
Expand All @@ -160,16 +174,23 @@ interface ICreditManagerV2 is ICreditManagerV2Events, ICreditManagerV2Exceptions
/// @param targetContract Spender to change allowance for
/// @param token Collateral token to approve
/// @param amount New allowance amount
function approveCreditAccount(address borrower, address targetContract, address token, uint256 amount) external;
function approveCreditAccount(
address borrower,
address targetContract,
address token,
uint256 amount
) external;

/// @dev Requests a Credit Account to make a low-level call with provided data
/// This is the intended pathway for state-changing interactions with 3rd-party protocols
/// @param borrower Borrower's address
/// @param targetContract Contract to be called
/// @param data Data to pass with the call
function executeOrder(address borrower, address targetContract, bytes memory data)
external
returns (bytes memory);
function executeOrder(
address borrower,
address targetContract,
bytes memory data
) external returns (bytes memory);

//
// COLLATERAL VALIDITY AND ACCOUNT HEALTH CHECKS
Expand Down Expand Up @@ -219,15 +240,29 @@ interface ICreditManagerV2 is ICreditManagerV2Events, ICreditManagerV2Exceptions
/// but can also be called separately from the Credit Facade to remove
/// unwanted tokens
/// @return True if token mask was change otherwise False
function disableToken(address creditAccount, address token) external returns (bool);
function disableToken(address creditAccount, address token)
external
returns (bool);

/// Quota mgmt
/// @dev Updates quota for particular token, returns how much quota was given
/// @param token Token address of quoted token
/// @param quotaChange Change in quota amount
function updateQuota(address token, int96 quotaChange) external;

/// TODO: add description
function updateQuotas(QuotaUpdate[] memory quotaUpdates) external;

//
// GETTERS
//

/// @dev Returns the address of a borrower's Credit Account, or reverts if there is none.
/// @param borrower Borrower's address
function getCreditAccountOrRevert(address borrower) external view returns (address);
function getCreditAccountOrRevert(address borrower)
external
view
returns (address);

/// @dev Computes amounts that must be sent to various addresses before closing an account
/// @param totalValue Credit Accounts total value in underlying
Expand All @@ -247,7 +282,15 @@ interface ICreditManagerV2 is ICreditManagerV2Events, ICreditManagerV2Exceptions
ClosureAction closureActionType,
uint256 borrowedAmount,
uint256 borrowedAmountWithInterest
) external view returns (uint256 amountToPool, uint256 remainingFunds, uint256 profit, uint256 loss);
)
external
view
returns (
uint256 amountToPool,
uint256 remainingFunds,
uint256 profit,
uint256 loss
);

/// @dev Calculates the debt accrued by a Credit Account
/// @param creditAccount Address of the Credit Account
Expand All @@ -257,21 +300,34 @@ interface ICreditManagerV2 is ICreditManagerV2Events, ICreditManagerV2Exceptions
function calcCreditAccountAccruedInterest(address creditAccount)
external
view
returns (uint256 borrowedAmount, uint256 borrowedAmountWithInterest, uint256 borrowedAmountWithInterestAndFees);
returns (
uint256 borrowedAmount,
uint256 borrowedAmountWithInterest,
uint256 borrowedAmountWithInterestAndFees
);

/// @dev Maps Credit Accounts to bit masks encoding their enabled token sets
/// Only enabled tokens are counted as collateral for the Credit Account
/// @notice An enabled token mask encodes an enabled token by setting
/// the bit at the position equal to token's index to 1
function enabledTokensMap(address creditAccount) external view returns (uint256);
function enabledTokensMap(address creditAccount)
external
view
returns (uint256);

/// @dev Maps the Credit Account to its current percentage drop across all swaps since
/// the last full check, in RAY format
function cumulativeDropAtFastCheckRAY(address creditAccount) external view returns (uint256);
function cumulativeDropAtFastCheckRAY(address creditAccount)
external
view
returns (uint256);

/// @dev Returns the collateral token at requested index and its liquidation threshold
/// @param id The index of token to return
function collateralTokens(uint256 id) external view returns (address token, uint16 liquidationThreshold);
function collateralTokens(uint256 id)
external
view
returns (address token, uint16 liquidationThreshold);

/// @dev Returns the collateral token with requested mask and its liquidationThreshold
/// @param tokenMask Token mask corresponding to the token
Expand All @@ -294,7 +350,10 @@ interface ICreditManagerV2 is ICreditManagerV2Events, ICreditManagerV2Exceptions
function adapterToContract(address adapter) external view returns (address);

/// @dev Maps 3rd party contracts to their respective adapters
function contractToAdapter(address targetContract) external view returns (address);
function contractToAdapter(address targetContract)
external
view
returns (address);

/// @dev Address of the underlying asset
function underlying() external view returns (address);
Expand All @@ -317,7 +376,10 @@ interface ICreditManagerV2 is ICreditManagerV2Events, ICreditManagerV2Exceptions

/// @dev Returns the liquidation threshold for the provided token
/// @param token Token to retrieve the LT for
function liquidationThresholds(address token) external view returns (uint16);
function liquidationThresholds(address token)
external
view
returns (uint16);

/// @dev The maximal number of enabled tokens on a single Credit Account
function maxAllowedEnabledTokenLength() external view returns (uint8);
Expand Down Expand Up @@ -364,5 +426,7 @@ interface ICreditManagerV2 is ICreditManagerV2Events, ICreditManagerV2Exceptions
function version() external view returns (uint256);

/// @dev Paused() state
function checkEmergencyPausable(address caller, bool state) external returns (bool);
function checkEmergencyPausable(address caller, bool state)
external
returns (bool);
}
72 changes: 57 additions & 15 deletions contracts/interfaces/IPool4626.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// (c) Gearbox Holdings, 2022
pragma solidity ^0.8.10;

import {IERC4626} from "@openzeppelin/contracts/interfaces/IERC4626.sol";
import {IVersion} from "./IVersion.sol";
import { IERC4626 } from "@openzeppelin/contracts/interfaces/IERC4626.sol";
import { IVersion } from "./IVersion.sol";

struct Pool4626Opts {
address addressProvider;
Expand All @@ -29,13 +29,27 @@ interface IPool4626Exceptions {

interface IPool4626Events {
/// @dev Emits on new liquidity being added to the pool
event DepositReferral(address indexed sender, address indexed onBehalfOf, uint256 amount, uint16 referralCode);
event DepositReferral(
address indexed sender,
address indexed onBehalfOf,
uint256 amount,
uint16 referralCode
);

/// @dev Emits on a Credit Manager borrowing funds for a Credit Account
event Borrow(address indexed creditManager, address indexed creditAccount, uint256 amount);
event Borrow(
address indexed creditManager,
address indexed creditAccount,
uint256 amount
);

/// @dev Emits on repayment of a Credit Account's debt
event Repay(address indexed creditManager, uint256 borrowedAmount, uint256 profit, uint256 loss);
event Repay(
address indexed creditManager,
uint256 borrowedAmount,
uint256 profit,
uint256 loss
);

/// @dev Emits on updating the interest rate model
event NewInterestRateModel(address indexed newInterestRateModel);
Expand Down Expand Up @@ -64,14 +78,34 @@ interface IPool4626Events {
/// - Managing diesel tokens & diesel rates
/// - Taking/repaying Credit Manager debt
/// More: https://dev.gearbox.fi/developers/pool/abstractpoolservice
interface IPool4626 is IPool4626Events, IPool4626Exceptions, IERC4626, IVersion {
function depositReferral(uint256 assets, address receiver, uint16 referralCode) external returns (uint256 shares);

function depositETHReferral(address receiver, uint16 referralCode) external payable returns (uint256 shares);

function withdrawETH(uint256 assets, address receiver, address owner) external returns (uint256 shares);

function redeemETH(uint256 shares, address receiver, address owner) external returns (uint256 assets);
interface IPool4626 is
IPool4626Events,
IPool4626Exceptions,
IERC4626,
IVersion
{
function depositReferral(
uint256 assets,
address receiver,
uint16 referralCode
) external returns (uint256 shares);

function depositETHReferral(address receiver, uint16 referralCode)
external
payable
returns (uint256 shares);

function withdrawETH(
uint256 assets,
address receiver,
address owner
) external returns (uint256 shares);

function redeemETH(
uint256 shares,
address receiver,
address owner
) external returns (uint256 assets);

function burn(uint256 shares) external;

Expand All @@ -80,15 +114,20 @@ interface IPool4626 is IPool4626Events, IPool4626Exceptions, IERC4626, IVersion
/// @dev Lends pool funds to a Credit Account
/// @param borrowedAmount Credit Account's debt principal
/// @param creditAccount Credit Account's address
function lendCreditAccount(uint256 borrowedAmount, address creditAccount) external;
function lendCreditAccount(uint256 borrowedAmount, address creditAccount)
external;

/// @dev Repays the Credit Account's debt
/// @param borrowedAmount Amount of principal ro repay
/// @param profit The treasury profit from repayment
/// @param loss Amount of underlying that the CA wan't able to repay
/// @notice Assumes that the underlying (including principal + interest + fees)
/// was already transferred
function repayCreditAccount(uint256 borrowedAmount, uint256 profit, uint256 loss) external;
function repayCreditAccount(
uint256 borrowedAmount,
uint256 profit,
uint256 loss
) external;

/// @dev Updates quota index
function updateQuotas(uint128 _quotaIndex) external;
Expand Down Expand Up @@ -133,6 +172,9 @@ interface IPool4626 is IPool4626Events, IPool4626Exceptions, IERC4626, IVersion
/// @dev Whether the pool supports quota premiums
function supportQuotaPremiums() external view returns (bool);

/// @dev PoolQuotaKeeper address
function poolQuotaKeeper() external view returns (address);

/// @dev Withdrawal fee
function withdrawFee() external view returns (uint16);

Expand Down
13 changes: 8 additions & 5 deletions contracts/interfaces/IPoolQuotaKeeper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// (c) Gearbox Holdings, 2022
pragma solidity ^0.8.10;

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

struct QuotaUpdate {
address token;
Expand All @@ -23,13 +23,16 @@ interface IPoolQuotaKeeperEvents {
}

/// @title Pool Quotas Interface
interface IPoolQuotaKeeper is IPoolQuotaKeeperEvents, IPoolQuotaKeeperExceptions, IVersion {
interface IPoolQuotaKeeper is
IPoolQuotaKeeperEvents,
IPoolQuotaKeeperExceptions,
IVersion
{
/// @dev Updates quota for particular token, returns how much quota was given
/// @param token Token address of quoted token
/// @param quotaChange Change in quota amount
/// @return change Quota change which was applied
function updateQuota(address token, int96 quotaChange) external returns (int96 change);
function updateQuota(address token, int96 quotaChange) external;

/// TODO: add description
function updateQuotas(QuotaUpdate[] memory quotaUpdates) external returns (int96[] memory changes);
function updateQuotas(QuotaUpdate[] memory quotaUpdates) external;
}
Loading

0 comments on commit 4286d45

Please sign in to comment.