Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
dglowinski committed Mar 24, 2024
1 parent 19999fa commit db5c03d
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 71 deletions.
3 changes: 1 addition & 2 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ number_underscore = "preserve"
override_spacing = true
ignore = [
"src/EVault/EVault.sol",
"src/EVault/shared/types/MarketStorage.sol",
"src/EVault/shared/types/MarketCache.sol"
"src/EVault/shared/types/Market.sol",
]


Expand Down
39 changes: 23 additions & 16 deletions src/EVault/modules/Governance.sol
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,9 @@ abstract contract GovernanceModule is IGovernance, Base, BalanceUtils, BorrowUti

if (marketCache.accumulatedFees.isZero()) return;

Market storage _marketStorage = marketStorage();
(address protocolReceiver, uint16 protocolFee) = protocolConfig.protocolFeeConfig(address(this));
address governorReceiver = marketStorage().feeReceiver;
address governorReceiver = _marketStorage.feeReceiver;

if (governorReceiver == address(0)) {
protocolFee = 1e4; // governor forfeits fees
Expand All @@ -170,10 +171,10 @@ abstract contract GovernanceModule is IGovernance, Base, BalanceUtils, BorrowUti
Shares governorShares = marketCache.accumulatedFees.mulDiv(1e4 - protocolFee, 1e4);
Shares protocolShares = marketCache.accumulatedFees - governorShares;

marketStorage().accumulatedFees = marketCache.accumulatedFees = Shares.wrap(0);
_marketStorage.accumulatedFees = marketCache.accumulatedFees = Shares.wrap(0);

// Decrease totalShares because increaseBalance will increase it by that total amount
marketStorage().totalShares = marketCache.totalShares = marketCache.totalShares - marketCache.accumulatedFees;
_marketStorage.totalShares = marketCache.totalShares = marketCache.totalShares - marketCache.accumulatedFees;

// For the Deposit events in increaseBalance the assets amount is zero - the shares are covered with the accrued interest
if (!governorShares.isZero()) {
Expand Down Expand Up @@ -222,17 +223,18 @@ abstract contract GovernanceModule is IGovernance, Base, BalanceUtils, BorrowUti
// self-collateralization is not allowed
if (collateral == address(this)) revert E_InvalidLTVAsset();

Market storage _marketStorage = marketStorage();
ConfigAmount newLTVAmount = ltv.toConfigAmount();
LTVConfig memory origLTV = marketStorage().ltvLookup[collateral];
LTVConfig memory origLTV = _marketStorage.ltvLookup[collateral];

// If new LTV is higher than the previous, or the same, it should take effect immediately
if (!(newLTVAmount < origLTV.getLTV(LTVType.LIQUIDATION)) && rampDuration > 0) revert E_LTVRamp();

LTVConfig memory newLTV = origLTV.setLTV(newLTVAmount, rampDuration);

marketStorage().ltvLookup[collateral] = newLTV;
_marketStorage.ltvLookup[collateral] = newLTV;

if (!origLTV.initialized) marketStorage().ltvList.push(collateral);
if (!origLTV.initialized) _marketStorage.ltvList.push(collateral);

emit GovSetLTV(
collateral,
Expand All @@ -255,8 +257,9 @@ abstract contract GovernanceModule is IGovernance, Base, BalanceUtils, BorrowUti
function setInterestRateModel(address newModel) public virtual nonReentrant governorOnly {
MarketCache memory marketCache = updateMarket();

marketStorage().interestRateModel = newModel;
marketStorage().interestRate = 0;
Market storage _marketStorage = marketStorage();
_marketStorage.interestRateModel = newModel;
_marketStorage.interestRate = 0;

uint256 newInterestRate = computeInterestRate(marketCache);

Expand All @@ -267,17 +270,19 @@ abstract contract GovernanceModule is IGovernance, Base, BalanceUtils, BorrowUti

/// @inheritdoc IGovernance
function setDisabledOps(uint32 newDisabledOps) public virtual nonReentrant governorOrPauseGuardianOnly {
Market storage _marketStorage = marketStorage();

// Overwrite bits of locked ops with their currently set values
newDisabledOps = (newDisabledOps & ~marketStorage().lockedOps.toUint32())
| (marketStorage().disabledOps.toUint32() & marketStorage().lockedOps.toUint32());
newDisabledOps = (newDisabledOps & ~_marketStorage.lockedOps.toUint32())
| (_marketStorage.disabledOps.toUint32() & _marketStorage.lockedOps.toUint32());

// market is updated because:
// if disabling interest accrual - the pending interest should be accrued
// if re-enabling interest - last updated timestamp needs to be reset to skip the disabled period
MarketCache memory marketCache = updateMarket();
logMarketStatus(marketCache, marketStorage().interestRate);
logMarketStatus(marketCache, _marketStorage.interestRate);

marketStorage().disabledOps = Flags.wrap(newDisabledOps);
_marketStorage.disabledOps = Flags.wrap(newDisabledOps);
emit GovSetDisabledOps(newDisabledOps);
}

Expand All @@ -302,8 +307,9 @@ abstract contract GovernanceModule is IGovernance, Base, BalanceUtils, BorrowUti
AmountCap _borrowCap = AmountCap.wrap(borrowCap);
if (borrowCap > 0 && _borrowCap.toUint() > MAX_SANE_AMOUNT) revert E_BadBorrowCap();

marketStorage().supplyCap = _supplyCap;
marketStorage().borrowCap = _borrowCap;
Market storage _marketStorage = marketStorage();
_marketStorage.supplyCap = _supplyCap;
_marketStorage.borrowCap = _borrowCap;

emit GovSetCaps(supplyCap, borrowCap);
}
Expand All @@ -312,14 +318,15 @@ abstract contract GovernanceModule is IGovernance, Base, BalanceUtils, BorrowUti
function setInterestFee(uint16 newInterestFee) public virtual nonReentrant governorOnly {
// Update market to apply the current interest fee to the pending interest
MarketCache memory marketCache = updateMarket();
logMarketStatus(marketCache, marketStorage().interestRate);
Market storage _marketStorage = marketStorage();
logMarketStatus(marketCache, _marketStorage.interestRate);

// Interest fees in guaranteed range are always allowed, otherwise ask protocolConfig
if (newInterestFee < GUARANTEED_INTEREST_FEE_MIN || newInterestFee > GUARANTEED_INTEREST_FEE_MAX) {
if (!protocolConfig.isValidInterestFee(address(this), newInterestFee)) revert E_BadFee();
}

marketStorage().interestFee = newInterestFee.toConfigAmount();
_marketStorage.interestFee = newInterestFee.toConfigAmount();

emit GovSetInterestFee(newInterestFee);
}
Expand Down
10 changes: 6 additions & 4 deletions src/EVault/modules/Initialize.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ abstract contract InitializeModule is IInitialize, Base, BorrowUtils {

// Initialize storage

marketStorage().lastInterestAccumulatorUpdate = uint48(block.timestamp);
marketStorage().interestAccumulator = INITIAL_INTEREST_ACCUMULATOR;
marketStorage().interestFee = DEFAULT_INTEREST_FEE.toConfigAmount();
marketStorage().creator = marketStorage().governorAdmin = marketStorage().pauseGuardian = proxyCreator;
Market storage _marketStorage = marketStorage();

_marketStorage.lastInterestAccumulatorUpdate = uint48(block.timestamp);
_marketStorage.interestAccumulator = INITIAL_INTEREST_ACCUMULATOR;
_marketStorage.interestFee = DEFAULT_INTEREST_FEE.toConfigAmount();
_marketStorage.creator = _marketStorage.governorAdmin = _marketStorage.pauseGuardian = proxyCreator;

snapshotStorage().reset();

Expand Down
30 changes: 18 additions & 12 deletions src/EVault/shared/BalanceUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ abstract contract BalanceUtils is Base {
) internal {
if (account == address(0)) revert E_BadSharesReceiver();

Market storage _marketStorage = marketStorage();
(Shares origBalance, bool balanceForwarderEnabled) =
marketStorage().users[account].getBalanceAndBalanceForwarder();
_marketStorage.users[account].getBalanceAndBalanceForwarder();
Shares newBalance = origBalance + amount;

marketStorage().users[account].setBalance(newBalance);
marketStorage().totalShares = marketCache.totalShares = marketCache.totalShares + amount;
_marketStorage.users[account].setBalance(newBalance);
_marketStorage.totalShares = marketCache.totalShares = marketCache.totalShares + amount;

if (balanceForwarderEnabled) {
tryBalanceTrackerHook(account, newBalance.toUint(), false);
Expand All @@ -44,17 +45,19 @@ abstract contract BalanceUtils is Base {
Shares amount,
Assets assets
) internal {
Market storage _marketStorage = marketStorage();

(Shares origBalance, bool balanceForwarderEnabled) =
marketStorage().users[account].getBalanceAndBalanceForwarder();
_marketStorage.users[account].getBalanceAndBalanceForwarder();
if (origBalance < amount) revert E_InsufficientBalance();

Shares newBalance;
unchecked {
newBalance = origBalance - amount;
}

marketStorage().users[account].setBalance(newBalance);
marketStorage().totalShares = marketCache.totalShares = marketCache.totalShares - amount;
_marketStorage.users[account].setBalance(newBalance);
_marketStorage.totalShares = marketCache.totalShares = marketCache.totalShares - amount;

if (balanceForwarderEnabled) {
tryBalanceTrackerHook(account, newBalance.toUint(), isControlCollateralInProgress());
Expand All @@ -65,12 +68,14 @@ abstract contract BalanceUtils is Base {
}

function transferBalance(address from, address to, Shares amount) internal {
Market storage _marketStorage = marketStorage();

if (!amount.isZero()) {
(Shares origFromBalance, bool fromBalanceForwarderEnabled) =
marketStorage().users[from].getBalanceAndBalanceForwarder();
_marketStorage.users[from].getBalanceAndBalanceForwarder();

(Shares origToBalance, bool toBalanceForwarderEnabled) =
marketStorage().users[to].getBalanceAndBalanceForwarder();
_marketStorage.users[to].getBalanceAndBalanceForwarder();

if (origFromBalance < amount) revert E_InsufficientBalance();

Expand All @@ -80,8 +85,8 @@ abstract contract BalanceUtils is Base {
}
Shares newToBalance = origToBalance + amount;

marketStorage().users[from].setBalance(newFromBalance);
marketStorage().users[to].setBalance(newToBalance);
_marketStorage.users[from].setBalance(newFromBalance);
_marketStorage.users[to].setBalance(newToBalance);

if (fromBalanceForwarderEnabled) {
tryBalanceTrackerHook(from, newFromBalance.toUint(), isControlCollateralInProgress());
Expand All @@ -106,14 +111,15 @@ abstract contract BalanceUtils is Base {

function decreaseAllowance(address owner, address spender, Shares amount) internal {
if (amount.isZero()) return;
Market storage _marketStorage = marketStorage();

uint256 allowance = marketStorage().users[owner].eTokenAllowance[spender];
uint256 allowance = _marketStorage.users[owner].eTokenAllowance[spender];
if (owner != spender && allowance != type(uint256).max) {
if (allowance < amount.toUint()) revert E_InsufficientAllowance();
unchecked {
allowance -= amount.toUint();
}
marketStorage().users[owner].eTokenAllowance[spender] = allowance;
_marketStorage.users[owner].eTokenAllowance[spender] = allowance;
emit Approval(owner, spender, allowance);
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/EVault/shared/Base.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ abstract contract Base is EVCClient, Cache, SnapshotStorage {
} // documentation only

modifier nonReentrant() {
if (marketStorage().reentrancyLocked) revert E_Reentrancy();
Market storage _marketStorage = marketStorage();
if (_marketStorage.reentrancyLocked) revert E_Reentrancy();

marketStorage().reentrancyLocked = true;
_marketStorage.reentrancyLocked = true;
_;
marketStorage().reentrancyLocked = false;
_marketStorage.reentrancyLocked = false;
}

modifier nonReentrantView() {
Expand Down
32 changes: 19 additions & 13 deletions src/EVault/shared/BorrowUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ abstract contract BorrowUtils is Base {
private
returns (Owed newOwed, Owed prevOwed)
{
prevOwed = marketStorage().users[account].getOwed();
Market storage _marketStorage = marketStorage();
prevOwed = _marketStorage.users[account].getOwed();
newOwed = getCurrentOwed(marketCache, account, prevOwed);

marketStorage().users[account].setOwed(newOwed);
marketStorage().users[account].interestAccumulator = marketCache.interestAccumulator;
_marketStorage.users[account].setOwed(newOwed);
_marketStorage.users[account].interestAccumulator = marketCache.interestAccumulator;
}

function increaseBorrow(MarketCache memory marketCache, address account, Assets assets) internal {
Expand All @@ -40,8 +41,9 @@ abstract contract BorrowUtils is Base {
Owed amount = assets.toOwed();
owed = owed + amount;

marketStorage().users[account].setOwed(owed);
marketStorage().totalBorrows = marketCache.totalBorrows = marketCache.totalBorrows + amount;
Market storage _marketStorage = marketStorage();
_marketStorage.users[account].setOwed(owed);
_marketStorage.totalBorrows = marketCache.totalBorrows = marketCache.totalBorrows + amount;

logBorrowChange(account, prevOwed, owed);
}
Expand All @@ -57,8 +59,9 @@ abstract contract BorrowUtils is Base {
owedRemaining = (owed - amount).toOwed();
}

marketStorage().users[account].setOwed(owedRemaining);
marketStorage().totalBorrows = marketCache.totalBorrows =
Market storage _marketStorage = marketStorage();
_marketStorage.users[account].setOwed(owedRemaining);
_marketStorage.totalBorrows = marketCache.totalBorrows =
marketCache.totalBorrows > owedExact ? marketCache.totalBorrows - owedExact + owedRemaining : owedRemaining;

logBorrowChange(account, prevOwed, owedRemaining);
Expand All @@ -84,17 +87,19 @@ abstract contract BorrowUtils is Base {

toOwed = toOwed + amount;

marketStorage().users[from].setOwed(fromOwed);
marketStorage().users[to].setOwed(toOwed);
Market storage _marketStorage = marketStorage();
_marketStorage.users[from].setOwed(fromOwed);
_marketStorage.users[to].setOwed(toOwed);

logBorrowChange(from, fromOwedPrev, fromOwed);
logBorrowChange(to, toOwedPrev, toOwed);
}

function computeInterestRate(MarketCache memory marketCache) internal virtual returns (uint256) {
// single sload
address irm = marketStorage().interestRateModel;
uint256 newInterestRate = marketStorage().interestRate;
Market storage _marketStorage = marketStorage();
address irm = _marketStorage.interestRateModel;
uint256 newInterestRate = _marketStorage.interestRate;

if (irm != address(0)) {
(bool success, bytes memory data) = irm.call(
Expand All @@ -116,8 +121,9 @@ abstract contract BorrowUtils is Base {

function computeInterestRateView(MarketCache memory marketCache) internal view virtual returns (uint256) {
// single sload
address irm = marketStorage().interestRateModel;
uint256 newInterestRate = marketStorage().interestRate;
Market storage _marketStorage = marketStorage();
address irm = _marketStorage.interestRateModel;
uint256 newInterestRate = _marketStorage.interestRate;

if (irm != address(0) && isVaultStatusCheckDeferred()) {
(bool success, bytes memory data) = irm.staticcall(
Expand Down
39 changes: 20 additions & 19 deletions src/EVault/shared/Cache.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ contract Cache is MarketStorage, Errors {
// If different from MarketStorage, updates MarketStorage
function updateMarket() internal returns (MarketCache memory marketCache) {
if (initMarketCache(marketCache)) {
marketStorage().lastInterestAccumulatorUpdate = marketCache.lastInterestAccumulatorUpdate;
marketStorage().accumulatedFees = marketCache.accumulatedFees;
Market storage _marketStorage = marketStorage();
_marketStorage.lastInterestAccumulatorUpdate = marketCache.lastInterestAccumulatorUpdate;
_marketStorage.accumulatedFees = marketCache.accumulatedFees;

marketStorage().totalShares = marketCache.totalShares;
marketStorage().totalBorrows = marketCache.totalBorrows;
_marketStorage.totalShares = marketCache.totalShares;
_marketStorage.totalBorrows = marketCache.totalBorrows;

marketStorage().interestAccumulator = marketCache.interestAccumulator;
_marketStorage.interestAccumulator = marketCache.interestAccumulator;
}
}

Expand All @@ -44,21 +45,21 @@ contract Cache is MarketStorage, Errors {
(marketCache.asset, marketCache.oracle, marketCache.unitOfAccount) = ProxyUtils.metadata();

// Storage loads
Market storage _marketStorage = marketStorage();
marketCache.lastInterestAccumulatorUpdate = _marketStorage.lastInterestAccumulatorUpdate;
marketCache.cash = _marketStorage.cash;
marketCache.supplyCap = _marketStorage.supplyCap.toUint();
marketCache.borrowCap = _marketStorage.borrowCap.toUint();
marketCache.disabledOps = _marketStorage.disabledOps;
marketCache.snapshotInitialized = _marketStorage.snapshotInitialized;

marketCache.lastInterestAccumulatorUpdate = marketStorage().lastInterestAccumulatorUpdate;
marketCache.cash = marketStorage().cash;
marketCache.supplyCap = marketStorage().supplyCap.toUint();
marketCache.borrowCap = marketStorage().borrowCap.toUint();
marketCache.disabledOps = marketStorage().disabledOps;
marketCache.snapshotInitialized = marketStorage().snapshotInitialized;
marketCache.totalShares = _marketStorage.totalShares;
marketCache.totalBorrows = _marketStorage.totalBorrows;

marketCache.totalShares = marketStorage().totalShares;
marketCache.totalBorrows = marketStorage().totalBorrows;
marketCache.accumulatedFees = _marketStorage.accumulatedFees;
marketCache.configFlags = _marketStorage.configFlags;

marketCache.accumulatedFees = marketStorage().accumulatedFees;
marketCache.configFlags = marketStorage().configFlags;

marketCache.interestAccumulator = marketStorage().interestAccumulator;
marketCache.interestAccumulator = _marketStorage.interestAccumulator;

// Update interest accumulator and fees balance
uint256 deltaT = block.timestamp - marketCache.lastInterestAccumulatorUpdate;
Expand All @@ -73,8 +74,8 @@ contract Cache is MarketStorage, Errors {

// Compute new values. Use full precision for intermediate results.

ConfigAmount interestFee = marketStorage().interestFee;
uint256 interestRate = marketStorage().interestRate;
ConfigAmount interestFee = _marketStorage.interestFee;
uint256 interestRate = _marketStorage.interestRate;

uint256 newInterestAccumulator = marketCache.interestAccumulator;

Expand Down
2 changes: 0 additions & 2 deletions src/EVault/shared/types/MarketCache.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ struct MarketCache {
IERC20 asset;
IPriceOracle oracle;
address unitOfAccount;

uint48 lastInterestAccumulatorUpdate;
Assets cash;
Owed totalBorrows;
Shares totalShares;
Shares accumulatedFees;
uint256 interestAccumulator;

uint256 supplyCap;
uint256 borrowCap;
Flags disabledOps;
Expand Down

0 comments on commit db5c03d

Please sign in to comment.