Skip to content

Commit

Permalink
Merge pull request #84 from GenerationSoftware/gen-1079-daily-prize-t…
Browse files Browse the repository at this point in the history
…ier-needs-to-be-reclaimed-upon-contraction

Reclaimed both daily and canary tier upon contraction
  • Loading branch information
asselstine authored Feb 12, 2024
2 parents 448ef89 + 090828d commit 03cbc18
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 52 deletions.
33 changes: 10 additions & 23 deletions src/abstract/TieredLiquidityDistributor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -200,18 +200,8 @@ contract TieredLiquidityDistributor {
_prizeTokenLiquidity
);

// need to redistribute to the canary tier and any new tiers (if expanding)
uint8 start;
uint8 end;
// if we are expanding, need to reset the canary tier and all of the new tiers
if (_nextNumberOfTiers > numTiers) {
start = numTiers - 1;
end = _nextNumberOfTiers;
} else {
// just reset the canary tier
start = _nextNumberOfTiers - 1;
end = _nextNumberOfTiers;
}
uint8 start = _computeReclamationStart(numTiers, _nextNumberOfTiers);
uint8 end = _nextNumberOfTiers;
for (uint8 i = start; i < end; i++) {
_tiers[i] = Tier({
drawId: _awardingDraw,
Expand Down Expand Up @@ -248,17 +238,8 @@ contract TieredLiquidityDistributor {
UD60x18 reclaimedLiquidity;
{
// need to redistribute to the canary tier and any new tiers (if expanding)
uint8 start;
uint8 end;
// if we are shrinking, we need to reclaim including the new canary tier
if (_nextNumberOfTiers < _numberOfTiers) {
start = _nextNumberOfTiers - 1;
end = _numberOfTiers;
} else {
// just reset the canary tier
start = _numberOfTiers - 1;
end = _numberOfTiers;
}
uint8 start = _computeReclamationStart(_numberOfTiers, _nextNumberOfTiers);
uint8 end = _numberOfTiers;
for (uint8 i = start; i < end; i++) {
reclaimedLiquidity = reclaimedLiquidity.add(
_getTierRemainingLiquidity(
Expand Down Expand Up @@ -338,6 +319,12 @@ contract TieredLiquidityDistributor {
return uint256(_numberOfTiers) * uint256(tierShares) + uint256(reserveShares);
}

function _computeReclamationStart(uint8 _numberOfTiers, uint8 _nextNumberOfTiers) internal pure returns (uint8) {
// if we are expanding, need to reset the canary tier and all of the new tiers
// else reset the daily and canary tiers
return _nextNumberOfTiers > _numberOfTiers ? _numberOfTiers - 1 : _nextNumberOfTiers - 2;
}

/// @notice Consumes liquidity from the given tier.
/// @param _tierStruct The tier to consume liquidity from
/// @param _tier The tier number
Expand Down
20 changes: 5 additions & 15 deletions test/PrizePool.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@ contract PrizePoolTest is Test {

// new liq + reclaimed canary
uint256 draw2Liquidity = 8999999999999998700;
uint256 reclaimedLiquidity = (TIER_SHARES * firstPrizesPerShare);
// reclaim from two tiers
uint256 reclaimedLiquidity = (2 * TIER_SHARES * firstPrizesPerShare);
uint256 newLiquidity = draw2Liquidity + reclaimedLiquidity;
uint256 newPrizesPerShare = newLiquidity / prizePool.getTotalShares();
uint256 remainder = newLiquidity - newPrizesPerShare * prizePool.getTotalShares();
Expand Down Expand Up @@ -643,27 +644,16 @@ contract PrizePoolTest is Test {
4567,
startingTiers,
4, // change is limited to 1 tier
2607317073170731770 /*reserve from output*/,
UD34x4.wrap(2607317073170731540000) /*prize tokens per share from output*/,
2851219512195122170 /*reserve from output*/,
UD34x4.wrap(2851219512195121780000) /*prize tokens per share from output*/,
firstDrawOpensAt + drawPeriodSeconds
);

// award second draw
// reclaimed tiers: 10e18 + 10e18 = 20e18
// liquidity for second draw = 0.1 * (510e18 - 51e18) + 20e18 = 65.9e18
// reserve for second draw = 65.9e18 * (10 / 410) = 1607317073170731800
// total reserve = 1e18 + 1607317073170731800 = 2607317073170732000
awardDraw(4567);

assertEq(prizePool.numberOfTiers(), 4, "number of tiers");

// two tiers + canary tier = 30e18
// total liquidity for second draw is 45.9e18
// new liquidity for second draw = 75.9e18
// reserve for second draw = (10/310)*75.9e18 = 2.445e18
// total reserve = 3.445e18

assertEq(prizePool.reserve(), 2607317073170731770, "size of reserve");
assertEq(prizePool.reserve(), 2851219512195122170, "size of reserve");
}

function testAwardDraw_expandingTiers() public {
Expand Down
63 changes: 49 additions & 14 deletions test/abstract/TieredLiquidityDistributor.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,32 @@ contract TieredLiquidityDistributorTest is Test {
}

function testAwardDraw_liquidity_shrinkTiers1() public {
distributor.awardDraw(5, 100e18);
distributor.awardDraw(4, 100e18);
assertEq(_computeLiquidity(), 200e18);
distributor.awardDraw(5, 510e18); // will reclaim 3 tiers
distributor.awardDraw(4, 110e18);

assertEq(distributor.remainingTierLiquidity(0), 200e18);
assertEq(distributor.remainingTierLiquidity(1), 200e18);
assertEq(distributor.remainingTierLiquidity(2), 100e18);
assertEq(distributor.remainingTierLiquidity(3), 100e18);
assertEq(distributor.remainingTierLiquidity(4), 0);
assertEq(distributor.reserve(), 20e18);

assertEq(_computeLiquidity(), 620e18);
}

function testAwardDraw_liquidity_shrinkTiers2() public {
distributor.awardDraw(5, 100e18);
distributor.awardDraw(3, 100e18);
assertEq(_computeLiquidity(), 200e18);
distributor.awardDraw(7, 710e18); // will reclaim 4 tiers: (3,4,5,6)
distributor.awardDraw(5, 110e18);

assertEq(distributor.remainingTierLiquidity(0), 200e18);
assertEq(distributor.remainingTierLiquidity(1), 200e18);
assertEq(distributor.remainingTierLiquidity(2), 200e18);
assertEq(distributor.remainingTierLiquidity(3), 100e18);
assertEq(distributor.remainingTierLiquidity(4), 100e18);
assertEq(distributor.remainingTierLiquidity(5), 0);
assertEq(distributor.reserve(), 20e18);

assertEq(_computeLiquidity(), 820e18);
}

function testAwardDraw_liquidity_sameTiers() public {
Expand All @@ -59,9 +76,18 @@ contract TieredLiquidityDistributorTest is Test {
}

function testAwardDraw_liquidity_growTiers2() public {
distributor.awardDraw(5, 100e18);
distributor.awardDraw(7, 100e18);
assertEq(_computeLiquidity(), 200e18);
distributor.awardDraw(5, 510e18); // will reclaim canary + 100e18
distributor.awardDraw(7, 610e18);

assertEq(distributor.remainingTierLiquidity(0), 200e18, "old tier continues to accrue");
assertEq(distributor.remainingTierLiquidity(1), 200e18, "old tier continues to accrue");
assertEq(distributor.remainingTierLiquidity(2), 200e18, "old tier continues to accrue");
assertEq(distributor.remainingTierLiquidity(3), 200e18, "old tier continues to accrue");
assertEq(distributor.remainingTierLiquidity(4), 100e18, "old canary gets reclaimed");
assertEq(distributor.remainingTierLiquidity(5), 100e18, "new tier who dis");
assertEq(distributor.remainingTierLiquidity(6), 100e18, "new tier who dis");

assertEq(_computeLiquidity(), 1120e18);
}

function testConstructor_numberOfTiersTooLarge() public {
Expand Down Expand Up @@ -159,11 +185,20 @@ contract TieredLiquidityDistributorTest is Test {

function testExpansionTierLiquidity() public {
distributor.awardDraw(3, 310e18); // canary gets 100e18
assertEq(distributor.getTierRemainingLiquidity(2), 100e18, "canary initial liquidity");
distributor.awardDraw(5, 410e18); // should be 510 distributed

assertEq(distributor.getTierRemainingLiquidity(3), 100e18, "new tier liquidity");
assertEq(distributor.getTierRemainingLiquidity(4), 100e18, "canary liquidity");
assertEq(distributor.getTierRemainingLiquidity(0), 100e18, "grand prize liquidity");
assertEq(distributor.getTierRemainingLiquidity(1), 100e18, "tier 1 liquidity");
assertEq(distributor.getTierRemainingLiquidity(2), 100e18, "canary liquidity");
assertEq(distributor.getTierRemainingLiquidity(3), 0, "last tier");
assertEq(distributor.reserve(), 10e18, "reserve");

// 200e18 will be reclaimed from tier 1 and canary
distributor.awardDraw(4, 310e18); // total will be 200e18 + 210e18 = 410e18

assertEq(distributor.getTierRemainingLiquidity(0), 200e18, "grand prize liquidity");
assertEq(distributor.getTierRemainingLiquidity(1), 200e18, "tier 1 liquidity");
assertEq(distributor.getTierRemainingLiquidity(2), 100e18, "tier 2 liquidity");
assertEq(distributor.getTierRemainingLiquidity(3), 100e18, "tier 3 liquidity");
assertEq(distributor.getTierRemainingLiquidity(4), 0, "last tier out");
}

function testExpansionTierLiquidity_max() public {
Expand Down

0 comments on commit 03cbc18

Please sign in to comment.