CCIP v1.5.1 RateLimiter Library API Reference

The RateLimiter library implements Token Bucket rate limiting for CCIP operations.

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()

Events

TokensConsumed

event TokensConsumed(uint256 tokens)

ConfigChanged

event ConfigChanged(Config config)

Structs

TokenBucket

struct TokenBucket {
  uint128 tokens;
  uint32 lastUpdated;
  bool isEnabled;
  uint128 capacity;
  uint128 rate;
}
NameTypeDescription
tokensuint128Current number of tokens that are in the bucket
lastUpdateduint32Timestamp in seconds of the last token refill, good for 100+ years
isEnabledboolIndication whether the rate limiting is enabled or not
capacityuint128Maximum number of tokens that can be in the bucket
rateuint128Number of tokens per second that the bucket is refilled

Config

struct Config {
  bool isEnabled;
  uint128 capacity;
  uint128 rate;
}
NameTypeDescription
isEnabledboolIndication whether the rate limiting should be enabled
capacityuint128Specifies the capacity of the rate limiter
rateuint128Specifies the rate of the rate limiter

Functions

_consume

function _consume(TokenBucket storage s_bucket, uint256 requestTokens, address tokenAddress) internal

Removes the given tokens from the pool, lowering the rate tokens allowed to be consumed for subsequent calls.

Parameters

NameTypeDescription
s_bucketTokenBucketThe token bucket to consume from
requestTokensuint256The total tokens to be consumed from the bucket
tokenAddressaddressThe token to consume capacity for, use 0x0 to indicate aggregate value capacity

_currentTokenBucketState

function _currentTokenBucketState(TokenBucket memory bucket) internal view returns (TokenBucket memory)

Gets the token bucket with its values for the block it was requested at.

Parameters

NameTypeDescription
bucketTokenBucketThe token bucket

Return Values

TypeDescription
TokenBucketThe token bucket with current block values

_setTokenBucketConfig

function _setTokenBucketConfig(TokenBucket storage s_bucket, Config memory config) internal

Sets the rate limited config.

Parameters

NameTypeDescription
s_bucketTokenBucketThe token bucket
configConfigThe new config

_validateTokenBucketConfig

function _validateTokenBucketConfig(Config memory config, bool mustBeDisabled) internal pure

Validates the token bucket config.

Parameters

NameTypeDescription
configConfigThe config to validate
mustBeDisabledboolWhether the config must be disabled

_calculateRefill

function _calculateRefill(uint256 capacity, uint256 tokens, uint256 timeDiff, uint256 rate) private pure returns (uint256)

Calculate refilled tokens.

Parameters

NameTypeDescription
capacityuint256bucket capacity
tokensuint256current bucket tokens
timeDiffuint256block time difference since last refill
rateuint256bucket refill rate

Return Values

TypeDescription
uint256the value of tokens after refill

_min

function _min(uint256 a, uint256 b) internal pure returns (uint256)

Return the smallest of two integers.

Parameters

NameTypeDescription
auint256first int
buint256second int

Return Values

TypeDescription
uint256smallest integer

Get the latest Chainlink content straight to your inbox.