-
Notifications
You must be signed in to change notification settings - Fork 52
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
Storage Refactor #269
Closed
Closed
Storage Refactor #269
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ca4308e
refactor: initial storage refactor draft
pakim249CAL eb5f30c
Merge remote-tracking branch 'origin/main' into refactor/storage
pakim249CAL d27e61a
refactor: increase/decrease
pakim249CAL a641cd6
refactor: fee scale
pakim249CAL 41fd346
test: re-add test
pakim249CAL 447779d
chore: lint
pakim249CAL 6b8f27c
Merge remote-tracking branch 'origin/main' into refactor/storage
pakim249CAL 4c0c8ce
chore: rework comments
pakim249CAL 4b9a40d
Merge remote-tracking branch 'origin/refactor/virtual-shares' into re…
pakim249CAL df6026e
Merge remote-tracking branch 'origin/refactor/virtual-shares' into re…
pakim249CAL bde1b61
Merge remote-tracking branch 'origin/refactor/virtual-shares' into re…
pakim249CAL 5099792
chore: remove unused imports
pakim249CAL 6dab605
refactor: apply suggested changes
pakim249CAL File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity >=0.5.0; | ||
|
||
import {Market} from "./IMorpho.sol"; | ||
import {MarketParams} from "./IMorpho.sol"; | ||
|
||
/// @title IIrm | ||
/// @author Morpho Labs | ||
/// @custom:contact [email protected] | ||
/// @notice Interface that IRMs used by Morpho must implement. | ||
interface IIrm { | ||
/// @notice Returns the borrow rate of a `market`. | ||
function borrowRate(Market memory market) external returns (uint256); | ||
/// @notice Returns the borrow rate of a the market defined by `marketParams`. | ||
function borrowRate(MarketParams memory marketParams) external returns (uint256); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,137 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.0; | ||
|
||
import {Id, Market} from "../interfaces/IMorpho.sol"; | ||
import {MarketParams, Id} from "../interfaces/IMorpho.sol"; | ||
import {SafeCastLib} from "solmate/utils/SafeCastLib.sol"; | ||
|
||
struct Market { | ||
mapping(address => UserBalances) userBalances; | ||
uint128 totalSupply; // Market total supply. | ||
uint128 totalSupplyShares; // Market total supply shares. | ||
uint128 totalBorrow; // Market total borrow. | ||
uint128 totalBorrowShares; // Market total borrow shares. | ||
uint128 lastUpdate; // Interests last update (used to check if a market has been created). | ||
uint128 fee; // Fee. | ||
} | ||
|
||
struct UserBalances { | ||
uint128 borrowShares; // User' borrow balances. | ||
uint128 collateral; // User' collateral balance. | ||
uint128 supplyShares; // User' supply balances. | ||
} | ||
|
||
/// @title MarketLib | ||
/// @author Morpho Labs | ||
/// @custom:contact [email protected] | ||
/// @notice Library to convert a market to its id. | ||
library MarketLib { | ||
/// @notice Returns the id of a `market`. | ||
function id(Market memory market) internal pure returns (Id) { | ||
return Id.wrap(keccak256(abi.encode(market))); | ||
using SafeCastLib for uint256; | ||
|
||
function id(MarketParams memory marketParams) internal pure returns (Id) { | ||
return Id.wrap(keccak256(abi.encode(marketParams))); | ||
} | ||
|
||
function supplyShares(Market storage market, address user) internal view returns (uint256) { | ||
return market.userBalances[user].supplyShares; | ||
} | ||
|
||
function borrowShares(Market storage market, address user) internal view returns (uint256) { | ||
return market.userBalances[user].borrowShares; | ||
} | ||
|
||
function collateral(Market storage market, address user) internal view returns (uint256) { | ||
return market.userBalances[user].collateral; | ||
} | ||
|
||
function setSupplyShares(Market storage market, address user, uint256 amount) internal { | ||
market.userBalances[user].supplyShares = amount.safeCastTo128(); | ||
} | ||
|
||
function increaseSupplyShares(Market storage market, address user, uint256 amount) internal { | ||
setSupplyShares(market, user, supplyShares(market, user) + amount); | ||
} | ||
|
||
function decreaseSupplyShares(Market storage market, address user, uint256 amount) internal { | ||
setSupplyShares(market, user, supplyShares(market, user) - amount); | ||
} | ||
|
||
function setBorrowShares(Market storage market, address user, uint256 amount) internal { | ||
market.userBalances[user].borrowShares = amount.safeCastTo128(); | ||
} | ||
|
||
function increaseBorrowShares(Market storage market, address user, uint256 amount) internal { | ||
setBorrowShares(market, user, borrowShares(market, user) + amount); | ||
} | ||
|
||
function decreaseBorrowShares(Market storage market, address user, uint256 amount) internal { | ||
setBorrowShares(market, user, borrowShares(market, user) - amount); | ||
} | ||
|
||
function setCollateral(Market storage market, address user, uint256 amount) internal { | ||
market.userBalances[user].collateral = amount.safeCastTo128(); | ||
} | ||
|
||
function increaseCollateral(Market storage market, address user, uint256 amount) internal { | ||
setCollateral(market, user, collateral(market, user) + amount); | ||
} | ||
|
||
function decreaseCollateral(Market storage market, address user, uint256 amount) internal { | ||
setCollateral(market, user, collateral(market, user) - amount); | ||
} | ||
|
||
function setTotalSupply(Market storage market, uint256 amount) internal { | ||
market.totalSupply = amount.safeCastTo128(); | ||
} | ||
|
||
function increaseTotalSupply(Market storage market, uint256 amount) internal { | ||
setTotalSupply(market, market.totalSupply + amount); | ||
} | ||
|
||
function decreaseTotalSupply(Market storage market, uint256 amount) internal { | ||
setTotalSupply(market, market.totalSupply - amount); | ||
} | ||
|
||
function setTotalSupplyShares(Market storage market, uint256 amount) internal { | ||
market.totalSupplyShares = amount.safeCastTo128(); | ||
} | ||
|
||
function increaseTotalSupplyShares(Market storage market, uint256 amount) internal { | ||
setTotalSupplyShares(market, market.totalSupplyShares + amount); | ||
} | ||
|
||
function decreaseTotalSupplyShares(Market storage market, uint256 amount) internal { | ||
setTotalSupplyShares(market, market.totalSupplyShares - amount); | ||
} | ||
|
||
function setTotalBorrow(Market storage market, uint256 amount) internal { | ||
market.totalBorrow = amount.safeCastTo128(); | ||
} | ||
|
||
function increaseTotalBorrow(Market storage market, uint256 amount) internal { | ||
setTotalBorrow(market, market.totalBorrow + amount); | ||
} | ||
|
||
function decreaseTotalBorrow(Market storage market, uint256 amount) internal { | ||
setTotalBorrow(market, market.totalBorrow - amount); | ||
} | ||
|
||
function setTotalBorrowShares(Market storage market, uint256 amount) internal { | ||
market.totalBorrowShares = amount.safeCastTo128(); | ||
} | ||
|
||
function increaseTotalBorrowShares(Market storage market, uint256 amount) internal { | ||
setTotalBorrowShares(market, market.totalBorrowShares + amount); | ||
} | ||
|
||
function decreaseTotalBorrowShares(Market storage market, uint256 amount) internal { | ||
setTotalBorrowShares(market, market.totalBorrowShares - amount); | ||
} | ||
|
||
function setLastUpdate(Market storage market, uint256 amount) internal { | ||
market.lastUpdate = amount.safeCastTo128(); | ||
} | ||
|
||
function setFee(Market storage market, uint256 amount) internal { | ||
market.fee = amount.safeCastTo128(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I disagree with this since I find it problematic to use different storage types for the same type of variable. Is it possible that this particular point be isolated to another PR with its own scope of discussion?
Made an issue for it #309