-
Notifications
You must be signed in to change notification settings - Fork 49
/
PrizeDistributionBuffer.sol
228 lines (186 loc) Β· 8.73 KB
/
PrizeDistributionBuffer.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.6;
import "@pooltogether/owner-manager-contracts/contracts/Manageable.sol";
import "./libraries/DrawRingBufferLib.sol";
import "./interfaces/IPrizeDistributionBuffer.sol";
/**
* @title PoolTogether V4 PrizeDistributionBuffer
* @author PoolTogether Inc Team
* @notice The PrizeDistributionBuffer contract provides historical lookups of PrizeDistribution struct parameters (linked with a Draw ID) via a
circular ring buffer. Historical PrizeDistribution parameters can be accessed on-chain using a drawId to calculate
ring buffer storage slot. The PrizeDistribution parameters can be created by manager/owner and existing PrizeDistribution
parameters can only be updated the owner. When adding a new PrizeDistribution basic sanity checks will be used to
validate the incoming parameters.
*/
contract PrizeDistributionBuffer is IPrizeDistributionBuffer, Manageable {
using DrawRingBufferLib for DrawRingBufferLib.Buffer;
/// @notice The maximum cardinality of the prize distribution ring buffer.
/// @dev even with daily draws, 256 will give us over 8 months of history.
uint256 internal constant MAX_CARDINALITY = 256;
/// @notice The ceiling for prize distributions. 1e9 = 100%.
/// @dev It's fixed point 9 because 1e9 is the largest "1" that fits into 2**32
uint256 internal constant TIERS_CEILING = 1e9;
/// @notice Emitted when the contract is deployed.
/// @param cardinality The maximum number of records in the buffer before they begin to expire.
event Deployed(uint8 cardinality);
/// @notice PrizeDistribution ring buffer history.
IPrizeDistributionBuffer.PrizeDistribution[MAX_CARDINALITY]
internal prizeDistributionRingBuffer;
/// @notice Ring buffer metadata (nextIndex, lastId, cardinality)
DrawRingBufferLib.Buffer internal bufferMetadata;
/* ============ Constructor ============ */
/**
* @notice Constructor for PrizeDistributionBuffer
* @param _owner Address of the PrizeDistributionBuffer owner
* @param _cardinality Cardinality of the `bufferMetadata`
*/
constructor(address _owner, uint8 _cardinality) Ownable(_owner) {
bufferMetadata.cardinality = _cardinality;
emit Deployed(_cardinality);
}
/* ============ External Functions ============ */
/// @inheritdoc IPrizeDistributionBuffer
function getBufferCardinality() external view override returns (uint32) {
return bufferMetadata.cardinality;
}
/// @inheritdoc IPrizeDistributionBuffer
function getPrizeDistribution(uint32 _drawId)
external
view
override
returns (IPrizeDistributionBuffer.PrizeDistribution memory)
{
return _getPrizeDistribution(bufferMetadata, _drawId);
}
/// @inheritdoc IPrizeDistributionSource
function getPrizeDistributions(uint32[] calldata _drawIds)
external
view
override
returns (IPrizeDistributionBuffer.PrizeDistribution[] memory)
{
uint256 drawIdsLength = _drawIds.length;
DrawRingBufferLib.Buffer memory buffer = bufferMetadata;
IPrizeDistributionBuffer.PrizeDistribution[]
memory _prizeDistributions = new IPrizeDistributionBuffer.PrizeDistribution[](
drawIdsLength
);
for (uint256 i = 0; i < drawIdsLength; i++) {
_prizeDistributions[i] = _getPrizeDistribution(buffer, _drawIds[i]);
}
return _prizeDistributions;
}
/// @inheritdoc IPrizeDistributionBuffer
function getPrizeDistributionCount() external view override returns (uint32) {
DrawRingBufferLib.Buffer memory buffer = bufferMetadata;
if (buffer.lastDrawId == 0) {
return 0;
}
uint32 bufferNextIndex = buffer.nextIndex;
// If the buffer is full return the cardinality, else retun the nextIndex
if (prizeDistributionRingBuffer[bufferNextIndex].matchCardinality != 0) {
return buffer.cardinality;
} else {
return bufferNextIndex;
}
}
/// @inheritdoc IPrizeDistributionBuffer
function getNewestPrizeDistribution()
external
view
override
returns (IPrizeDistributionBuffer.PrizeDistribution memory prizeDistribution, uint32 drawId)
{
DrawRingBufferLib.Buffer memory buffer = bufferMetadata;
return (prizeDistributionRingBuffer[buffer.getIndex(buffer.lastDrawId)], buffer.lastDrawId);
}
/// @inheritdoc IPrizeDistributionBuffer
function getOldestPrizeDistribution()
external
view
override
returns (IPrizeDistributionBuffer.PrizeDistribution memory prizeDistribution, uint32 drawId)
{
DrawRingBufferLib.Buffer memory buffer = bufferMetadata;
// if the ring buffer is full, the oldest is at the nextIndex
prizeDistribution = prizeDistributionRingBuffer[buffer.nextIndex];
// The PrizeDistribution at index 0 IS by default the oldest prizeDistribution.
if (buffer.lastDrawId == 0) {
drawId = 0; // return 0 to indicate no prizeDistribution ring buffer history
} else if (prizeDistribution.bitRangeSize == 0) {
// IF the next PrizeDistribution.bitRangeSize == 0 the ring buffer HAS NOT looped around so the oldest is the first entry.
prizeDistribution = prizeDistributionRingBuffer[0];
drawId = (buffer.lastDrawId + 1) - buffer.nextIndex;
} else {
// Calculates the drawId using the ring buffer cardinality
// Sequential drawIds are gauranteed by DrawRingBufferLib.push()
drawId = (buffer.lastDrawId + 1) - buffer.cardinality;
}
}
/// @inheritdoc IPrizeDistributionBuffer
function pushPrizeDistribution(
uint32 _drawId,
IPrizeDistributionBuffer.PrizeDistribution calldata _prizeDistribution
) external override onlyManagerOrOwner returns (bool) {
return _pushPrizeDistribution(_drawId, _prizeDistribution);
}
/// @inheritdoc IPrizeDistributionBuffer
function setPrizeDistribution(
uint32 _drawId,
IPrizeDistributionBuffer.PrizeDistribution calldata _prizeDistribution
) external override onlyOwner returns (uint32) {
DrawRingBufferLib.Buffer memory buffer = bufferMetadata;
uint32 index = buffer.getIndex(_drawId);
prizeDistributionRingBuffer[index] = _prizeDistribution;
emit PrizeDistributionSet(_drawId, _prizeDistribution);
return _drawId;
}
/* ============ Internal Functions ============ */
/**
* @notice Gets the PrizeDistributionBuffer for a drawId
* @param _buffer DrawRingBufferLib.Buffer
* @param _drawId drawId
*/
function _getPrizeDistribution(DrawRingBufferLib.Buffer memory _buffer, uint32 _drawId)
internal
view
returns (IPrizeDistributionBuffer.PrizeDistribution memory)
{
return prizeDistributionRingBuffer[_buffer.getIndex(_drawId)];
}
/**
* @notice Set newest PrizeDistributionBuffer in ring buffer storage.
* @param _drawId drawId
* @param _prizeDistribution PrizeDistributionBuffer struct
*/
function _pushPrizeDistribution(
uint32 _drawId,
IPrizeDistributionBuffer.PrizeDistribution calldata _prizeDistribution
) internal returns (bool) {
require(_drawId > 0, "DrawCalc/draw-id-gt-0");
require(_prizeDistribution.matchCardinality > 0, "DrawCalc/matchCardinality-gt-0");
require(
_prizeDistribution.bitRangeSize <= 256 / _prizeDistribution.matchCardinality,
"DrawCalc/bitRangeSize-too-large"
);
require(_prizeDistribution.bitRangeSize > 0, "DrawCalc/bitRangeSize-gt-0");
require(_prizeDistribution.maxPicksPerUser > 0, "DrawCalc/maxPicksPerUser-gt-0");
require(_prizeDistribution.expiryDuration > 0, "DrawCalc/expiryDuration-gt-0");
// ensure that the sum of the tiers are not gt 100%
uint256 sumTotalTiers = 0;
uint256 tiersLength = _prizeDistribution.tiers.length;
for (uint256 index = 0; index < tiersLength; index++) {
uint256 tier = _prizeDistribution.tiers[index];
sumTotalTiers += tier;
}
// Each tier amount stored as uint32 - summed can't exceed 1e9
require(sumTotalTiers <= TIERS_CEILING, "DrawCalc/tiers-gt-100%");
DrawRingBufferLib.Buffer memory buffer = bufferMetadata;
// store the PrizeDistribution in the ring buffer
prizeDistributionRingBuffer[buffer.nextIndex] = _prizeDistribution;
// update the ring buffer data
bufferMetadata = buffer.push(_drawId);
emit PrizeDistributionSet(_drawId, _prizeDistribution);
return true;
}
}