Skip to content

Commit

Permalink
Merge pull request #32 from morpho-labs/refactor/to-id
Browse files Browse the repository at this point in the history
Use pure function for to get the id of a market
  • Loading branch information
MathisGD authored Jul 5, 2023
2 parents 9fdc2c0 + c0282d8 commit 3bf3db0
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/Blue.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ 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.
Expand Down Expand Up @@ -55,7 +60,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);
Expand All @@ -64,7 +69,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");

Expand All @@ -85,7 +90,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");

Expand All @@ -105,7 +110,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");

Expand All @@ -129,7 +134,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");

Expand All @@ -147,7 +152,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");

Expand All @@ -159,7 +164,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");

Expand All @@ -175,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");

Expand Down

0 comments on commit 3bf3db0

Please sign in to comment.