Skip to content

Commit

Permalink
Merge pull request #86 from GenerationSoftware/gen-1082-add-emergency…
Browse files Browse the repository at this point in the history
…-shutdown-mode-to-the-prize-pool

Gen 1082 add emergency shutdown mode to the prize pool
  • Loading branch information
asselstine authored Feb 13, 2024
2 parents 3596bbe + da93c00 commit 25f5b6e
Show file tree
Hide file tree
Showing 16 changed files with 818 additions and 446 deletions.
12 changes: 8 additions & 4 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ src = 'src'
out = 'out'
libs = ['lib']
solc = "0.8.19"
gas_limit = "18446744073709551615" # u64::MAX
gas_reports_ignore = ["ERC20Mintable", "TwabController"]
gas_limit = "18446744073709551615" # u64::MAX
block_gas_limit = "18446744073709551615" # u64::MAX
fs_permissions = [{ access = "read-write", path = "./data"}]
optimizer = true
via_ir = false
via_ir = true
ffi = true

[profile.default.optimizer_details]
Expand All @@ -22,8 +22,12 @@ constant_optimizer = true
yul = true

[invariant]
fail_on_revert = true

runs = 4
depth = 400

[fuzz]
seed = "0x0ca1b799da18587180cdb11fc96564bf73af56a3f4e6981971452fc78cb0dcbc"

[rpc_endpoints]
mainnet = "${MAINNET_RPC_URL}"
arbitrum = "${ARBITRUM_RPC_URL}"
Expand Down
2 changes: 1 addition & 1 deletion lib/pt-v5-twab-controller
513 changes: 242 additions & 271 deletions src/PrizePool.sol

Large diffs are not rendered by default.

59 changes: 11 additions & 48 deletions src/abstract/TieredLiquidityDistributor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ contract TieredLiquidityDistributor {

uint8 start = _computeReclamationStart(numTiers, _nextNumberOfTiers);
uint8 end = _nextNumberOfTiers;
for (uint8 i = start; i < end; i++) {
for (uint8 i = start; i != end; i++) {
_tiers[i] = Tier({
drawId: _awardingDraw,
prizeTokenPerShare: _prizeTokenPerShare,
Expand Down Expand Up @@ -240,7 +240,7 @@ contract TieredLiquidityDistributor {
// need to redistribute to the canary tier and any new tiers (if expanding)
uint8 start = _computeReclamationStart(_numberOfTiers, _nextNumberOfTiers);
uint8 end = _numberOfTiers;
for (uint8 i = start; i < end; i++) {
for (uint8 i = start; i != end; i++) {
reclaimedLiquidity = reclaimedLiquidity.add(
_getTierRemainingLiquidity(
fromUD34x4toUD60x18(_tiers[i].prizeTokenPerShare),
Expand Down Expand Up @@ -280,11 +280,8 @@ contract TieredLiquidityDistributor {
/// @notice Returns the estimated number of prizes for the given tier.
/// @param _tier The tier to retrieve
/// @return The estimated number of prizes
function getTierPrizeCount(uint8 _tier) external view returns (uint32) {
return
!TierCalculationLib.isValidTier(_tier, numberOfTiers)
? 0
: uint32(TierCalculationLib.prizeCount(_tier));
function getTierPrizeCount(uint8 _tier) external pure returns (uint32) {
return uint32(TierCalculationLib.prizeCount(_tier));
}

/// @notice Retrieves an up-to-date Tier struct for the given tier.
Expand Down Expand Up @@ -385,7 +382,7 @@ contract TieredLiquidityDistributor {
tierShares
);
bool canExpand = _numberOfTiers < MAXIMUM_NUMBER_OF_TIERS;
if (canExpand && _isCanaryTier(_tier, _numberOfTiers)) {
if (canExpand && _tier == _numberOfTiers - 1) {
// make canary prizes smaller to account for reduction in shares for next number of tiers
prizeSize =
(prizeSize * _getTotalShares(_numberOfTiers)) /
Expand Down Expand Up @@ -416,37 +413,18 @@ contract TieredLiquidityDistributor {
);
}

/// @notice Determines if the given tier is the canary tier.
/// @param _tier The tier to check
/// @param _numberOfTiers The current number of tiers
/// @return True if the tier is the canary tier
function _isCanaryTier(uint8 _tier, uint8 _numberOfTiers) internal pure returns (bool) {
return _tier == _numberOfTiers - 1;
}

/// @notice Computes the remaining liquidity available to a tier.
/// @param _tier The tier to compute the liquidity for
/// @return The remaining liquidity
function getTierRemainingLiquidity(uint8 _tier) public view returns (uint256) {
return _getTierRemainingLiquidity(_tier, fromUD34x4toUD60x18(prizeTokenPerShare));
}

/// @notice Computes the remaining liquidity available to a tier.
/// @param _tier The tier to compute the liquidity for
/// @param _prizeTokenPerShare The global prizeTokenPerShare
/// @return The remaining liquidity
function _getTierRemainingLiquidity(
uint8 _tier,
UD60x18 _prizeTokenPerShare
) internal view returns (uint256) {
uint8 _numTiers = numberOfTiers;
return
!TierCalculationLib.isValidTier(_tier, _numTiers)
? 0
: convert(
_getTierRemainingLiquidity(
fromUD34x4toUD60x18(_getTier(_tier, _numTiers).prizeTokenPerShare),
_prizeTokenPerShare
fromUD34x4toUD60x18(prizeTokenPerShare)
)
);
}
Expand All @@ -468,14 +446,7 @@ contract TieredLiquidityDistributor {
/// @notice Estimates the number of prizes for the current number of tiers, including the canary tier
/// @return The estimated number of prizes including the canary tier
function estimatedPrizeCount() external view returns (uint32) {
return _estimatePrizeCountPerDrawUsingNumberOfTiers(numberOfTiers);
}

/// @notice Estimates the number of prizes that will be awarded given a number of tiers. Includes canary tier
/// @param numTiers The number of tiers
/// @return The estimated prize count for the given number of tiers
function estimatedPrizeCount(uint8 numTiers) external view returns (uint32) {
return _estimatePrizeCountPerDrawUsingNumberOfTiers(numTiers);
return estimatedPrizeCount(numberOfTiers);
}

/// @notice Returns the balance of the reserve.
Expand All @@ -487,9 +458,9 @@ contract TieredLiquidityDistributor {
/// @notice Estimates the prize count for the given tier.
/// @param numTiers The number of prize tiers
/// @return The estimated total number of prizes
function _estimatePrizeCountPerDrawUsingNumberOfTiers(
function estimatedPrizeCount(
uint8 numTiers
) internal view returns (uint32) {
) public view returns (uint32) {
if (numTiers == 3) {
return ESTIMATED_PRIZES_PER_DRAW_FOR_3_TIERS;
} else if (numTiers == 4) {
Expand Down Expand Up @@ -540,14 +511,6 @@ contract TieredLiquidityDistributor {
return 10;
}

/// @notice Computes the odds for a tier given the number of tiers.
/// @param _tier The tier to compute odds for
/// @param _numTiers The number of prize tiers
/// @return The odds of the tier
function getTierOdds(uint8 _tier, uint8 _numTiers) external view returns (SD59x18) {
return _tierOdds(_tier, _numTiers);
}

/// @notice Computes the expected number of prizes for a given number of tiers.
/// @dev Includes the canary tier
/// @param _numTiers The number of tiers
Expand All @@ -556,7 +519,7 @@ contract TieredLiquidityDistributor {
uint32 prizeCount;
uint8 i = 0;
do {
prizeCount += TierCalculationLib.tierPrizeCountPerDraw(i, _tierOdds(i, _numTiers));
prizeCount += TierCalculationLib.tierPrizeCountPerDraw(i, getTierOdds(i, _numTiers));
i++;
} while (i < _numTiers);
return prizeCount;
Expand All @@ -566,7 +529,7 @@ contract TieredLiquidityDistributor {
/// @param _tier The tier to compute odds for
/// @param _numTiers The number of prize tiers
/// @return The odds of the tier
function _tierOdds(uint8 _tier, uint8 _numTiers) internal view returns (SD59x18) {
function getTierOdds(uint8 _tier, uint8 _numTiers) public view returns (SD59x18) {
if (_tier == 0) return TIER_ODDS_0;
if (_numTiers == 3) {
if (_tier <= 2) return TIER_ODDS_EVERY_DRAW;
Expand Down
7 changes: 5 additions & 2 deletions src/libraries/DrawAccumulatorLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ library DrawAccumulatorLib {

/// @notice An accumulator for a draw.
struct Accumulator {
RingBufferInfo ringBufferInfo;
uint24[366] drawRingBuffer;
RingBufferInfo ringBufferInfo; // 32 bits
uint24[366] drawRingBuffer; // 8784 bits
// 8784 + 32 = 8816 bits in total
// 256 * 35 = 8960
// 8960 - 8816 = 144 bits left over
mapping(uint256 drawId => Observation observation) observations;
}

Expand Down
Loading

0 comments on commit 25f5b6e

Please sign in to comment.