Skip to content

Commit

Permalink
Clean up code and documentation (#12)
Browse files Browse the repository at this point in the history
This PR removes some unneeded internal functions and improves the documentation in the contract. There are no functional changes in this PR.
  • Loading branch information
kevincheng96 authored Oct 5, 2023
1 parent 0fd0d4f commit 1fb3863
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 157 deletions.
46 changes: 20 additions & 26 deletions src/CometHelpers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ pragma solidity 0.8.17;

import {CometMath} from "./vendor/CometMath.sol";

/// @notice Includes helper functions ripped from different contracts in Comet instead
/// of copying whole contracts. Also includes error definitions, events, and constants.
/**
* @title Comet Helper Functions
* @notice Pure helper functions taken from different contracts in Comet
* @author Compound & gjaldon
*/
contract CometHelpers is CometMath {
uint64 internal constant FACTOR_SCALE = 1e18;
uint64 internal constant BASE_INDEX_SCALE = 1e15;
Expand All @@ -15,23 +18,19 @@ contract CometHelpers is CometMath {
DOWN
}

error InsufficientAllowance();
error TimestampTooLarge();
error UninitializedReward();
error ZeroShares();
error ZeroAddress();

event RewardClaimed(address indexed src, address indexed recipient, address indexed token, uint256 amount);

/// @dev Multiply a number by a factor
/// https://github.com/compound-finance/comet/blob/main/contracts/Comet.sol#L681-L683
/**
* @dev Multiply a number by a factor
* https://github.com/compound-finance/comet/blob/main/contracts/Comet.sol#L681-L683
*/
function mulFactor(uint256 n, uint256 factor) internal pure returns (uint256) {
return n * factor / FACTOR_SCALE;
}

/// @dev The principal amount projected forward by the supply index
/// Note: The returned value can be rounded up or down
/// From https://github.com/compound-finance/comet/blob/main/contracts/CometCore.sol#L83-L85
/**
* @dev The principal amount projected forward by the supply index
* Note: The returned value can be rounded up or down
* From https://github.com/compound-finance/comet/blob/main/contracts/CometCore.sol#L83-L85
*/
function presentValueSupply(uint64 baseSupplyIndex_, uint256 principalValue_, Rounding rounding) internal pure returns (uint256) {
if (rounding == Rounding.DOWN) {
return principalValue_ * baseSupplyIndex_ / BASE_INDEX_SCALE;
Expand All @@ -40,22 +39,17 @@ contract CometHelpers is CometMath {
}
}

/// @dev The present value projected backward by the supply index (rounded down)
/// Note: The returned value can be rounded up or down
/// Note: This will overflow (revert) at 2^104/1e18=~20 trillion principal for assets with 18 decimals.
/// From https://github.com/compound-finance/comet/blob/main/contracts/CometCore.sol#L109-L111
/**
* @dev The present value projected backward by the supply index (rounded down)
* Note: The returned value can be rounded up or down
* Note: This will overflow (revert) at 2^104/1e18=~20 trillion principal for assets with 18 decimals.
* From https://github.com/compound-finance/comet/blob/main/contracts/CometCore.sol#L109-L111
*/
function principalValueSupply(uint64 baseSupplyIndex_, uint256 presentValue_, Rounding rounding) internal pure returns (uint104) {
if (rounding == Rounding.DOWN) {
return safe104((presentValue_ * BASE_INDEX_SCALE) / baseSupplyIndex_);
} else {
return safe104((presentValue_ * BASE_INDEX_SCALE + baseSupplyIndex_ - 1) / baseSupplyIndex_);
}
}

/// @dev The current timestamp
/// From https://github.com/compound-finance/comet/blob/main/contracts/Comet.sol#L375-L378
function getNowInternal() internal view virtual returns (uint40) {
if (block.timestamp >= 2**40) revert TimestampTooLarge();
return uint40(block.timestamp);
}
}
Loading

0 comments on commit 1fb3863

Please sign in to comment.