Skip to content

Commit

Permalink
Merge pull request #111 from GenerationSoftware/gen-1337-investigate-…
Browse files Browse the repository at this point in the history
…shutdownbalanceof-logic

Fixed shutdown withdrawals
  • Loading branch information
asselstine committed Apr 17, 2024
2 parents 8d6f219 + 6f827b2 commit b3b672e
Show file tree
Hide file tree
Showing 9 changed files with 476 additions and 183 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ jobs:
env:
MAINNET_RPC_URL: ${{ secrets.MAINNET_RPC_URL }}
run: |
forge coverage --report lcov && lcov --remove lcov.info -o lcov.info 'test/*' 'script/*'
forge coverage --report lcov && lcov --remove lcov.info -o lcov.info 'test/*'
id: coverage

- name: Report code coverage
uses: zgosalvez/github-actions-report-lcov@v1.5.0
with:
coverage-files: lcov.info
minimum-coverage: 100
minimum-coverage: 99
github-token: ${{ secrets.GITHUB_TOKEN }}
2 changes: 1 addition & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
src = 'src'
out = 'out'
libs = ['lib']
solc = "0.8.19"
solc = "0.8.24"
gas_reports_ignore = ["ERC20Mintable", "TwabController"]
gas_limit = "18446744073709551615" # u64::MAX
block_gas_limit = "18446744073709551615" # u64::MAX
Expand Down
288 changes: 191 additions & 97 deletions src/PrizePool.sol

Large diffs are not rendered by default.

38 changes: 24 additions & 14 deletions src/libraries/DrawAccumulatorLib.sol
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.19;
pragma solidity ^0.8.24;

import { SafeCast } from "openzeppelin/utils/math/SafeCast.sol";
import { RingBufferLib } from "ring-buffer-lib/RingBufferLib.sol";

// The maximum number of observations that can be recorded.
uint16 constant MAX_OBSERVATION_CARDINALITY = 366;

/// @notice Thrown when adding balance for draw zero.
error AddToDrawZero();

Expand All @@ -26,19 +29,17 @@ struct Observation {
uint160 disbursed;
}

/// @notice The metadata for using the ring buffer.
struct RingBufferInfo {
uint16 nextIndex;
uint16 cardinality;
}

/// @title Draw Accumulator Lib
/// @author G9 Software Inc.
/// @notice This contract distributes tokens over time according to an exponential weighted average.
/// Time is divided into discrete "draws", of which each is allocated tokens.
library DrawAccumulatorLib {
/// @notice The maximum number of observations that can be recorded.
uint16 internal constant MAX_CARDINALITY = 366;

/// @notice The metadata for using the ring buffer.
struct RingBufferInfo {
uint16 nextIndex;
uint16 cardinality;
}

/// @notice An accumulator for a draw.
/// @param ringBufferInfo The metadata for the drawRingBuffer
Expand Down Expand Up @@ -70,7 +71,7 @@ library DrawAccumulatorLib {
RingBufferInfo memory ringBufferInfo = accumulator.ringBufferInfo;

uint24 newestDrawId_ = accumulator.drawRingBuffer[
RingBufferLib.newestIndex(ringBufferInfo.nextIndex, MAX_CARDINALITY)
RingBufferLib.newestIndex(ringBufferInfo.nextIndex, MAX_OBSERVATION_CARDINALITY)
];

if (_drawId < newestDrawId_) {
Expand All @@ -82,7 +83,7 @@ library DrawAccumulatorLib {
Observation memory newestObservation_ = accumulatorObservations[newestDrawId_];
if (_drawId != newestDrawId_) {
uint16 cardinality = ringBufferInfo.cardinality;
if (ringBufferInfo.cardinality < MAX_CARDINALITY) {
if (ringBufferInfo.cardinality < MAX_OBSERVATION_CARDINALITY) {
cardinality += 1;
} else {
// Delete the old observation to save gas (older than 1 year)
Expand All @@ -99,7 +100,7 @@ library DrawAccumulatorLib {
});

accumulator.ringBufferInfo = RingBufferInfo({
nextIndex: uint16(RingBufferLib.nextIndex(ringBufferInfo.nextIndex, MAX_CARDINALITY)),
nextIndex: uint16(RingBufferLib.nextIndex(ringBufferInfo.nextIndex, MAX_OBSERVATION_CARDINALITY)),
cardinality: cardinality
});

Expand All @@ -120,10 +121,19 @@ library DrawAccumulatorLib {
function newestDrawId(Accumulator storage accumulator) internal view returns (uint256) {
return
accumulator.drawRingBuffer[
RingBufferLib.newestIndex(accumulator.ringBufferInfo.nextIndex, MAX_CARDINALITY)
RingBufferLib.newestIndex(accumulator.ringBufferInfo.nextIndex, MAX_OBSERVATION_CARDINALITY)
];
}

/// @notice Returns the newest draw id from the accumulator.
/// @param accumulator The accumulator to get the newest draw id from
/// @return The newest draw id
function newestObservation(Accumulator storage accumulator) internal view returns (Observation memory) {
return accumulator.observations[
newestDrawId(accumulator)
];
}

/// @notice Gets the balance that was disbursed between the given start and end draw ids, inclusive.
/// @param _accumulator The accumulator to get the disbursed balance from
/// @param _startDrawId The start draw id, inclusive
Expand All @@ -148,7 +158,7 @@ library DrawAccumulatorLib {
RingBufferLib.oldestIndex(
ringBufferInfo.nextIndex,
ringBufferInfo.cardinality,
MAX_CARDINALITY
MAX_OBSERVATION_CARDINALITY
)
);
uint16 newestIndex = uint16(
Expand Down

0 comments on commit b3b672e

Please sign in to comment.