From 0af912c77f92d4403bdacbecbf2d01046695f5e4 Mon Sep 17 00:00:00 2001 From: MerlinEgalite Date: Wed, 5 Jul 2023 10:22:06 +0200 Subject: [PATCH 1/3] refactor: use helper id function to avoid redundant code --- src/Market.sol | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/Market.sol b/src/Market.sol index 6f7cb8148..b4b91ed2c 100644 --- a/src/Market.sol +++ b/src/Market.sol @@ -9,6 +9,7 @@ import {SafeTransferLib} from "src/libraries/SafeTransferLib.sol"; // Market id. type Id is bytes32; +using {toId} for Id; // Market. struct Market { @@ -25,6 +26,10 @@ function irm(uint utilization) pure returns (uint) { return utilization / 365 days; } +function toId() pure returns (bytes32) { + return market.toId(); +} + contract Blue { using MathLib for uint; using SafeTransferLib for IERC20; @@ -51,7 +56,7 @@ contract Blue { // Markets management. function createMarket(Market calldata market) external { - Id id = Id.wrap(keccak256(abi.encode(market))); + Id id = market.toId(); require(lastUpdate[id] == 0, "market already exists"); accrueInterests(id); @@ -60,7 +65,7 @@ contract Blue { // Supply management. function supply(Market calldata market, uint amount) external { - Id id = Id.wrap(keccak256(abi.encode(market))); + Id id = market.toId(); require(lastUpdate[id] != 0, "unknown market"); require(amount > 0, "zero amount"); @@ -81,7 +86,7 @@ contract Blue { } function withdraw(Market calldata market, uint amount) external { - Id id = Id.wrap(keccak256(abi.encode(market))); + Id id = market.toId(); require(lastUpdate[id] != 0, "unknown market"); require(amount > 0, "zero amount"); @@ -101,7 +106,7 @@ contract Blue { // Borrow management. function borrow(Market calldata market, uint amount) external { - Id id = Id.wrap(keccak256(abi.encode(market))); + Id id = market.toId(); require(lastUpdate[id] != 0, "unknown market"); require(amount > 0, "zero amount"); @@ -125,7 +130,7 @@ contract Blue { } function repay(Market calldata market, uint amount) external { - Id id = Id.wrap(keccak256(abi.encode(market))); + Id id = market.toId(); require(lastUpdate[id] != 0, "unknown market"); require(amount > 0, "zero amount"); @@ -143,7 +148,7 @@ contract Blue { // Collateral management. function supplyCollateral(Market calldata market, uint amount) external { - Id id = Id.wrap(keccak256(abi.encode(market))); + Id id = market.toId(); require(lastUpdate[id] != 0, "unknown market"); require(amount > 0, "zero amount"); @@ -155,7 +160,7 @@ contract Blue { } function withdrawCollateral(Market calldata market, uint amount) external { - Id id = Id.wrap(keccak256(abi.encode(market))); + Id id = market.toId(); require(lastUpdate[id] != 0, "unknown market"); require(amount > 0, "zero amount"); From 5737a62a30b2ab360cc0def3c9fda5f84c087fa9 Mon Sep 17 00:00:00 2001 From: MerlinEgalite Date: Wed, 5 Jul 2023 10:26:56 +0200 Subject: [PATCH 2/3] fix: correct function --- src/Market.sol | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Market.sol b/src/Market.sol index b4b91ed2c..025c74bab 100644 --- a/src/Market.sol +++ b/src/Market.sol @@ -9,7 +9,6 @@ import {SafeTransferLib} from "src/libraries/SafeTransferLib.sol"; // Market id. type Id is bytes32; -using {toId} for Id; // Market. struct Market { @@ -26,8 +25,9 @@ function irm(uint utilization) pure returns (uint) { return utilization / 365 days; } -function toId() pure returns (bytes32) { - return market.toId(); +using {toId} for Market; +function toId(Market calldata market) pure returns (Id) { + return Id.wrap(keccak256(abi.encode(market))); } contract Blue { From c0282d862224652cb27df6af9d4a32c302b72b9d Mon Sep 17 00:00:00 2001 From: MerlinEgalite Date: Wed, 5 Jul 2023 11:18:10 +0200 Subject: [PATCH 3/3] feat: id for liquidate + move function upward --- src/Blue.sol | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Blue.sol b/src/Blue.sol index 13f3c4ae5..0235c69e7 100644 --- a/src/Blue.sol +++ b/src/Blue.sol @@ -23,17 +23,17 @@ struct Market { uint lLTV; } +using {toId} for Market; +function toId(Market calldata market) pure returns (Id) { + return Id.wrap(keccak256(abi.encode(market))); +} + function irm(uint utilization) pure returns (uint) { // Divide by the number of seconds in a year. // This is a very simple model (to refine later) where x% utilization corresponds to x% APR. return utilization / 365 days; } -using {toId} for Market; -function toId(Market calldata market) pure returns (Id) { - return Id.wrap(keccak256(abi.encode(market))); -} - contract Blue { using MathLib for uint; using SafeTransferLib for IERC20; @@ -180,7 +180,7 @@ contract Blue { // Liquidation. function liquidate(Market calldata market, address borrower, uint seized) external { - Id id = Id.wrap(keccak256(abi.encode(market))); + Id id = market.toId(); require(lastUpdate[id] != 0, "unknown market"); require(seized > 0, "zero amount");