Skip to content

Commit

Permalink
removed Draw struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Aodhgan committed Oct 6, 2021
1 parent 7c30458 commit b98d3e0
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 33 deletions.
2 changes: 1 addition & 1 deletion contracts/DrawBeacon.sol
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ contract DrawBeacon is IDrawBeacon, Ownable {
uint64 _time = _currentTime();

// create Draw struct
DrawLib.Draw memory _draw = DrawLib.Draw({
Draw memory _draw = Draw({
winningRandomNumber: randomNumber,
drawId: _nextDrawId,
timestamp: rngRequest.requestedAt, // must use the startAward() timestamp to prevent front-running
Expand Down
10 changes: 5 additions & 5 deletions contracts/DrawCalculator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ 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 IDrawBeacon using the drawIds from drawHistory
IDrawBeacon[] 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
Expand Down Expand Up @@ -117,7 +117,7 @@ contract DrawCalculator is IDrawCalculator, Ownable {
override
returns (uint256[] memory)
{
IDrawBeacon[] memory _draws = drawHistory.getDraws(_drawIds);
IDrawBeacon.Draw[] memory _draws = drawHistory.getDraws(_drawIds);
DrawLib.PrizeDistribution[] memory _prizeDistributions = prizeDistributionHistory
.getPrizeDistributions(_drawIds);

Expand All @@ -133,7 +133,7 @@ contract DrawCalculator is IDrawCalculator, Ownable {
uint32[] memory drawIds = new uint32[](1);
drawIds[0] = _drawId;

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

Expand Down Expand Up @@ -238,7 +238,7 @@ contract DrawCalculator is IDrawCalculator, Ownable {
*/
function _getNormalizedBalancesAt(
address _user,
IDrawBeacon[] memory _draws,
IDrawBeacon.Draw[] memory _draws,
DrawLib.PrizeDistribution[] memory _prizeDistributions
) internal view returns (uint256[] memory) {
uint32[] memory _timestampsWithStartCutoffTimes = new uint32[](_draws.length);
Expand Down
26 changes: 13 additions & 13 deletions contracts/DrawHistory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,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 +45,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 +54,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 +84,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 +103,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 +113,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 +141,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
27 changes: 14 additions & 13 deletions contracts/interfaces/IDrawHistory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,31 @@
pragma solidity 0.8.6;

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

interface IDrawHistory {
/**
* @notice Emit when a new draw has been created.
* @param drawId Draw id
* @param draw The Draw struct
*/
event DrawSet(uint32 indexed drawId, DrawLib.Draw draw);
event DrawSet(uint32 indexed drawId, IDrawBeacon.Draw draw);

/**
* @notice Read a Draw from the draws ring buffer.
* @dev Read a Draw using the Draw.drawId to calculate position in the draws ring buffer.
* @param drawId Draw.drawId
* @return DrawLib.Draw
* @return IDrawBeacon.Draw
*/
function getDraw(uint32 drawId) external view returns (DrawLib.Draw memory);
function getDraw(uint32 drawId) external view returns (IDrawBeacon.Draw memory);

/**
* @notice Read multiple Draws from the draws ring buffer.
* @dev Read multiple Draws using each Draw.drawId to calculate position in the draws ring buffer.
* @param drawIds Array of Draw.drawIds
* @return DrawLib.Draw[]
* @return IDrawBeacon.Draw[]
*/
function getDraws(uint32[] calldata drawIds) external view returns (DrawLib.Draw[] memory);
function getDraws(uint32[] calldata drawIds) external view returns (IDrawBeacon.Draw[] memory);

/**
* @notice Gets the number of Draws held in the draw ring buffer.
Expand All @@ -40,30 +41,30 @@ interface IDrawHistory {
/**
* @notice Read newest Draw from the draws ring buffer.
* @dev Uses the nextDrawIndex to calculate the most recently added Draw.
* @return DrawLib.Draw
* @return IDrawBeacon.Draw
*/
function getNewestDraw() external view returns (DrawLib.Draw memory);
function getNewestDraw() external view returns (IDrawBeacon.Draw memory);

/**
* @notice Read oldest Draw from the draws ring buffer.
* @dev Finds the oldest Draw by comparing and/or diffing totalDraws with the cardinality.
* @return DrawLib.Draw
* @return IDrawBeacon.Draw
*/
function getOldestDraw() external view returns (DrawLib.Draw memory);
function getOldestDraw() external view returns (IDrawBeacon.Draw memory);

/**
* @notice Push Draw onto draws ring buffer history.
* @dev Push new draw onto draws history via authorized manager or owner.
* @param draw DrawLib.Draw
* @param draw IDrawBeacon.Draw
* @return Draw.drawId
*/
function pushDraw(DrawLib.Draw calldata draw) external returns (uint32);
function pushDraw(IDrawBeacon.Draw calldata draw) external returns (uint32);

/**
* @notice Set existing Draw in draws ring buffer with new parameters.
* @dev Updating a Draw should be used sparingly and only in the event an incorrect Draw parameter has been stored.
* @param newDraw DrawLib.Draw
* @param newDraw IDrawBeacon.Draw
* @return Draw.drawId
*/
function setDraw(DrawLib.Draw calldata newDraw) external returns (uint32);
function setDraw(IDrawBeacon.Draw calldata newDraw) external returns (uint32);
}
1 change: 1 addition & 0 deletions contracts/interfaces/IDrawPrize.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity 0.8.6;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "./IDrawHistory.sol";
import "./IDrawCalculator.sol";
import "./IDrawBeacon.sol";
import "../libraries/DrawLib.sol";

interface IDrawPrize {
Expand Down
1 change: 1 addition & 0 deletions contracts/interfaces/IPrizeDistributionHistory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
pragma solidity 0.8.6;

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

interface IPrizeDistributionHistory {
/**
Expand Down
3 changes: 2 additions & 1 deletion contracts/test/DrawHistoryHarness.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pragma solidity 0.8.6;

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

contract DrawHistoryHarness is DrawHistory {
constructor(address owner, uint8 card) DrawHistory(owner, card) {}
Expand All @@ -15,7 +16,7 @@ contract DrawHistoryHarness is DrawHistory {
uint256 _winningRandomNumber
) external {
for (uint256 index = _start; index <= _numberOfDraws; index++) {
DrawLib.Draw memory _draw = DrawLib.Draw({
IDrawBeacon.Draw memory _draw = IDrawBeacon.Draw({
winningRandomNumber: _winningRandomNumber,
drawId: uint32(index),
timestamp: _timestamp,
Expand Down

0 comments on commit b98d3e0

Please sign in to comment.