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 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
266 changes: 202 additions & 64 deletions src/RepoTokenList.sol

Large diffs are not rendered by default.

48 changes: 44 additions & 4 deletions src/RepoTokenUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,25 @@ pragma solidity ^0.8.18;
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {ITermRepoToken} from "./interfaces/term/ITermRepoToken.sol";

/*//////////////////////////////////////////////////////////////
LIBRARY: RepoTokenUtils
//////////////////////////////////////////////////////////////*/

library RepoTokenUtils {
uint256 public constant THREESIXTY_DAYCOUNT_SECONDS = 360 days;
uint256 public constant RATE_PRECISION = 1e18;

/*//////////////////////////////////////////////////////////////
PURE FUNCTIONS
//////////////////////////////////////////////////////////////*/

/**
* @notice Convert repoToken amount to purchase token precision
* @param repoTokenPrecision The precision of the repoToken
* @param purchaseTokenPrecision The precision of the purchase token
* @param purchaseTokenAmountInRepoPrecision The amount of purchase token in repoToken precision
* @return The amount in purchase token precision
*/
function repoToPurchasePrecision(
uint256 repoTokenPrecision,
uint256 purchaseTokenPrecision,
Expand All @@ -16,6 +31,13 @@ library RepoTokenUtils {
return (purchaseTokenAmountInRepoPrecision * purchaseTokenPrecision) / repoTokenPrecision;
}

/**
* @notice Convert purchase token amount to repoToken precision
* @param repoTokenPrecision The precision of the repoToken
* @param purchaseTokenPrecision The precision of the purchase token
* @param repoTokenAmount The amount of repoToken
* @return The amount in repoToken precision
*/
function purchaseToRepoPrecision(
uint256 repoTokenPrecision,
uint256 purchaseTokenPrecision,
Expand All @@ -24,22 +46,40 @@ library RepoTokenUtils {
return (repoTokenAmount * repoTokenPrecision) / purchaseTokenPrecision;
}

/*//////////////////////////////////////////////////////////////
VIEW FUNCTIONS
//////////////////////////////////////////////////////////////*/

/**
* @notice Calculate the present value of a repoToken
* @param repoTokenAmountInBaseAssetPrecision The amount of repoToken in base asset precision
* @param purchaseTokenPrecision The precision of the purchase token
* @param redemptionTimestamp The redemption timestamp of the repoToken
* @param discountRate The auction rate
* @return presentValue The present value of the repoToken
*/
function calculatePresentValue(
uint256 repoTokenAmountInBaseAssetPrecision,
uint256 purchaseTokenPrecision,
uint256 redemptionTimestamp,
uint256 auctionRate
uint256 discountRate
) internal view returns (uint256 presentValue) {
uint256 timeLeftToMaturityDayFraction =
((redemptionTimestamp - block.timestamp) * purchaseTokenPrecision) / THREESIXTY_DAYCOUNT_SECONDS;

// repoTokenAmountInBaseAssetPrecision / (1 + r * days / 360)
presentValue =
(repoTokenAmountInBaseAssetPrecision * purchaseTokenPrecision) /
(purchaseTokenPrecision + (auctionRate * timeLeftToMaturityDayFraction / RATE_PRECISION));
(purchaseTokenPrecision + (discountRate * timeLeftToMaturityDayFraction / RATE_PRECISION));
}

// returns repo token amount in base asset precision
/**
* @notice Get the normalized amount of a repoToken in base asset precision
* @param repoToken The address of the repoToken
* @param repoTokenAmount The amount of the repoToken
* @param purchaseTokenPrecision The precision of the purchase token
* @return repoTokenAmountInBaseAssetPrecision The normalized amount of the repoToken in base asset precision
*/
function getNormalizedRepoTokenAmount(
address repoToken,
uint256 repoTokenAmount,
Expand All @@ -51,4 +91,4 @@ library RepoTokenUtils {
(redemptionValue * repoTokenAmount * purchaseTokenPrecision) /
(repoTokenPrecision * RepoTokenUtils.RATE_PRECISION);
}
}
}
Loading
Loading