From 82c913572dbee21930e934c72afd544f842c1a4e Mon Sep 17 00:00:00 2001 From: Patrick Kim Date: Wed, 9 Aug 2023 22:46:17 +0200 Subject: [PATCH 1/2] refactor: move events back to lib --- src/Blue.sol | 37 +++++++++++------------ src/interfaces/IBlue.sol | 57 ------------------------------------ src/libraries/Events.sol | 63 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 75 deletions(-) create mode 100644 src/libraries/Events.sol diff --git a/src/Blue.sol b/src/Blue.sol index 9099e62d1..a7236f525 100644 --- a/src/Blue.sol +++ b/src/Blue.sol @@ -14,6 +14,7 @@ import {IOracle} from "./interfaces/IOracle.sol"; import {Id, Market, Signature, IBlue} from "./interfaces/IBlue.sol"; import {Errors} from "./libraries/Errors.sol"; +import {Events} from "./libraries/Events.sol"; import {UtilsLib} from "./libraries/UtilsLib.sol"; import {SharesMath} from "./libraries/SharesMath.sol"; import {FixedPointMathLib} from "./libraries/FixedPointMathLib.sol"; @@ -93,20 +94,20 @@ contract Blue is IBlue { function setOwner(address newOwner) external onlyOwner { owner = newOwner; - emit SetOwner(newOwner); + emit Events.SetOwner(newOwner); } function enableIrm(address irm) external onlyOwner { isIrmEnabled[irm] = true; - emit EnableIrm(address(irm)); + emit Events.EnableIrm(address(irm)); } function enableLltv(uint256 lltv) external onlyOwner { require(lltv < FixedPointMathLib.WAD, Errors.LLTV_TOO_HIGH); isLltvEnabled[lltv] = true; - emit EnableLltv(lltv); + emit Events.EnableLltv(lltv); } /// @notice It is the owner's responsibility to ensure a fee recipient is set before setting a non-zero fee. @@ -120,13 +121,13 @@ contract Blue is IBlue { fee[id] = newFee; - emit SetFee(id, newFee); + emit Events.SetFee(id, newFee); } function setFeeRecipient(address recipient) external onlyOwner { feeRecipient = recipient; - emit SetFeeRecipient(recipient); + emit Events.SetFeeRecipient(recipient); } // Markets management. @@ -139,7 +140,7 @@ contract Blue is IBlue { lastUpdate[id] = block.timestamp; - emit CreateMarket(id, market); + emit Events.CreateMarket(id, market); } // Supply management. @@ -161,7 +162,7 @@ contract Blue is IBlue { totalSupplyShares[id] += shares; totalSupply[id] += amount; - emit Supply(id, msg.sender, onBehalf, amount, shares); + emit Events.Supply(id, msg.sender, onBehalf, amount, shares); if (data.length > 0) IBlueSupplyCallback(msg.sender).onBlueSupply(amount, data); @@ -187,7 +188,7 @@ contract Blue is IBlue { totalSupplyShares[id] -= shares; totalSupply[id] -= amount; - emit Withdraw(id, msg.sender, onBehalf, receiver, amount, shares); + emit Events.Withdraw(id, msg.sender, onBehalf, receiver, amount, shares); require(totalBorrow[id] <= totalSupply[id], Errors.INSUFFICIENT_LIQUIDITY); @@ -215,7 +216,7 @@ contract Blue is IBlue { totalBorrowShares[id] += shares; totalBorrow[id] += amount; - emit Borrow(id, msg.sender, onBehalf, receiver, amount, shares); + emit Events.Borrow(id, msg.sender, onBehalf, receiver, amount, shares); require(_isHealthy(market, id, onBehalf), Errors.INSUFFICIENT_COLLATERAL); require(totalBorrow[id] <= totalSupply[id], Errors.INSUFFICIENT_LIQUIDITY); @@ -240,7 +241,7 @@ contract Blue is IBlue { totalBorrowShares[id] -= shares; totalBorrow[id] -= amount; - emit Repay(id, msg.sender, onBehalf, amount, shares); + emit Events.Repay(id, msg.sender, onBehalf, amount, shares); if (data.length > 0) IBlueRepayCallback(msg.sender).onBlueRepay(amount, data); @@ -260,7 +261,7 @@ contract Blue is IBlue { collateral[id][onBehalf] += amount; - emit SupplyCollateral(id, msg.sender, onBehalf, amount); + emit Events.SupplyCollateral(id, msg.sender, onBehalf, amount); if (data.length > 0) IBlueSupplyCollateralCallback(msg.sender).onBlueSupplyCollateral(amount, data); @@ -279,7 +280,7 @@ contract Blue is IBlue { collateral[id][onBehalf] -= amount; - emit WithdrawCollateral(id, msg.sender, onBehalf, receiver, amount); + emit Events.WithdrawCollateral(id, msg.sender, onBehalf, receiver, amount); require(_isHealthy(market, id, onBehalf), Errors.INSUFFICIENT_COLLATERAL); @@ -324,7 +325,7 @@ contract Blue is IBlue { IERC20(market.collateralAsset).safeTransfer(msg.sender, seized); - emit Liquidate(id, msg.sender, borrower, repaid, repaidShares, seized, badDebtShares); + emit Events.Liquidate(id, msg.sender, borrower, repaid, repaidShares, seized, badDebtShares); if (data.length > 0) IBlueLiquidateCallback(msg.sender).onBlueLiquidate(seized, repaid, data); @@ -336,7 +337,7 @@ contract Blue is IBlue { function flashLoan(address token, uint256 amount, bytes calldata data) external { IERC20(token).safeTransfer(msg.sender, amount); - emit FlashLoan(msg.sender, token, amount); + emit Events.FlashLoan(msg.sender, token, amount); IBlueFlashLoanCallback(msg.sender).onBlueFlashLoan(token, amount, data); @@ -348,7 +349,7 @@ contract Blue is IBlue { function setAuthorization(address authorized, bool newIsAuthorized) external { isAuthorized[msg.sender][authorized] = newIsAuthorized; - emit SetAuthorization(msg.sender, msg.sender, authorized, newIsAuthorized); + emit Events.SetAuthorization(msg.sender, msg.sender, authorized, newIsAuthorized); } /// @dev The signature is malleable, but it has no impact on the security here. @@ -369,11 +370,11 @@ contract Blue is IBlue { require(signatory != address(0) && authorizer == signatory, Errors.INVALID_SIGNATURE); - emit IncrementNonce(msg.sender, authorizer, usedNonce); + emit Events.IncrementNonce(msg.sender, authorizer, usedNonce); isAuthorized[authorizer][authorized] = newIsAuthorized; - emit SetAuthorization(msg.sender, authorizer, authorized, newIsAuthorized); + emit Events.SetAuthorization(msg.sender, authorizer, authorized, newIsAuthorized); } function _isSenderAuthorized(address user) internal view returns (bool) { @@ -404,7 +405,7 @@ contract Blue is IBlue { totalSupplyShares[id] += feeShares; } - emit AccrueInterests(id, borrowRate, accruedInterests, feeShares); + emit Events.AccrueInterests(id, borrowRate, accruedInterests, feeShares); } lastUpdate[id] = block.timestamp; diff --git a/src/interfaces/IBlue.sol b/src/interfaces/IBlue.sol index ccc22c156..f19f10e2e 100644 --- a/src/interfaces/IBlue.sol +++ b/src/interfaces/IBlue.sol @@ -21,63 +21,6 @@ struct Signature { } interface IBlue is IFlashLender { - event SupplyCollateral(Id indexed id, address indexed caller, address indexed onBehalf, uint256 amount); - event WithdrawCollateral( - Id indexed id, address caller, address indexed onBehalf, address indexed receiver, uint256 amount - ); - - event Supply(Id indexed id, address indexed caller, address indexed onBehalf, uint256 amount, uint256 shares); - event Withdraw( - Id indexed id, - address caller, - address indexed onBehalf, - address indexed receiver, - uint256 amount, - uint256 shares - ); - - event Borrow( - Id indexed id, - address caller, - address indexed onBehalf, - address indexed receiver, - uint256 amount, - uint256 shares - ); - event Repay(Id indexed id, address indexed caller, address indexed onBehalf, uint256 amount, uint256 shares); - - event Liquidate( - Id indexed id, - address indexed caller, - address indexed borrower, - uint256 repaid, - uint256 repaidShares, - uint256 seized, - uint256 badDebtShares - ); - - event FlashLoan(address indexed caller, address indexed token, uint256 amount); - - event SetOwner(address indexed newOwner); - - event SetFee(Id indexed id, uint256 fee); - - event SetFeeRecipient(address indexed feeRecipient); - - event CreateMarket(Id indexed id, Market market); - - event SetAuthorization( - address indexed caller, address indexed authorizer, address indexed authorized, bool isAuthorized - ); - - event IncrementNonce(address indexed caller, address indexed signatory, uint256 usedNonce); - - event EnableIrm(address indexed irm); - - event EnableLltv(uint256 lltv); - - event AccrueInterests(Id indexed id, uint256 borrowRate, uint256 accruedInterests, uint256 feeShares); - function DOMAIN_SEPARATOR() external view returns (bytes32); function owner() external view returns (address); diff --git a/src/libraries/Events.sol b/src/libraries/Events.sol new file mode 100644 index 000000000..fb5e358e2 --- /dev/null +++ b/src/libraries/Events.sol @@ -0,0 +1,63 @@ +// SPDX-License-Identifier: UNLICENSED +pragma solidity ^0.8.0; + +import {Id, Market} from "src/interfaces/IBlue.sol"; + +library Events { + event SupplyCollateral(Id indexed id, address indexed caller, address indexed onBehalf, uint256 amount); + event WithdrawCollateral( + Id indexed id, address caller, address indexed onBehalf, address indexed receiver, uint256 amount + ); + + event Supply(Id indexed id, address indexed caller, address indexed onBehalf, uint256 amount, uint256 shares); + event Withdraw( + Id indexed id, + address caller, + address indexed onBehalf, + address indexed receiver, + uint256 amount, + uint256 shares + ); + + event Borrow( + Id indexed id, + address caller, + address indexed onBehalf, + address indexed receiver, + uint256 amount, + uint256 shares + ); + event Repay(Id indexed id, address indexed caller, address indexed onBehalf, uint256 amount, uint256 shares); + + event Liquidate( + Id indexed id, + address indexed caller, + address indexed borrower, + uint256 repaid, + uint256 repaidShares, + uint256 seized, + uint256 badDebtShares + ); + + event FlashLoan(address indexed caller, address indexed token, uint256 amount); + + event SetOwner(address indexed newOwner); + + event SetFee(Id indexed id, uint256 fee); + + event SetFeeRecipient(address indexed feeRecipient); + + event CreateMarket(Id indexed id, Market market); + + event SetAuthorization( + address indexed caller, address indexed authorizer, address indexed authorized, bool isAuthorized + ); + + event IncrementNonce(address indexed caller, address indexed signatory, uint256 usedNonce); + + event EnableIrm(address indexed irm); + + event EnableLltv(uint256 lltv); + + event AccrueInterests(Id indexed id, uint256 borrowRate, uint256 accruedInterests, uint256 feeShares); +} From fff42680be436556bd9df6f4d76c2b769bec4c20 Mon Sep 17 00:00:00 2001 From: Patrick Kim Date: Wed, 9 Aug 2023 22:52:42 +0200 Subject: [PATCH 2/2] refactor: use lib naming --- src/Blue.sol | 38 ++++++++++----------- src/libraries/{Events.sol => EventsLib.sol} | 2 +- 2 files changed, 20 insertions(+), 20 deletions(-) rename src/libraries/{Events.sol => EventsLib.sol} (98%) diff --git a/src/Blue.sol b/src/Blue.sol index a7236f525..dda3d94e2 100644 --- a/src/Blue.sol +++ b/src/Blue.sol @@ -14,7 +14,7 @@ import {IOracle} from "./interfaces/IOracle.sol"; import {Id, Market, Signature, IBlue} from "./interfaces/IBlue.sol"; import {Errors} from "./libraries/Errors.sol"; -import {Events} from "./libraries/Events.sol"; +import {EventsLib} from "./libraries/EventsLib.sol"; import {UtilsLib} from "./libraries/UtilsLib.sol"; import {SharesMath} from "./libraries/SharesMath.sol"; import {FixedPointMathLib} from "./libraries/FixedPointMathLib.sol"; @@ -94,20 +94,20 @@ contract Blue is IBlue { function setOwner(address newOwner) external onlyOwner { owner = newOwner; - emit Events.SetOwner(newOwner); + emit EventsLib.SetOwner(newOwner); } function enableIrm(address irm) external onlyOwner { isIrmEnabled[irm] = true; - emit Events.EnableIrm(address(irm)); + emit EventsLib.EnableIrm(address(irm)); } function enableLltv(uint256 lltv) external onlyOwner { require(lltv < FixedPointMathLib.WAD, Errors.LLTV_TOO_HIGH); isLltvEnabled[lltv] = true; - emit Events.EnableLltv(lltv); + emit EventsLib.EnableLltv(lltv); } /// @notice It is the owner's responsibility to ensure a fee recipient is set before setting a non-zero fee. @@ -121,13 +121,13 @@ contract Blue is IBlue { fee[id] = newFee; - emit Events.SetFee(id, newFee); + emit EventsLib.SetFee(id, newFee); } function setFeeRecipient(address recipient) external onlyOwner { feeRecipient = recipient; - emit Events.SetFeeRecipient(recipient); + emit EventsLib.SetFeeRecipient(recipient); } // Markets management. @@ -140,7 +140,7 @@ contract Blue is IBlue { lastUpdate[id] = block.timestamp; - emit Events.CreateMarket(id, market); + emit EventsLib.CreateMarket(id, market); } // Supply management. @@ -162,7 +162,7 @@ contract Blue is IBlue { totalSupplyShares[id] += shares; totalSupply[id] += amount; - emit Events.Supply(id, msg.sender, onBehalf, amount, shares); + emit EventsLib.Supply(id, msg.sender, onBehalf, amount, shares); if (data.length > 0) IBlueSupplyCallback(msg.sender).onBlueSupply(amount, data); @@ -188,7 +188,7 @@ contract Blue is IBlue { totalSupplyShares[id] -= shares; totalSupply[id] -= amount; - emit Events.Withdraw(id, msg.sender, onBehalf, receiver, amount, shares); + emit EventsLib.Withdraw(id, msg.sender, onBehalf, receiver, amount, shares); require(totalBorrow[id] <= totalSupply[id], Errors.INSUFFICIENT_LIQUIDITY); @@ -216,7 +216,7 @@ contract Blue is IBlue { totalBorrowShares[id] += shares; totalBorrow[id] += amount; - emit Events.Borrow(id, msg.sender, onBehalf, receiver, amount, shares); + emit EventsLib.Borrow(id, msg.sender, onBehalf, receiver, amount, shares); require(_isHealthy(market, id, onBehalf), Errors.INSUFFICIENT_COLLATERAL); require(totalBorrow[id] <= totalSupply[id], Errors.INSUFFICIENT_LIQUIDITY); @@ -241,7 +241,7 @@ contract Blue is IBlue { totalBorrowShares[id] -= shares; totalBorrow[id] -= amount; - emit Events.Repay(id, msg.sender, onBehalf, amount, shares); + emit EventsLib.Repay(id, msg.sender, onBehalf, amount, shares); if (data.length > 0) IBlueRepayCallback(msg.sender).onBlueRepay(amount, data); @@ -261,7 +261,7 @@ contract Blue is IBlue { collateral[id][onBehalf] += amount; - emit Events.SupplyCollateral(id, msg.sender, onBehalf, amount); + emit EventsLib.SupplyCollateral(id, msg.sender, onBehalf, amount); if (data.length > 0) IBlueSupplyCollateralCallback(msg.sender).onBlueSupplyCollateral(amount, data); @@ -280,7 +280,7 @@ contract Blue is IBlue { collateral[id][onBehalf] -= amount; - emit Events.WithdrawCollateral(id, msg.sender, onBehalf, receiver, amount); + emit EventsLib.WithdrawCollateral(id, msg.sender, onBehalf, receiver, amount); require(_isHealthy(market, id, onBehalf), Errors.INSUFFICIENT_COLLATERAL); @@ -325,7 +325,7 @@ contract Blue is IBlue { IERC20(market.collateralAsset).safeTransfer(msg.sender, seized); - emit Events.Liquidate(id, msg.sender, borrower, repaid, repaidShares, seized, badDebtShares); + emit EventsLib.Liquidate(id, msg.sender, borrower, repaid, repaidShares, seized, badDebtShares); if (data.length > 0) IBlueLiquidateCallback(msg.sender).onBlueLiquidate(seized, repaid, data); @@ -337,7 +337,7 @@ contract Blue is IBlue { function flashLoan(address token, uint256 amount, bytes calldata data) external { IERC20(token).safeTransfer(msg.sender, amount); - emit Events.FlashLoan(msg.sender, token, amount); + emit EventsLib.FlashLoan(msg.sender, token, amount); IBlueFlashLoanCallback(msg.sender).onBlueFlashLoan(token, amount, data); @@ -349,7 +349,7 @@ contract Blue is IBlue { function setAuthorization(address authorized, bool newIsAuthorized) external { isAuthorized[msg.sender][authorized] = newIsAuthorized; - emit Events.SetAuthorization(msg.sender, msg.sender, authorized, newIsAuthorized); + emit EventsLib.SetAuthorization(msg.sender, msg.sender, authorized, newIsAuthorized); } /// @dev The signature is malleable, but it has no impact on the security here. @@ -370,11 +370,11 @@ contract Blue is IBlue { require(signatory != address(0) && authorizer == signatory, Errors.INVALID_SIGNATURE); - emit Events.IncrementNonce(msg.sender, authorizer, usedNonce); + emit EventsLib.IncrementNonce(msg.sender, authorizer, usedNonce); isAuthorized[authorizer][authorized] = newIsAuthorized; - emit Events.SetAuthorization(msg.sender, authorizer, authorized, newIsAuthorized); + emit EventsLib.SetAuthorization(msg.sender, authorizer, authorized, newIsAuthorized); } function _isSenderAuthorized(address user) internal view returns (bool) { @@ -405,7 +405,7 @@ contract Blue is IBlue { totalSupplyShares[id] += feeShares; } - emit Events.AccrueInterests(id, borrowRate, accruedInterests, feeShares); + emit EventsLib.AccrueInterests(id, borrowRate, accruedInterests, feeShares); } lastUpdate[id] = block.timestamp; diff --git a/src/libraries/Events.sol b/src/libraries/EventsLib.sol similarity index 98% rename from src/libraries/Events.sol rename to src/libraries/EventsLib.sol index fb5e358e2..19d80d386 100644 --- a/src/libraries/Events.sol +++ b/src/libraries/EventsLib.sol @@ -3,7 +3,7 @@ pragma solidity ^0.8.0; import {Id, Market} from "src/interfaces/IBlue.sol"; -library Events { +library EventsLib { event SupplyCollateral(Id indexed id, address indexed caller, address indexed onBehalf, uint256 amount); event WithdrawCollateral( Id indexed id, address caller, address indexed onBehalf, address indexed receiver, uint256 amount