diff --git a/src/Blue.sol b/src/Blue.sol index 85e4eacea..0affcc7a0 100644 --- a/src/Blue.sol +++ b/src/Blue.sol @@ -156,150 +156,150 @@ contract Blue is IBlue { /* SUPPLY MANAGEMENT */ /// @inheritdoc IBlue - function supply(Market memory market, uint256 amount, uint256 shares, address onBehalf, bytes calldata data) + function supply(Market memory market, uint256 assets, uint256 shares, address onBehalf, bytes calldata data) external { Id id = market.id(); require(lastUpdate[id] != 0, ErrorsLib.MARKET_NOT_CREATED); - require(UtilsLib.exactlyOneZero(amount, shares), ErrorsLib.INCONSISTENT_INPUT); + require(UtilsLib.exactlyOneZero(assets, shares), ErrorsLib.INCONSISTENT_INPUT); require(onBehalf != address(0), ErrorsLib.ZERO_ADDRESS); _accrueInterests(market, id); - if (amount > 0) shares = amount.toSharesDown(totalSupply[id], totalSupplyShares[id]); - else amount = shares.toAssetsUp(totalSupply[id], totalSupplyShares[id]); + if (assets > 0) shares = assets.toSharesDown(totalSupply[id], totalSupplyShares[id]); + else assets = shares.toAssetsUp(totalSupply[id], totalSupplyShares[id]); supplyShares[id][onBehalf] += shares; totalSupplyShares[id] += shares; - totalSupply[id] += amount; + totalSupply[id] += assets; - emit EventsLib.Supply(id, msg.sender, onBehalf, amount, shares); + emit EventsLib.Supply(id, msg.sender, onBehalf, assets, shares); - if (data.length > 0) IBlueSupplyCallback(msg.sender).onBlueSupply(amount, data); + if (data.length > 0) IBlueSupplyCallback(msg.sender).onBlueSupply(assets, data); - IERC20(market.borrowableAsset).safeTransferFrom(msg.sender, address(this), amount); + IERC20(market.borrowableAsset).safeTransferFrom(msg.sender, address(this), assets); } /// @inheritdoc IBlue - function withdraw(Market memory market, uint256 amount, uint256 shares, address onBehalf, address receiver) + function withdraw(Market memory market, uint256 assets, uint256 shares, address onBehalf, address receiver) external { Id id = market.id(); require(lastUpdate[id] != 0, ErrorsLib.MARKET_NOT_CREATED); - require(UtilsLib.exactlyOneZero(amount, shares), ErrorsLib.INCONSISTENT_INPUT); + require(UtilsLib.exactlyOneZero(assets, shares), ErrorsLib.INCONSISTENT_INPUT); // No need to verify that onBehalf != address(0) thanks to the authorization check. require(receiver != address(0), ErrorsLib.ZERO_ADDRESS); require(_isSenderAuthorized(onBehalf), ErrorsLib.UNAUTHORIZED); _accrueInterests(market, id); - if (amount > 0) shares = amount.toSharesUp(totalSupply[id], totalSupplyShares[id]); - else amount = shares.toAssetsDown(totalSupply[id], totalSupplyShares[id]); + if (assets > 0) shares = assets.toSharesUp(totalSupply[id], totalSupplyShares[id]); + else assets = shares.toAssetsDown(totalSupply[id], totalSupplyShares[id]); supplyShares[id][onBehalf] -= shares; totalSupplyShares[id] -= shares; - totalSupply[id] -= amount; + totalSupply[id] -= assets; - emit EventsLib.Withdraw(id, msg.sender, onBehalf, receiver, amount, shares); + emit EventsLib.Withdraw(id, msg.sender, onBehalf, receiver, assets, shares); require(totalBorrow[id] <= totalSupply[id], ErrorsLib.INSUFFICIENT_LIQUIDITY); - IERC20(market.borrowableAsset).safeTransfer(receiver, amount); + IERC20(market.borrowableAsset).safeTransfer(receiver, assets); } /* BORROW MANAGEMENT */ /// @inheritdoc IBlue - function borrow(Market memory market, uint256 amount, uint256 shares, address onBehalf, address receiver) + function borrow(Market memory market, uint256 assets, uint256 shares, address onBehalf, address receiver) external { Id id = market.id(); require(lastUpdate[id] != 0, ErrorsLib.MARKET_NOT_CREATED); - require(UtilsLib.exactlyOneZero(amount, shares), ErrorsLib.INCONSISTENT_INPUT); + require(UtilsLib.exactlyOneZero(assets, shares), ErrorsLib.INCONSISTENT_INPUT); // No need to verify that onBehalf != address(0) thanks to the authorization check. require(receiver != address(0), ErrorsLib.ZERO_ADDRESS); require(_isSenderAuthorized(onBehalf), ErrorsLib.UNAUTHORIZED); _accrueInterests(market, id); - if (amount > 0) shares = amount.toSharesUp(totalBorrow[id], totalBorrowShares[id]); - else amount = shares.toAssetsDown(totalBorrow[id], totalBorrowShares[id]); + if (assets > 0) shares = assets.toSharesUp(totalBorrow[id], totalBorrowShares[id]); + else assets = shares.toAssetsDown(totalBorrow[id], totalBorrowShares[id]); borrowShares[id][onBehalf] += shares; totalBorrowShares[id] += shares; - totalBorrow[id] += amount; + totalBorrow[id] += assets; - emit EventsLib.Borrow(id, msg.sender, onBehalf, receiver, amount, shares); + emit EventsLib.Borrow(id, msg.sender, onBehalf, receiver, assets, shares); require(_isHealthy(market, id, onBehalf), ErrorsLib.INSUFFICIENT_COLLATERAL); require(totalBorrow[id] <= totalSupply[id], ErrorsLib.INSUFFICIENT_LIQUIDITY); - IERC20(market.borrowableAsset).safeTransfer(receiver, amount); + IERC20(market.borrowableAsset).safeTransfer(receiver, assets); } /// @inheritdoc IBlue - function repay(Market memory market, uint256 amount, uint256 shares, address onBehalf, bytes calldata data) + function repay(Market memory market, uint256 assets, uint256 shares, address onBehalf, bytes calldata data) external { Id id = market.id(); require(lastUpdate[id] != 0, ErrorsLib.MARKET_NOT_CREATED); - require(UtilsLib.exactlyOneZero(amount, shares), ErrorsLib.INCONSISTENT_INPUT); + require(UtilsLib.exactlyOneZero(assets, shares), ErrorsLib.INCONSISTENT_INPUT); require(onBehalf != address(0), ErrorsLib.ZERO_ADDRESS); _accrueInterests(market, id); - if (amount > 0) shares = amount.toSharesDown(totalBorrow[id], totalBorrowShares[id]); - else amount = shares.toAssetsUp(totalBorrow[id], totalBorrowShares[id]); + if (assets > 0) shares = assets.toSharesDown(totalBorrow[id], totalBorrowShares[id]); + else assets = shares.toAssetsUp(totalBorrow[id], totalBorrowShares[id]); borrowShares[id][onBehalf] -= shares; totalBorrowShares[id] -= shares; - totalBorrow[id] -= amount; + totalBorrow[id] -= assets; - emit EventsLib.Repay(id, msg.sender, onBehalf, amount, shares); + emit EventsLib.Repay(id, msg.sender, onBehalf, assets, shares); - if (data.length > 0) IBlueRepayCallback(msg.sender).onBlueRepay(amount, data); + if (data.length > 0) IBlueRepayCallback(msg.sender).onBlueRepay(assets, data); - IERC20(market.borrowableAsset).safeTransferFrom(msg.sender, address(this), amount); + IERC20(market.borrowableAsset).safeTransferFrom(msg.sender, address(this), assets); } /* COLLATERAL MANAGEMENT */ /// @inheritdoc IBlue - function supplyCollateral(Market memory market, uint256 amount, address onBehalf, bytes calldata data) external { + function supplyCollateral(Market memory market, uint256 assets, address onBehalf, bytes calldata data) external { Id id = market.id(); require(lastUpdate[id] != 0, ErrorsLib.MARKET_NOT_CREATED); - require(amount != 0, ErrorsLib.ZERO_AMOUNT); + require(assets != 0, ErrorsLib.ZERO_AMOUNT); require(onBehalf != address(0), ErrorsLib.ZERO_ADDRESS); // Don't accrue interests because it's not required and it saves gas. - collateral[id][onBehalf] += amount; + collateral[id][onBehalf] += assets; - emit EventsLib.SupplyCollateral(id, msg.sender, onBehalf, amount); + emit EventsLib.SupplyCollateral(id, msg.sender, onBehalf, assets); - if (data.length > 0) IBlueSupplyCollateralCallback(msg.sender).onBlueSupplyCollateral(amount, data); + if (data.length > 0) IBlueSupplyCollateralCallback(msg.sender).onBlueSupplyCollateral(assets, data); - IERC20(market.collateralAsset).safeTransferFrom(msg.sender, address(this), amount); + IERC20(market.collateralAsset).safeTransferFrom(msg.sender, address(this), assets); } /// @inheritdoc IBlue - function withdrawCollateral(Market memory market, uint256 amount, address onBehalf, address receiver) external { + function withdrawCollateral(Market memory market, uint256 assets, address onBehalf, address receiver) external { Id id = market.id(); require(lastUpdate[id] != 0, ErrorsLib.MARKET_NOT_CREATED); - require(amount != 0, ErrorsLib.ZERO_AMOUNT); + require(assets != 0, ErrorsLib.ZERO_AMOUNT); // No need to verify that onBehalf != address(0) thanks to the authorization check. require(receiver != address(0), ErrorsLib.ZERO_ADDRESS); require(_isSenderAuthorized(onBehalf), ErrorsLib.UNAUTHORIZED); _accrueInterests(market, id); - collateral[id][onBehalf] -= amount; + collateral[id][onBehalf] -= assets; - emit EventsLib.WithdrawCollateral(id, msg.sender, onBehalf, receiver, amount); + emit EventsLib.WithdrawCollateral(id, msg.sender, onBehalf, receiver, assets); require(_isHealthy(market, id, onBehalf), ErrorsLib.INSUFFICIENT_COLLATERAL); - IERC20(market.collateralAsset).safeTransfer(receiver, amount); + IERC20(market.collateralAsset).safeTransfer(receiver, assets); } /* LIQUIDATION */ @@ -350,14 +350,14 @@ contract Blue is IBlue { /* FLASH LOANS */ /// @inheritdoc IFlashLender - function flashLoan(address token, uint256 amount, bytes calldata data) external { - IERC20(token).safeTransfer(msg.sender, amount); + function flashLoan(address token, uint256 assets, bytes calldata data) external { + IERC20(token).safeTransfer(msg.sender, assets); - emit EventsLib.FlashLoan(msg.sender, token, amount); + emit EventsLib.FlashLoan(msg.sender, token, assets); - IBlueFlashLoanCallback(msg.sender).onBlueFlashLoan(token, amount, data); + IBlueFlashLoanCallback(msg.sender).onBlueFlashLoan(token, assets, data); - IERC20(token).safeTransferFrom(msg.sender, address(this), amount); + IERC20(token).safeTransferFrom(msg.sender, address(this), assets); } /* AUTHORIZATION */ @@ -418,7 +418,7 @@ contract Blue is IBlue { uint256 feeShares; if (fee[id] != 0) { uint256 feeAmount = accruedInterests.wMulDown(fee[id]); - // The fee amount is subtracted from the total supply in this calculation to compensate for the fact that total supply is already updated. + // The fee assets is subtracted from the total supply in this calculation to compensate for the fact that total supply is already updated. feeShares = feeAmount.mulDivDown(totalSupplyShares[id], totalSupply[id] - feeAmount); supplyShares[id][feeRecipient] += feeShares; totalSupplyShares[id] += feeShares; diff --git a/src/interfaces/IBlue.sol b/src/interfaces/IBlue.sol index 70662edd5..f28ef043d 100644 --- a/src/interfaces/IBlue.sol +++ b/src/interfaces/IBlue.sol @@ -45,14 +45,14 @@ interface IBlue is IFlashLender { /// @notice The `user`'s collateral balance on the market defined by the given `id`. function collateral(Id id, address user) external view returns (uint256); - /// @notice The total amount of assets supplied to the market defined by the given `id`. + /// @notice The total assets supplied to the market defined by the given `id`. /// @dev The value can be incaccurate since it does not take into account the accrued interests. function totalSupply(Id id) external view returns (uint256); /// @notice The total supply shares of the market defined by the given `id`. function totalSupplyShares(Id id) external view returns (uint256); - /// @notice The total amount of assets borrowed from the market defined by the given `id`. + /// @notice The total assets borrowed from the market defined by the given `id`. /// @dev The value can be incaccurate since it does not take into account the accrued interests. function totalBorrow(Id id) external view returns (uint256); @@ -97,78 +97,78 @@ interface IBlue is IFlashLender { /// @notice Creates `market`. function createMarket(Market memory market) external; - /// @notice Supplies the given `amount` of assets or `shares` to the given `market` on behalf of `onBehalf`, + /// @notice Supplies the given amount of `assets` or `shares` to the given `market` on behalf of `onBehalf`, /// optionally calling back the caller's `onBlueSupply` function with the given `data`. - /// @dev Either `amount` or `shares` should be zero. - /// Most usecases should rely on `amount` as an input so the caller - /// is guaranteed to have `amount` tokens pulled from their balance, - /// but the possibility to mint a specific amount of shares is given + /// @dev Either `assets` or `shares` should be zero. + /// Most usecases should rely on `assets` as an input so the caller + /// is guaranteed to have `assets` tokens pulled from their balance, + /// but the possibility to mint a specific assets of shares is given /// for full compatibility and precision. /// @param market The market to supply assets to. - /// @param amount The amount of assets to supply. + /// @param assets The amount of assets to supply. /// @param shares The amount of shares to mint. /// @param onBehalf The address that will receive the position. /// @param data Arbitrary data to pass to the `onBlueSupply` callback. Pass empty data if not needed. - function supply(Market memory market, uint256 amount, uint256 shares, address onBehalf, bytes memory data) + function supply(Market memory market, uint256 assets, uint256 shares, address onBehalf, bytes memory data) external; - /// @notice Withdraws the given `amount` of assets or `shares` from the given `market` on behalf of `onBehalf`. - /// @dev Either `amount` or `shares` should be zero. + /// @notice Withdraws the given `assets` or `shares` from the given `market` on behalf of `onBehalf`. + /// @dev Either `assets` or `shares` should be zero. /// To withdraw the whole position, pass the `shares`'s balance of `onBehalf`. /// @dev If `msg.sender != onBehalf`, `msg.sender` must be authorized to withdraw from `onBehalf`. /// @param market The market to withdraw assets from. - /// @param shares The amount of amount to withdraw. + /// @param shares The amount of assets to withdraw. /// @param shares The amount of shares to burn. /// @param onBehalf The address of the owner of the withdrawn assets. /// @param receiver The address that will receive the withdrawn assets. - function withdraw(Market memory market, uint256 amount, uint256 shares, address onBehalf, address receiver) + function withdraw(Market memory market, uint256 assets, uint256 shares, address onBehalf, address receiver) external; - /// @notice Borrows the given `amount` of assets or `shares` from the given `market` on behalf of `onBehalf`. - /// @dev Either `amount` or `shares` should be zero. - /// Most usecases should rely on `amount` as an input so the caller - /// is guaranteed to borrow `amount` of tokens, - /// but the possibility to burn a specific amount of shares is given + /// @notice Borrows the given `assets` or `shares` from the given `market` on behalf of `onBehalf`. + /// @dev Either `assets` or `shares` should be zero. + /// Most usecases should rely on `assets` as an input so the caller + /// is guaranteed to borrow `assets` of tokens, + /// but the possibility to burn a specific assets of shares is given /// for full compatibility and precision. /// @param market The market to borrow assets from. - /// @param amount The amount of assets to borrow. + /// @param assets The amount of assets to borrow. /// @param shares The amount of shares to mint. /// @param onBehalf The address of the owner of the debt. /// @param receiver The address that will receive the debt. /// @dev If `msg.sender != onBehalf`, `msg.sender` must be authorized to withdraw from `onBehalf`. - function borrow(Market memory market, uint256 amount, uint256 shares, address onBehalf, address receiver) + function borrow(Market memory market, uint256 assets, uint256 shares, address onBehalf, address receiver) external; - /// @notice Repays the given `amount` of assets or `shares` to the given `market` on behalf of `onBehalf`, + /// @notice Repays the given `assets` or `shares` to the given `market` on behalf of `onBehalf`, /// optionally calling back the caller's `onBlueReplay` function with the given `data`. - /// @dev Either `amount` or `shares` should be zero. + /// @dev Either `assets` or `shares` should be zero. /// To repay the whole debt, pass the `shares`'s balance of `onBehalf`. /// @param market The market to repay assets to. - /// @param amount The amount of assets to repay. + /// @param assets The amount of assets to repay. /// @param shares The amount of shares to burn. /// @param onBehalf The address of the owner of the debt. /// @param data Arbitrary data to pass to the `onBlueRepay` callback. Pass empty data if not needed. - function repay(Market memory market, uint256 amount, uint256 shares, address onBehalf, bytes memory data) + function repay(Market memory market, uint256 assets, uint256 shares, address onBehalf, bytes memory data) external; - /// @notice Supplies the given `amount` of collateral to the given `market` on behalf of `onBehalf`, + /// @notice Supplies the given `assets` of collateral to the given `market` on behalf of `onBehalf`, /// optionally calling back the caller's `onBlueSupplyCollateral` function with the given `data`. /// @dev Interests are not accrued since it's not required and it saves gas. /// @param market The market to supply collateral to. - /// @param amount The amount of collateral to supply. + /// @param assets The amount of collateral to supply. /// @param onBehalf The address that will receive the collateral. /// @param data Arbitrary data to pass to the `onBlueSupplyCollateral` callback. Pass empty data if not needed. - function supplyCollateral(Market memory market, uint256 amount, address onBehalf, bytes memory data) external; + function supplyCollateral(Market memory market, uint256 assets, address onBehalf, bytes memory data) external; - /// @notice Withdraws the given `amount` of collateral from the given `market` on behalf of `onBehalf`. + /// @notice Withdraws the given `assets` of collateral from the given `market` on behalf of `onBehalf`. /// @dev If `msg.sender != onBehalf`, `msg.sender` must be authorized to withdraw from `onBehalf`. /// @param market The market to withdraw collateral from. - /// @param amount The amount of collateral to withdraw. + /// @param assets The amount of collateral to withdraw. /// @param onBehalf The address of the owner of the collateral. /// @param receiver The address that will receive the withdrawn collateral. - function withdrawCollateral(Market memory market, uint256 amount, address onBehalf, address receiver) external; + function withdrawCollateral(Market memory market, uint256 assets, address onBehalf, address receiver) external; - /// @notice Liquidates the given `seized` amount to the given `market` of the given `borrower`'s position, + /// @notice Liquidates the given `seized` assets to the given `market` of the given `borrower`'s position, /// optionally calling back the caller's `onBlueLiquidate` function with the given `data`. /// @param market The market of the position. /// @param borrower The owner of the position. diff --git a/src/interfaces/IBlueCallbacks.sol b/src/interfaces/IBlueCallbacks.sol index ddaf3c43e..07e65acb9 100644 --- a/src/interfaces/IBlueCallbacks.sol +++ b/src/interfaces/IBlueCallbacks.sol @@ -6,8 +6,8 @@ pragma solidity >=0.5.0; interface IBlueLiquidateCallback { /// @notice Callback called when a liquidation occurs. /// @dev The callback is called only if data is not empty. - /// @param seized The amount of seized tokens. - /// @param repaid The amount of repaid tokens. + /// @param seized The amount of seized assets. + /// @param repaid The amount of repaid assets. /// @param data Arbitrary data passed to the `liquidate` function. function onBlueLiquidate(uint256 seized, uint256 repaid, bytes calldata data) external; } @@ -17,9 +17,9 @@ interface IBlueLiquidateCallback { interface IBlueRepayCallback { /// @notice Callback called when a repayment occurs. /// @dev The callback is called only if data is not empty. - /// @param amount The amount of repaid tokens. + /// @param assets The amount of repaid assets. /// @param data Arbitrary data passed to the `repay` function. - function onBlueRepay(uint256 amount, bytes calldata data) external; + function onBlueRepay(uint256 assets, bytes calldata data) external; } /// @title IBlueSupplyCallback @@ -27,9 +27,9 @@ interface IBlueRepayCallback { interface IBlueSupplyCallback { /// @notice Callback called when a supply occurs. /// @dev The callback is called only if data is not empty. - /// @param amount The amount of supplied tokens. + /// @param assets The amount of supplied assets. /// @param data Arbitrary data passed to the `supply` function. - function onBlueSupply(uint256 amount, bytes calldata data) external; + function onBlueSupply(uint256 assets, bytes calldata data) external; } /// @title IBlueSupplyCollateralCallback @@ -37,9 +37,9 @@ interface IBlueSupplyCallback { interface IBlueSupplyCollateralCallback { /// @notice Callback called when a supply occurs. /// @dev The callback is called only if data is not empty. - /// @param amount The amount of supplied tokens. + /// @param assets The amount of supplied assets. /// @param data Arbitrary data passed to the `supplyCollateral` function. - function onBlueSupplyCollateral(uint256 amount, bytes calldata data) external; + function onBlueSupplyCollateral(uint256 assets, bytes calldata data) external; } /// @title IBlueWithdrawCallback @@ -48,7 +48,7 @@ interface IBlueFlashLoanCallback { /// @notice Callback called when a flash loan occurs. /// @dev The callback is called only if data is not empty. /// @param token The token that was flash loaned. - /// @param amount The amount that was flash loaned. + /// @param assets The amount that was flash loaned. /// @param data Arbitrary data passed to the `flashLoan` function. - function onBlueFlashLoan(address token, uint256 amount, bytes calldata data) external; + function onBlueFlashLoan(address token, uint256 assets, bytes calldata data) external; } diff --git a/src/interfaces/IFlashLender.sol b/src/interfaces/IFlashLender.sol index f23682ec7..2950faa20 100644 --- a/src/interfaces/IFlashLender.sol +++ b/src/interfaces/IFlashLender.sol @@ -8,7 +8,7 @@ pragma solidity >=0.5.0; interface IFlashLender { /// @notice Executes a flash loan. /// @param token The token to flash loan. - /// @param amount The amount to flash loan. + /// @param assets The assets to flash loan. /// @param data Arbitrary data to pass to the `onBlueFlashLoan` callback. - function flashLoan(address token, uint256 amount, bytes calldata data) external; + function flashLoan(address token, uint256 assets, bytes calldata data) external; } diff --git a/src/libraries/ErrorsLib.sol b/src/libraries/ErrorsLib.sol index 38b7b42c0..34a1e9646 100644 --- a/src/libraries/ErrorsLib.sol +++ b/src/libraries/ErrorsLib.sol @@ -26,8 +26,8 @@ library ErrorsLib { /// @notice Thrown when one of the input is not consistent. string constant INCONSISTENT_INPUT = "inconsistent input"; - /// @notice Thrown when a zero amount is passed as input. - string internal constant ZERO_AMOUNT = "zero amount"; + /// @notice Thrown when a zero assets amount is passed as input. + string internal constant ZERO_AMOUNT = "zero assets"; /// @notice Thrown when a zero shares amount is passed as input. string internal constant ZERO_SHARES = "zero shares"; diff --git a/src/libraries/EventsLib.sol b/src/libraries/EventsLib.sol index b366d1967..d5566d88a 100644 --- a/src/libraries/EventsLib.sol +++ b/src/libraries/EventsLib.sol @@ -34,23 +34,23 @@ library EventsLib { /// @param id The market id. /// @param caller The caller. /// @param onBehalf The address that will receive the position. - /// @param amount The amount of assets supplied. + /// @param assets The amount of assets supplied. /// @param shares The amount of shares minted. - event Supply(Id indexed id, address indexed caller, address indexed onBehalf, uint256 amount, uint256 shares); + event Supply(Id indexed id, address indexed caller, address indexed onBehalf, uint256 assets, uint256 shares); /// @notice Emitted on withdrawal of assets. /// @param id The market id. /// @param caller The caller. /// @param onBehalf The address from which the assets are withdrawn. /// @param receiver The address that will receive the withdrawn assets. - /// @param amount The amount of assets withdrawn. + /// @param assets The amount of assets withdrawn. /// @param shares The amount of shares burned. event Withdraw( Id indexed id, address caller, address indexed onBehalf, address indexed receiver, - uint256 amount, + uint256 assets, uint256 shares ); @@ -59,14 +59,14 @@ library EventsLib { /// @param caller The caller. /// @param onBehalf The address from which the assets are borrowed. /// @param receiver The address that will receive the borrowed assets. - /// @param amount The amount of assets borrowed. + /// @param assets The amount of assets borrowed. /// @param shares The amount of shares minted. event Borrow( Id indexed id, address caller, address indexed onBehalf, address indexed receiver, - uint256 amount, + uint256 assets, uint256 shares ); @@ -74,25 +74,25 @@ library EventsLib { /// @param id The market id. /// @param caller The caller. /// @param onBehalf The address for which the assets are repaid. - /// @param amount The amount of assets repaid. + /// @param assets The amount of assets repaid. /// @param shares The amount of shares burned. - event Repay(Id indexed id, address indexed caller, address indexed onBehalf, uint256 amount, uint256 shares); + event Repay(Id indexed id, address indexed caller, address indexed onBehalf, uint256 assets, uint256 shares); /// @notice Emitted on supply of collateral. /// @param id The market id. /// @param caller The caller. /// @param onBehalf The address that will receive the position. - /// @param amount The amount of collateral supplied. - event SupplyCollateral(Id indexed id, address indexed caller, address indexed onBehalf, uint256 amount); + /// @param assets The amount of collateral supplied. + event SupplyCollateral(Id indexed id, address indexed caller, address indexed onBehalf, uint256 assets); /// @notice Emitted on withdrawal of collateral. /// @param id The market id. /// @param caller The caller. /// @param onBehalf The address from which the collateral is withdrawn. /// @param receiver The address that will receive the withdrawn collateral. - /// @param amount The amount of collateral withdrawn. + /// @param assets The amount of collateral withdrawn. event WithdrawCollateral( - Id indexed id, address caller, address indexed onBehalf, address indexed receiver, uint256 amount + Id indexed id, address caller, address indexed onBehalf, address indexed receiver, uint256 assets ); /// @notice Emitted on liquidation of a position. @@ -116,8 +116,8 @@ library EventsLib { /// @notice Emitted on flash loan. /// @param caller The caller.. /// @param token The token that was flash loaned. - /// @param amount The amount that was flash loaned. - event FlashLoan(address indexed caller, address indexed token, uint256 amount); + /// @param assets The amount of assets that was flash loaned. + event FlashLoan(address indexed caller, address indexed token, uint256 assets); /// @notice Emitted when setting an authorization. /// @param caller The caller. diff --git a/src/mocks/ERC20Mock.sol b/src/mocks/ERC20Mock.sol index cef49b8c3..6ee8fbce1 100644 --- a/src/mocks/ERC20Mock.sol +++ b/src/mocks/ERC20Mock.sol @@ -6,7 +6,7 @@ import {ERC20} from "solmate/tokens/ERC20.sol"; contract ERC20Mock is ERC20 { constructor(string memory _name, string memory _symbol, uint8 _decimals) ERC20(_name, _symbol, _decimals) {} - function setBalance(address owner, uint256 amount) external { - balanceOf[owner] = amount; + function setBalance(address owner, uint256 assets) external { + balanceOf[owner] = assets; } } diff --git a/src/mocks/FlashBorrowerMock.sol b/src/mocks/FlashBorrowerMock.sol index 91334ff6d..daad5c744 100644 --- a/src/mocks/FlashBorrowerMock.sol +++ b/src/mocks/FlashBorrowerMock.sol @@ -15,12 +15,12 @@ contract FlashBorrowerMock is IBlueFlashLoanCallback { BLUE = newBlue; } - function flashLoan(address token, uint256 amount, bytes calldata data) external { - BLUE.flashLoan(token, amount, data); + function flashLoan(address token, uint256 assets, bytes calldata data) external { + BLUE.flashLoan(token, assets, data); } - function onBlueFlashLoan(address token, uint256 amount, bytes calldata) external { + function onBlueFlashLoan(address token, uint256 assets, bytes calldata) external { require(msg.sender == address(BLUE)); - ERC20(token).safeApprove(address(BLUE), amount); + ERC20(token).safeApprove(address(BLUE), assets); } } diff --git a/test/forge/Blue.t.sol b/test/forge/Blue.t.sol index 6d0cd6694..f2502a512 100644 --- a/test/forge/Blue.t.sol +++ b/test/forge/Blue.t.sol @@ -310,32 +310,32 @@ contract BlueTest is blue.createMarket(marketFuzz); } - function testSupplyAmount(uint256 amount, address onBehalf) public { + function testSupplyAmount(uint256 assets, address onBehalf) public { vm.assume(onBehalf != address(0)); vm.assume(onBehalf != address(blue)); - amount = bound(amount, 1, 2 ** 64); - uint256 shares = amount.toSharesDown(blue.totalSupply(id), blue.totalSupplyShares(id)); + assets = bound(assets, 1, 2 ** 64); + uint256 shares = assets.toSharesDown(blue.totalSupply(id), blue.totalSupplyShares(id)); - borrowableAsset.setBalance(address(this), amount); - blue.supply(market, amount, 0, onBehalf, hex""); + borrowableAsset.setBalance(address(this), assets); + blue.supply(market, assets, 0, onBehalf, hex""); assertEq(blue.supplyShares(id, onBehalf), shares, "supply share"); assertEq(borrowableAsset.balanceOf(onBehalf), 0, "lender balance"); - assertEq(borrowableAsset.balanceOf(address(blue)), amount, "blue balance"); + assertEq(borrowableAsset.balanceOf(address(blue)), assets, "blue balance"); } function testSupplyShares(uint256 shares, address onBehalf) public { vm.assume(onBehalf != address(0)); vm.assume(onBehalf != address(blue)); shares = bound(shares, 1, 2 ** 64); - uint256 amount = shares.toAssetsUp(blue.totalSupply(id), blue.totalSupplyShares(id)); + uint256 assets = shares.toAssetsUp(blue.totalSupply(id), blue.totalSupplyShares(id)); - borrowableAsset.setBalance(address(this), amount); + borrowableAsset.setBalance(address(this), assets); blue.supply(market, 0, shares, onBehalf, hex""); assertEq(blue.supplyShares(id, onBehalf), shares, "supply share"); assertEq(borrowableAsset.balanceOf(onBehalf), 0, "lender balance"); - assertEq(borrowableAsset.balanceOf(address(blue)), amount, "blue balance"); + assertEq(borrowableAsset.balanceOf(address(blue)), assets, "blue balance"); } function testBorrowAmount(uint256 amountLent, uint256 amountBorrowed, address receiver) public { @@ -369,10 +369,10 @@ contract BlueTest is function testBorrowShares(uint256 shares) public { shares = bound(shares, 1, 2 ** 64); - uint256 amount = shares.toAssetsDown(blue.totalBorrow(id), blue.totalBorrowShares(id)); + uint256 assets = shares.toAssetsDown(blue.totalBorrow(id), blue.totalBorrowShares(id)); - borrowableAsset.setBalance(address(this), amount); - if (amount > 0) blue.supply(market, amount, 0, address(this), hex""); + borrowableAsset.setBalance(address(this), assets); + if (assets > 0) blue.supply(market, assets, 0, address(this), hex""); uint256 collateralAmount = shares.toAssetsUp(blue.totalBorrow(id), blue.totalBorrowShares(id)).wDivUp(LLTV); collateralAsset.setBalance(address(this), collateralAmount); @@ -382,7 +382,7 @@ contract BlueTest is blue.borrow(market, 0, shares, BORROWER, BORROWER); assertEq(blue.borrowShares(id, BORROWER), shares, "borrow share"); - assertEq(borrowableAsset.balanceOf(BORROWER), amount, "receiver balance"); + assertEq(borrowableAsset.balanceOf(BORROWER), assets, "receiver balance"); assertEq(borrowableAsset.balanceOf(address(blue)), 0, "blue balance"); } @@ -535,17 +535,17 @@ contract BlueTest is assertEq(borrowableAsset.balanceOf(address(blue)), amountRepaid, "blue balance"); } - function testSupplyCollateralOnBehalf(uint256 amount, address onBehalf) public { + function testSupplyCollateralOnBehalf(uint256 assets, address onBehalf) public { vm.assume(onBehalf != address(0)); vm.assume(onBehalf != address(blue)); - amount = bound(amount, 1, 2 ** 64); + assets = bound(assets, 1, 2 ** 64); - collateralAsset.setBalance(address(this), amount); - blue.supplyCollateral(market, amount, onBehalf, hex""); + collateralAsset.setBalance(address(this), assets); + blue.supplyCollateral(market, assets, onBehalf, hex""); - assertEq(blue.collateral(id, onBehalf), amount, "collateral"); + assertEq(blue.collateral(id, onBehalf), assets, "collateral"); assertEq(collateralAsset.balanceOf(onBehalf), 0, "onBehalf balance"); - assertEq(collateralAsset.balanceOf(address(blue)), amount, "blue balance"); + assertEq(collateralAsset.balanceOf(address(blue)), assets, "blue balance"); } function testWithdrawCollateral(uint256 amountDeposited, uint256 amountWithdrawn, address receiver) public { @@ -800,17 +800,17 @@ contract BlueTest is blue.withdrawCollateral(market, 1, address(this), address(0)); } - function testEmptyMarket(uint256 amount) public { - amount = bound(amount, 1, type(uint256).max / SharesMathLib.VIRTUAL_SHARES); + function testEmptyMarket(uint256 assets) public { + assets = bound(assets, 1, type(uint256).max / SharesMathLib.VIRTUAL_SHARES); vm.expectRevert(stdError.arithmeticError); - blue.withdraw(market, amount, 0, address(this), address(this)); + blue.withdraw(market, assets, 0, address(this), address(this)); vm.expectRevert(stdError.arithmeticError); - blue.repay(market, amount, 0, address(this), hex""); + blue.repay(market, assets, 0, address(this), hex""); vm.expectRevert(stdError.arithmeticError); - blue.withdrawCollateral(market, amount, address(this), address(this)); + blue.withdrawCollateral(market, assets, address(this), address(this)); } function testSetAuthorization(address authorized, bool isAuthorized) public { @@ -879,15 +879,15 @@ contract BlueTest is assertEq(blue.nonce(authorizer), 1); } - function testFlashLoan(uint256 amount) public { - amount = bound(amount, 1, 2 ** 64); + function testFlashLoan(uint256 assets) public { + assets = bound(assets, 1, 2 ** 64); - borrowableAsset.setBalance(address(this), amount); - blue.supply(market, amount, 0, address(this), hex""); + borrowableAsset.setBalance(address(this), assets); + blue.supply(market, assets, 0, address(this), hex""); - blue.flashLoan(address(borrowableAsset), amount, bytes("")); + blue.flashLoan(address(borrowableAsset), assets, bytes("")); - assertEq(borrowableAsset.balanceOf(address(blue)), amount, "balanceOf"); + assertEq(borrowableAsset.balanceOf(address(blue)), assets, "balanceOf"); } function testExtsLoad(uint256 slot, bytes32 value0) public { @@ -906,76 +906,76 @@ contract BlueTest is assertEq(values[1], value1, "value1"); } - function testSupplyCallback(uint256 amount) public { - amount = bound(amount, 1, 2 ** 64); - borrowableAsset.setBalance(address(this), amount); + function testSupplyCallback(uint256 assets) public { + assets = bound(assets, 1, 2 ** 64); + borrowableAsset.setBalance(address(this), assets); borrowableAsset.approve(address(blue), 0); vm.expectRevert(); - blue.supply(market, amount, 0, address(this), hex""); - blue.supply(market, amount, 0, address(this), abi.encode(this.testSupplyCallback.selector, hex"")); + blue.supply(market, assets, 0, address(this), hex""); + blue.supply(market, assets, 0, address(this), abi.encode(this.testSupplyCallback.selector, hex"")); } - function testSupplyCollateralCallback(uint256 amount) public { - amount = bound(amount, 1, 2 ** 64); - collateralAsset.setBalance(address(this), amount); + function testSupplyCollateralCallback(uint256 assets) public { + assets = bound(assets, 1, 2 ** 64); + collateralAsset.setBalance(address(this), assets); collateralAsset.approve(address(blue), 0); vm.expectRevert(); - blue.supplyCollateral(market, amount, address(this), hex""); + blue.supplyCollateral(market, assets, address(this), hex""); blue.supplyCollateral( - market, amount, address(this), abi.encode(this.testSupplyCollateralCallback.selector, hex"") + market, assets, address(this), abi.encode(this.testSupplyCollateralCallback.selector, hex"") ); } - function testRepayCallback(uint256 amount) public { - amount = bound(amount, 1, 2 ** 64); + function testRepayCallback(uint256 assets) public { + assets = bound(assets, 1, 2 ** 64); - borrowableAsset.setBalance(address(this), amount); - blue.supply(market, amount, 0, address(this), hex""); + borrowableAsset.setBalance(address(this), assets); + blue.supply(market, assets, 0, address(this), hex""); - uint256 collateralAmount = amount.wDivUp(LLTV); + uint256 collateralAmount = assets.wDivUp(LLTV); collateralAsset.setBalance(address(this), collateralAmount); blue.supplyCollateral(market, collateralAmount, address(this), hex""); - blue.borrow(market, amount, 0, address(this), address(this)); + blue.borrow(market, assets, 0, address(this), address(this)); borrowableAsset.approve(address(blue), 0); vm.expectRevert("TRANSFER_FROM_FAILED"); - blue.repay(market, amount, 0, address(this), hex""); - blue.repay(market, amount, 0, address(this), abi.encode(this.testRepayCallback.selector, hex"")); + blue.repay(market, assets, 0, address(this), hex""); + blue.repay(market, assets, 0, address(this), abi.encode(this.testRepayCallback.selector, hex"")); } - function testLiquidateCallback(uint256 amount) public { - amount = bound(amount, 10, 2 ** 64); + function testLiquidateCallback(uint256 assets) public { + assets = bound(assets, 10, 2 ** 64); - borrowableAsset.setBalance(address(this), amount); - blue.supply(market, amount, 0, address(this), hex""); + borrowableAsset.setBalance(address(this), assets); + blue.supply(market, assets, 0, address(this), hex""); - uint256 collateralAmount = amount.wDivUp(LLTV); + uint256 collateralAmount = assets.wDivUp(LLTV); collateralAsset.setBalance(address(this), collateralAmount); blue.supplyCollateral(market, collateralAmount, address(this), hex""); - blue.borrow(market, amount.wMulDown(LLTV), 0, address(this), address(this)); + blue.borrow(market, assets.wMulDown(LLTV), 0, address(this), address(this)); oracle.setPrice(0.5e18); - borrowableAsset.setBalance(address(this), amount); + borrowableAsset.setBalance(address(this), assets); borrowableAsset.approve(address(blue), 0); vm.expectRevert("TRANSFER_FROM_FAILED"); blue.liquidate(market, address(this), collateralAmount, hex""); blue.liquidate(market, address(this), collateralAmount, abi.encode(this.testLiquidateCallback.selector, hex"")); } - function testFlashActions(uint256 amount) public { - amount = bound(amount, 10, 2 ** 64); + function testFlashActions(uint256 assets) public { + assets = bound(assets, 10, 2 ** 64); oracle.setPrice(1e18); - uint256 toBorrow = amount.wMulDown(LLTV); + uint256 toBorrow = assets.wMulDown(LLTV); borrowableAsset.setBalance(address(this), 2 * toBorrow); blue.supply(market, toBorrow, 0, address(this), hex""); blue.supplyCollateral( - market, amount, address(this), abi.encode(this.testFlashActions.selector, abi.encode(toBorrow)) + market, assets, address(this), abi.encode(this.testFlashActions.selector, abi.encode(toBorrow)) ); assertGt(blue.borrowShares(market.id(), address(this)), 0, "no borrow"); @@ -984,42 +984,42 @@ contract BlueTest is 0, blue.borrowShares(id, address(this)), address(this), - abi.encode(this.testFlashActions.selector, abi.encode(amount)) + abi.encode(this.testFlashActions.selector, abi.encode(assets)) ); assertEq(blue.collateral(market.id(), address(this)), 0, "no withdraw collateral"); } // Callback functions. - function onBlueSupply(uint256 amount, bytes memory data) external { + function onBlueSupply(uint256 assets, bytes memory data) external { require(msg.sender == address(blue)); bytes4 selector; (selector, data) = abi.decode(data, (bytes4, bytes)); if (selector == this.testSupplyCallback.selector) { - borrowableAsset.approve(address(blue), amount); + borrowableAsset.approve(address(blue), assets); } } - function onBlueSupplyCollateral(uint256 amount, bytes memory data) external { + function onBlueSupplyCollateral(uint256 assets, bytes memory data) external { require(msg.sender == address(blue)); bytes4 selector; (selector, data) = abi.decode(data, (bytes4, bytes)); if (selector == this.testSupplyCollateralCallback.selector) { - collateralAsset.approve(address(blue), amount); + collateralAsset.approve(address(blue), assets); } else if (selector == this.testFlashActions.selector) { uint256 toBorrow = abi.decode(data, (uint256)); - collateralAsset.setBalance(address(this), amount); + collateralAsset.setBalance(address(this), assets); borrowableAsset.setBalance(address(this), toBorrow); blue.borrow(market, toBorrow, 0, address(this), address(this)); } } - function onBlueRepay(uint256 amount, bytes memory data) external { + function onBlueRepay(uint256 assets, bytes memory data) external { require(msg.sender == address(blue)); bytes4 selector; (selector, data) = abi.decode(data, (bytes4, bytes)); if (selector == this.testRepayCallback.selector) { - borrowableAsset.approve(address(blue), amount); + borrowableAsset.approve(address(blue), assets); } else if (selector == this.testFlashActions.selector) { uint256 toWithdraw = abi.decode(data, (uint256)); blue.withdrawCollateral(market, toWithdraw, address(this), address(this)); @@ -1035,8 +1035,8 @@ contract BlueTest is } } - function onBlueFlashLoan(address token, uint256 amount, bytes calldata) external { - ERC20(token).approve(address(blue), amount); + function onBlueFlashLoan(address token, uint256 assets, bytes calldata) external { + ERC20(token).approve(address(blue), assets); } } diff --git a/test/hardhat/Blue.spec.ts b/test/hardhat/Blue.spec.ts index 72cf8d345..39d0c548d 100644 --- a/test/hardhat/Blue.spec.ts +++ b/test/hardhat/Blue.spec.ts @@ -117,26 +117,26 @@ describe("Blue", () => { const user = signers[i]; - let amount = BigNumber.WAD.mul(1 + Math.floor(random() * 100)); + let assets = BigNumber.WAD.mul(1 + Math.floor(random() * 100)); if (random() < 2 / 3) { Promise.all([ - blue.connect(user).supply(market, amount, 0, user.address, []), - blue.connect(user).withdraw(market, amount.div(2), 0, user.address, user.address), + blue.connect(user).supply(market, assets, 0, user.address, []), + blue.connect(user).withdraw(market, assets.div(2), 0, user.address, user.address), ]); } else { const totalSupply = await blue.totalSupply(id); const totalBorrow = await blue.totalBorrow(id); const liquidity = BigNumber.from(totalSupply).sub(BigNumber.from(totalBorrow)); - amount = BigNumber.min(amount, BigNumber.from(liquidity).div(2)); + assets = BigNumber.min(assets, BigNumber.from(liquidity).div(2)); - if (amount > BigNumber.from(0)) { + if (assets > BigNumber.from(0)) { Promise.all([ - blue.connect(user).supplyCollateral(market, amount, user.address, []), - blue.connect(user).borrow(market, amount.div(2), 0, user.address, user.address), - blue.connect(user).repay(market, amount.div(4), 0, user.address, []), - blue.connect(user).withdrawCollateral(market, amount.div(8), user.address, user.address), + blue.connect(user).supplyCollateral(market, assets, user.address, []), + blue.connect(user).borrow(market, assets.div(2), 0, user.address, user.address), + blue.connect(user).repay(market, assets.div(4), 0, user.address, []), + blue.connect(user).withdrawCollateral(market, assets.div(8), user.address, user.address), ]); } } @@ -153,8 +153,8 @@ describe("Blue", () => { const borrower = signers[nbLiquidations + i]; const lltv = BigNumber.WAD.mul(i + 1).div(nbLiquidations + 1); - const amount = BigNumber.WAD.mul(1 + Math.floor(random() * 100)); - const borrowedAmount = amount.wadMulDown(lltv.sub(1)); + const assets = BigNumber.WAD.mul(1 + Math.floor(random() * 100)); + const borrowedAmount = assets.wadMulDown(lltv.sub(1)); if (!(await blue.isLltvEnabled(lltv))) { await blue.enableLltv(lltv); @@ -165,17 +165,17 @@ describe("Blue", () => { updateMarket({ lltv }); // We use 2 different users to borrow from a market so that liquidations do not put the borrow storage back to 0 on that market. - await blue.connect(user).supply(market, amount, 0, user.address, "0x"); - await blue.connect(user).supplyCollateral(market, amount, user.address, "0x"); + await blue.connect(user).supply(market, assets, 0, user.address, "0x"); + await blue.connect(user).supplyCollateral(market, assets, user.address, "0x"); await blue.connect(user).borrow(market, borrowedAmount, 0, user.address, user.address); - await blue.connect(borrower).supply(market, amount, 0, borrower.address, "0x"); - await blue.connect(borrower).supplyCollateral(market, amount, borrower.address, "0x"); + await blue.connect(borrower).supply(market, assets, 0, borrower.address, "0x"); + await blue.connect(borrower).supplyCollateral(market, assets, borrower.address, "0x"); await blue.connect(borrower).borrow(market, borrowedAmount, 0, borrower.address, user.address); await oracle.setPrice(BigNumber.WAD.div(10)); - const seized = closePositions ? constants.MaxUint256 : amount.div(2); + const seized = closePositions ? constants.MaxUint256 : assets.div(2); await blue.connect(liquidator).liquidate(market, borrower.address, seized, "0x"); @@ -191,10 +191,10 @@ describe("Blue", () => { it("should simuate gas cost [flashLoans]", async () => { const user = signers[0]; - const amount = BigNumber.WAD; + const assets = BigNumber.WAD; - await blue.connect(user).supply(market, amount, 0, user.address, "0x"); + await blue.connect(user).supply(market, assets, 0, user.address, "0x"); - await flashBorrower.flashLoan(borrowable.address, amount.div(2), []); + await flashBorrower.flashLoan(borrowable.address, assets.div(2), []); }); });