Skip to content

Commit 8bdc1f5

Browse files
committed
feat(RateLimit): Adding a new maxLimit representing the maximum value an admin can set the current and dailyLimit to. Adding new functions to reset the currentLimit to a given value regardless of the dailyLimit
1 parent 6161528 commit 8bdc1f5

File tree

1 file changed

+86
-16
lines changed

1 file changed

+86
-16
lines changed

src/RateLimitsUpgradeable.sol

Lines changed: 86 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ contract RateLimitsUpgradeable {
66
*
77
* @param timestamp The timestamp of the last mint/burn
88
* @param ratePerSecond The rate per second one can mint/burn
9-
* @param maxLimit The max limit
9+
* @param dailyLimit The max limit per day
1010
* @param currentLimit The current limit
11+
* @param maxLimit the maximum someone can set the currentLimit to;
1112
*/
1213
struct RateLimitParameters {
1314
uint256 timestamp;
1415
uint256 ratePerSecond;
15-
uint256 maxLimit;
16+
uint256 dailyLimit;
1617
uint256 currentLimit;
18+
uint256 maxLimit; // Should we cap this ? it is so that an admin can set a current higher than the daily but only up to a certain amount.
1719
}
1820

1921
/**
@@ -50,6 +52,32 @@ contract RateLimitsUpgradeable {
5052
}
5153
}
5254

55+
/**
56+
* @notice set the maxlimit of a minter
57+
* @param account The address of the minter who is being changed
58+
* @param newMax The new maxLimit
59+
*/
60+
function _setMinterMaxLimit(address account, uint256 newMax) internal {
61+
RateLimitParameters storage m = _getRateLimitsStorage()
62+
._limits[account]
63+
.mint;
64+
65+
m.maxLimit = newMax;
66+
}
67+
68+
/**
69+
* @notice set the maxlimit of a burner
70+
* @param account The address of the burner who is being changed
71+
* @param newMax The new maxLimit
72+
*/
73+
function _setBurnerMaxLimit(address account, uint256 newMax) internal {
74+
RateLimitParameters storage b = _getRateLimitsStorage()
75+
._limits[account]
76+
.burn;
77+
78+
b.maxLimit = newMax;
79+
}
80+
5381
/**
5482
* @notice Uses the limit of a minter
5583
* @param account The address of the minter who is being changed
@@ -80,6 +108,44 @@ contract RateLimitsUpgradeable {
80108
b.currentLimit = currentLimit - change;
81109
}
82110

111+
/**
112+
* @notice Resets the minting limit for a given account to a specified value
113+
* @param account The address of the minter whose limit is to be reset
114+
* @param limit The new current limit to be set for minting
115+
*/
116+
function _resetMinterCurrentLimit(
117+
address account,
118+
uint256 limit
119+
) internal {
120+
RateLimitParameters storage m = _getRateLimitsStorage()
121+
._limits[account]
122+
.mint;
123+
124+
require(limit <= m.maxLimit, "too high");
125+
126+
m.currentLimit = limit;
127+
m.timestamp = block.timestamp;
128+
}
129+
130+
/**
131+
* @notice Resets the burning limit for a given account to a specified value
132+
* @param account The address of the burner whose limit is to be reset
133+
* @param limit The new current limit to be set for burning
134+
*/
135+
function _resetBurnerCurrentLimit(
136+
address account,
137+
uint256 limit
138+
) internal {
139+
RateLimitParameters storage b = _getRateLimitsStorage()
140+
._limits[account]
141+
.burn;
142+
143+
require(limit <= b.maxLimit, "too high");
144+
145+
b.currentLimit = limit;
146+
b.timestamp = block.timestamp;
147+
}
148+
83149
/**
84150
* @notice Updates the limit of a minter
85151
* @param account The address of the minter we are setting the limit too
@@ -90,9 +156,11 @@ contract RateLimitsUpgradeable {
90156
._limits[account]
91157
.mint;
92158

93-
uint256 oldLimit = m.maxLimit;
159+
require(limit <= m.maxLimit, "too high");
160+
161+
uint256 oldLimit = m.dailyLimit;
94162
uint256 currentLimit = mintingCurrentLimitOf(account);
95-
m.maxLimit = limit;
163+
m.dailyLimit = limit;
96164

97165
m.currentLimit = _calculateNewCurrentLimit(
98166
limit,
@@ -114,9 +182,11 @@ contract RateLimitsUpgradeable {
114182
._limits[account]
115183
.burn;
116184

117-
uint256 oldLimit = b.maxLimit;
185+
require(limit <= b.maxLimit, "too high");
186+
187+
uint256 oldLimit = b.dailyLimit;
118188
uint256 currentLimit = burningCurrentLimitOf(account);
119-
b.maxLimit = limit;
189+
b.dailyLimit = limit;
120190

121191
b.currentLimit = _calculateNewCurrentLimit(
122192
limit,
@@ -140,7 +210,7 @@ contract RateLimitsUpgradeable {
140210
RateLimitParameters storage m = _getRateLimitsStorage()
141211
._limits[account]
142212
.mint;
143-
limit = m.maxLimit;
213+
limit = m.dailyLimit;
144214
}
145215

146216
/**
@@ -155,7 +225,7 @@ contract RateLimitsUpgradeable {
155225
RateLimitParameters storage b = _getRateLimitsStorage()
156226
._limits[account]
157227
.burn;
158-
limit = b.maxLimit;
228+
limit = b.dailyLimit;
159229
}
160230

161231
/**
@@ -172,7 +242,7 @@ contract RateLimitsUpgradeable {
172242
.mint;
173243
limit = _getCurrentLimit(
174244
m.currentLimit,
175-
m.maxLimit,
245+
m.dailyLimit,
176246
m.timestamp,
177247
m.ratePerSecond
178248
);
@@ -192,7 +262,7 @@ contract RateLimitsUpgradeable {
192262
.burn;
193263
limit = _getCurrentLimit(
194264
b.currentLimit,
195-
b.maxLimit,
265+
b.dailyLimit,
196266
b.timestamp,
197267
b.ratePerSecond
198268
);
@@ -228,27 +298,27 @@ contract RateLimitsUpgradeable {
228298
* @notice Gets the current limit
229299
*
230300
* @param _currentLimit The current limit
231-
* @param _maxLimit The max limit
301+
* @param _dailyLimit The max limit
232302
* @param _timestamp The timestamp of the last update
233303
* @param _ratePerSecond The rate per second
234304
* @return _limit The current limit
235305
*/
236306
function _getCurrentLimit(
237307
uint256 _currentLimit,
238-
uint256 _maxLimit,
308+
uint256 _dailyLimit,
239309
uint256 _timestamp,
240310
uint256 _ratePerSecond
241311
) internal view returns (uint256 _limit) {
242312
_limit = _currentLimit;
243-
if (_limit == _maxLimit) {
313+
if (_limit >= _dailyLimit) {
244314
return _limit;
245315
} else if (_timestamp + _DURATION <= block.timestamp) {
246-
_limit = _maxLimit;
316+
_limit = _dailyLimit;
247317
} else if (_timestamp + _DURATION > block.timestamp) {
248318
uint256 _timePassed = block.timestamp - _timestamp;
249319
uint256 _calculatedLimit = _limit + (_timePassed * _ratePerSecond);
250-
_limit = _calculatedLimit > _maxLimit
251-
? _maxLimit
320+
_limit = _calculatedLimit > _dailyLimit
321+
? _dailyLimit
252322
: _calculatedLimit;
253323
}
254324
}

0 commit comments

Comments
 (0)