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

prevent discount rate manipulation #16

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
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
18 changes: 16 additions & 2 deletions src/TermDiscountRateAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {ITermRepoToken} from "./interfaces/term/ITermRepoToken.sol";
contract TermDiscountRateAdapter is ITermDiscountRateAdapter {
/// @notice The Term Controller contract
ITermController public immutable TERM_CONTROLLER;
mapping(address => mapping (bytes32 => bool)) public rateInvalid;

/**
* @notice Constructor to initialize the TermDiscountRateAdapter
Expand All @@ -33,8 +34,21 @@ contract TermDiscountRateAdapter is ITermDiscountRateAdapter {
(AuctionMetadata[] memory auctionMetadata, ) = TERM_CONTROLLER.getTermAuctionResults(ITermRepoToken(repoToken).termRepoId());

uint256 len = auctionMetadata.length;
require(len > 0);
require(len > 0, "No auctions found");

return auctionMetadata[len - 1].auctionClearingRate;
if (len > 1) {
if ((block.timestamp - auctionMetadata[len - 1].auctionClearingBlockTimestamp) < 30 minutes) {
uint256 i = 0;
while (!rateInvalid[repoToken][auctionMetadata[len - 1].termAuctionId]) {
i--;
require(i >= 0, "No valid auction rate found");
}
return auctionMetadata[i].auctionClearingRate;
}
}

require(!rateInvalid[repoToken][auctionMetadata[0].termAuctionId], "Most recent auction rate is invalid");

return auctionMetadata[0].auctionClearingRate;
Copy link

Choose a reason for hiding this comment

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

Review and fix the loop logic in getDiscountRate.

The loop intended to find a valid auction rate has several issues:

  • The index i is decremented from len - 1 but is not initialized before the loop starts.
  • The loop condition checks rateInvalid but does not correctly decrement i within the bounds of the array, which can lead to an out-of-bounds error.

Consider the following refactor to address these issues:

-                uint256 i = 0;
-                while (!rateInvalid[repoToken][auctionMetadata[len - 1].termAuctionId]) {
-                    i--;
-                    require(i >= 0, "No valid auction rate found");
-                }
+                int256 i = int256(len - 1);
+                while (i >= 0 && !rateInvalid[repoToken][auctionMetadata[uint256(i)].termAuctionId]) {
+                    i--;
+                }
+                require(i >= 0, "No valid auction rate found");
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
require(len > 0, "No auctions found");
return auctionMetadata[len - 1].auctionClearingRate;
if (len > 1) {
if ((block.timestamp - auctionMetadata[len - 1].auctionClearingBlockTimestamp) < 30 minutes) {
uint256 i = 0;
while (!rateInvalid[repoToken][auctionMetadata[len - 1].termAuctionId]) {
i--;
require(i >= 0, "No valid auction rate found");
}
return auctionMetadata[i].auctionClearingRate;
}
}
require(!rateInvalid[repoToken][auctionMetadata[0].termAuctionId], "Most recent auction rate is invalid");
return auctionMetadata[0].auctionClearingRate;
require(len > 0, "No auctions found");
if (len > 1) {
if ((block.timestamp - auctionMetadata[len - 1].auctionClearingBlockTimestamp) < 30 minutes) {
int256 i = int256(len - 1);
while (i >= 0 && !rateInvalid[repoToken][auctionMetadata[uint256(i)].termAuctionId]) {
i--;
}
require(i >= 0, "No valid auction rate found");
return auctionMetadata[uint256(i)].auctionClearingRate;
}
}
require(!rateInvalid[repoToken][auctionMetadata[0].termAuctionId], "Most recent auction rate is invalid");
return auctionMetadata[0].auctionClearingRate;

}
}
Loading