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

Use maps for precomputing #43

Closed
wants to merge 1 commit into from
Closed
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
20 changes: 1 addition & 19 deletions script/generateConstants.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ contract GenerateConstants is Script {
);
console.log("\n");

console.log("/// @notice The estiamted number of prizes given X tiers.");
console.log("/// @notice The estimated number of prizes given X tiers.");
uint8 MIN_TIERS = 2;
uint8 MAX_TIERS = 14;
// Precompute the prizes per draw
Expand All @@ -31,23 +31,5 @@ contract GenerateConstants is Script {
uint256(TierCalculationLib.estimatedClaimCount(numTiers, GRAND_PRIZE_PERIOD_DRAWS))
);
}

console.log("\n");
console.log("/// @notice The odds for each tier and number of tiers pair.");
MIN_TIERS = 3;
MAX_TIERS = 16;
// Precompute the odds for each tier
for (uint8 numTiers = MIN_TIERS; numTiers <= MAX_TIERS; numTiers++) {
for (uint8 tier = 0; tier < numTiers; tier++) {
console.log(
"SD59x18 internal constant TIER_ODDS_%d_%d = SD59x18.wrap(%d);",
uint256(tier),
uint256(numTiers),
uint256(
SD59x18.unwrap(TierCalculationLib.getTierOdds(tier, numTiers, GRAND_PRIZE_PERIOD_DRAWS))
)
);
}
}
}
}
11 changes: 7 additions & 4 deletions src/PrizePool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ error CallerNotDrawManager(address caller, address drawManager);
* @param prizeToken The token to use for prizes
* @param twabController The Twab Controller to retrieve time-weighted average balances from
* @param drawManager The address of the draw manager for the prize pool
* @param grandPrizePeriodDraws The average number of draws between grand prizes. This determines the statistical frequency of grand prizes.
* @param drawPeriodSeconds The number of seconds between draws. E.g. a Prize Pool with a daily draw should have a draw period of 86400 seconds.
* @param firstDrawStartsAt The timestamp at which the first draw will start.
* @param numberOfTiers The number of tiers to start with. Must be greater than or equal to the minimum number of tiers.
Expand All @@ -105,6 +106,7 @@ struct ConstructorParams {
IERC20 prizeToken;
TwabController twabController;
address drawManager;
uint16 grandPrizePeriodDraws;
uint32 drawPeriodSeconds;
uint64 firstDrawStartsAt;
uint8 numberOfTiers;
Expand Down Expand Up @@ -255,6 +257,7 @@ contract PrizePool is TieredLiquidityDistributor {
ConstructorParams memory params
)
TieredLiquidityDistributor(
params.grandPrizePeriodDraws,
params.numberOfTiers,
params.tierShares,
params.canaryShares,
Expand Down Expand Up @@ -542,7 +545,7 @@ contract PrizePool is TieredLiquidityDistributor {
/// @return The number of draws
function getTierAccrualDurationInDraws(uint8 _tier) external view returns (uint16) {
return
uint16(TierCalculationLib.estimatePrizeFrequencyInDraws(_tierOdds(_tier, numberOfTiers)));
uint16(TierCalculationLib.estimatePrizeFrequencyInDraws(_tierOdds[numberOfTiers][_tier]));
}

/// @notice The total amount of prize tokens that have been claimed for all time
Expand Down Expand Up @@ -670,7 +673,7 @@ contract PrizePool is TieredLiquidityDistributor {

startTimestamp = uint64(
endTimestamp -
TierCalculationLib.estimatePrizeFrequencyInDraws(_tierOdds(_tier, numberOfTiers)) *
TierCalculationLib.estimatePrizeFrequencyInDraws(_tierOdds[numberOfTiers][_tier]) *
drawPeriodSeconds
);
}
Expand Down Expand Up @@ -764,7 +767,7 @@ contract PrizePool is TieredLiquidityDistributor {
) &&
claimCount >=
fromUD60x18(
intoUD60x18(_claimExpansionThreshold).mul(toUD60x18(_estimatedPrizeCount(_numTiers)))
intoUD60x18(_claimExpansionThreshold).mul(toUD60x18(_estimatedPrizeCount[_numTiers]))
)
) {
// increase the number of tiers to include a new tier
Expand Down Expand Up @@ -866,7 +869,7 @@ contract PrizePool is TieredLiquidityDistributor {
revert InvalidTier(_tier, _numberOfTiers);
}

tierOdds = _tierOdds(_tier, numberOfTiers);
tierOdds = _tierOdds[numberOfTiers][_tier];
drawDuration = uint16(TierCalculationLib.estimatePrizeFrequencyInDraws(tierOdds));
vaultPortion = _getVaultPortion(
_vault,
Expand Down
392 changes: 40 additions & 352 deletions src/abstract/TieredLiquidityDistributor.sol

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions test/PrizePool.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ contract PrizePoolTest is Test {
prizeToken,
twabController,
drawManager,
365,
drawPeriodSeconds,
lastCompletedDrawStartedAt,
initialNumberOfTiers, // minimum number of tiers
Expand Down Expand Up @@ -459,6 +460,7 @@ contract PrizePoolTest is Test {
prizeToken,
twabController,
address(this),
365,
drawPeriodSeconds,
lastCompletedDrawStartedAt,
startingTiers, // higher number of tiers
Expand Down Expand Up @@ -999,9 +1001,6 @@ contract PrizePoolTest is Test {

function testEstimatedPrizeCount() public {
// assumes grand prize is 365
assertEq(prizePool.estimatedPrizeCount(0), 0);
assertEq(prizePool.estimatedPrizeCount(1), 0);
assertEq(prizePool.estimatedPrizeCount(2), 0);
assertEq(prizePool.estimatedPrizeCount(3), 4);
assertEq(prizePool.estimatedPrizeCount(4), 16);
assertEq(prizePool.estimatedPrizeCount(5), 66);
Expand All @@ -1015,7 +1014,6 @@ contract PrizePoolTest is Test {
assertEq(prizePool.estimatedPrizeCount(13), 4912619);
assertEq(prizePool.estimatedPrizeCount(14), 19805536);
assertEq(prizePool.estimatedPrizeCount(15), 79777187);
assertEq(prizePool.estimatedPrizeCount(16), 0);
}

function testcanaryPrizeCountFractional() public {
Expand Down
2 changes: 2 additions & 0 deletions test/abstract/TieredLiquidityDistributor.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ contract TieredLiquidityDistributorTest is Test {
uint8 reserveShares;

function setUp() external {
grandPrizePeriodDraws = 365;
numberOfTiers = 3;
tierShares = 100;
canaryShares = 10;
reserveShares = 10;

distributor = new TieredLiquidityDistributorWrapper(
grandPrizePeriodDraws,
numberOfTiers,
tierShares,
canaryShares,
Expand Down
11 changes: 10 additions & 1 deletion test/abstract/helper/TieredLiquidityDistributorWrapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,20 @@ import { TieredLiquidityDistributor, Tier, fromUD34x4toUD60x18, fromUD60x18 } fr

contract TieredLiquidityDistributorWrapper is TieredLiquidityDistributor {
constructor(
uint16 _grandPrizePeriodDraws,
uint8 _numberOfTiers,
uint8 _tierShares,
uint8 _canaryShares,
uint8 _reserveShares
) TieredLiquidityDistributor(_numberOfTiers, _tierShares, _canaryShares, _reserveShares) {}
)
TieredLiquidityDistributor(
_grandPrizePeriodDraws,
_numberOfTiers,
_tierShares,
_canaryShares,
_reserveShares
)
{}

function nextDraw(uint8 _nextNumTiers, uint96 liquidity) external {
_nextDraw(_nextNumTiers, liquidity);
Expand Down
2 changes: 2 additions & 0 deletions test/invariants/helpers/PrizePoolFuzzHarness.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ contract PrizePoolFuzzHarness is CommonBase {

constructor() {
address drawManager = address(this);
uint16 grandPrizePeriodDraws = 365;
uint32 drawPeriodSeconds = 1 hours;
uint64 nextDrawStartsAt = uint64(block.timestamp);
uint8 numberOfTiers = 3;
Expand All @@ -38,6 +39,7 @@ contract PrizePoolFuzzHarness is CommonBase {
token,
twabController,
drawManager,
grandPrizePeriodDraws,
drawPeriodSeconds,
nextDrawStartsAt,
numberOfTiers,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ contract TieredLiquidityDistributorFuzzHarness is TieredLiquidityDistributor {
uint256 public totalAdded;
uint256 public totalConsumed;

constructor() TieredLiquidityDistributor(3, 100, 10, 10) {}
constructor() TieredLiquidityDistributor(365, 3, 100, 10, 10) {}

function nextDraw(uint8 _nextNumTiers, uint96 liquidity) external {
uint8 nextNumTiers = _nextNumTiers / 16; // map to [0, 15]
Expand Down
Loading