Skip to content

Commit

Permalink
Added some more docs
Browse files Browse the repository at this point in the history
  • Loading branch information
asselstine committed Oct 6, 2021
1 parent d65a552 commit 7422feb
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 82 deletions.
12 changes: 2 additions & 10 deletions contracts/DrawBeacon.sol
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ contract DrawBeacon is IDrawBeacon, Ownable {
return _beaconPeriodEndAt();
}


function getBeaconPeriodSeconds() external view returns (uint32) {
return beaconPeriodSeconds;
}
Expand All @@ -277,12 +276,10 @@ contract DrawBeacon is IDrawBeacon, Ownable {
return beaconPeriodStartedAt;
}


function getDrawHistory() external view returns (IDrawHistory) {
return drawHistory;
}



function getNextDrawId() external view returns (uint32) {
return nextDrawId;
}
Expand Down Expand Up @@ -346,12 +343,7 @@ contract DrawBeacon is IDrawBeacon, Ownable {
}

/// @inheritdoc IDrawBeacon
function setRngTimeout(uint32 _rngTimeout)
external
override
onlyOwner
requireDrawNotStarted
{
function setRngTimeout(uint32 _rngTimeout) external override onlyOwner requireDrawNotStarted {
_setRngTimeout(_rngTimeout);
}

Expand Down
20 changes: 9 additions & 11 deletions contracts/PrizeDistributionHistory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ contract PrizeDistributionHistory is IPrizeDistributionHistory, Manageable {
uint256 internal constant MAX_CARDINALITY = 256;

uint256 internal constant DISTRIBUTION_CEILING = 1e9;

event Deployed(uint8 cardinality);

/// @notice PrizeDistribution ring buffer history.
Expand Down Expand Up @@ -114,7 +114,7 @@ contract PrizeDistributionHistory is IPrizeDistributionHistory, Manageable {
returns (DrawLib.PrizeDistribution memory prizeDistribution, uint32 drawId)
{
DrawRingBufferLib.Buffer memory buffer = _prizeDistributionsRingBufferData;

// if the ring buffer is full, the oldest is at the nextIndex
prizeDistribution = _prizeDistributionsRingBuffer[buffer.nextIndex];

Expand Down Expand Up @@ -161,10 +161,11 @@ contract PrizeDistributionHistory is IPrizeDistributionHistory, Manageable {
* @param _buffer DrawRingBufferLib.Buffer
* @param _drawId drawId
*/
function _getPrizeDistribution(
DrawRingBufferLib.Buffer memory _buffer,
uint32 _drawId
) internal view returns (DrawLib.PrizeDistribution memory) {
function _getPrizeDistribution(DrawRingBufferLib.Buffer memory _buffer, uint32 _drawId)
internal
view
returns (DrawLib.PrizeDistribution memory)
{
return _prizeDistributionsRingBuffer[_buffer.getIndex(_drawId)];
}

Expand Down Expand Up @@ -208,13 +209,10 @@ contract PrizeDistributionHistory is IPrizeDistributionHistory, Manageable {
"DrawCalc/matchCardinality-gte-distributions"
);

DrawRingBufferLib.Buffer
memory buffer = _prizeDistributionsRingBufferData;
DrawRingBufferLib.Buffer memory buffer = _prizeDistributionsRingBufferData;

// store the PrizeDistribution in the ring buffer
_prizeDistributionsRingBuffer[
buffer.nextIndex
] = _prizeDistribution;
_prizeDistributionsRingBuffer[buffer.nextIndex] = _prizeDistribution;

// update the ring buffer data
_prizeDistributionsRingBufferData = buffer.push(_drawId);
Expand Down
72 changes: 36 additions & 36 deletions contracts/Ticket.sol
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,9 @@ contract Ticket is ControlledToken, ITicket {
/* ============ Internal Functions ============ */

/**
* @notice Returns the ERC20 ticket token balance of a ticket holder.
* @return uint256 `_user` ticket token balance.
*/
* @notice Returns the ERC20 ticket token balance of a ticket holder.
* @return uint256 `_user` ticket token balance.
*/
function _balanceOf(address _user) internal view returns (uint256) {
return balances[_user];
}
Expand Down Expand Up @@ -264,14 +264,14 @@ contract Ticket is ControlledToken, ITicket {
}

/**
* @notice Overridding of the `_transfer` function of the base ERC20 contract.
* @dev `_sender` cannot be the zero address.
* @dev `_recipient` cannot be the zero address.
* @dev `_sender` must have a balance of at least `_amount`.
* @param _sender Address of the `_sender`that will send `_amount` of tokens.
* @param _recipient Address of the `_recipient`that will receive `_amount` of tokens.
* @param _amount Amount of tokens to be transferred from `_sender` to `_recipient`.
*/
* @notice Overridding of the `_transfer` function of the base ERC20 contract.
* @dev `_sender` cannot be the zero address.
* @dev `_recipient` cannot be the zero address.
* @dev `_sender` must have a balance of at least `_amount`.
* @param _sender Address of the `_sender`that will send `_amount` of tokens.
* @param _recipient Address of the `_recipient`that will receive `_amount` of tokens.
* @param _amount Amount of tokens to be transferred from `_sender` to `_recipient`.
*/
function _transfer(
address _sender,
address _recipient,
Expand Down Expand Up @@ -318,11 +318,11 @@ contract Ticket is ControlledToken, ITicket {
}

/**
* @notice Overridding of the `_mint` function of the base ERC20 contract.
* @dev `_to` cannot be the zero address.
* @param _to Address that will be minted `_amount` of tokens.
* @param _amount Amount of tokens to be minted to `_to`.
*/
* @notice Overridding of the `_mint` function of the base ERC20 contract.
* @dev `_to` cannot be the zero address.
* @param _to Address that will be minted `_amount` of tokens.
* @param _amount Amount of tokens to be minted to `_to`.
*/
function _mint(address _to, uint256 _amount) internal virtual override {
require(_to != address(0), "ERC20: mint to the zero address");

Expand Down Expand Up @@ -356,12 +356,12 @@ contract Ticket is ControlledToken, ITicket {
}

/**
* @notice Overridding of the `_burn` function of the base ERC20 contract.
* @dev `_from` cannot be the zero address.
* @dev `_from` must have at least `_amount` of tokens.
* @param _from Address that will be burned `_amount` of tokens.
* @param _amount Amount of tokens to be burnt from `_from`.
*/
* @notice Overridding of the `_burn` function of the base ERC20 contract.
* @dev `_from` cannot be the zero address.
* @dev `_from` must have at least `_amount` of tokens.
* @param _from Address that will be burned `_amount` of tokens.
* @param _amount Amount of tokens to be burnt from `_from`.
*/
function _burn(address _from, uint256 _amount) internal virtual override {
require(_from != address(0), "ERC20: burn from the zero address");

Expand Down Expand Up @@ -406,13 +406,13 @@ contract Ticket is ControlledToken, ITicket {
}

/**
* @notice Increase `_user` TWAB balance.
* @dev If `_user` has not set a delegate address, `_user` TWAB balance will be increased.
* @dev Otherwise, `_delegate` TWAB balance will be increased.
* @param _user Address of the user.
* @param _delegate Address of the delegate.
* @param _amount Amount of tokens to be added to `_user` TWAB balance.
*/
* @notice Increase `_user` TWAB balance.
* @dev If `_user` has not set a delegate address, `_user` TWAB balance will be increased.
* @dev Otherwise, `_delegate` TWAB balance will be increased.
* @param _user Address of the user.
* @param _delegate Address of the delegate.
* @param _amount Amount of tokens to be added to `_user` TWAB balance.
*/
function _increaseUserTwab(
address _user,
address _delegate,
Expand All @@ -434,13 +434,13 @@ contract Ticket is ControlledToken, ITicket {
}

/**
* @notice Decrease `_user` TWAB balance.
* @dev If `_user` has not set a delegate address, `_user` TWAB balance will be decreased.
* @dev Otherwise, `_delegate` TWAB balance will be decreased.
* @param _user Address of the user.
* @param _delegate Address of the delegate.
* @param _amount Amount of tokens to be added to `_user` TWAB balance.
*/
* @notice Decrease `_user` TWAB balance.
* @dev If `_user` has not set a delegate address, `_user` TWAB balance will be decreased.
* @dev Otherwise, `_delegate` TWAB balance will be decreased.
* @param _user Address of the user.
* @param _delegate Address of the delegate.
* @param _amount Amount of tokens to be added to `_user` TWAB balance.
*/
function _decreaseUserTwab(
address _user,
address _delegate,
Expand Down
7 changes: 4 additions & 3 deletions contracts/interfaces/IPrizeDistributionHistory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@ interface IPrizeDistributionHistory {
* @param drawId drawId to store PrizeDistribution for
* @param prizeDistribution PrizeDistribution to store
*/
function pushPrizeDistribution(uint32 drawId, DrawLib.PrizeDistribution calldata prizeDistribution)
external
returns (bool);
function pushPrizeDistribution(
uint32 drawId,
DrawLib.PrizeDistribution calldata prizeDistribution
) external returns (bool);

/**
* @notice Set existing Draw in prize distributions ring buffer with new parameters.
Expand Down
1 change: 0 additions & 1 deletion contracts/libraries/DrawRingBufferLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ library DrawRingBufferLib {
/// @param _drawId The drawID to push.
/// @return The new buffer.
function push(Buffer memory _buffer, uint32 _drawId) internal pure returns (Buffer memory) {

require(!isInitialized(_buffer) || _drawId == _buffer.lastDrawId + 1, "DRB/must-be-contig");

return
Expand Down
34 changes: 21 additions & 13 deletions contracts/libraries/TwabLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import "./ObservationLib.sol";
ring buffer parameters. Every token.transfer() creates a new TWAB checkpoint. The new TWAB
checkpoint is stored in the circular ring buffer, as either a new checkpoint or rewriting
a previous checkpoint with new parameters. The TwabLib (using existing blocktimes 1block/15sec)
guarantees on average 6 months of search history.
guarantees minimum 7.4 years of search history.
*/
library TwabLib {
using OverflowSafeComparatorLib for uint32;
Expand All @@ -28,19 +28,24 @@ library TwabLib {
* @notice Sets max ring buffer length in the Account.twabs Observation list.
As users transfer/mint/burn tickets new Observation checkpoints are
recorded. The current max cardinality guarantees a six month minimum,
of historical accurate lookups with current estimates of 1 new block
every 15 seconds - the of course contain a transfer to trigger an
of historical accurate lookups with current estimates of 1 new block
every 15 seconds - the of course contain a transfer to trigger an
observation write to storage.
* @dev The user Account.AccountDetails.cardinality parameter can NOT exceed
the max cardinality variable. Preventing "corrupted" ring buffer lookup
pointers and new observation checkpoints.
The MAX_CARDINALITY in fact guarantees at least 7.4 years of records:
If 14 = block time in seconds
(2**24) * 14 = 234881024 seconds of history
234881024 / (365 * 24 * 60 * 60) ~= 7.44 years
*/
uint24 public constant MAX_CARDINALITY = 16777215; // 2**24

/** @notice Struct ring buffer parameters for single user Account
* @param balance Current balance for an Account
* @param nextTwabIndex Next uninitialized or updatable ring buffer checkpoint storage slot
* @param cardinality Current total "initialized" ring buffer checkpoints for single user AccountDetails.
* @param cardinality Current total "initialized" ring buffer checkpoints for single user AccountDetails.
Used to set initial boundary conditions for an efficient binary search.
*/
struct AccountDetails {
Expand Down Expand Up @@ -160,7 +165,9 @@ library TwabLib {
ObservationLib.Observation[MAX_CARDINALITY] storage _twabs,
AccountDetails memory _accountDetails
) internal view returns (uint24 index, ObservationLib.Observation memory twab) {
index = uint24(RingBufferLib.mostRecentIndex(_accountDetails.nextTwabIndex, MAX_CARDINALITY));
index = uint24(
RingBufferLib.mostRecentIndex(_accountDetails.nextTwabIndex, MAX_CARDINALITY)
);
twab = _twabs[index];
}

Expand Down Expand Up @@ -227,7 +234,7 @@ library TwabLib {
}

/** @notice Searches TWAB history and calculate the difference between amount(s)/timestamp(s) to return average balance
between the Observations closes to the supplied targetTime.
between the Observations closes to the supplied targetTime.
* @param _twabs Individual user Observation recorded checkpoints passed as storage pointer
* @param _accountDetails User AccountDetails struct loaded in memory
* @param _target Target timestamp to filter Observations in the ring buffer binary search
Expand Down Expand Up @@ -270,7 +277,8 @@ library TwabLib {
);

// Difference in amount / time
return (afterOrAt.amount - beforeOrAt.amount) / (afterOrAt.timestamp - beforeOrAt.timestamp);
return
(afterOrAt.amount - beforeOrAt.amount) / (afterOrAt.timestamp - beforeOrAt.timestamp);
}

/** @notice Calculates a user TWAB for a target timestamp using the historical TWAB records.
Expand Down Expand Up @@ -347,12 +355,12 @@ library TwabLib {
}

/**
* @notice Calculates the next TWAB using the newestTwab and updated balance.
* @dev Storage of the TWAB obersation is managed by the calling function and not _computeNextTwab.
* @param _currentTwab Newest Observation in the Account.twabs list
* @param _currentBalance User balance at time of most recent (newest) checkpoint write
* @param _time Current block.timestamp
* @return TWAB Observation
* @notice Calculates the next TWAB using the newestTwab and updated balance.
* @dev Storage of the TWAB obersation is managed by the calling function and not _computeNextTwab.
* @param _currentTwab Newest Observation in the Account.twabs list
* @param _currentBalance User balance at time of most recent (newest) checkpoint write
* @param _time Current block.timestamp
* @return TWAB Observation
*/
function _computeNextTwab(
ObservationLib.Observation memory _currentTwab,
Expand Down
2 changes: 1 addition & 1 deletion test/DrawCalculator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ describe('DrawCalculator', () => {
const userRandomNumber =
'0x369ddb959b07c1d22a9bada1f3420961d0e0252f73c0f5b2173d7f7c6fe12b70'; // intentionally same as winning random number

const prizeDistributionIndex: BigNumber =
const prizeDistributionIndex: BigNumber =
await drawCalculator.calculateDistributionIndex(
userRandomNumber,
winningRandomNumber,
Expand Down
8 changes: 6 additions & 2 deletions test/Ticket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -876,15 +876,19 @@ describe('Ticket', () => {
const beforeTimestamp = (await provider.getBlock('latest')).timestamp;

expect(await ticket.delegateOf(wallet1.address)).to.equal(wallet2.address);
expect(await ticket.getBalanceAt(wallet2.address, beforeTimestamp)).to.equal(toWei('100'));
expect(await ticket.getBalanceAt(wallet2.address, beforeTimestamp)).to.equal(
toWei('100'),
);

await ticket.delegate(AddressZero);

const afterTimestamp = (await provider.getBlock('latest')).timestamp;

expect(await ticket.delegateOf(wallet1.address)).to.equal(AddressZero);
expect(await ticket.getBalanceAt(wallet2.address, afterTimestamp)).to.equal(toWei('0'));
expect(await ticket.getBalanceAt(wallet1.address, afterTimestamp)).to.equal(toWei('100'));
expect(await ticket.getBalanceAt(wallet1.address, afterTimestamp)).to.equal(
toWei('100'),
);
});

it('should clear old delegates if any', async () => {
Expand Down
Loading

0 comments on commit 7422feb

Please sign in to comment.