Skip to content

Commit

Permalink
Merge pull request #13 from GenerationSoftware/gen-336-macro-issue-l-…
Browse files Browse the repository at this point in the history
…2-overflow-of-lastcloseddrawid-uint16-will

Increase drawId to uint24 so that the contract lasts 40k+ years
  • Loading branch information
asselstine committed Aug 15, 2023
2 parents 74b3704 + 775a882 commit 2155e33
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 113 deletions.
42 changes: 21 additions & 21 deletions src/PrizePool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ contract PrizePool is TieredLiquidityDistributor {
address indexed vault,
address indexed winner,
address indexed recipient,
uint16 drawId,
uint24 drawId,
uint8 tier,
uint32 prizeIndex,
uint152 payout,
Expand All @@ -151,7 +151,7 @@ contract PrizePool is TieredLiquidityDistributor {
/// @param prizeTokensPerShare The amount of prize tokens per share for the next draw
/// @param drawStartedAt The start timestamp of the draw
event DrawClosed(
uint16 indexed drawId,
uint24 indexed drawId,
uint256 winningRandomNumber,
uint8 numTiers,
uint8 nextNumTiers,
Expand All @@ -174,7 +174,7 @@ contract PrizePool is TieredLiquidityDistributor {
/// @param vault The address of the vault that is contributing tokens
/// @param drawId The ID of the first draw that the tokens will be applied to
/// @param amount The amount of tokens contributed
event ContributePrizeTokens(address indexed vault, uint16 indexed drawId, uint256 amount);
event ContributePrizeTokens(address indexed vault, uint24 indexed drawId, uint256 amount);

/// @notice Emitted when an address withdraws their prize claim rewards.
/// @param to The address the rewards are sent to
Expand All @@ -198,7 +198,7 @@ contract PrizePool is TieredLiquidityDistributor {

/// @notice Records the claim record for a winner.
/// @dev vault => account => drawId => tier => prizeIndex => claimed
mapping(address => mapping(address => mapping(uint16 => mapping(uint8 => mapping(uint32 => bool)))))
mapping(address => mapping(address => mapping(uint24 => mapping(uint8 => mapping(uint32 => bool)))))
internal claimedPrizes;

/// @notice Tracks the total fees accrued to each claimer.
Expand Down Expand Up @@ -322,7 +322,7 @@ contract PrizePool is TieredLiquidityDistributor {
/// @notice Allows the Manager to withdraw tokens from the reserve.
/// @param _to The address to send the tokens to
/// @param _amount The amount of tokens to withdraw
function withdrawReserve(address _to, uint104 _amount) external onlyDrawManager {
function withdrawReserve(address _to, uint96 _amount) external onlyDrawManager {
if (_amount > _reserve) {
revert InsufficientReserve(_amount, _reserve);
}
Expand All @@ -335,7 +335,7 @@ contract PrizePool is TieredLiquidityDistributor {
/// Updates the number of tiers, the winning random number and the prize pool reserve.
/// @param winningRandomNumber_ The winning random number for the current draw
/// @return The ID of the closed draw
function closeDraw(uint256 winningRandomNumber_) external onlyDrawManager returns (uint16) {
function closeDraw(uint256 winningRandomNumber_) external onlyDrawManager returns (uint24) {
// check winning random number
if (winningRandomNumber_ == 0) {
revert RandomNumberIsZero();
Expand All @@ -353,7 +353,7 @@ contract PrizePool is TieredLiquidityDistributor {

uint64 openDrawStartedAt_ = _openDrawStartedAt();

_nextDraw(_nextNumberOfTiers, SafeCast.toUint96(_contributionsForDraw(lastClosedDrawId + 1)));
_nextDraw(_nextNumberOfTiers, _contributionsForDraw(lastClosedDrawId + 1));

_winningRandomNumber = winningRandomNumber_;
claimCount = 0;
Expand Down Expand Up @@ -406,7 +406,7 @@ contract PrizePool is TieredLiquidityDistributor {
revert FeeTooLarge(_fee, tierLiquidity.prizeSize);
}

(SD59x18 _vaultPortion, SD59x18 _tierOdds, uint16 _drawDuration) = _computeVaultTierDetails(
(SD59x18 _vaultPortion, SD59x18 _tierOdds, uint24 _drawDuration) = _computeVaultTierDetails(
msg.sender,
_tier,
numberOfTiers,
Expand Down Expand Up @@ -477,7 +477,7 @@ contract PrizePool is TieredLiquidityDistributor {
/// @notice Allows anyone to deposit directly into the Prize Pool reserve.
/// @dev Ensure caller has sufficient balance and has approved the Prize Pool to transfer the tokens
/// @param _amount The amount of tokens to increase the reserve by
function contributeReserve(uint104 _amount) external {
function contributeReserve(uint96 _amount) external {
_reserve += _amount;
directlyContributedReserve += _amount;
prizeToken.safeTransferFrom(msg.sender, address(this), _amount);
Expand Down Expand Up @@ -598,7 +598,7 @@ contract PrizePool is TieredLiquidityDistributor {
(, uint104 newReserve, ) = _computeNewDistributions(
_numTiers,
_nextNumberOfTiers,
SafeCast.toUint96(_contributionsForDraw(lastClosedDrawId + 1))
_contributionsForDraw(lastClosedDrawId + 1)
);

return newReserve;
Expand Down Expand Up @@ -646,7 +646,7 @@ contract PrizePool is TieredLiquidityDistributor {
uint8 _tier,
uint32 _prizeIndex
) external view returns (bool) {
(SD59x18 vaultPortion, SD59x18 tierOdds, uint16 drawDuration) = _computeVaultTierDetails(
(SD59x18 vaultPortion, SD59x18 tierOdds, uint24 drawDuration) = _computeVaultTierDetails(
_vault,
_tier,
numberOfTiers,
Expand Down Expand Up @@ -705,8 +705,8 @@ contract PrizePool is TieredLiquidityDistributor {
*/
function getVaultPortion(
address _vault,
uint16 _startDrawId,
uint16 _endDrawId
uint24 _startDrawId,
uint24 _endDrawId
) external view returns (SD59x18) {
return _getVaultPortion(_vault, _startDrawId, _endDrawId, smoothing.intoSD59x18());
}
Expand Down Expand Up @@ -780,7 +780,7 @@ contract PrizePool is TieredLiquidityDistributor {
/// @notice Computes the tokens to be disbursed from the accumulator for a given draw.
/// @param _drawId The ID of the draw to compute the disbursement for.
/// @return The amount of tokens contributed to the accumulator for the given draw.
function _contributionsForDraw(uint16 _drawId) internal view returns (uint256) {
function _contributionsForDraw(uint24 _drawId) internal view returns (uint256) {
return
DrawAccumulatorLib.getDisbursedBetween(
totalAccumulator,
Expand Down Expand Up @@ -814,7 +814,7 @@ contract PrizePool is TieredLiquidityDistributor {
uint32 _prizeIndex,
SD59x18 _vaultPortion,
SD59x18 _tierOdds,
uint16 _drawDuration
uint24 _drawDuration
) internal view returns (bool) {
uint32 tierPrizeCount = uint32(TierCalculationLib.prizeCount(_tier));

Expand Down Expand Up @@ -858,18 +858,18 @@ contract PrizePool is TieredLiquidityDistributor {
address _vault,
uint8 _tier,
uint8 _numberOfTiers,
uint16 _lastClosedDrawId
) internal view returns (SD59x18 vaultPortion, SD59x18 tierOdds, uint16 drawDuration) {
uint24 _lastClosedDrawId
) internal view returns (SD59x18 vaultPortion, SD59x18 tierOdds, uint24 drawDuration) {
if (_lastClosedDrawId == 0) {
revert NoClosedDraw();
}
_checkValidTier(_tier, _numberOfTiers);

tierOdds = _tierOdds(_tier, numberOfTiers);
drawDuration = uint16(TierCalculationLib.estimatePrizeFrequencyInDraws(tierOdds));
drawDuration = uint24(TierCalculationLib.estimatePrizeFrequencyInDraws(tierOdds));
vaultPortion = _getVaultPortion(
_vault,
uint16(drawDuration > _lastClosedDrawId ? 0 : _lastClosedDrawId - drawDuration + 1),
SafeCast.toUint24(drawDuration > _lastClosedDrawId ? 0 : _lastClosedDrawId - drawDuration + 1),
_lastClosedDrawId,
smoothing.intoSD59x18()
);
Expand Down Expand Up @@ -911,8 +911,8 @@ contract PrizePool is TieredLiquidityDistributor {
*/
function _getVaultPortion(
address _vault,
uint16 _startDrawId,
uint16 _endDrawId,
uint24 _startDrawId,
uint24 _endDrawId,
SD59x18 _smoothing
) internal view returns (SD59x18) {
uint256 totalContributed = DrawAccumulatorLib.getDisbursedBetween(
Expand Down
40 changes: 20 additions & 20 deletions src/abstract/TieredLiquidityDistributor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import { TierCalculationLib } from "../libraries/TierCalculationLib.sol";

/// @notice Struct that tracks tier liquidity information.
struct Tier {
uint16 drawId;
uint96 prizeSize;
uint24 drawId;
uint104 prizeSize;
UD34x4 prizeTokenPerShare;
}

Expand Down Expand Up @@ -205,10 +205,10 @@ contract TieredLiquidityDistributor {
uint8 public numberOfTiers;

/// @notice The draw id of the last closed draw.
uint16 internal lastClosedDrawId;
uint24 internal lastClosedDrawId;

/// @notice The amount of available reserve.
uint104 internal _reserve;
uint96 internal _reserve;

/**
* @notice Constructs a new Prize Pool.
Expand All @@ -232,16 +232,16 @@ contract TieredLiquidityDistributor {
/// @notice Adjusts the number of tiers and distributes new liquidity.
/// @param _nextNumberOfTiers The new number of tiers. Must be greater than minimum
/// @param _prizeTokenLiquidity The amount of fresh liquidity to distribute across the tiers and reserve
function _nextDraw(uint8 _nextNumberOfTiers, uint96 _prizeTokenLiquidity) internal {
function _nextDraw(uint8 _nextNumberOfTiers, uint256 _prizeTokenLiquidity) internal {
if (_nextNumberOfTiers < MINIMUM_NUMBER_OF_TIERS) {
revert NumberOfTiersLessThanMinimum(_nextNumberOfTiers);
}

uint8 numTiers = numberOfTiers;
UD60x18 _prizeTokenPerShare = fromUD34x4toUD60x18(prizeTokenPerShare);
(
uint16 closedDrawId,
uint104 newReserve,
uint24 closedDrawId,
uint96 newReserve,
UD60x18 newPrizeTokenPerShare
) = _computeNewDistributions(
numTiers,
Expand Down Expand Up @@ -287,7 +287,7 @@ contract TieredLiquidityDistributor {
uint8 _numberOfTiers,
uint8 _nextNumberOfTiers,
uint256 _prizeTokenLiquidity
) internal view returns (uint16 closedDrawId, uint104 newReserve, UD60x18 newPrizeTokenPerShare) {
) internal view returns (uint24 closedDrawId, uint96 newReserve, UD60x18 newPrizeTokenPerShare) {
return
_computeNewDistributions(
_numberOfTiers,
Expand All @@ -310,7 +310,7 @@ contract TieredLiquidityDistributor {
uint8 _nextNumberOfTiers,
UD60x18 _currentPrizeTokenPerShare,
uint _prizeTokenLiquidity
) internal view returns (uint16 closedDrawId, uint104 newReserve, UD60x18 newPrizeTokenPerShare) {
) internal view returns (uint24 closedDrawId, uint96 newReserve, UD60x18 newPrizeTokenPerShare) {
closedDrawId = lastClosedDrawId + 1;

uint reclaimedLiquidity = _getTierLiquidityToReclaim(
Expand All @@ -325,7 +325,7 @@ contract TieredLiquidityDistributor {

newPrizeTokenPerShare = _currentPrizeTokenPerShare.add(deltaPrizeTokensPerShare);

newReserve = uint104(
newReserve = SafeCast.toUint96(
// reserve portion of new liquidity
convert(deltaPrizeTokensPerShare.mul(convert(reserveShares))) +
// remainder left over from shares
Expand All @@ -338,7 +338,7 @@ contract TieredLiquidityDistributor {
/// @notice Returns the prize size for the given tier.
/// @param _tier The tier to retrieve
/// @return The prize size for the tier
function getTierPrizeSize(uint8 _tier) external view returns (uint96) {
function getTierPrizeSize(uint8 _tier) external view returns (uint104) {
return _getTier(_tier, numberOfTiers).prizeSize;
}

Expand All @@ -355,7 +355,7 @@ contract TieredLiquidityDistributor {
/// @return An up-to-date Tier struct; if the prize is outdated then it is recomputed based on available liquidity and the draw id updated.
function _getTier(uint8 _tier, uint8 _numberOfTiers) internal view returns (Tier memory) {
Tier memory tier = _tiers[_tier];
uint16 _lastClosedDrawId = lastClosedDrawId;
uint24 _lastClosedDrawId = lastClosedDrawId;
if (tier.drawId != _lastClosedDrawId) {
tier.drawId = _lastClosedDrawId;
tier.prizeSize = _computePrizeSize(
Expand Down Expand Up @@ -395,7 +395,7 @@ contract TieredLiquidityDistributor {
uint104 _liquidity
) internal returns (Tier memory) {
uint8 _shares = tierShares;
uint104 remainingLiquidity = uint104(
uint104 remainingLiquidity = SafeCast.toUint104(
convert(
_getTierRemainingLiquidity(
_shares,
Expand All @@ -405,7 +405,7 @@ contract TieredLiquidityDistributor {
)
);
if (_liquidity > remainingLiquidity) {
uint104 excess = _liquidity - remainingLiquidity;
uint96 excess = SafeCast.toUint96(_liquidity - remainingLiquidity);
if (excess > _reserve) {
revert InsufficientLiquidity(_liquidity);
}
Expand Down Expand Up @@ -433,7 +433,7 @@ contract TieredLiquidityDistributor {
uint8 _numberOfTiers,
UD60x18 _tierPrizeTokenPerShare,
UD60x18 _prizeTokenPerShare
) internal view returns (uint96) {
) internal view returns (uint104) {
uint256 prizeSize;
if (_prizeTokenPerShare.gt(_tierPrizeTokenPerShare)) {
prizeSize = _computePrizeSize(
Expand All @@ -447,10 +447,10 @@ contract TieredLiquidityDistributor {
prizeSize = (prizeSize * _getTotalShares(_numberOfTiers)) / _getTotalShares(_numberOfTiers + 1);
}
}
if (prizeSize > type(uint96).max) {
return type(uint96).max;
if (prizeSize > type(uint104).max) {
return type(uint104).max;
} else {
return uint96(prizeSize);
return uint104(prizeSize);
}
}

Expand Down Expand Up @@ -547,7 +547,7 @@ contract TieredLiquidityDistributor {

/// @notice Retrieves the id of the next draw to be closed.
/// @return The next draw id
function getOpenDrawId() external view returns (uint16) {
function getOpenDrawId() external view returns (uint24) {
return lastClosedDrawId + 1;
}

Expand All @@ -566,7 +566,7 @@ contract TieredLiquidityDistributor {

/// @notice Returns the balance of the reserve.
/// @return The amount of tokens that have been reserved.
function reserve() external view returns (uint256) {
function reserve() external view returns (uint96) {
return _reserve;
}

Expand Down
Loading

0 comments on commit 2155e33

Please sign in to comment.