Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move events back to lib #263

Merged
merged 2 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions src/Blue.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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 {EventsLib} from "./libraries/EventsLib.sol";
import {UtilsLib} from "./libraries/UtilsLib.sol";
import {SharesMath} from "./libraries/SharesMath.sol";
import {FixedPointMathLib} from "./libraries/FixedPointMathLib.sol";
Expand Down Expand Up @@ -93,20 +94,20 @@ contract Blue is IBlue {
function setOwner(address newOwner) external onlyOwner {
owner = newOwner;

emit SetOwner(newOwner);
emit EventsLib.SetOwner(newOwner);
}

function enableIrm(address irm) external onlyOwner {
isIrmEnabled[irm] = true;

emit 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 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.
Expand All @@ -120,13 +121,13 @@ contract Blue is IBlue {

fee[id] = newFee;

emit SetFee(id, newFee);
emit EventsLib.SetFee(id, newFee);
}

function setFeeRecipient(address recipient) external onlyOwner {
feeRecipient = recipient;

emit SetFeeRecipient(recipient);
emit EventsLib.SetFeeRecipient(recipient);
}

// Markets management.
Expand All @@ -139,7 +140,7 @@ contract Blue is IBlue {

lastUpdate[id] = block.timestamp;

emit CreateMarket(id, market);
emit EventsLib.CreateMarket(id, market);
}

// Supply management.
Expand All @@ -161,7 +162,7 @@ contract Blue is IBlue {
totalSupplyShares[id] += shares;
totalSupply[id] += amount;

emit 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);

Expand All @@ -187,7 +188,7 @@ contract Blue is IBlue {
totalSupplyShares[id] -= shares;
totalSupply[id] -= amount;

emit 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);

Expand Down Expand Up @@ -215,7 +216,7 @@ contract Blue is IBlue {
totalBorrowShares[id] += shares;
totalBorrow[id] += amount;

emit 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);
Expand All @@ -240,7 +241,7 @@ contract Blue is IBlue {
totalBorrowShares[id] -= shares;
totalBorrow[id] -= amount;

emit 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);

Expand All @@ -260,7 +261,7 @@ contract Blue is IBlue {

collateral[id][onBehalf] += amount;

emit SupplyCollateral(id, msg.sender, onBehalf, amount);
emit EventsLib.SupplyCollateral(id, msg.sender, onBehalf, amount);

if (data.length > 0) IBlueSupplyCollateralCallback(msg.sender).onBlueSupplyCollateral(amount, data);

Expand All @@ -279,7 +280,7 @@ contract Blue is IBlue {

collateral[id][onBehalf] -= amount;

emit WithdrawCollateral(id, msg.sender, onBehalf, receiver, amount);
emit EventsLib.WithdrawCollateral(id, msg.sender, onBehalf, receiver, amount);

require(_isHealthy(market, id, onBehalf), Errors.INSUFFICIENT_COLLATERAL);

Expand Down Expand Up @@ -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 EventsLib.Liquidate(id, msg.sender, borrower, repaid, repaidShares, seized, badDebtShares);

if (data.length > 0) IBlueLiquidateCallback(msg.sender).onBlueLiquidate(seized, repaid, data);

Expand All @@ -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 EventsLib.FlashLoan(msg.sender, token, amount);

IBlueFlashLoanCallback(msg.sender).onBlueFlashLoan(token, amount, data);

Expand All @@ -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 EventsLib.SetAuthorization(msg.sender, msg.sender, authorized, newIsAuthorized);
}

/// @dev The signature is malleable, but it has no impact on the security here.
Expand All @@ -369,11 +370,11 @@ contract Blue is IBlue {

require(signatory != address(0) && authorizer == signatory, Errors.INVALID_SIGNATURE);

emit IncrementNonce(msg.sender, authorizer, usedNonce);
emit EventsLib.IncrementNonce(msg.sender, authorizer, usedNonce);

isAuthorized[authorizer][authorized] = newIsAuthorized;

emit SetAuthorization(msg.sender, authorizer, authorized, newIsAuthorized);
emit EventsLib.SetAuthorization(msg.sender, authorizer, authorized, newIsAuthorized);
}

function _isSenderAuthorized(address user) internal view returns (bool) {
Expand Down Expand Up @@ -404,7 +405,7 @@ contract Blue is IBlue {
totalSupplyShares[id] += feeShares;
}

emit AccrueInterests(id, borrowRate, accruedInterests, feeShares);
emit EventsLib.AccrueInterests(id, borrowRate, accruedInterests, feeShares);
}

lastUpdate[id] = block.timestamp;
Expand Down
57 changes: 0 additions & 57 deletions src/interfaces/IBlue.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
63 changes: 63 additions & 0 deletions src/libraries/EventsLib.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

import {Id, Market} from "src/interfaces/IBlue.sol";

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
);

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);
}