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

Adding repo concentration limit #5

Merged
merged 9 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
32 changes: 14 additions & 18 deletions src/RepoTokenList.sol
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.18;

import "forge-std/console.sol";
import {ITermRepoToken} from "./interfaces/term/ITermRepoToken.sol";
import {ITermRepoServicer} from "./interfaces/term/ITermRepoServicer.sol";
import {ITermRepoCollateralManager} from "./interfaces/term/ITermRepoCollateralManager.sol";
import {ITermDiscountRateAdapter} from "./interfaces/term/ITermDiscountRateAdapter.sol";
import {ITermController, AuctionMetadata} from "./interfaces/term/ITermController.sol";
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {RepoTokenUtils} from "./RepoTokenUtils.sol";
Expand Down Expand Up @@ -118,6 +118,7 @@ library RepoTokenList {
address prev = current;
while (current != NULL_NODE) {
address next;

if (getRepoTokenMaturity(current) < block.timestamp) {
bool removeMaturedToken;
uint256 repoTokenBalance = ITermRepoToken(current).balanceOf(address(this));
Expand Down Expand Up @@ -158,18 +159,6 @@ library RepoTokenList {
}
}

function getAuctionRate(ITermController termController, ITermRepoToken repoToken) internal view returns (uint256) {
(AuctionMetadata[] memory auctionMetadata, ) = termController.getTermAuctionResults(repoToken.termRepoId());

uint256 len = auctionMetadata.length;

if (len == 0) {
revert InvalidRepoToken(address(repoToken));
}

return auctionMetadata[len - 1].auctionClearingRate;
}

function validateRepoToken(
RepoTokenListData storage listData,
ITermRepoToken repoToken,
Expand Down Expand Up @@ -212,9 +201,9 @@ library RepoTokenList {
RepoTokenListData storage listData,
ITermRepoToken repoToken,
ITermController termController,
ITermDiscountRateAdapter discountRateAdapter,
address asset
) internal returns (uint256 auctionRate, uint256 redemptionTimestamp)
{
) internal returns (uint256 auctionRate, uint256 redemptionTimestamp) {
auctionRate = listData.auctionRates[address(repoToken)];
if (auctionRate != INVALID_AUCTION_RATE) {
(redemptionTimestamp, , ,) = repoToken.config();
Expand All @@ -224,14 +213,14 @@ library RepoTokenList {
revert InvalidRepoToken(address(repoToken));
}

uint256 oracleRate = getAuctionRate(termController, repoToken);
uint256 oracleRate = discountRateAdapter.getDiscountRate(address(repoToken));
if (oracleRate != INVALID_AUCTION_RATE) {
if (auctionRate != oracleRate) {
listData.auctionRates[address(repoToken)] = oracleRate;
}
}
} else {
auctionRate = getAuctionRate(termController, repoToken);
auctionRate = discountRateAdapter.getDiscountRate(address(repoToken));

redemptionTimestamp = validateRepoToken(listData, repoToken, termController, asset);

Expand Down Expand Up @@ -283,7 +272,8 @@ library RepoTokenList {

function getPresentValue(
RepoTokenListData storage listData,
uint256 purchaseTokenPrecision
uint256 purchaseTokenPrecision,
address repoTokenToMatch
) internal view returns (uint256 totalPresentValue) {
if (listData.head == NULL_NODE) return 0;

Expand All @@ -307,6 +297,12 @@ library RepoTokenList {
totalPresentValue += repoTokenBalanceInBaseAssetPrecision;
}

if (repoTokenToMatch != address(0) && current == repoTokenToMatch) {
// matching a specific repo token and terminate early because the list is sorted
// with no duplicates
break;
}

current = _getNext(listData, current);
}
}
Expand Down
Loading
Loading