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

Runtime fv #37

Merged
merged 5 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
90 changes: 69 additions & 21 deletions src/TermDiscountRateAdapter.sol
Original file line number Diff line number Diff line change
@@ -1,31 +1,21 @@
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.18;
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

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, AccessControlUpgradeable {
import {AccessControl} from "@openzeppelin/contracts/access/AccessControl.sol";

contract TermDiscountRateAdapter is ITermDiscountRateAdapter, AccessControl {
bytes32 public constant ORACLE_ROLE = keccak256("ORACLE_ROLE");
/// @notice The Term Controller contract

ITermController public immutable TERM_CONTROLLER;
mapping(address => mapping (bytes32 => bool)) public rateInvalid;
mapping(address => uint256) public repoRedemptionHaircut;

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

/**
Expand All @@ -35,13 +25,60 @@ contract TermDiscountRateAdapter is ITermDiscountRateAdapter, AccessControlUpgra
* @dev This function fetches the auction results for the repo token's term repo ID
* and returns the clearing rate of the most recent auction
*/
function getDiscountRate(address repoToken) external view returns (uint256) {
function getDiscountRate(address repoToken) public view virtual returns (uint256) {
if (repoToken == address(0)) return 0;
(AuctionMetadata[] memory auctionMetadata, ) = TERM_CONTROLLER.getTermAuctionResults(ITermRepoToken(repoToken).termRepoId());

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

// If there is a re-opening auction, e.g. 2 or more results for the same token
if (len > 1) {
uint256 latestAuctionTime = auctionMetadata[len - 1].auctionClearingBlockTimestamp;
if ((block.timestamp - latestAuctionTime) < 30 minutes) {
for (int256 i = int256(len) - 2; i >= 0; i--) {
if (!rateInvalid[repoToken][auctionMetadata[uint256(i)].termAuctionId]) {
return auctionMetadata[uint256(i)].auctionClearingRate;
}
}
} else {
for (int256 i = int256(len) - 1; i >= 0; i--) {
if (!rateInvalid[repoToken][auctionMetadata[uint256(i)].termAuctionId]) {
return auctionMetadata[uint256(i)].auctionClearingRate;
}
}
}
revert("No valid auction rate found");
}

// If there is only 1 result (not a re-opening) then always return result
return auctionMetadata[0].auctionClearingRate;
}

return auctionMetadata[len - 1].auctionClearingRate;
/**
* @notice Sets the invalidity of the result of a specific auction for a given repo token
* @dev This function is used to mark auction results as invalid or not, typically in cases of suspected manipulation
* @param repoToken The address of the repo token associated with the auction
* @param termAuctionId The unique identifier of the term auction to be invalidated
* @param isInvalid The status of the rate invalidation
* @custom:access Restricted to accounts with the ORACLE_ROLE
*/
function setAuctionRateValidator(
address repoToken,
bytes32 termAuctionId,
bool isInvalid
) external onlyRole(ORACLE_ROLE) {
// Fetch the auction metadata for the given repo token
(AuctionMetadata[] memory auctionMetadata, ) = TERM_CONTROLLER.getTermAuctionResults(ITermRepoToken(repoToken).termRepoId());

// Check if the termAuctionId exists in the metadata
bool auctionExists = _validateAuctionExistence(auctionMetadata, termAuctionId);

// Revert if the auction doesn't exist
require(auctionExists, "Auction ID not found in metadata");

// Update the rate invalidation status
rateInvalid[repoToken][termAuctionId] = isInvalid;
}

/**
Expand All @@ -52,4 +89,15 @@ contract TermDiscountRateAdapter is ITermDiscountRateAdapter, AccessControlUpgra
function setRepoRedemptionHaircut(address repoToken, uint256 haircut) external onlyRole(ORACLE_ROLE) {
repoRedemptionHaircut[repoToken] = haircut;
}
}

function _validateAuctionExistence(AuctionMetadata[] memory auctionMetadata, bytes32 termAuctionId) private view returns(bool auctionExists) {
// Check if the termAuctionId exists in the metadata
bool auctionExists;
for (uint256 i = 0; i < auctionMetadata.length; i++) {
if (auctionMetadata[i].termAuctionId == termAuctionId) {
auctionExists = true;
break;
}
}
}
}
63 changes: 0 additions & 63 deletions src/test/Oracle.t.sol

This file was deleted.

Loading