Skip to content

Commit

Permalink
removed DrawLin (#195)
Browse files Browse the repository at this point in the history
Co-authored-by: Aodhgan <aodhgan@pooltogether.com>
  • Loading branch information
aodhgan and Aodhgan committed Oct 6, 2021
1 parent 582a504 commit cf4bb35
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 121 deletions.
4 changes: 2 additions & 2 deletions contracts/DrawBeacon.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import "@pooltogether/owner-manager-contracts/contracts/Ownable.sol";

import "./interfaces/IDrawBeacon.sol";
import "./interfaces/IDrawHistory.sol";
import "./libraries/DrawLib.sol";


/**
* @title PoolTogether V4 DrawBeacon
Expand Down Expand Up @@ -223,7 +223,7 @@ contract DrawBeacon is IDrawBeacon, Ownable {
uint64 _time = _currentTime();

// create Draw struct
DrawLib.Draw memory _draw = DrawLib.Draw({
IDrawBeacon.Draw memory _draw = IDrawBeacon.Draw({
winningRandomNumber: randomNumber,
drawId: _nextDrawId,
timestamp: rngRequest.requestedAt, // must use the startAward() timestamp to prevent front-running
Expand Down
36 changes: 18 additions & 18 deletions contracts/DrawCalculator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import "./interfaces/IDrawCalculator.sol";
import "./interfaces/ITicket.sol";
import "./interfaces/IDrawHistory.sol";
import "./interfaces/IPrizeDistributionHistory.sol";
import "./libraries/DrawLib.sol";
import "./interfaces/IDrawBeacon.sol";
import "./libraries/DrawRingBufferLib.sol";

/**
Expand Down Expand Up @@ -71,11 +71,11 @@ contract DrawCalculator is IDrawCalculator, Ownable {
uint64[][] memory pickIndices = abi.decode(_pickIndicesForDraws, (uint64 [][]));
require(pickIndices.length == _drawIds.length, "DrawCalc/invalid-pick-indices-length");

// READ list of DrawLib.Draw using the drawIds from drawHistory
DrawLib.Draw[] memory draws = drawHistory.getDraws(_drawIds);
// READ list of IDrawBeacon.Draw using the drawIds from drawHistory
IDrawBeacon.Draw[] memory draws = drawHistory.getDraws(_drawIds);

// READ list of DrawLib.PrizeDistribution using the drawIds
DrawLib.PrizeDistribution[] memory _prizeDistributions = prizeDistributionHistory
// READ list of IPrizeDistributionHistory.PrizeDistribution using the drawIds
IPrizeDistributionHistory.PrizeDistribution[] memory _prizeDistributions = prizeDistributionHistory
.getPrizeDistributions(_drawIds);

// The userBalances are fractions representing their portion of the liquidity for a draw.
Expand Down Expand Up @@ -116,8 +116,8 @@ contract DrawCalculator is IDrawCalculator, Ownable {
override
returns (uint256[] memory)
{
DrawLib.Draw[] memory _draws = drawHistory.getDraws(_drawIds);
DrawLib.PrizeDistribution[] memory _prizeDistributions = prizeDistributionHistory
IDrawBeacon.Draw[] memory _draws = drawHistory.getDraws(_drawIds);
IPrizeDistributionHistory.PrizeDistribution[] memory _prizeDistributions = prizeDistributionHistory
.getPrizeDistributions(_drawIds);

return _getNormalizedBalancesAt(_user, _draws, _prizeDistributions);
Expand All @@ -132,8 +132,8 @@ contract DrawCalculator is IDrawCalculator, Ownable {
uint32[] memory drawIds = new uint32[](1);
drawIds[0] = _drawId;

DrawLib.Draw[] memory _draws = drawHistory.getDraws(drawIds);
DrawLib.PrizeDistribution[] memory _prizeDistributions = prizeDistributionHistory
IDrawBeacon.Draw[] memory _draws = drawHistory.getDraws(drawIds);
IPrizeDistributionHistory.PrizeDistribution[] memory _prizeDistributions = prizeDistributionHistory
.getPrizeDistributions(drawIds);

uint256[] memory userBalances = _getNormalizedBalancesAt(
Expand Down Expand Up @@ -188,9 +188,9 @@ contract DrawCalculator is IDrawCalculator, Ownable {
function _calculatePrizesAwardable(
uint256[] memory _normalizedUserBalances,
bytes32 _userRandomNumber,
DrawLib.Draw[] memory _draws,
IDrawBeacon.Draw[] memory _draws,
uint64[][] memory _pickIndicesForDraws,
DrawLib.PrizeDistribution[] memory _prizeDistributions
IPrizeDistributionHistory.PrizeDistribution[] memory _prizeDistributions
) internal pure returns (uint256[] memory) {
uint256[] memory prizesAwardable = new uint256[](_normalizedUserBalances.length);

Expand Down Expand Up @@ -221,7 +221,7 @@ contract DrawCalculator is IDrawCalculator, Ownable {
* @return The number of picks a user gets for a Draw
*/
function _calculateNumberOfUserPicks(
DrawLib.PrizeDistribution memory _prizeDistribution,
IPrizeDistributionHistory.PrizeDistribution memory _prizeDistribution,
uint256 _normalizedUserBalance
) internal pure returns (uint64) {
return uint64((_normalizedUserBalance * _prizeDistribution.numberOfPicks) / 1 ether);
Expand All @@ -236,8 +236,8 @@ contract DrawCalculator is IDrawCalculator, Ownable {
*/
function _getNormalizedBalancesAt(
address _user,
DrawLib.Draw[] memory _draws,
DrawLib.PrizeDistribution[] memory _prizeDistributions
IDrawBeacon.Draw[] memory _draws,
IPrizeDistributionHistory.PrizeDistribution[] memory _prizeDistributions
) internal view returns (uint256[] memory) {
uint32[] memory _timestampsWithStartCutoffTimes = new uint32[](_draws.length);
uint32[] memory _timestampsWithEndCutoffTimes = new uint32[](_draws.length);
Expand Down Expand Up @@ -290,7 +290,7 @@ contract DrawCalculator is IDrawCalculator, Ownable {
uint256 _totalUserPicks,
bytes32 _userRandomNumber,
uint64[] memory _picks,
DrawLib.PrizeDistribution memory _prizeDistribution
IPrizeDistributionHistory.PrizeDistribution memory _prizeDistribution
) internal pure returns (uint256) {
// prizeCounts stores the number of wins at a distribution index
uint256[] memory prizeCounts = new uint256[](_prizeDistribution.tiers.length);
Expand Down Expand Up @@ -392,7 +392,7 @@ contract DrawCalculator is IDrawCalculator, Ownable {
* @param _prizeDistribution The PrizeDistribution to use to calculate the masks
* @return An array of bitmasks
*/
function _createBitMasks(DrawLib.PrizeDistribution memory _prizeDistribution)
function _createBitMasks(IPrizeDistributionHistory.PrizeDistribution memory _prizeDistribution)
internal
pure
returns (uint256[] memory)
Expand All @@ -417,7 +417,7 @@ contract DrawCalculator is IDrawCalculator, Ownable {
* @return returns the fraction of the total prize (base 1e18)
*/
function _calculatePrizeTierFraction(
DrawLib.PrizeDistribution memory _prizeDistribution,
IPrizeDistributionHistory.PrizeDistribution memory _prizeDistribution,
uint256 _prizeTierIndex
) internal pure returns (uint256) {
// get the prize fraction at that index
Expand All @@ -439,7 +439,7 @@ contract DrawCalculator is IDrawCalculator, Ownable {
* @return returns an array of prize tiers fractions
*/
function _calculatePrizeTierFractions(
DrawLib.PrizeDistribution memory _prizeDistribution,
IPrizeDistributionHistory.PrizeDistribution memory _prizeDistribution,
uint8 maxWinningTierIndex
) internal pure returns (uint256[] memory) {
uint256[] memory prizeDistributionFractions = new uint256[](
Expand Down
29 changes: 15 additions & 14 deletions contracts/DrawHistory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ pragma solidity 0.8.6;
import "@pooltogether/owner-manager-contracts/contracts/Manageable.sol";

import "./interfaces/IDrawHistory.sol";
import "./libraries/DrawLib.sol";
import "./interfaces/IDrawBeacon.sol";
import "./interfaces/IDrawBeacon.sol";
import "./libraries/DrawRingBufferLib.sol";

/**
Expand All @@ -26,7 +27,7 @@ contract DrawHistory is IDrawHistory, Manageable {
uint16 public constant MAX_CARDINALITY = 256;

/// @notice Draws ring buffer array.
DrawLib.Draw[MAX_CARDINALITY] private _draws;
IDrawBeacon.Draw[MAX_CARDINALITY] private _draws;

/// @notice Holds ring buffer information
DrawRingBufferLib.Buffer internal drawRingBuffer;
Expand All @@ -45,7 +46,7 @@ contract DrawHistory is IDrawHistory, Manageable {
/* ============ External Functions ============ */

/// @inheritdoc IDrawHistory
function getDraw(uint32 drawId) external view override returns (DrawLib.Draw memory) {
function getDraw(uint32 drawId) external view override returns (IDrawBeacon.Draw memory) {
return _draws[_drawIdToDrawIndex(drawRingBuffer, drawId)];
}

Expand All @@ -54,9 +55,9 @@ contract DrawHistory is IDrawHistory, Manageable {
external
view
override
returns (DrawLib.Draw[] memory)
returns (IDrawBeacon.Draw[] memory)
{
DrawLib.Draw[] memory draws = new DrawLib.Draw[](drawIds.length);
IDrawBeacon.Draw[] memory draws = new IDrawBeacon.Draw[](drawIds.length);
DrawRingBufferLib.Buffer memory buffer = drawRingBuffer;

for (uint256 index = 0; index < drawIds.length; index++) {
Expand Down Expand Up @@ -84,15 +85,15 @@ contract DrawHistory is IDrawHistory, Manageable {
}

/// @inheritdoc IDrawHistory
function getNewestDraw() external view override returns (DrawLib.Draw memory) {
function getNewestDraw() external view override returns (IDrawBeacon.Draw memory) {
return _getNewestDraw(drawRingBuffer);
}

/// @inheritdoc IDrawHistory
function getOldestDraw() external view override returns (DrawLib.Draw memory) {
function getOldestDraw() external view override returns (IDrawBeacon.Draw memory) {
// oldest draw should be next available index, otherwise it's at 0
DrawRingBufferLib.Buffer memory buffer = drawRingBuffer;
DrawLib.Draw memory draw = _draws[buffer.nextIndex];
IDrawBeacon.Draw memory draw = _draws[buffer.nextIndex];

if (draw.timestamp == 0) {
// if draw is not init, then use draw at 0
Expand All @@ -103,7 +104,7 @@ contract DrawHistory is IDrawHistory, Manageable {
}

/// @inheritdoc IDrawHistory
function pushDraw(DrawLib.Draw memory _draw)
function pushDraw(IDrawBeacon.Draw memory _draw)
external
override
onlyManagerOrOwner
Expand All @@ -113,7 +114,7 @@ contract DrawHistory is IDrawHistory, Manageable {
}

/// @inheritdoc IDrawHistory
function setDraw(DrawLib.Draw memory _newDraw) external override onlyOwner returns (uint32) {
function setDraw(IDrawBeacon.Draw memory _newDraw) external override onlyOwner returns (uint32) {
DrawRingBufferLib.Buffer memory buffer = drawRingBuffer;
uint32 index = buffer.getIndex(_newDraw.drawId);
_draws[index] = _newDraw;
Expand Down Expand Up @@ -141,23 +142,23 @@ contract DrawHistory is IDrawHistory, Manageable {
* @notice Read newest Draw from the draws ring buffer.
* @dev Uses the lastDrawId to calculate the most recently added Draw.
* @param _buffer Draw ring buffer
* @return DrawLib.Draw
* @return IDrawBeacon.Draw
*/
function _getNewestDraw(DrawRingBufferLib.Buffer memory _buffer)
internal
view
returns (DrawLib.Draw memory)
returns (IDrawBeacon.Draw memory)
{
return _draws[_buffer.getIndex(_buffer.lastDrawId)];
}

/**
* @notice Push Draw onto draws ring buffer history.
* @dev Push new draw onto draws list via authorized manager or owner.
* @param _newDraw DrawLib.Draw
* @param _newDraw IDrawBeacon.Draw
* @return Draw.drawId
*/
function _pushDraw(DrawLib.Draw memory _newDraw) internal returns (uint32) {
function _pushDraw(IDrawBeacon.Draw memory _newDraw) internal returns (uint32) {
DrawRingBufferLib.Buffer memory _buffer = drawRingBuffer;
_draws[_buffer.nextIndex] = _newDraw;
drawRingBuffer = _buffer.push(_newDraw.drawId);
Expand Down
2 changes: 1 addition & 1 deletion contracts/DrawPrize.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import "@pooltogether/owner-manager-contracts/contracts/Ownable.sol";

import "./interfaces/IDrawPrize.sol";
import "./interfaces/IDrawCalculator.sol";
import "./libraries/DrawLib.sol";
import "./interfaces/IDrawBeacon.sol";

/**
* @title PoolTogether V4 DrawPrize
Expand Down
21 changes: 10 additions & 11 deletions contracts/PrizeDistributionHistory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ pragma solidity 0.8.6;

import "@pooltogether/owner-manager-contracts/contracts/Manageable.sol";

import "./libraries/DrawLib.sol";
import "./libraries/DrawRingBufferLib.sol";
import "./interfaces/IPrizeDistributionHistory.sol";

Expand All @@ -31,7 +30,7 @@ contract PrizeDistributionHistory is IPrizeDistributionHistory, Manageable {
event Deployed(uint8 cardinality);

/// @notice PrizeDistribution ring buffer history.
DrawLib.PrizeDistribution[MAX_CARDINALITY] internal prizeDistributionRingBuffer;
IPrizeDistributionHistory.PrizeDistribution[MAX_CARDINALITY] internal prizeDistributionRingBuffer;

/// @notice Ring buffer metadata (nextIndex, lastId, cardinality)
DrawRingBufferLib.Buffer internal bufferMetadata;
Expand All @@ -55,7 +54,7 @@ contract PrizeDistributionHistory is IPrizeDistributionHistory, Manageable {
external
view
override
returns (DrawLib.PrizeDistribution memory)
returns (IPrizeDistributionHistory.PrizeDistribution memory)
{
return _getPrizeDistribution(bufferMetadata, _drawId);
}
Expand All @@ -65,10 +64,10 @@ contract PrizeDistributionHistory is IPrizeDistributionHistory, Manageable {
external
view
override
returns (DrawLib.PrizeDistribution[] memory)
returns (IPrizeDistributionHistory.PrizeDistribution[] memory)
{
DrawRingBufferLib.Buffer memory buffer = bufferMetadata;
DrawLib.PrizeDistribution[] memory _prizeDistributions = new DrawLib.PrizeDistribution[](
IPrizeDistributionHistory.PrizeDistribution[] memory _prizeDistributions = new IPrizeDistributionHistory.PrizeDistribution[](
_drawIds.length
);

Expand Down Expand Up @@ -102,7 +101,7 @@ contract PrizeDistributionHistory is IPrizeDistributionHistory, Manageable {
external
view
override
returns (DrawLib.PrizeDistribution memory prizeDistribution, uint32 drawId)
returns (IPrizeDistributionHistory.PrizeDistribution memory prizeDistribution, uint32 drawId)
{
DrawRingBufferLib.Buffer memory buffer = bufferMetadata;

Expand All @@ -117,7 +116,7 @@ contract PrizeDistributionHistory is IPrizeDistributionHistory, Manageable {
external
view
override
returns (DrawLib.PrizeDistribution memory prizeDistribution, uint32 drawId)
returns (IPrizeDistributionHistory.PrizeDistribution memory prizeDistribution, uint32 drawId)
{
DrawRingBufferLib.Buffer memory buffer = bufferMetadata;

Expand All @@ -141,15 +140,15 @@ contract PrizeDistributionHistory is IPrizeDistributionHistory, Manageable {
/// @inheritdoc IPrizeDistributionHistory
function pushPrizeDistribution(
uint32 _drawId,
DrawLib.PrizeDistribution calldata _prizeDistribution
IPrizeDistributionHistory.PrizeDistribution calldata _prizeDistribution
) external override onlyManagerOrOwner returns (bool) {
return _pushPrizeDistribution(_drawId, _prizeDistribution);
}

/// @inheritdoc IPrizeDistributionHistory
function setPrizeDistribution(
uint32 _drawId,
DrawLib.PrizeDistribution calldata _prizeDistribution
IPrizeDistributionHistory.PrizeDistribution calldata _prizeDistribution
) external override onlyOwner returns (uint32) {
DrawRingBufferLib.Buffer memory buffer = bufferMetadata;
uint32 index = buffer.getIndex(_drawId);
Expand All @@ -170,7 +169,7 @@ contract PrizeDistributionHistory is IPrizeDistributionHistory, Manageable {
function _getPrizeDistribution(DrawRingBufferLib.Buffer memory _buffer, uint32 _drawId)
internal
view
returns (DrawLib.PrizeDistribution memory)
returns (IPrizeDistributionHistory.PrizeDistribution memory)
{
return prizeDistributionRingBuffer[_buffer.getIndex(_drawId)];
}
Expand All @@ -182,7 +181,7 @@ contract PrizeDistributionHistory is IPrizeDistributionHistory, Manageable {
*/
function _pushPrizeDistribution(
uint32 _drawId,
DrawLib.PrizeDistribution calldata _prizeDistribution
IPrizeDistributionHistory.PrizeDistribution calldata _prizeDistribution
) internal returns (bool) {

require(_drawId > 0, "DrawCalc/draw-id-gt-0");
Expand Down
16 changes: 15 additions & 1 deletion contracts/interfaces/IDrawBeacon.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,23 @@ pragma solidity 0.8.6;

import "@pooltogether/pooltogether-rng-contracts/contracts/RNGInterface.sol";
import "./IDrawHistory.sol";
import "../libraries/DrawLib.sol";

interface IDrawBeacon {

/// @notice Draw struct created every draw
/// @param winningRandomNumber The random number returned from the RNG service
/// @param drawId The monotonically increasing drawId for each draw
/// @param timestamp Unix timestamp of the draw. Recorded when the draw is created by the DrawBeacon.
/// @param beaconPeriodStartedAt Unix timestamp of when the draw started
/// @param beaconPeriodSeconds Unix timestamp of the beacon draw period for this draw.
struct Draw {
uint256 winningRandomNumber;
uint32 drawId;
uint64 timestamp;
uint64 beaconPeriodStartedAt;
uint32 beaconPeriodSeconds;
}

/**
* @notice Emit when a new DrawHistory has been set.
* @param newDrawHistory The new DrawHistory address
Expand Down
1 change: 0 additions & 1 deletion contracts/interfaces/IDrawCalculator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import "./ITicket.sol";
import "./IDrawHistory.sol";
import "../PrizeDistributionHistory.sol";
import "../DrawPrize.sol";
import "../libraries/DrawLib.sol";

/**
* @title PoolTogether V4 DrawCalculator
Expand Down

0 comments on commit cf4bb35

Please sign in to comment.