@@ -36,59 +36,6 @@ library RateLimiter {
36
36
uint128 rate; // ───────╯ Specifies the rate of the rate limiter
37
37
}
38
38
39
- /// @notice _consume removes the given tokens from the pool, lowering the
40
- /// rate tokens allowed to be consumed for subsequent calls.
41
- /// @param requestTokens The total tokens to be consumed from the bucket.
42
- /// @param tokenAddress The token to consume capacity for, use 0x0 to indicate aggregate value capacity.
43
- /// @dev Reverts when requestTokens exceeds bucket capacity or available tokens in the bucket
44
- /// @dev emits removal of requestTokens if requestTokens is > 0
45
- function _consume (
46
- TokenBucket storage s_bucket ,
47
- uint256 requestTokens ,
48
- address tokenAddress
49
- ) internal {
50
- // If there is no value to remove or rate limiting is turned off, skip this step to reduce gas usage
51
- if (! s_bucket.isEnabled || requestTokens == 0 ) {
52
- return ;
53
- }
54
-
55
- uint256 tokens = s_bucket.tokens;
56
- uint256 capacity = s_bucket.capacity;
57
- uint256 timeDiff = block .timestamp - s_bucket.lastUpdated;
58
-
59
- if (timeDiff != 0 ) {
60
- if (tokens > capacity) revert BucketOverfilled ();
61
-
62
- // Refill tokens when arriving at a new block time
63
- tokens = _calculateRefill (capacity, tokens, timeDiff, s_bucket.rate);
64
-
65
- s_bucket.lastUpdated = uint32 (block .timestamp );
66
- }
67
-
68
- if (capacity < requestTokens) {
69
- // Token address 0 indicates consuming aggregate value rate limit capacity.
70
- if (tokenAddress == address (0 ))
71
- revert AggregateValueMaxCapacityExceeded (capacity, requestTokens);
72
- revert TokenMaxCapacityExceeded (capacity, requestTokens, tokenAddress);
73
- }
74
- if (tokens < requestTokens) {
75
- uint256 rate = s_bucket.rate;
76
- // Wait required until the bucket is refilled enough to accept this value, round up to next higher second
77
- // Consume is not guaranteed to succeed after wait time passes if there is competing traffic.
78
- // This acts as a lower bound of wait time.
79
- uint256 minWaitInSeconds = ((requestTokens - tokens) + (rate - 1 )) / rate;
80
-
81
- if (tokenAddress == address (0 ))
82
- revert AggregateValueRateLimitReached (minWaitInSeconds, tokens);
83
- revert TokenRateLimitReached (minWaitInSeconds, tokens, tokenAddress);
84
- }
85
- tokens -= requestTokens;
86
-
87
- // Downcast is safe here, as tokens is not larger than capacity
88
- s_bucket.tokens = uint128 (tokens);
89
- emit TokensConsumed (requestTokens);
90
- }
91
-
92
39
/// @notice Gets the token bucket with its values for the block it was requested at.
93
40
/// @return The token bucket.
94
41
function _currentTokenBucketState (
0 commit comments