API Version: v1.5.1

CCIP v1.5.1 RateLimiter Library API Reference

RateLimiter

A library implementing the Token Bucket algorithm for rate limiting cross-chain operations.

Git Source

Events

TokensConsumed

event TokensConsumed(uint256 tokens);

Parameters

NameTypeDescription
tokensuint256The number of tokens consumed

ConfigChanged

event ConfigChanged(Config config);

Parameters

NameTypeDescription
configConfigThe 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

NameTypeDescription
s_bucketTokenBucketThe token bucket to consume from
requestTokensuint256The number of tokens to consume
tokenAddressaddressThe 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

TypeDescription
TokenBucketThe current state of the token bucket

_setTokenBucketConfig

Updates the rate limiter configuration.

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

Parameters

NameTypeDescription
s_bucketTokenBucketThe token bucket to configure
configConfigThe new configuration to apply

_validateTokenBucketConfig

Validates rate limiter configuration parameters.

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

Parameters

NameTypeDescription
configConfigThe configuration to validate
mustBeDisabledboolWhether 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

NameTypeDescription
capacityuint256Maximum token capacity
tokensuint256Current token balance
timeDiffuint256Time elapsed since last refill (in seconds)
rateuint256Tokens per second refill rate

Returns

TypeDescription
uint256The new token balance after refill

_min

Returns the smaller of two numbers.

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

Parameters

NameTypeDescription
auint256First number
buint256Second number

Returns

TypeDescription
uint256The smaller of the two numbers

Get the latest Chainlink content straight to your inbox.