Skip to content
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

Yearn audit variable optimizations #119

Merged
merged 5 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/RepoTokenList.sol
Original file line number Diff line number Diff line change
Expand Up @@ -340,22 +340,21 @@
* @param discountRateAdapter The discount rate adapter
* @param asset The address of the base asset
* @return validRepoToken Whether the repoToken is valid
* @return discountRate The discount rate to be applied to the validated repoToken
* @return redemptionTimestamp The redemption timestamp of the validated repoToken
*/
function validateAndInsertRepoToken(
RepoTokenListData storage listData,
ITermRepoToken repoToken,
ITermDiscountRateAdapter discountRateAdapter,
address asset
) internal returns (bool validRepoToken, uint256 discountRate, uint256 redemptionTimestamp) {
discountRate = listData.discountRates[address(repoToken)];
) internal returns (bool validRepoToken, uint256 redemptionTimestamp) {
uint256 discountRate = listData.discountRates[address(repoToken)];
if (discountRate != INVALID_AUCTION_RATE) {
(redemptionTimestamp, , ,) = repoToken.config();

// skip matured repoTokens
if (redemptionTimestamp < block.timestamp) {
return (false, discountRate, redemptionTimestamp); //revert InvalidRepoToken(address(repoToken));
return (false, redemptionTimestamp); //revert InvalidRepoToken(address(repoToken));

Check warning on line 357 in src/RepoTokenList.sol

View check run for this annotation

Codecov / codecov/patch

src/RepoTokenList.sol#L357

Added line #L357 was not covered by tests
}

uint256 oracleRate;
Expand All @@ -374,20 +373,20 @@
discountRate = rate == 0 ? ZERO_AUCTION_RATE : rate;
} catch {
discountRate = INVALID_AUCTION_RATE;
return (false, discountRate, redemptionTimestamp);
return (false, redemptionTimestamp);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consistent error handling pattern needed

The function has inconsistent error handling:

  • Line 357: Returns false with redemptionTimestamp
  • Line 376: Returns false with redemptionTimestamp
  • Line 383: Returns false with redemptionTimestamp
  • Line 389: Returns true with redemptionTimestamp

Consider using a more consistent pattern, either:

  1. Always revert with InvalidRepoToken for invalid cases (commented out in line 357)
  2. Return false with a specific error code/reason

Also applies to: 383-383, 389-389

}

bool isRepoTokenValid;

(isRepoTokenValid, redemptionTimestamp) = validateRepoToken(listData, repoToken, asset);
if (!isRepoTokenValid) {
return (false, discountRate, redemptionTimestamp);
return (false, redemptionTimestamp);
}
insertSorted(listData, address(repoToken));
listData.discountRates[address(repoToken)] = discountRate;
}

return (true, discountRate, redemptionTimestamp);
return (true, redemptionTimestamp);
}

/**
Expand Down
7 changes: 2 additions & 5 deletions src/Strategy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ contract Strategy is BaseStrategy, Pausable, AccessControl {
) external onlyRole(GOVERNOR_ROLE) {
require(newTermControllerAddr != address(0));
require(ITermController(newTermControllerAddr).getProtocolReserveAddress() != address(0));
ITermController newTermController = ITermController(newTermControllerAddr);
address currentIteration = repoTokenListData.head;
while (currentIteration != address(0)) {
if (!_isTermDeployed(currentIteration)) {
Expand All @@ -191,7 +190,7 @@ contract Strategy is BaseStrategy, Pausable, AccessControl {
newTermControllerAddr
);
strategyState.prevTermController = ITermController(current);
strategyState.currTermController = newTermController;
strategyState.currTermController = ITermController(newTermControllerAddr);
}

/**
Expand Down Expand Up @@ -578,8 +577,6 @@ contract Strategy is BaseStrategy, Pausable, AccessControl {
* and the present value of all pending offers to calculate the total asset value.
*/
function _totalAssetValue(uint256 liquidBalance) internal view returns (uint256 totalValue) {
ITermController prevTermController = strategyState.prevTermController;
ITermController currTermController = strategyState.currTermController;

return
liquidBalance +
Expand Down Expand Up @@ -1087,7 +1084,7 @@ contract Strategy is BaseStrategy, Pausable, AccessControl {
}

// Validate and insert the repoToken into the list, retrieve auction rate and redemption timestamp
(bool isRepoTokenValid , , uint256 redemptionTimestamp) = repoTokenListData
(bool isRepoTokenValid , uint256 redemptionTimestamp) = repoTokenListData
.validateAndInsertRepoToken(
ITermRepoToken(repoToken),
strategyState.discountRateAdapter,
Expand Down
2 changes: 1 addition & 1 deletion src/TermAuctionList.sol
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ library TermAuctionList {
// because the offerLocker will have already removed the offer.
// This applies if the repoToken hasn't been added to the repoTokenList
// (only for new auctions, not reopenings).
(bool isValidRepoToken, , uint256 redemptionTimestamp ) = repoTokenListData.validateAndInsertRepoToken(
(bool isValidRepoToken, uint256 redemptionTimestamp ) = repoTokenListData.validateAndInsertRepoToken(
ITermRepoToken(offer.repoToken), discountRateAdapter, asset
);
if (!isValidRepoToken && block.timestamp > redemptionTimestamp) {
Expand Down
Loading