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 2 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
30 changes: 26 additions & 4 deletions src/TermDiscountRateAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,27 @@ pragma solidity ^0.8.18;
import {ITermDiscountRateAdapter} from "./interfaces/term/ITermDiscountRateAdapter.sol";
import {ITermController, AuctionMetadata} from "./interfaces/term/ITermController.sol";
import {ITermRepoToken} from "./interfaces/term/ITermRepoToken.sol";
import "@openzeppelin/contracts-upgradeable/contracts/access/AccessControlUpgradeable.sol";

/**
* @title TermDiscountRateAdapter
* @notice Adapter contract to retrieve discount rates for Term repo tokens
* @dev This contract implements the ITermDiscountRateAdapter interface and interacts with the Term Controller
*/
contract TermDiscountRateAdapter is ITermDiscountRateAdapter {
contract TermDiscountRateAdapter is ITermDiscountRateAdapter, AccessControlUpgradeable {
bytes32 public constant DEVOPS_ROLE = bytes32(keccak256("DEVOPS_ROLE"));

/// @notice The Term Controller contract
ITermController public immutable TERM_CONTROLLER;
mapping(address => mapping (bytes32 => bool)) public rateInvalid;

/**
* @notice Constructor to initialize the TermDiscountRateAdapter
* @param termController_ The address of the Term Controller contract
*/
constructor(address termController_) {
constructor(address termController_, address devopsWallet_) {
TERM_CONTROLLER = ITermController(termController_);
_grantRole(DEVOPS_ROLE, devopsWallet_);
}

/**
Expand All @@ -33,8 +38,25 @@ 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");

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;
}

return auctionMetadata[len - 1].auctionClearingRate;
function markRateInvalid(address repoToken, bytes32 termAuctionId) external onlyRole(DEVOPS_ROLE) {
rateInvalid[repoToken][termAuctionId] = true;
}
}
Loading