Skip to content

Commit

Permalink
Reclaimed both daily and canary tier upon contraction
Browse files Browse the repository at this point in the history
  • Loading branch information
asselstine committed Jan 30, 2024
1 parent 1b4b5cd commit 4896745
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 32 deletions.
12 changes: 6 additions & 6 deletions src/abstract/TieredLiquidityDistributor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,13 @@ contract TieredLiquidityDistributor {
// 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 we are expanding, need to reset the daily tier, canary tier and all of the new tiers
if (_nextNumberOfTiers > numTiers) {
start = numTiers - 1;
start = numTiers - 2;
end = _nextNumberOfTiers;
} else {
// just reset the canary tier
start = _nextNumberOfTiers - 1;
// just reset the daily and canary tiers
start = _nextNumberOfTiers - 2;
end = _nextNumberOfTiers;
}
for (uint8 i = start; i < end; i++) {
Expand Down Expand Up @@ -252,11 +252,11 @@ contract TieredLiquidityDistributor {
uint8 end;
// if we are shrinking, we need to reclaim including the new canary tier
if (_nextNumberOfTiers < _numberOfTiers) {
start = _nextNumberOfTiers - 1;
start = _nextNumberOfTiers - 2;
end = _numberOfTiers;
} else {
// just reset the canary tier
start = _numberOfTiers - 1;
start = _numberOfTiers - 2;
end = _numberOfTiers;
}
for (uint8 i = start; i < end; i++) {
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
48 changes: 37 additions & 11 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 Down Expand Up @@ -159,11 +176,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, 210e18); // total will be 200e18 + 210e18 = 410e18

assertEq(distributor.getTierRemainingLiquidity(0), 200e18, "grand prize liquidity");
assertEq(distributor.getTierRemainingLiquidity(1), 100e18, "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 4896745

Please sign in to comment.