Skip to content

Commit

Permalink
dToken rounding and gas golfing
Browse files Browse the repository at this point in the history
dtoken rounding
  • Loading branch information
frontier159 committed Aug 14, 2023
1 parent 81553f6 commit e0464a6
Show file tree
Hide file tree
Showing 9 changed files with 293 additions and 246 deletions.
42 changes: 21 additions & 21 deletions protocol/contracts/interfaces/v2/ITempleDebtToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,33 @@ interface ITempleDebtToken is IERC20, IERC20Metadata, ITempleElevatedAccess {
function baseRate() external view returns (uint96);

/**
* @notice The (base rate) total number of shares allocated out to users for internal book keeping
* @notice The last checkpoint time of the (base rate) principal and interest checkpoint
*/
function baseShares() external view returns (uint256);
function baseCheckpointTime() external view returns (uint32);

/**
* @notice The (base rate) total principal and interest owed across all debtors as of the latest checkpoint
*/
function baseCheckpoint() external view returns (uint256);
function baseCheckpoint() external view returns (uint128);

/**
* @notice The last checkpoint time of the (base rate) principal and interest checkpoint
* @notice The (base rate) total number of shares allocated out to users for internal book keeping
*/
function baseCheckpointTime() external view returns (uint32);
function baseShares() external view returns (uint128);

/**
* @notice The net amount of principal amount of debt minted across all users.
*/
function totalPrincipal() external view returns (uint128);

/**
* @notice The latest estimate of the (risk premium) interest (no principal) owed.
* @dev Indicative only. This total is only updated on a per strategy basis when that strategy gets
* checkpointed (on borrow/repay rate change).
* So it is generally always going to be out of date as each strategy will accrue interest independently
* on different rates.
*/
function estimatedTotalRiskPremiumInterest() external view returns (uint128);

/// @dev byte packed into two slots.
struct Debtor {
Expand Down Expand Up @@ -82,20 +96,6 @@ interface ITempleDebtToken is IERC20, IERC20Metadata, ITempleElevatedAccess {
uint32 checkpointTime
);

/**
* @notice The net amount of principal amount of debt minted across all users.
*/
function totalPrincipal() external view returns (uint256);

/**
* @notice The latest estimate of the (risk premium) interest (no principal) owed.
* @dev Indicative only. This total is only updated on a per strategy basis when that strategy gets
* checkpointed (on borrow/repay rate change).
* So it is generally always going to be out of date as each strategy will accrue interest independently
* on different rates.
*/
function estimatedTotalRiskPremiumInterest() external view returns (uint256);

/// @notice A set of addresses which are approved to mint/burn
function minters(address account) external view returns (bool);

Expand Down Expand Up @@ -200,10 +200,10 @@ interface ITempleDebtToken is IERC20, IERC20Metadata, ITempleElevatedAccess {
/**
* @notice Convert a (base interest) debt amount into proportional amount of shares
*/
function baseDebtToShares(uint256 debt) external view returns (uint256);
function baseDebtToShares(uint128 debt) external view returns (uint128);

/**
* @notice Convert a number of (base interest) shares into proportional amount of debt
*/
function baseSharesToDebt(uint256 shares) external view returns (uint256);
function baseSharesToDebt(uint128 shares) external view returns (uint128);
}
218 changes: 110 additions & 108 deletions protocol/contracts/v2/TempleDebtToken.sol

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ contract TempleLineOfCredit is ITempleLineOfCredit, TempleElevatedAccess {
_cache,
_accountData.debtCheckpoint,
_accountData.interestAccumulator,
false // don't round on the way in
false // don't round up on the way in
) + amount;

// Update the state
Expand Down Expand Up @@ -633,7 +633,7 @@ contract TempleLineOfCredit is ITempleLineOfCredit, TempleElevatedAccess {
}

// Only compound if we're on a new block
uint256 interestAccumulatorUpdatedAt = debtTokenData.interestAccumulatorUpdatedAt;
uint32 interestAccumulatorUpdatedAt = debtTokenData.interestAccumulatorUpdatedAt;
uint32 blockTs = uint32(block.timestamp);
if (blockTs != interestAccumulatorUpdatedAt) {
dirty = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,14 @@ contract TempleDebtTokenTestBase is TempleTest {
function makeExpected(
uint256 baseShares,
uint256 baseTotal,
uint256 debtorInterestOnly
uint256 debtorInterestOnly,
bool roundUp
) internal pure returns (Expected memory) {
return Expected({
baseShares: baseShares,
baseTotal: baseTotal,
debtorInterestOnly: debtorInterestOnly,
balanceOf: baseTotal + debtorInterestOnly
balanceOf: baseTotal + debtorInterestOnly + (roundUp ? 1 : 0)
});
}
}
Expand Down
Loading

0 comments on commit e0464a6

Please sign in to comment.