Skip to content

Commit

Permalink
remove discount rate from RepoTokenList library cache
Browse files Browse the repository at this point in the history
  • Loading branch information
aazhou1 committed Sep 6, 2024
1 parent 2272739 commit 0dad113
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 26 deletions.
32 changes: 8 additions & 24 deletions src/RepoTokenList.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ struct RepoTokenListNode {
struct RepoTokenListData {
address head;
mapping(address => RepoTokenListNode) nodes;
mapping(address => uint256) discountRates;
/// @notice keyed by collateral token
mapping(address => uint256) collateralTokenParams;
}
Expand Down Expand Up @@ -174,6 +173,7 @@ library RepoTokenList {
* @notice Get the present value of repoTokens
* @param listData The list data
* @param purchaseTokenPrecision The precision of the purchase token
* @param discountRateAdapter The discount rate adapter
* @param repoTokenToMatch The address of the repoToken to match (optional)
* @return totalPresentValue The total present value of the repoTokens
* @dev If the `repoTokenToMatch` parameter is provided (non-zero address), the function will filter
Expand All @@ -187,6 +187,7 @@ library RepoTokenList {
function getPresentValue(
RepoTokenListData storage listData,
uint256 purchaseTokenPrecision,
ITermDiscountRateAdapter discountRateAdapter,
address repoTokenToMatch
) internal view returns (uint256 totalPresentValue) {
// If the list is empty, return 0
Expand All @@ -205,7 +206,7 @@ library RepoTokenList {
uint256 currentMaturity = getRepoTokenMaturity(current);
uint256 repoTokenBalance = ITermRepoToken(current).balanceOf(address(this));
uint256 repoTokenPrecision = 10**ERC20(current).decimals();
uint256 discountRate = listData.discountRates[current];
uint256 discountRate = discountRateAdapter.getDiscountRate(current);

// Convert repo token balance to base asset precision
// (ratePrecision * repoPrecision * purchasePrecision) / (repoPrecision * ratePrecision) = purchasePrecision
Expand Down Expand Up @@ -291,7 +292,6 @@ library RepoTokenList {

listData.nodes[prev].next = next;
delete listData.nodes[current];
delete listData.discountRates[current];
}
} else {
/// @dev early exit because list is sorted
Expand Down Expand Up @@ -364,29 +364,13 @@ library RepoTokenList {
ITermDiscountRateAdapter discountRateAdapter,
address asset
) internal returns (uint256 discountRate, uint256 redemptionTimestamp) {
discountRate = listData.discountRates[address(repoToken)];
if (discountRate != INVALID_AUCTION_RATE) {
(redemptionTimestamp, , ,) = repoToken.config();

// skip matured repoTokens
if (redemptionTimestamp < block.timestamp) {
revert InvalidRepoToken(address(repoToken));
}

uint256 oracleRate = discountRateAdapter.getDiscountRate(address(repoToken));
if (oracleRate != INVALID_AUCTION_RATE) {
if (discountRate != oracleRate) {
listData.discountRates[address(repoToken)] = oracleRate;
}
}
} else {
discountRate = discountRateAdapter.getDiscountRate(address(repoToken));

discountRate = discountRateAdapter.getDiscountRate(address(repoToken));

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

insertSorted(listData, address(repoToken));
listData.discountRates[address(repoToken)] = discountRate;
}
insertSorted(listData, address(repoToken));

}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/Strategy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ contract Strategy is BaseStrategy, Pausable, ReentrancyGuard {
return
repoTokenListData.getPresentValue(
PURCHASE_TOKEN_PRECISION,
discountRateAdapter,
repoToken
) +
termAuctionListData.getPresentValue(
Expand Down Expand Up @@ -490,6 +491,7 @@ contract Strategy is BaseStrategy, Pausable, ReentrancyGuard {
liquidBalance +
repoTokenListData.getPresentValue(
PURCHASE_TOKEN_PRECISION,
discountRateAdapter,
address(0)
) +
termAuctionListData.getPresentValue(
Expand Down Expand Up @@ -611,6 +613,7 @@ contract Strategy is BaseStrategy, Pausable, ReentrancyGuard {
bool foundInOfferList
) = termAuctionListData.getCumulativeOfferData(
repoTokenListData,
discountRateAdapter,
repoToken,
repoTokenAmount,
PURCHASE_TOKEN_PRECISION
Expand Down
6 changes: 4 additions & 2 deletions src/TermAuctionList.sol
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ library TermAuctionList {
// Handle new or unseen repo tokens
/// @dev offer processed, but auctionClosed not yet called and auction is new so repoToken not on List and wont be picked up
/// checking repoTokendiscountRates to make sure we are not double counting on re-openings
if (offer.termAuction.auctionCompleted() && repoTokenListData.discountRates[offer.repoToken] == 0) {
if (offer.termAuction.auctionCompleted() && discountRateAdapter.getDiscountRate(offer.repoToken) == 0) {
if (!offer.isRepoTokenSeen) {
uint256 repoTokenAmountInBaseAssetPrecision = RepoTokenUtils.getNormalizedRepoTokenAmount(
offer.repoToken,
Expand Down Expand Up @@ -316,6 +316,7 @@ library TermAuctionList {
* @notice Get cumulative offer data for a specified repoToken
* @param listData The list data
* @param repoTokenListData The repoToken list data
* @param discountRateAdapter The discount rate adapter
* @param repoToken The address of the repoToken (optional)
* @param newOfferAmount The new offer amount for the specified repoToken
* @param purchaseTokenPrecision The precision of the purchase token
Expand All @@ -331,6 +332,7 @@ library TermAuctionList {
function getCumulativeOfferData(
TermAuctionListData storage listData,
RepoTokenListData storage repoTokenListData,
ITermDiscountRateAdapter discountRateAdapter,
address repoToken,
uint256 newOfferAmount,
uint256 purchaseTokenPrecision
Expand All @@ -355,7 +357,7 @@ library TermAuctionList {
// Handle new repo tokens or reopening auctions
/// @dev offer processed, but auctionClosed not yet called and auction is new so repoToken not on List and wont be picked up
/// checking repoTokendiscountRates to make sure we are not double counting on re-openings
if (offer.termAuction.auctionCompleted() && repoTokenListData.discountRates[offer.repoToken] == 0) {
if (offer.termAuction.auctionCompleted() && discountRateAdapter.getDiscountRate(offer.repoToken) == 0) {
// use normalized repoToken amount if repoToken is not in the list
if (!offer.isRepoTokenSeen) {
offerAmount = RepoTokenUtils.getNormalizedRepoTokenAmount(
Expand Down

0 comments on commit 0dad113

Please sign in to comment.