From 96fb09d0becb3456bec99a69236503e59e14f027 Mon Sep 17 00:00:00 2001 From: Rubilmax Date: Wed, 12 Jul 2023 09:26:19 +0200 Subject: [PATCH 01/14] feat(events): emit events on interactions --- src/Blue.sol | 54 +++++++++++++++++++++++++--------------- src/libraries/Events.sol | 34 +++++++++++++++++++++++++ test/forge/Blue.t.sol | 6 ++--- 3 files changed, 71 insertions(+), 23 deletions(-) create mode 100644 src/libraries/Events.sol diff --git a/src/Blue.sol b/src/Blue.sol index 64a7633a9..6a11fd570 100644 --- a/src/Blue.sol +++ b/src/Blue.sol @@ -5,10 +5,13 @@ import {IIrm} from "src/interfaces/IIrm.sol"; import {IERC20} from "src/interfaces/IERC20.sol"; import {IOracle} from "src/interfaces/IOracle.sol"; +import {Events} from "./libraries/Events.sol"; import {SharesMath} from "./libraries/SharesMath.sol"; import {FixedPointMathLib} from "solmate/utils/FixedPointMathLib.sol"; import {SafeTransferLib} from "src/libraries/SafeTransferLib.sol"; +import {Owned} from "solmate/auth/Owned.sol"; + uint256 constant WAD = 1e18; uint256 constant ALPHA = 0.5e18; @@ -58,7 +61,7 @@ function toId(Market calldata market) pure returns (Id) { return Id.wrap(keccak256(abi.encode(market))); } -contract Blue { +contract Blue is Owned { using SharesMath for uint256; using FixedPointMathLib for uint256; using SafeTransferLib for IERC20; @@ -69,8 +72,6 @@ contract Blue { // Storage. - // Owner. - address public owner; // User' supply balances. mapping(Id => mapping(address => uint256)) public supplyShare; // User' borrow balances. @@ -98,9 +99,7 @@ contract Blue { // Constructor. - constructor(address newOwner) { - owner = newOwner; - + constructor(address newOwner) Owned(newOwner) { domainSeparator = keccak256( abi.encode( EIP712_DOMAIN_TYPEHASH, @@ -112,26 +111,19 @@ contract Blue { ); } - // Modifiers. - - modifier onlyOwner() { - require(msg.sender == owner, "not owner"); - _; - } - // Only owner functions. - function transferOwnership(address newOwner) external onlyOwner { - owner = newOwner; - } - function enableIrm(IIrm irm) external onlyOwner { isIrmEnabled[irm] = true; + + emit Events.IrmEnabled(address(irm)); } function enableLltv(uint256 lltv) external onlyOwner { require(lltv < WAD, "LLTV too high"); isLltvEnabled[lltv] = true; + + emit Events.LltvEnabled(lltv); } // Markets management. @@ -160,6 +152,8 @@ contract Blue { totalSupply[id] += amount; + emit Events.Supply(Id.unwrap(id), msg.sender, onBehalf, amount, shares); + market.borrowableAsset.safeTransferFrom(msg.sender, address(this), amount); } @@ -177,6 +171,8 @@ contract Blue { totalSupply[id] -= amount; + emit Events.Withdraw(Id.unwrap(id), msg.sender, onBehalf, amount, shares); + require(totalBorrow[id] <= totalSupply[id], "not enough liquidity"); market.borrowableAsset.safeTransfer(msg.sender, amount); @@ -198,6 +194,8 @@ contract Blue { totalBorrow[id] += amount; + emit Events.Borrow(Id.unwrap(id), msg.sender, onBehalf, amount, shares); + require(isHealthy(market, id, onBehalf), "not enough collateral"); require(totalBorrow[id] <= totalSupply[id], "not enough liquidity"); @@ -217,6 +215,8 @@ contract Blue { totalBorrow[id] -= amount; + emit Events.Repay(Id.unwrap(id), msg.sender, onBehalf, amount, shares); + market.borrowableAsset.safeTransferFrom(msg.sender, address(this), amount); } @@ -232,6 +232,8 @@ contract Blue { collateral[id][onBehalf] += amount; + emit Events.CollateralSupply(Id.unwrap(id), msg.sender, onBehalf, amount); + market.collateralAsset.safeTransferFrom(msg.sender, address(this), amount); } @@ -245,6 +247,8 @@ contract Blue { collateral[id][onBehalf] -= amount; + emit Events.CollateralWithdraw(Id.unwrap(id), msg.sender, onBehalf, amount); + require(isHealthy(market, id, onBehalf), "not enough collateral"); market.collateralAsset.safeTransfer(msg.sender, amount); @@ -274,13 +278,18 @@ contract Blue { collateral[id][borrower] -= seized; + emit Events.Liquidation(Id.unwrap(id), msg.sender, borrower, repaid, repaidShares, seized); + // Realize the bad debt if needed. if (collateral[id][borrower] == 0) { - uint256 badDebt = borrowShare[id][borrower].toAssetsUp(totalBorrow[id], totalBorrowShares[id]); + uint256 badDebtShares = borrowShare[id][borrower]; + uint256 badDebt = badDebtShares.toAssetsUp(totalBorrow[id], totalBorrowShares[id]); totalSupply[id] -= badDebt; totalBorrow[id] -= badDebt; - totalBorrowShares[id] -= borrowShare[id][borrower]; + totalBorrowShares[id] -= badDebtShares; borrowShare[id][borrower] = 0; + + emit Events.BadDebtRealized(Id.unwrap(id), borrower, badDebt, badDebtShares); } market.collateralAsset.safeTransfer(msg.sender, seized); @@ -320,6 +329,8 @@ contract Blue { function _setApproval(address delegator, address manager, bool isAllowed) internal { approval[delegator][manager] = isAllowed; + + emit Events.Approval(msg.sender, delegator, manager, isAllowed); } function _isSenderApprovedFor(address user) internal view returns (bool) { @@ -331,14 +342,17 @@ contract Blue { function accrueInterests(Market calldata market, Id id) private { uint256 marketTotalBorrow = totalBorrow[id]; + uint256 accruedInterests; if (marketTotalBorrow != 0) { uint256 borrowRate = market.irm.borrowRate(market); - uint256 accruedInterests = marketTotalBorrow.mulWadDown(borrowRate * (block.timestamp - lastUpdate[id])); + accruedInterests = marketTotalBorrow.mulWadDown(borrowRate * (block.timestamp - lastUpdate[id])); totalBorrow[id] = marketTotalBorrow + accruedInterests; totalSupply[id] += accruedInterests; } lastUpdate[id] = block.timestamp; + + emit Events.InterestsAccrued(Id.unwrap(id), accruedInterests); } // Health check. diff --git a/src/libraries/Events.sol b/src/libraries/Events.sol new file mode 100644 index 000000000..48376fbf3 --- /dev/null +++ b/src/libraries/Events.sol @@ -0,0 +1,34 @@ +// SPDX-License-Identifier: UNLICENSED +pragma solidity ^0.8.0; + +library Events { + event CollateralSupply(bytes32 indexed id, address indexed caller, address indexed onBehalf, uint256 amount); + event CollateralWithdraw(bytes32 indexed id, address caller, address indexed onBehalf, uint256 amount); + + event Supply(bytes32 indexed id, address indexed caller, address indexed onBehalf, uint256 amount, uint256 shares); + event Withdraw( + bytes32 indexed id, address indexed caller, address indexed onBehalf, uint256 amount, uint256 shares + ); + + event Borrow(bytes32 indexed id, address indexed caller, address indexed onBehalf, uint256 amount, uint256 shares); + event Repay(bytes32 indexed id, address indexed caller, address indexed onBehalf, uint256 amount, uint256 shares); + + event Liquidation( + bytes32 indexed id, + address indexed caller, + address indexed borrower, + uint256 repaid, + uint256 repaidShares, + uint256 seized + ); + + event BadDebtRealized(bytes32 indexed id, address indexed borrower, uint256 amount, uint256 shares); + + event Approval(address indexed caller, address indexed delegator, address indexed manager, bool isApproved); + + event IrmEnabled(address indexed irm); + + event LltvEnabled(uint256 lltv); + + event InterestsAccrued(bytes32 indexed id, uint256 accruedInterests); +} diff --git a/test/forge/Blue.t.sol b/test/forge/Blue.t.sol index 19d63caad..52e358e33 100644 --- a/test/forge/Blue.t.sol +++ b/test/forge/Blue.t.sol @@ -131,7 +131,7 @@ contract BlueTest is Test { Blue blue2 = new Blue(OWNER); vm.prank(attacker); - vm.expectRevert("not owner"); + vm.expectRevert("UNAUTHORIZED"); blue2.transferOwnership(newOwner); } @@ -139,7 +139,7 @@ contract BlueTest is Test { vm.assume(attacker != blue.owner()); vm.prank(attacker); - vm.expectRevert("not owner"); + vm.expectRevert("UNAUTHORIZED"); blue.enableIrm(newIrm); } @@ -171,7 +171,7 @@ contract BlueTest is Test { vm.assume(attacker != OWNER); vm.prank(attacker); - vm.expectRevert("not owner"); + vm.expectRevert("UNAUTHORIZED"); blue.enableLltv(newLltv); } From c6d929c1c6d3369f9952aa547f6c16cc21f86b12 Mon Sep 17 00:00:00 2001 From: MerlinEgalite Date: Sat, 29 Jul 2023 13:22:35 +0200 Subject: [PATCH 02/14] feat: add missing events --- src/Blue.sol | 28 ++++++++++++++++++++++++---- src/libraries/Events.sol | 14 ++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/Blue.sol b/src/Blue.sol index f2aa8045c..44d10724a 100644 --- a/src/Blue.sol +++ b/src/Blue.sol @@ -87,9 +87,10 @@ contract Blue is IFlashLender { constructor(address newOwner) { owner = newOwner; - domainSeparator = keccak256(abi.encode(EIP712_DOMAIN_TYPEHASH, keccak256(bytes(EIP712_NAME)), block.chainid, address(this))); + + emit Events.OwnershipTransferred(address(0), newOwner); } // Modifiers. @@ -103,6 +104,8 @@ contract Blue is IFlashLender { function transferOwnership(address newOwner) external onlyOwner { owner = newOwner; + + emit Events.OwnershipTransferred(owner, newOwner); } function enableIrm(IIrm irm) external onlyOwner { @@ -124,10 +127,14 @@ contract Blue is IFlashLender { require(lastUpdate[id] != 0, Errors.MARKET_NOT_CREATED); require(newFee <= MAX_FEE, Errors.MAX_FEE_EXCEEDED); fee[id] = newFee; + + emit Events.FeeSet(Id.unwrap(id), newFee); } function setFeeRecipient(address recipient) external onlyOwner { feeRecipient = recipient; + + emit Events.FeeRecipientSet(recipient); } // Markets management. @@ -138,6 +145,8 @@ contract Blue is IFlashLender { require(isLltvEnabled[market.lltv], Errors.LLTV_NOT_ENABLED); require(lastUpdate[id] == 0, Errors.MARKET_CREATED); + emit Events.MarketCreated(market); + _accrueInterests(market, id); } @@ -323,9 +332,12 @@ contract Blue is IFlashLender { address signatory = ecrecover(digest, signature.v, signature.r, signature.s); require(delegator == signatory, Errors.INVALID_SIGNATURE); - require(nonce == userNonce[signatory]++, Errors.INVALID_NONCE); + uint256 usedNonce = userNonce[signatory]++; + require(nonce == usedNonce, Errors.INVALID_NONCE); - isApproved[signatory][manager] = approval; + emit Events.NonceIncremented(msg.sender, signatory, usedNonce); + + _setApproval(signatory, manager, approval); } // Flash Loans. @@ -336,19 +348,27 @@ contract Blue is IFlashLender { receiver.onFlashLoan(msg.sender, token, amount, data); + emit Events.Flashloan(msg.sender, token, address(receiver), amount); + IERC20(token).safeTransferFrom(address(receiver), address(this), amount); } // Position management. function setApproval(address manager, bool isAllowed) external { - isApproved[msg.sender][manager] = isAllowed; + _setApproval(msg.sender, manager, isAllowed); } function _isSenderOrIsApproved(address user) internal view returns (bool) { return msg.sender == user || isApproved[user][msg.sender]; } + function _setApproval(address delegator, address manager, bool isAllowed) internal { + isApproved[delegator][manager] = isAllowed; + + emit Events.Approval(msg.sender, delegator, manager, isAllowed); + } + // Interests management. function _accrueInterests(Market memory market, Id id) internal { diff --git a/src/libraries/Events.sol b/src/libraries/Events.sol index 48376fbf3..b77247fdf 100644 --- a/src/libraries/Events.sol +++ b/src/libraries/Events.sol @@ -1,6 +1,8 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.0; +import {Market} from "src/libraries/MarketLib.sol"; + library Events { event CollateralSupply(bytes32 indexed id, address indexed caller, address indexed onBehalf, uint256 amount); event CollateralWithdraw(bytes32 indexed id, address caller, address indexed onBehalf, uint256 amount); @@ -22,10 +24,22 @@ library Events { uint256 seized ); + event Flashloan(address indexed caller, address indexed token, address indexed receiver, uint256 amount); + + event OwnershipTransferred(address indexed oldOwner, address indexed newOwner); + + event FeeSet(bytes32 indexed id, uint256 fee); + + event FeeRecipientSet(address indexed feeRecipient); + + event MarketCreated(Market market); + event BadDebtRealized(bytes32 indexed id, address indexed borrower, uint256 amount, uint256 shares); event Approval(address indexed caller, address indexed delegator, address indexed manager, bool isApproved); + event NonceIncremented(address indexed caller, address indexed signatory, uint256 usedNonce); + event IrmEnabled(address indexed irm); event LltvEnabled(uint256 lltv); From 218f6064679c09c248564a0ae8e7656e2ddf4deb Mon Sep 17 00:00:00 2001 From: MerlinEgalite Date: Tue, 1 Aug 2023 16:07:13 +0200 Subject: [PATCH 03/14] feat: use Id --- src/Blue.sol | 20 ++++++++++---------- src/libraries/Events.sol | 29 +++++++++++------------------ 2 files changed, 21 insertions(+), 28 deletions(-) diff --git a/src/Blue.sol b/src/Blue.sol index b57b71a50..a62e95808 100644 --- a/src/Blue.sol +++ b/src/Blue.sol @@ -122,7 +122,7 @@ contract Blue is IFlashLender { require(newFee <= MAX_FEE, Errors.MAX_FEE_EXCEEDED); fee[id] = newFee; - emit Events.FeeSet(Id.unwrap(id), newFee); + emit Events.FeeSet(id, newFee); } function setFeeRecipient(address recipient) external onlyOwner { @@ -159,7 +159,7 @@ contract Blue is IFlashLender { totalSupply[id] += amount; - emit Events.Supply(Id.unwrap(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); @@ -180,7 +180,7 @@ contract Blue is IFlashLender { totalSupply[id] -= amount; - emit Events.Withdraw(Id.unwrap(id), msg.sender, onBehalf, amount, shares); + emit Events.Withdraw(id, msg.sender, onBehalf, amount, shares); require(totalBorrow[id] <= totalSupply[id], Errors.INSUFFICIENT_LIQUIDITY); @@ -203,7 +203,7 @@ contract Blue is IFlashLender { totalBorrow[id] += amount; - emit Events.Borrow(Id.unwrap(id), msg.sender, onBehalf, amount, shares); + emit Events.Borrow(id, msg.sender, onBehalf, amount, shares); require(_isHealthy(market, id, onBehalf), Errors.INSUFFICIENT_COLLATERAL); require(totalBorrow[id] <= totalSupply[id], Errors.INSUFFICIENT_LIQUIDITY); @@ -224,7 +224,7 @@ contract Blue is IFlashLender { totalBorrow[id] -= amount; - emit Events.Repay(Id.unwrap(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); @@ -243,7 +243,7 @@ contract Blue is IFlashLender { collateral[id][onBehalf] += amount; - emit Events.CollateralSupply(Id.unwrap(id), msg.sender, onBehalf, amount); + emit Events.CollateralSupply(id, msg.sender, onBehalf, amount); if (data.length > 0) IBlueSupplyCollateralCallback(msg.sender).onBlueSupplyCollateral(amount, data); @@ -260,7 +260,7 @@ contract Blue is IFlashLender { collateral[id][onBehalf] -= amount; - emit Events.CollateralWithdraw(Id.unwrap(id), msg.sender, onBehalf, amount); + emit Events.CollateralWithdraw(id, msg.sender, onBehalf, amount); require(_isHealthy(market, id, onBehalf), Errors.INSUFFICIENT_COLLATERAL); @@ -293,7 +293,7 @@ contract Blue is IFlashLender { collateral[id][borrower] -= seized; - emit Events.Liquidation(Id.unwrap(id), msg.sender, borrower, repaid, repaidShares, seized); + emit Events.Liquidation(id, msg.sender, borrower, repaid, repaidShares, seized); // Realize the bad debt if needed. if (collateral[id][borrower] == 0) { @@ -304,7 +304,7 @@ contract Blue is IFlashLender { totalBorrowShares[id] -= badDebtShares; borrowShare[id][borrower] = 0; - emit Events.BadDebtRealized(Id.unwrap(id), borrower, badDebt, badDebtShares); + emit Events.BadDebtRealized(id, borrower, badDebt, badDebtShares); } market.collateralAsset.safeTransfer(msg.sender, seized); @@ -391,7 +391,7 @@ contract Blue is IFlashLender { lastUpdate[id] = block.timestamp; - emit Events.InterestsAccrued(Id.unwrap(id), accruedInterests); + emit Events.InterestsAccrued(id, accruedInterests); } // Health check. diff --git a/src/libraries/Events.sol b/src/libraries/Events.sol index b77247fdf..be00b4adb 100644 --- a/src/libraries/Events.sol +++ b/src/libraries/Events.sol @@ -1,40 +1,33 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.0; -import {Market} from "src/libraries/MarketLib.sol"; +import {Id, Market} from "src/libraries/MarketLib.sol"; library Events { - event CollateralSupply(bytes32 indexed id, address indexed caller, address indexed onBehalf, uint256 amount); - event CollateralWithdraw(bytes32 indexed id, address caller, address indexed onBehalf, uint256 amount); + event CollateralSupply(Id id, address indexed caller, address indexed onBehalf, uint256 amount); + event CollateralWithdraw(Id id, address caller, address indexed onBehalf, uint256 amount); - event Supply(bytes32 indexed id, address indexed caller, address indexed onBehalf, uint256 amount, uint256 shares); - event Withdraw( - bytes32 indexed id, address indexed caller, address indexed onBehalf, uint256 amount, uint256 shares - ); + event Supply(Id id, address indexed caller, address indexed onBehalf, uint256 amount, uint256 shares); + event Withdraw(Id id, address indexed caller, address indexed onBehalf, uint256 amount, uint256 shares); - event Borrow(bytes32 indexed id, address indexed caller, address indexed onBehalf, uint256 amount, uint256 shares); - event Repay(bytes32 indexed id, address indexed caller, address indexed onBehalf, uint256 amount, uint256 shares); + event Borrow(Id id, address indexed caller, address indexed onBehalf, uint256 amount, uint256 shares); + event Repay(Id id, address indexed caller, address indexed onBehalf, uint256 amount, uint256 shares); event Liquidation( - bytes32 indexed id, - address indexed caller, - address indexed borrower, - uint256 repaid, - uint256 repaidShares, - uint256 seized + Id id, address indexed caller, address indexed borrower, uint256 repaid, uint256 repaidShares, uint256 seized ); event Flashloan(address indexed caller, address indexed token, address indexed receiver, uint256 amount); event OwnershipTransferred(address indexed oldOwner, address indexed newOwner); - event FeeSet(bytes32 indexed id, uint256 fee); + event FeeSet(Id id, uint256 fee); event FeeRecipientSet(address indexed feeRecipient); event MarketCreated(Market market); - event BadDebtRealized(bytes32 indexed id, address indexed borrower, uint256 amount, uint256 shares); + event BadDebtRealized(Id id, address indexed borrower, uint256 amount, uint256 shares); event Approval(address indexed caller, address indexed delegator, address indexed manager, bool isApproved); @@ -44,5 +37,5 @@ library Events { event LltvEnabled(uint256 lltv); - event InterestsAccrued(bytes32 indexed id, uint256 accruedInterests); + event InterestsAccrued(Id id, uint256 accruedInterests); } From 99e5068c917957dc6a4a00f5b7c6bf92328e3c55 Mon Sep 17 00:00:00 2001 From: MerlinEgalite Date: Tue, 1 Aug 2023 20:59:58 +0200 Subject: [PATCH 04/14] feat: indexed id --- src/libraries/Events.sol | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/libraries/Events.sol b/src/libraries/Events.sol index be00b4adb..a46e90b8c 100644 --- a/src/libraries/Events.sol +++ b/src/libraries/Events.sol @@ -4,30 +4,35 @@ pragma solidity ^0.8.0; import {Id, Market} from "src/libraries/MarketLib.sol"; library Events { - event CollateralSupply(Id id, address indexed caller, address indexed onBehalf, uint256 amount); - event CollateralWithdraw(Id id, address caller, address indexed onBehalf, uint256 amount); + event CollateralSupply(Id indexed id, address indexed caller, address indexed onBehalf, uint256 amount); + event CollateralWithdraw(Id indexed id, address caller, address indexed onBehalf, uint256 amount); - event Supply(Id id, address indexed caller, address indexed onBehalf, uint256 amount, uint256 shares); - event Withdraw(Id id, address indexed caller, address indexed onBehalf, uint256 amount, uint256 shares); + event Supply(Id indexed id, address indexed caller, address indexed onBehalf, uint256 amount, uint256 shares); + event Withdraw(Id indexed id, address indexed caller, address indexed onBehalf, uint256 amount, uint256 shares); - event Borrow(Id id, address indexed caller, address indexed onBehalf, uint256 amount, uint256 shares); - event Repay(Id id, address indexed caller, address indexed onBehalf, uint256 amount, uint256 shares); + event Borrow(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 amount, uint256 shares); event Liquidation( - Id id, address indexed caller, address indexed borrower, uint256 repaid, uint256 repaidShares, uint256 seized + Id indexed id, + address indexed caller, + address indexed borrower, + uint256 repaid, + uint256 repaidShares, + uint256 seized ); event Flashloan(address indexed caller, address indexed token, address indexed receiver, uint256 amount); event OwnershipTransferred(address indexed oldOwner, address indexed newOwner); - event FeeSet(Id id, uint256 fee); + event FeeSet(Id indexed id, uint256 fee); event FeeRecipientSet(address indexed feeRecipient); event MarketCreated(Market market); - event BadDebtRealized(Id id, address indexed borrower, uint256 amount, uint256 shares); + event BadDebtRealized(Id indexed id, address indexed borrower, uint256 amount, uint256 shares); event Approval(address indexed caller, address indexed delegator, address indexed manager, bool isApproved); @@ -37,5 +42,5 @@ library Events { event LltvEnabled(uint256 lltv); - event InterestsAccrued(Id id, uint256 accruedInterests); + event InterestsAccrued(Id indexed id, uint256 accruedInterests); } From 0cedf699917143b0e3f28882d6ab50fe6a0f6b8b Mon Sep 17 00:00:00 2001 From: MerlinEgalite Date: Tue, 1 Aug 2023 21:11:28 +0200 Subject: [PATCH 05/14] feat: apply suggestions --- src/Blue.sol | 12 ++++++------ src/libraries/Events.sol | 28 +++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/Blue.sol b/src/Blue.sol index a62e95808..7e6a35fc5 100644 --- a/src/Blue.sol +++ b/src/Blue.sol @@ -99,7 +99,7 @@ contract Blue is IFlashLender { function setOwner(address newOwner) external onlyOwner { owner = newOwner; - emit Events.OwnershipTransferred(owner, newOwner); + emit Events.OwnerSet(newOwner); } function enableIrm(IIrm irm) external onlyOwner { @@ -180,7 +180,7 @@ contract Blue is IFlashLender { totalSupply[id] -= amount; - emit Events.Withdraw(id, msg.sender, onBehalf, amount, shares); + emit Events.Withdraw(id, msg.sender, onBehalf, receiver, amount, shares); require(totalBorrow[id] <= totalSupply[id], Errors.INSUFFICIENT_LIQUIDITY); @@ -203,7 +203,7 @@ contract Blue is IFlashLender { totalBorrow[id] += amount; - emit Events.Borrow(id, msg.sender, onBehalf, 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); @@ -260,7 +260,7 @@ contract Blue is IFlashLender { collateral[id][onBehalf] -= amount; - emit Events.CollateralWithdraw(id, msg.sender, onBehalf, amount); + emit Events.CollateralWithdraw(id, msg.sender, onBehalf, receiver, amount); require(_isHealthy(market, id, onBehalf), Errors.INSUFFICIENT_COLLATERAL); @@ -361,7 +361,7 @@ contract Blue is IFlashLender { function _setAuthorization(address authorizer, address authorized, bool newIsAuthorized) internal { isAuthorized[authorizer][authorized] = newIsAuthorized; - emit Events.Approval(msg.sender, authorizer, authorized, newIsAuthorized); + emit Events.Authorization(msg.sender, authorizer, authorized, newIsAuthorized); } // Interests management. @@ -376,7 +376,7 @@ contract Blue is IFlashLender { uint256 accruedInterests; if (marketTotalBorrow != 0) { uint256 borrowRate = market.irm.borrowRate(market); - uint256 accruedInterests = marketTotalBorrow.mulWadDown(borrowRate * elapsed); + accruedInterests = marketTotalBorrow.mulWadDown(borrowRate * elapsed); totalBorrow[id] = marketTotalBorrow + accruedInterests; totalSupply[id] += accruedInterests; diff --git a/src/libraries/Events.sol b/src/libraries/Events.sol index a46e90b8c..c2db7bf02 100644 --- a/src/libraries/Events.sol +++ b/src/libraries/Events.sol @@ -5,12 +5,28 @@ import {Id, Market} from "src/libraries/MarketLib.sol"; library Events { event CollateralSupply(Id indexed id, address indexed caller, address indexed onBehalf, uint256 amount); - event CollateralWithdraw(Id indexed id, address caller, address indexed onBehalf, uint256 amount); + event CollateralWithdraw( + 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 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 indexed caller, address indexed onBehalf, 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 Liquidation( @@ -24,7 +40,7 @@ library Events { event Flashloan(address indexed caller, address indexed token, address indexed receiver, uint256 amount); - event OwnershipTransferred(address indexed oldOwner, address indexed newOwner); + event OwnerSet(address indexed newOwner); event FeeSet(Id indexed id, uint256 fee); @@ -34,7 +50,9 @@ library Events { event BadDebtRealized(Id indexed id, address indexed borrower, uint256 amount, uint256 shares); - event Approval(address indexed caller, address indexed delegator, address indexed manager, bool isApproved); + event Authorization( + address indexed caller, address indexed authorizer, address indexed authorized, bool isAuthorized + ); event NonceIncremented(address indexed caller, address indexed signatory, uint256 usedNonce); From abc03fd4f5d8fcf2461d9086abe04922b7bf8cbe Mon Sep 17 00:00:00 2001 From: MerlinEgalite Date: Tue, 1 Aug 2023 21:18:43 +0200 Subject: [PATCH 06/14] feat: applys suggestions 2 --- src/Blue.sol | 5 +++-- src/libraries/Events.sol | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Blue.sol b/src/Blue.sol index 7e6a35fc5..086b68285 100644 --- a/src/Blue.sol +++ b/src/Blue.sol @@ -139,7 +139,7 @@ contract Blue is IFlashLender { require(isLltvEnabled[market.lltv], Errors.LLTV_NOT_ENABLED); require(lastUpdate[id] == 0, Errors.MARKET_CREATED); - emit Events.MarketCreated(market); + emit Events.MarketCreated(id, market); _accrueInterests(market, id); } @@ -374,6 +374,7 @@ contract Blue is IFlashLender { uint256 marketTotalBorrow = totalBorrow[id]; uint256 accruedInterests; + uint256 feeShares; if (marketTotalBorrow != 0) { uint256 borrowRate = market.irm.borrowRate(market); accruedInterests = marketTotalBorrow.mulWadDown(borrowRate * elapsed); @@ -391,7 +392,7 @@ contract Blue is IFlashLender { lastUpdate[id] = block.timestamp; - emit Events.InterestsAccrued(id, accruedInterests); + emit Events.InterestsAccrued(id, accruedInterests, feeShares); } // Health check. diff --git a/src/libraries/Events.sol b/src/libraries/Events.sol index c2db7bf02..d2f0adbed 100644 --- a/src/libraries/Events.sol +++ b/src/libraries/Events.sol @@ -46,7 +46,7 @@ library Events { event FeeRecipientSet(address indexed feeRecipient); - event MarketCreated(Market market); + event MarketCreated(Id indexed id, Market market); event BadDebtRealized(Id indexed id, address indexed borrower, uint256 amount, uint256 shares); @@ -60,5 +60,5 @@ library Events { event LltvEnabled(uint256 lltv); - event InterestsAccrued(Id indexed id, uint256 accruedInterests); + event InterestsAccrued(Id indexed id, uint256 accruedInterests, uint256 feeShares); } From 88a5d10b6dd0c9f72b3f64b8d46cafa90d0deea3 Mon Sep 17 00:00:00 2001 From: MerlinEgalite Date: Wed, 2 Aug 2023 09:58:51 +0200 Subject: [PATCH 07/14] feat: events in if block --- src/Blue.sol | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/Blue.sol b/src/Blue.sol index 086b68285..8cc726d6b 100644 --- a/src/Blue.sol +++ b/src/Blue.sol @@ -373,26 +373,25 @@ contract Blue is IFlashLender { uint256 marketTotalBorrow = totalBorrow[id]; - uint256 accruedInterests; - uint256 feeShares; if (marketTotalBorrow != 0) { uint256 borrowRate = market.irm.borrowRate(market); - accruedInterests = marketTotalBorrow.mulWadDown(borrowRate * elapsed); + uint256 accruedInterests = marketTotalBorrow.mulWadDown(borrowRate * elapsed); totalBorrow[id] = marketTotalBorrow + accruedInterests; totalSupply[id] += accruedInterests; + uint256 feeShares; if (fee[id] != 0) { uint256 feeAmount = accruedInterests.mulWadDown(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. - uint256 feeShares = feeAmount.mulDivDown(totalSupplyShares[id], totalSupply[id] - feeAmount); + feeShares = feeAmount.mulDivDown(totalSupplyShares[id], totalSupply[id] - feeAmount); supplyShare[id][feeRecipient] += feeShares; totalSupplyShares[id] += feeShares; } + + emit Events.InterestsAccrued(id, accruedInterests, feeShares); } lastUpdate[id] = block.timestamp; - - emit Events.InterestsAccrued(id, accruedInterests, feeShares); } // Health check. From 2903b500a1a3bad43964a2d064538d4b17e0e2c2 Mon Sep 17 00:00:00 2001 From: MerlinEgalite Date: Wed, 2 Aug 2023 10:07:34 +0200 Subject: [PATCH 08/14] feat: add missing event + renaming --- src/Blue.sol | 26 ++++++++++++++------------ src/libraries/Events.sol | 20 ++++++++++---------- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/Blue.sol b/src/Blue.sol index 8cc726d6b..024186147 100644 --- a/src/Blue.sol +++ b/src/Blue.sol @@ -99,20 +99,20 @@ contract Blue is IFlashLender { function setOwner(address newOwner) external onlyOwner { owner = newOwner; - emit Events.OwnerSet(newOwner); + emit Events.SetOwner(newOwner); } function enableIrm(IIrm irm) external onlyOwner { isIrmEnabled[irm] = true; - emit Events.IrmEnabled(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 Events.LltvEnabled(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. @@ -122,13 +122,13 @@ contract Blue is IFlashLender { require(newFee <= MAX_FEE, Errors.MAX_FEE_EXCEEDED); fee[id] = newFee; - emit Events.FeeSet(id, newFee); + emit Events.SetFee(id, newFee); } function setFeeRecipient(address recipient) external onlyOwner { feeRecipient = recipient; - emit Events.FeeRecipientSet(recipient); + emit Events.SetFeeRecipient(recipient); } // Markets management. @@ -139,7 +139,7 @@ contract Blue is IFlashLender { require(isLltvEnabled[market.lltv], Errors.LLTV_NOT_ENABLED); require(lastUpdate[id] == 0, Errors.MARKET_CREATED); - emit Events.MarketCreated(id, market); + emit Events.CreateMarket(id, market); _accrueInterests(market, id); } @@ -293,7 +293,7 @@ contract Blue is IFlashLender { collateral[id][borrower] -= seized; - emit Events.Liquidation(id, msg.sender, borrower, repaid, repaidShares, seized); + emit Events.Liquidate(id, msg.sender, borrower, repaid, repaidShares, seized); // Realize the bad debt if needed. if (collateral[id][borrower] == 0) { @@ -304,7 +304,7 @@ contract Blue is IFlashLender { totalBorrowShares[id] -= badDebtShares; borrowShare[id][borrower] = 0; - emit Events.BadDebtRealized(id, borrower, badDebt, badDebtShares); + emit Events.RealizeBadDebt(id, borrower, badDebt, badDebtShares); } market.collateralAsset.safeTransfer(msg.sender, seized); @@ -339,14 +339,16 @@ contract Blue is IFlashLender { ) external { require(block.timestamp < deadline, Errors.SIGNATURE_EXPIRED); - bytes32 hashStruct = keccak256( - abi.encode(AUTHORIZATION_TYPEHASH, authorizer, authorized, newIsAuthorized, nonce[authorizer]++, deadline) - ); + uint256 usedNonce = nonce[authorizer]++; + bytes32 hashStruct = + keccak256(abi.encode(AUTHORIZATION_TYPEHASH, authorizer, authorized, newIsAuthorized, usedNonce, deadline)); bytes32 digest = keccak256(abi.encodePacked("\x19\x01", DOMAIN_SEPARATOR, hashStruct)); address signatory = ecrecover(digest, signature.v, signature.r, signature.s); require(signatory != address(0) && authorizer == signatory, Errors.INVALID_SIGNATURE); + emit Events.IncrementNonce(msg.sender, signatory, usedNonce); + _setAuthorization(signatory, authorized, newIsAuthorized); } @@ -388,7 +390,7 @@ contract Blue is IFlashLender { totalSupplyShares[id] += feeShares; } - emit Events.InterestsAccrued(id, accruedInterests, feeShares); + emit Events.AccrueInterests(id, accruedInterests, feeShares); } lastUpdate[id] = block.timestamp; diff --git a/src/libraries/Events.sol b/src/libraries/Events.sol index d2f0adbed..b4663d0fe 100644 --- a/src/libraries/Events.sol +++ b/src/libraries/Events.sol @@ -29,7 +29,7 @@ library Events { ); event Repay(Id indexed id, address indexed caller, address indexed onBehalf, uint256 amount, uint256 shares); - event Liquidation( + event Liquidate( Id indexed id, address indexed caller, address indexed borrower, @@ -40,25 +40,25 @@ library Events { event Flashloan(address indexed caller, address indexed token, address indexed receiver, uint256 amount); - event OwnerSet(address indexed newOwner); + event SetOwner(address indexed newOwner); - event FeeSet(Id indexed id, uint256 fee); + event SetFee(Id indexed id, uint256 fee); - event FeeRecipientSet(address indexed feeRecipient); + event SetFeeRecipient(address indexed feeRecipient); - event MarketCreated(Id indexed id, Market market); + event CreateMarket(Id indexed id, Market market); - event BadDebtRealized(Id indexed id, address indexed borrower, uint256 amount, uint256 shares); + event RealizeBadDebt(Id indexed id, address indexed borrower, uint256 amount, uint256 shares); event Authorization( address indexed caller, address indexed authorizer, address indexed authorized, bool isAuthorized ); - event NonceIncremented(address indexed caller, address indexed signatory, uint256 usedNonce); + event IncrementNonce(address indexed caller, address indexed signatory, uint256 usedNonce); - event IrmEnabled(address indexed irm); + event EnableIrm(address indexed irm); - event LltvEnabled(uint256 lltv); + event EnableLltv(uint256 lltv); - event InterestsAccrued(Id indexed id, uint256 accruedInterests, uint256 feeShares); + event AccrueInterests(Id indexed id, uint256 accruedInterests, uint256 feeShares); } From aea47a998ebfc8ba863100fd934db7713b071146 Mon Sep 17 00:00:00 2001 From: MerlinEgalite Date: Wed, 2 Aug 2023 10:14:04 +0200 Subject: [PATCH 09/14] refactor: flashloan event renaming --- src/Blue.sol | 2 +- src/libraries/Events.sol | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Blue.sol b/src/Blue.sol index 024186147..e087aa00a 100644 --- a/src/Blue.sol +++ b/src/Blue.sol @@ -322,7 +322,7 @@ contract Blue is IFlashLender { receiver.onBlueFlashLoan(msg.sender, token, amount, data); - emit Events.Flashloan(msg.sender, token, address(receiver), amount); + emit Events.FlashLoan(msg.sender, token, address(receiver), amount); IERC20(token).safeTransferFrom(address(receiver), address(this), amount); } diff --git a/src/libraries/Events.sol b/src/libraries/Events.sol index b4663d0fe..704c2fafc 100644 --- a/src/libraries/Events.sol +++ b/src/libraries/Events.sol @@ -38,7 +38,7 @@ library Events { uint256 seized ); - event Flashloan(address indexed caller, address indexed token, address indexed receiver, uint256 amount); + event FlashLoan(address indexed caller, address indexed token, address indexed receiver, uint256 amount); event SetOwner(address indexed newOwner); From 9e9a6266325f826b4a5143ef3417c71f6dcc4691 Mon Sep 17 00:00:00 2001 From: MerlinEgalite Date: Wed, 2 Aug 2023 18:40:03 +0200 Subject: [PATCH 10/14] refactor: past tense --- src/Blue.sol | 34 +++++++++++++++++----------------- src/libraries/Events.sol | 34 +++++++++++++++++----------------- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/Blue.sol b/src/Blue.sol index e087aa00a..154097524 100644 --- a/src/Blue.sol +++ b/src/Blue.sol @@ -99,20 +99,20 @@ contract Blue is IFlashLender { function setOwner(address newOwner) external onlyOwner { owner = newOwner; - emit Events.SetOwner(newOwner); + emit Events.OwnerSet(newOwner); } function enableIrm(IIrm irm) external onlyOwner { isIrmEnabled[irm] = true; - emit Events.EnableIrm(address(irm)); + emit Events.IrmEnabled(address(irm)); } function enableLltv(uint256 lltv) external onlyOwner { require(lltv < FixedPointMathLib.WAD, Errors.LLTV_TOO_HIGH); isLltvEnabled[lltv] = true; - emit Events.EnableLltv(lltv); + emit Events.LltvEnabled(lltv); } /// @notice It is the owner's responsibility to ensure a fee recipient is set before setting a non-zero fee. @@ -122,13 +122,13 @@ contract Blue is IFlashLender { require(newFee <= MAX_FEE, Errors.MAX_FEE_EXCEEDED); fee[id] = newFee; - emit Events.SetFee(id, newFee); + emit Events.FeeSet(id, newFee); } function setFeeRecipient(address recipient) external onlyOwner { feeRecipient = recipient; - emit Events.SetFeeRecipient(recipient); + emit Events.FeeRecipientSet(recipient); } // Markets management. @@ -139,7 +139,7 @@ contract Blue is IFlashLender { require(isLltvEnabled[market.lltv], Errors.LLTV_NOT_ENABLED); require(lastUpdate[id] == 0, Errors.MARKET_CREATED); - emit Events.CreateMarket(id, market); + emit Events.MarketCreated(id, market); _accrueInterests(market, id); } @@ -159,7 +159,7 @@ contract Blue is IFlashLender { totalSupply[id] += amount; - emit Events.Supply(id, msg.sender, onBehalf, amount, shares); + emit Events.Supplied(id, msg.sender, onBehalf, amount, shares); if (data.length > 0) IBlueSupplyCallback(msg.sender).onBlueSupply(amount, data); @@ -180,7 +180,7 @@ contract Blue is IFlashLender { totalSupply[id] -= amount; - emit Events.Withdraw(id, msg.sender, onBehalf, receiver, amount, shares); + emit Events.Withdrawn(id, msg.sender, onBehalf, receiver, amount, shares); require(totalBorrow[id] <= totalSupply[id], Errors.INSUFFICIENT_LIQUIDITY); @@ -203,7 +203,7 @@ contract Blue is IFlashLender { totalBorrow[id] += amount; - emit Events.Borrow(id, msg.sender, onBehalf, receiver, amount, shares); + emit Events.Borrowed(id, msg.sender, onBehalf, receiver, amount, shares); require(_isHealthy(market, id, onBehalf), Errors.INSUFFICIENT_COLLATERAL); require(totalBorrow[id] <= totalSupply[id], Errors.INSUFFICIENT_LIQUIDITY); @@ -224,7 +224,7 @@ contract Blue is IFlashLender { totalBorrow[id] -= amount; - emit Events.Repay(id, msg.sender, onBehalf, amount, shares); + emit Events.Repaid(id, msg.sender, onBehalf, amount, shares); if (data.length > 0) IBlueRepayCallback(msg.sender).onBlueRepay(amount, data); @@ -243,7 +243,7 @@ contract Blue is IFlashLender { collateral[id][onBehalf] += amount; - emit Events.CollateralSupply(id, msg.sender, onBehalf, amount); + emit Events.SuppliedCollateral(id, msg.sender, onBehalf, amount); if (data.length > 0) IBlueSupplyCollateralCallback(msg.sender).onBlueSupplyCollateral(amount, data); @@ -260,7 +260,7 @@ contract Blue is IFlashLender { collateral[id][onBehalf] -= amount; - emit Events.CollateralWithdraw(id, msg.sender, onBehalf, receiver, amount); + emit Events.WithdrawnCollateral(id, msg.sender, onBehalf, receiver, amount); require(_isHealthy(market, id, onBehalf), Errors.INSUFFICIENT_COLLATERAL); @@ -293,7 +293,7 @@ contract Blue is IFlashLender { collateral[id][borrower] -= seized; - emit Events.Liquidate(id, msg.sender, borrower, repaid, repaidShares, seized); + emit Events.Liquidated(id, msg.sender, borrower, repaid, repaidShares, seized); // Realize the bad debt if needed. if (collateral[id][borrower] == 0) { @@ -304,7 +304,7 @@ contract Blue is IFlashLender { totalBorrowShares[id] -= badDebtShares; borrowShare[id][borrower] = 0; - emit Events.RealizeBadDebt(id, borrower, badDebt, badDebtShares); + emit Events.BadDebtRealized(id, borrower, badDebt, badDebtShares); } market.collateralAsset.safeTransfer(msg.sender, seized); @@ -347,7 +347,7 @@ contract Blue is IFlashLender { require(signatory != address(0) && authorizer == signatory, Errors.INVALID_SIGNATURE); - emit Events.IncrementNonce(msg.sender, signatory, usedNonce); + emit Events.NonceIncremented(msg.sender, signatory, usedNonce); _setAuthorization(signatory, authorized, newIsAuthorized); } @@ -363,7 +363,7 @@ contract Blue is IFlashLender { function _setAuthorization(address authorizer, address authorized, bool newIsAuthorized) internal { isAuthorized[authorizer][authorized] = newIsAuthorized; - emit Events.Authorization(msg.sender, authorizer, authorized, newIsAuthorized); + emit Events.AuthorizationSet(msg.sender, authorizer, authorized, newIsAuthorized); } // Interests management. @@ -390,7 +390,7 @@ contract Blue is IFlashLender { totalSupplyShares[id] += feeShares; } - emit Events.AccrueInterests(id, accruedInterests, feeShares); + emit Events.InterestsAccrued(id, accruedInterests, feeShares); } lastUpdate[id] = block.timestamp; diff --git a/src/libraries/Events.sol b/src/libraries/Events.sol index 704c2fafc..5dd5a9fe7 100644 --- a/src/libraries/Events.sol +++ b/src/libraries/Events.sol @@ -4,13 +4,13 @@ pragma solidity ^0.8.0; import {Id, Market} from "src/libraries/MarketLib.sol"; library Events { - event CollateralSupply(Id indexed id, address indexed caller, address indexed onBehalf, uint256 amount); - event CollateralWithdraw( + event SuppliedCollateral(Id indexed id, address indexed caller, address indexed onBehalf, uint256 amount); + event WithdrawnCollateral( 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( + event Supplied(Id indexed id, address indexed caller, address indexed onBehalf, uint256 amount, uint256 shares); + event Withdrawn( Id indexed id, address caller, address indexed onBehalf, @@ -19,7 +19,7 @@ library Events { uint256 shares ); - event Borrow( + event Borrowed( Id indexed id, address caller, address indexed onBehalf, @@ -27,9 +27,9 @@ library Events { uint256 amount, uint256 shares ); - event Repay(Id indexed id, address indexed caller, address indexed onBehalf, uint256 amount, uint256 shares); + event Repaid(Id indexed id, address indexed caller, address indexed onBehalf, uint256 amount, uint256 shares); - event Liquidate( + event Liquidated( Id indexed id, address indexed caller, address indexed borrower, @@ -40,25 +40,25 @@ library Events { event FlashLoan(address indexed caller, address indexed token, address indexed receiver, uint256 amount); - event SetOwner(address indexed newOwner); + event OwnerSet(address indexed newOwner); - event SetFee(Id indexed id, uint256 fee); + event FeeSet(Id indexed id, uint256 fee); - event SetFeeRecipient(address indexed feeRecipient); + event FeeRecipientSet(address indexed feeRecipient); - event CreateMarket(Id indexed id, Market market); + event MarketCreated(Id indexed id, Market market); - event RealizeBadDebt(Id indexed id, address indexed borrower, uint256 amount, uint256 shares); + event BadDebtRealized(Id indexed id, address indexed borrower, uint256 amount, uint256 shares); - event Authorization( + event AuthorizationSet( address indexed caller, address indexed authorizer, address indexed authorized, bool isAuthorized ); - event IncrementNonce(address indexed caller, address indexed signatory, uint256 usedNonce); + event NonceIncremented(address indexed caller, address indexed signatory, uint256 usedNonce); - event EnableIrm(address indexed irm); + event IrmEnabled(address indexed irm); - event EnableLltv(uint256 lltv); + event LltvEnabled(uint256 lltv); - event AccrueInterests(Id indexed id, uint256 accruedInterests, uint256 feeShares); + event InterestsAccrued(Id indexed id, uint256 accruedInterests, uint256 feeShares); } From a1c49979e1c93dd65af8ab3f420867551e4578ad Mon Sep 17 00:00:00 2001 From: MerlinEgalite Date: Wed, 2 Aug 2023 19:18:22 +0200 Subject: [PATCH 11/14] refactor: use authorizer like in event --- src/Blue.sol | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Blue.sol b/src/Blue.sol index 610a45e49..996103f9f 100644 --- a/src/Blue.sol +++ b/src/Blue.sol @@ -354,9 +354,9 @@ contract Blue { require(signatory != address(0) && authorizer == signatory, Errors.INVALID_SIGNATURE); - emit Events.NonceIncremented(msg.sender, signatory, usedNonce); + emit Events.NonceIncremented(msg.sender, authorizer, usedNonce); - _setAuthorization(signatory, authorized, newIsAuthorized); + _setAuthorization(authorizer, authorized, newIsAuthorized); } function setAuthorization(address authorized, bool newIsAuthorized) external { From 033dd32888a5c3a9981c1b87da83a6b7393d0378 Mon Sep 17 00:00:00 2001 From: MerlinEgalite Date: Wed, 2 Aug 2023 19:36:31 +0200 Subject: [PATCH 12/14] refactor: active form --- src/Blue.sol | 34 +++++++++++++++++----------------- src/libraries/Events.sol | 34 +++++++++++++++++----------------- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/Blue.sol b/src/Blue.sol index 996103f9f..dea69a962 100644 --- a/src/Blue.sol +++ b/src/Blue.sol @@ -98,20 +98,20 @@ contract Blue { function setOwner(address newOwner) external onlyOwner { owner = newOwner; - emit Events.OwnerSet(newOwner); + emit Events.SetOwner(newOwner); } function enableIrm(IIrm irm) external onlyOwner { isIrmEnabled[irm] = true; - emit Events.IrmEnabled(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 Events.LltvEnabled(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. @@ -121,13 +121,13 @@ contract Blue { require(newFee <= MAX_FEE, Errors.MAX_FEE_EXCEEDED); fee[id] = newFee; - emit Events.FeeSet(id, newFee); + emit Events.SetFee(id, newFee); } function setFeeRecipient(address recipient) external onlyOwner { feeRecipient = recipient; - emit Events.FeeRecipientSet(recipient); + emit Events.SetFeeRecipient(recipient); } // Markets management. @@ -138,7 +138,7 @@ contract Blue { require(isLltvEnabled[market.lltv], Errors.LLTV_NOT_ENABLED); require(lastUpdate[id] == 0, Errors.MARKET_CREATED); - emit Events.MarketCreated(id, market); + emit Events.CreateMarket(id, market); _accrueInterests(market, id); } @@ -159,7 +159,7 @@ contract Blue { totalSupply[id] += amount; - emit Events.Supplied(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); @@ -182,7 +182,7 @@ contract Blue { totalSupply[id] -= amount; - emit Events.Withdrawn(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); @@ -207,7 +207,7 @@ contract Blue { totalBorrow[id] += amount; - emit Events.Borrowed(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); @@ -229,7 +229,7 @@ contract Blue { totalBorrow[id] -= amount; - emit Events.Repaid(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); @@ -249,7 +249,7 @@ contract Blue { collateral[id][onBehalf] += amount; - emit Events.SuppliedCollateral(id, msg.sender, onBehalf, amount); + emit Events.SupplyCollateral(id, msg.sender, onBehalf, amount); if (data.length > 0) IBlueSupplyCollateralCallback(msg.sender).onBlueSupplyCollateral(amount, data); @@ -268,7 +268,7 @@ contract Blue { collateral[id][onBehalf] -= amount; - emit Events.WithdrawnCollateral(id, msg.sender, onBehalf, receiver, amount); + emit Events.WithdrawCollateral(id, msg.sender, onBehalf, receiver, amount); require(_isHealthy(market, id, onBehalf), Errors.INSUFFICIENT_COLLATERAL); @@ -301,7 +301,7 @@ contract Blue { collateral[id][borrower] -= seized; - emit Events.Liquidated(id, msg.sender, borrower, repaid, repaidShares, seized); + emit Events.Liquidate(id, msg.sender, borrower, repaid, repaidShares, seized); // Realize the bad debt if needed. if (collateral[id][borrower] == 0) { @@ -312,7 +312,7 @@ contract Blue { totalBorrowShares[id] -= badDebtShares; borrowShares[id][borrower] = 0; - emit Events.BadDebtRealized(id, borrower, badDebt, badDebtShares); + emit Events.RealizeBadDebt(id, borrower, badDebt, badDebtShares); } market.collateralAsset.safeTransfer(msg.sender, seized); @@ -354,7 +354,7 @@ contract Blue { require(signatory != address(0) && authorizer == signatory, Errors.INVALID_SIGNATURE); - emit Events.NonceIncremented(msg.sender, authorizer, usedNonce); + emit Events.IncrementNonce(msg.sender, authorizer, usedNonce); _setAuthorization(authorizer, authorized, newIsAuthorized); } @@ -370,7 +370,7 @@ contract Blue { function _setAuthorization(address authorizer, address authorized, bool newIsAuthorized) internal { isAuthorized[authorizer][authorized] = newIsAuthorized; - emit Events.AuthorizationSet(msg.sender, authorizer, authorized, newIsAuthorized); + emit Events.SetAuthorization(msg.sender, authorizer, authorized, newIsAuthorized); } // Interests management. @@ -397,7 +397,7 @@ contract Blue { totalSupplyShares[id] += feeShares; } - emit Events.InterestsAccrued(id, accruedInterests, feeShares); + emit Events.AccrueInterests(id, accruedInterests, feeShares); } lastUpdate[id] = block.timestamp; diff --git a/src/libraries/Events.sol b/src/libraries/Events.sol index 2759ce8ae..2a651b1fd 100644 --- a/src/libraries/Events.sol +++ b/src/libraries/Events.sol @@ -4,13 +4,13 @@ pragma solidity ^0.8.0; import {Id, Market} from "src/libraries/MarketLib.sol"; library Events { - event SuppliedCollateral(Id indexed id, address indexed caller, address indexed onBehalf, uint256 amount); - event WithdrawnCollateral( + 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 Supplied(Id indexed id, address indexed caller, address indexed onBehalf, uint256 amount, uint256 shares); - event Withdrawn( + 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, @@ -19,7 +19,7 @@ library Events { uint256 shares ); - event Borrowed( + event Borrow( Id indexed id, address caller, address indexed onBehalf, @@ -27,9 +27,9 @@ library Events { uint256 amount, uint256 shares ); - event Repaid(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 amount, uint256 shares); - event Liquidated( + event Liquidate( Id indexed id, address indexed caller, address indexed borrower, @@ -40,25 +40,25 @@ library Events { event FlashLoan(address indexed caller, address indexed token, uint256 amount); - event OwnerSet(address indexed newOwner); + event SetOwner(address indexed newOwner); - event FeeSet(Id indexed id, uint256 fee); + event SetFee(Id indexed id, uint256 fee); - event FeeRecipientSet(address indexed feeRecipient); + event SetFeeRecipient(address indexed feeRecipient); - event MarketCreated(Id indexed id, Market market); + event CreateMarket(Id indexed id, Market market); - event BadDebtRealized(Id indexed id, address indexed borrower, uint256 amount, uint256 shares); + event RealizeBadDebt(Id indexed id, address indexed borrower, uint256 amount, uint256 shares); - event AuthorizationSet( + event SetAuthorization( address indexed caller, address indexed authorizer, address indexed authorized, bool isAuthorized ); - event NonceIncremented(address indexed caller, address indexed signatory, uint256 usedNonce); + event IncrementNonce(address indexed caller, address indexed signatory, uint256 usedNonce); - event IrmEnabled(address indexed irm); + event EnableIrm(address indexed irm); - event LltvEnabled(uint256 lltv); + event EnableLltv(uint256 lltv); - event InterestsAccrued(Id indexed id, uint256 accruedInterests, uint256 feeShares); + event AccrueInterests(Id indexed id, uint256 accruedInterests, uint256 feeShares); } From 7934d2c51401ef7abfd908bedab8451a965d4627 Mon Sep 17 00:00:00 2001 From: MerlinEgalite Date: Wed, 2 Aug 2023 20:26:40 +0200 Subject: [PATCH 13/14] refactor: apply suggestions --- src/Blue.sol | 25 +++++++++++-------------- src/libraries/Events.sol | 7 +++---- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/Blue.sol b/src/Blue.sol index dea69a962..7bef3fb68 100644 --- a/src/Blue.sol +++ b/src/Blue.sol @@ -301,20 +301,19 @@ contract Blue { collateral[id][borrower] -= seized; - emit Events.Liquidate(id, msg.sender, borrower, repaid, repaidShares, seized); - // Realize the bad debt if needed. + uint256 badDebtShares; if (collateral[id][borrower] == 0) { - uint256 badDebtShares = borrowShares[id][borrower]; + badDebtShares = borrowShares[id][borrower]; uint256 badDebt = badDebtShares.toAssetsUp(totalBorrow[id], totalBorrowShares[id]); totalSupply[id] -= badDebt; totalBorrow[id] -= badDebt; totalBorrowShares[id] -= badDebtShares; borrowShares[id][borrower] = 0; - - emit Events.RealizeBadDebt(id, borrower, badDebt, badDebtShares); } + emit Events.Liquidate(id, msg.sender, borrower, repaid, repaidShares, seized, badDebtShares); + market.collateralAsset.safeTransfer(msg.sender, seized); if (data.length > 0) IBlueLiquidateCallback(msg.sender).onBlueLiquidate(seized, repaid, data); @@ -356,23 +355,21 @@ contract Blue { emit Events.IncrementNonce(msg.sender, authorizer, usedNonce); - _setAuthorization(authorizer, authorized, newIsAuthorized); + isAuthorized[authorizer][authorized] = newIsAuthorized; + + emit Events.SetAuthorization(msg.sender, authorizer, authorized, newIsAuthorized); } function setAuthorization(address authorized, bool newIsAuthorized) external { - _setAuthorization(msg.sender, authorized, newIsAuthorized); + isAuthorized[msg.sender][authorized] = newIsAuthorized; + + emit Events.SetAuthorization(msg.sender, msg.sender, authorized, newIsAuthorized); } function _isSenderAuthorized(address user) internal view returns (bool) { return msg.sender == user || isAuthorized[user][msg.sender]; } - function _setAuthorization(address authorizer, address authorized, bool newIsAuthorized) internal { - isAuthorized[authorizer][authorized] = newIsAuthorized; - - emit Events.SetAuthorization(msg.sender, authorizer, authorized, newIsAuthorized); - } - // Interests management. function _accrueInterests(Market memory market, Id id) internal { @@ -397,7 +394,7 @@ contract Blue { totalSupplyShares[id] += feeShares; } - emit Events.AccrueInterests(id, accruedInterests, feeShares); + emit Events.AccrueInterests(id, borrowRate, accruedInterests, feeShares); } lastUpdate[id] = block.timestamp; diff --git a/src/libraries/Events.sol b/src/libraries/Events.sol index 2a651b1fd..8c078af00 100644 --- a/src/libraries/Events.sol +++ b/src/libraries/Events.sol @@ -35,7 +35,8 @@ library Events { address indexed borrower, uint256 repaid, uint256 repaidShares, - uint256 seized + uint256 seized, + uint256 badDebtShares ); event FlashLoan(address indexed caller, address indexed token, uint256 amount); @@ -48,8 +49,6 @@ library Events { event CreateMarket(Id indexed id, Market market); - event RealizeBadDebt(Id indexed id, address indexed borrower, uint256 amount, uint256 shares); - event SetAuthorization( address indexed caller, address indexed authorizer, address indexed authorized, bool isAuthorized ); @@ -60,5 +59,5 @@ library Events { event EnableLltv(uint256 lltv); - event AccrueInterests(Id indexed id, uint256 accruedInterests, uint256 feeShares); + event AccrueInterests(Id indexed id, uint256 borrowRate, uint256 accruedInterests, uint256 feeShares); } From d1e67465e187f33e339eaf308c2605135ded1230 Mon Sep 17 00:00:00 2001 From: MerlinEgalite Date: Thu, 3 Aug 2023 10:21:26 +0200 Subject: [PATCH 14/14] refactor: move liquidate event after transfer --- src/Blue.sol | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Blue.sol b/src/Blue.sol index def0f304a..31291ab20 100644 --- a/src/Blue.sol +++ b/src/Blue.sol @@ -307,10 +307,10 @@ contract Blue is IBlue { borrowShares[id][borrower] = 0; } - emit Events.Liquidate(id, msg.sender, borrower, repaid, repaidShares, seized, badDebtShares); - IERC20(market.collateralAsset).safeTransfer(msg.sender, seized); + emit Events.Liquidate(id, msg.sender, borrower, repaid, repaidShares, seized, badDebtShares); + if (data.length > 0) IBlueLiquidateCallback(msg.sender).onBlueLiquidate(seized, repaid, data); IERC20(market.borrowableAsset).safeTransferFrom(msg.sender, address(this), repaid);