CCIP v1.5.1 RateLimiter Library API Reference
RateLimiter
A library implementing the Token Bucket algorithm for rate limiting cross-chain operations.
Events
TokensConsumed
event TokensConsumed(uint256 tokens);
Parameters
Name | Type | Description |
---|---|---|
tokens | uint256 | The number of tokens consumed |
ConfigChanged
event ConfigChanged(Config config);
Parameters
Name | Type | Description |
---|---|---|
config | Config | The new configuration applied |
Errors
BucketOverfilled
error BucketOverfilled();
OnlyCallableByAdminOrOwner
error OnlyCallableByAdminOrOwner();
TokenMaxCapacityExceeded
error TokenMaxCapacityExceeded(uint256 capacity, uint256 requested, address tokenAddress);
TokenRateLimitReached
error TokenRateLimitReached(uint256 minWaitInSeconds, uint256 available, address tokenAddress);
AggregateValueMaxCapacityExceeded
error AggregateValueMaxCapacityExceeded(uint256 capacity, uint256 requested);
AggregateValueRateLimitReached
error AggregateValueRateLimitReached(uint256 minWaitInSeconds, uint256 available);
InvalidRateLimitRate
error InvalidRateLimitRate(Config rateLimiterConfig);
DisabledNonZeroRateLimit
error DisabledNonZeroRateLimit(Config config);
RateLimitMustBeDisabled
error RateLimitMustBeDisabled();
Structs
TokenBucket
Represents the state and configuration of a token bucket rate limiter.
struct TokenBucket {
uint128 tokens;
uint32 lastUpdated;
bool isEnabled;
uint128 capacity;
uint128 rate;
}
Config
Configuration parameters for the rate limiter.
struct Config {
bool isEnabled;
uint128 capacity;
uint128 rate;
}
Functions
_consume
Removes tokens from the pool, reducing the available rate capacity for subsequent calls.
function _consume(TokenBucket storage s_bucket, uint256 requestTokens, address tokenAddress) internal;
Parameters
Name | Type | Description |
---|---|---|
s_bucket | TokenBucket | The token bucket to consume from |
requestTokens | uint256 | The number of tokens to consume |
tokenAddress | address | The token address (use address(0) for aggregate value capacity) |
_currentTokenBucketState
Retrieves the current state of a token bucket, including automatic refill calculations.
function _currentTokenBucketState(TokenBucket memory bucket) internal view returns (TokenBucket memory);
Returns
Type | Description |
---|---|
TokenBucket | The current state of the token bucket |
_setTokenBucketConfig
Updates the rate limiter configuration.
function _setTokenBucketConfig(TokenBucket storage s_bucket, Config memory config) internal;
Parameters
Name | Type | Description |
---|---|---|
s_bucket | TokenBucket | The token bucket to configure |
config | Config | The new configuration to apply |
_validateTokenBucketConfig
Validates rate limiter configuration parameters.
function _validateTokenBucketConfig(Config memory config, bool mustBeDisabled) internal pure;
Parameters
Name | Type | Description |
---|---|---|
config | Config | The configuration to validate |
mustBeDisabled | bool | Whether the configuration must be disabled |
_calculateRefill
Calculates the number of tokens to add during a refill operation.
function _calculateRefill(
uint256 capacity,
uint256 tokens,
uint256 timeDiff,
uint256 rate
) private pure returns (uint256);
Parameters
Name | Type | Description |
---|---|---|
capacity | uint256 | Maximum token capacity |
tokens | uint256 | Current token balance |
timeDiff | uint256 | Time elapsed since last refill (in seconds) |
rate | uint256 | Tokens per second refill rate |
Returns
Type | Description |
---|---|
uint256 | The new token balance after refill |
_min
Returns the smaller of two numbers.
function _min(uint256 a, uint256 b) internal pure returns (uint256);
Parameters
Name | Type | Description |
---|---|---|
a | uint256 | First number |
b | uint256 | Second number |
Returns
Type | Description |
---|---|
uint256 | The smaller of the two numbers |