From a9cda8f104026708e819f7e5e4f37a4ec1ba6217 Mon Sep 17 00:00:00 2001 From: Brendan Asselstine Date: Mon, 12 Feb 2024 20:03:59 -0800 Subject: [PATCH] Fixed tests --- src/abstract/TieredLiquidityDistributor.sol | 2 - src/libraries/DrawAccumulatorLib.sol | 77 ++++++--------------- test/PrizePool.t.sol | 30 +++----- 3 files changed, 33 insertions(+), 76 deletions(-) diff --git a/src/abstract/TieredLiquidityDistributor.sol b/src/abstract/TieredLiquidityDistributor.sol index 0b7169a..fc53ce0 100644 --- a/src/abstract/TieredLiquidityDistributor.sol +++ b/src/abstract/TieredLiquidityDistributor.sol @@ -2,8 +2,6 @@ pragma solidity ^0.8.19; -import "forge-std/console2.sol"; - import { SafeCast } from "openzeppelin/utils/math/SafeCast.sol"; import { SD59x18, sd } from "prb-math/SD59x18.sol"; import { UD60x18, ud, convert } from "prb-math/UD60x18.sol"; diff --git a/src/libraries/DrawAccumulatorLib.sol b/src/libraries/DrawAccumulatorLib.sol index 843fcda..4f8ef4c 100644 --- a/src/libraries/DrawAccumulatorLib.sol +++ b/src/libraries/DrawAccumulatorLib.sol @@ -2,8 +2,6 @@ pragma solidity ^0.8.19; -import "forge-std/console2.sol"; - import { SafeCast } from "openzeppelin/utils/math/SafeCast.sol"; import { RingBufferLib } from "ring-buffer-lib/RingBufferLib.sol"; @@ -144,17 +142,28 @@ library DrawAccumulatorLib { return 0; } - Pair48 memory indexes = computeIndices(ringBufferInfo); - Pair48 memory drawIds = readDrawIds(_accumulator, indexes); + uint16 oldestIndex = uint16( + RingBufferLib.oldestIndex( + ringBufferInfo.nextIndex, + ringBufferInfo.cardinality, + MAX_CARDINALITY + ) + ); + uint16 newestIndex = uint16( + RingBufferLib.newestIndex(ringBufferInfo.nextIndex, ringBufferInfo.cardinality) + ); + + uint24 oldestDrawId = _accumulator.drawRingBuffer[oldestIndex]; + uint24 newestDrawId = _accumulator.drawRingBuffer[newestIndex]; - if (_endDrawId < drawIds.first || _startDrawId > drawIds.second) { + if (_endDrawId < oldestDrawId || _startDrawId > newestDrawId) { // if out of range, return 0 return 0; } uint24 firstObservationDrawIdOccurringAtOrAfterStart; - if (_startDrawId <= drawIds.first || ringBufferInfo.cardinality == 1) { - firstObservationDrawIdOccurringAtOrAfterStart = drawIds.first; + if (_startDrawId <= oldestDrawId || ringBufferInfo.cardinality == 1) { + firstObservationDrawIdOccurringAtOrAfterStart = oldestDrawId; } else { // The start must be between newest and oldest uint24 beforeOrAtDrawId; @@ -166,8 +175,8 @@ library DrawAccumulatorLib { firstObservationDrawIdOccurringAtOrAfterStart ) = binarySearch( _accumulator.drawRingBuffer, - uint16(indexes.first), - uint16(indexes.second), + uint16(oldestIndex), + uint16(newestIndex), ringBufferInfo.cardinality, _startDrawId ); @@ -177,15 +186,15 @@ library DrawAccumulatorLib { } uint24 lastObservationDrawIdOccurringAtOrBeforeEnd; - if (_endDrawId >= drawIds.second || ringBufferInfo.cardinality == 1) { + if (_endDrawId >= newestDrawId || ringBufferInfo.cardinality == 1) { // then it must be the end - lastObservationDrawIdOccurringAtOrBeforeEnd = drawIds.second; + lastObservationDrawIdOccurringAtOrBeforeEnd = newestDrawId; } else { uint24 afterOrAtDrawId; (, lastObservationDrawIdOccurringAtOrBeforeEnd, ,afterOrAtDrawId) = binarySearch( _accumulator.drawRingBuffer, - uint16(indexes.first), - uint16(indexes.second), + uint16(oldestIndex), + uint16(newestIndex), ringBufferInfo.cardinality, _endDrawId ); @@ -197,56 +206,14 @@ library DrawAccumulatorLib { Observation memory atOrAfterStart = _accumulator.observations[ firstObservationDrawIdOccurringAtOrAfterStart ]; - // console2.log("atOrAfterStart drawId", firstObservationDrawIdOccurringAtOrAfterStart); - // console2.log("atOrAfterStart available", atOrAfterStart.available); - // console2.log("atOrAfterStart disbursed", atOrAfterStart.disbursed); Observation memory atOrBeforeEnd = _accumulator.observations[ lastObservationDrawIdOccurringAtOrBeforeEnd ]; - // console2.log("atOrBeforeEnd drawId", lastObservationDrawIdOccurringAtOrBeforeEnd); - // console2.log("atOrBeforeEnd available", atOrBeforeEnd.available); - // console2.log("atOrBeforeEnd disbursed", atOrBeforeEnd.disbursed); return atOrBeforeEnd.available + atOrBeforeEnd.disbursed - atOrAfterStart.disbursed; } - /// @notice Computes the first and last indices of observations for the given ring buffer info. - /// @param ringBufferInfo The ring buffer info to compute for - /// @return A pair of indices, where the first is the oldest index and the second is the newest index - function computeIndices( - RingBufferInfo memory ringBufferInfo - ) internal pure returns (Pair48 memory) { - return - Pair48({ - first: uint16( - RingBufferLib.oldestIndex( - ringBufferInfo.nextIndex, - ringBufferInfo.cardinality, - MAX_CARDINALITY - ) - ), - second: uint16( - RingBufferLib.newestIndex(ringBufferInfo.nextIndex, ringBufferInfo.cardinality) - ) - }); - } - - /// @notice Retrieves the draw ids for the given accumulator observation indices. - /// @param accumulator The accumulator to retrieve from - /// @param indices The indices to retrieve - /// @return A pair of draw ids, where the first is the draw id of the pair's first index and the second is the draw id of the pair's second index - function readDrawIds( - Accumulator storage accumulator, - Pair48 memory indices - ) internal view returns (Pair48 memory) { - return - Pair48({ - first: accumulator.drawRingBuffer[indices.first], - second: accumulator.drawRingBuffer[indices.second] - }); - } - /// @notice Binary searches an array of draw ids for the given target draw id. /// @dev The _targetDrawId MUST exist between the range of draws at _oldestIndex and _newestIndex (inclusive) /// @param _drawRingBuffer The array of draw ids to search diff --git a/test/PrizePool.t.sol b/test/PrizePool.t.sol index cdb40f5..96efc7a 100644 --- a/test/PrizePool.t.sol +++ b/test/PrizePool.t.sol @@ -865,24 +865,19 @@ contract PrizePoolTest is Test { assertEq(prizePool.estimateNextNumberOfTiers(), 4, "will reduce to 4"); - // first draw (same num tiers) - // total for first draw = 0.1 * 510e18 = 51e18 - // 5th tier (canary tier) = (100/510)*51e18 = 10e18 - // 4th tier (daily tier) = (100/510)*51.18 = 10e18 - // reserve = 51e18 * (10 / 510) = 1e18 awardDraw(1234); - assertEq(prizePool.reserve(), 1e18, "reserve after first draw"); + assertEq(prizePool.reserve(), 10e18, "reserve after first draw"); - assertEq(prizePool.numberOfTiers(), startingTiers); + assertEq(prizePool.numberOfTiers(), startingTiers, "number of tiers has not changed"); vm.expectEmit(); emit DrawAwarded( 2, 4567, startingTiers, 4, // change is limited to 1 tier - 2851219512195122170 /*reserve from output*/, - UD34x4.wrap(2851219512195121780000) /*prize tokens per share from output*/, + 17317073170731707600 /*reserve from output*/, + UD34x4.wrap(17317073170731707310000) /*prize tokens per share from output*/, firstDrawOpensAt + drawPeriodSeconds ); @@ -890,7 +885,7 @@ contract PrizePoolTest is Test { assertEq(prizePool.numberOfTiers(), 4, "number of tiers"); - assertEq(prizePool.reserve(), 2851219512195122170, "size of reserve"); + assertEq(prizePool.reserve(), 17317073170731707600, "size of reserve"); } function testAwardDraw_expandingTiers() public { @@ -932,6 +927,7 @@ contract PrizePoolTest is Test { contribute(1e18); awardDraw(1234); awardDraw(1234); + contribute(1e18); awardDraw(554); mockTwab(address(this), sender5, 1); @@ -1114,7 +1110,6 @@ contract PrizePoolTest is Test { } function testClaimPrize_zero() public { - contribute(1000); awardDraw(winningRandomNumber); address winner = makeAddr("winner"); mockTwab(address(this), winner, 1); @@ -1165,8 +1160,7 @@ contract PrizePoolTest is Test { address recipient = makeAddr("recipient"); mockTwab(address(this), winner, 1); - uint256 prize = 806451612903225800; - assertApproxEqAbs(prize, (10e18 * TIER_SHARES) / (4 * prizePool.getTotalShares()), 100); + uint256 prize = prizePool.getTierPrizeSize(1); vm.expectEmit(); emit ClaimedPrize(address(this), winner, recipient, 1, 1, 0, uint152(prize), 0, address(this)); @@ -1313,9 +1307,7 @@ contract PrizePoolTest is Test { mockTwab(address(this), winner, 1); uint96 fee = 0xfee; - uint256 prizeAmount = 806451612903225800; - uint256 prize = prizeAmount - fee; - assertApproxEqAbs(prizeAmount, (10e18 * TIER_SHARES) / (4 * prizePool.getTotalShares()), 100); + uint256 prize = prizePool.getTierPrizeSize(1); vm.expectEmit(); emit ClaimedPrize( @@ -1325,12 +1317,12 @@ contract PrizePoolTest is Test { 1, 1, 0, - uint152(prize), + uint152(prize-fee), fee, address(this) ); - assertEq(prizePool.claimPrize(winner, 1, 0, recipient, fee, address(this)), prizeAmount); - assertEq(prizeToken.balanceOf(recipient), prize, "recipient balance is good"); + prizePool.claimPrize(winner, 1, 0, recipient, fee, address(this)); + assertEq(prizeToken.balanceOf(recipient), prize - fee, "recipient balance is good"); assertEq(prizePool.claimCount(), 1); // Check if claim fees are accounted for