-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathRateLimits.sol
More file actions
136 lines (102 loc) · 5.37 KB
/
Copy pathRateLimits.sol
File metadata and controls
136 lines (102 loc) · 5.37 KB
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
// SPDX-License-Identifier: AGPL-3.0-or-later
pragma solidity ^0.8.21;
import { AccessControl } from "openzeppelin-contracts/contracts/access/AccessControl.sol";
import { IRateLimits } from "./interfaces/IRateLimits.sol";
contract RateLimits is IRateLimits, AccessControl {
/**********************************************************************************************/
/*** State variables ***/
/**********************************************************************************************/
bytes32 public override constant CONTROLLER = keccak256("CONTROLLER");
mapping(bytes32 => RateLimitData) private _data;
/**********************************************************************************************/
/*** Initialization ***/
/**********************************************************************************************/
constructor(address admin_) {
_grantRole(DEFAULT_ADMIN_ROLE, admin_);
}
/**********************************************************************************************/
/*** Admin functions ***/
/**********************************************************************************************/
function setRateLimitData(
bytes32 key,
uint256 maxAmount,
uint256 slope,
uint256 lastAmount,
uint256 lastUpdated
)
public override onlyRole(DEFAULT_ADMIN_ROLE)
{
require(lastAmount <= maxAmount, "RateLimits/invalid-lastAmount");
require(lastUpdated <= block.timestamp, "RateLimits/invalid-lastUpdated");
_data[key] = RateLimitData({
maxAmount: maxAmount,
slope: slope,
lastAmount: lastAmount,
lastUpdated: lastUpdated
});
emit RateLimitDataSet(key, maxAmount, slope, lastAmount, lastUpdated);
}
function setRateLimitData(bytes32 key, uint256 maxAmount, uint256 slope) external override {
setRateLimitData(key, maxAmount, slope, maxAmount, block.timestamp);
}
function setUnlimitedRateLimitData(bytes32 key) external override {
setRateLimitData(key, type(uint256).max, 0, type(uint256).max, block.timestamp);
}
/**********************************************************************************************/
/*** Getter Functions ***/
/**********************************************************************************************/
function getRateLimitData(bytes32 key) external override view returns (RateLimitData memory) {
return _data[key];
}
function getCurrentRateLimit(bytes32 key) public override view returns (uint256) {
RateLimitData memory d = _data[key];
// Unlimited rate limit case
if (d.maxAmount == type(uint256).max) {
return type(uint256).max;
}
return _min(
d.slope * (block.timestamp - d.lastUpdated) + d.lastAmount,
d.maxAmount
);
}
/**********************************************************************************************/
/*** Controller functions ***/
/**********************************************************************************************/
function triggerRateLimitDecrease(bytes32 key, uint256 amountToDecrease)
external
override
onlyRole(CONTROLLER)
returns (uint256 newLimit)
{
RateLimitData storage d = _data[key];
uint256 maxAmount = d.maxAmount;
require(maxAmount > 0, "RateLimits/zero-maxAmount");
if (maxAmount == type(uint256).max) return type(uint256).max; // Special case unlimited
uint256 currentRateLimit = getCurrentRateLimit(key);
require(amountToDecrease <= currentRateLimit, "RateLimits/rate-limit-exceeded");
d.lastAmount = newLimit = currentRateLimit - amountToDecrease;
d.lastUpdated = block.timestamp;
emit RateLimitDecreaseTriggered(key, amountToDecrease, currentRateLimit, newLimit);
}
function triggerRateLimitIncrease(bytes32 key, uint256 amountToIncrease)
external
override
onlyRole(CONTROLLER)
returns (uint256 newLimit)
{
RateLimitData storage d = _data[key];
uint256 maxAmount = d.maxAmount;
require(maxAmount > 0, "RateLimits/zero-maxAmount");
if (maxAmount == type(uint256).max) return type(uint256).max; // Special case unlimited
uint256 currentRateLimit = getCurrentRateLimit(key);
d.lastAmount = newLimit = _min(currentRateLimit + amountToIncrease, maxAmount);
d.lastUpdated = block.timestamp;
emit RateLimitIncreaseTriggered(key, amountToIncrease, currentRateLimit, newLimit);
}
/**********************************************************************************************/
/*** Internal Utility Functions ***/
/**********************************************************************************************/
function _min(uint256 a, uint256 b) internal pure returns (uint256) {
return a < b ? a : b;
}
}