Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
asselstine committed Feb 13, 2024
1 parent 1ac4db0 commit a9cda8f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 76 deletions.
2 changes: 0 additions & 2 deletions src/abstract/TieredLiquidityDistributor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
77 changes: 22 additions & 55 deletions src/libraries/DrawAccumulatorLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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;
Expand All @@ -166,8 +175,8 @@ library DrawAccumulatorLib {
firstObservationDrawIdOccurringAtOrAfterStart
) = binarySearch(
_accumulator.drawRingBuffer,
uint16(indexes.first),
uint16(indexes.second),
uint16(oldestIndex),
uint16(newestIndex),
ringBufferInfo.cardinality,
_startDrawId
);
Expand All @@ -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
);
Expand All @@ -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
Expand Down
30 changes: 11 additions & 19 deletions test/PrizePool.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -865,32 +865,27 @@ 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
);

awardDraw(4567);

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 {
Expand Down Expand Up @@ -932,6 +927,7 @@ contract PrizePoolTest is Test {
contribute(1e18);
awardDraw(1234);
awardDraw(1234);
contribute(1e18);
awardDraw(554);

mockTwab(address(this), sender5, 1);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand Down

0 comments on commit a9cda8f

Please sign in to comment.