Skip to content
Lattice

RateLimiter

Git Source

Inherits: IRateLimiter

Title: RateLimiter

Author: Modified from Chainlink CCIP (https://github.com/smartcontractkit/ccip/blob/ccip-develop/contracts/src/v0.8/ccip/libraries/RateLimiter.sol)

Thin Diamond facet that exposes token-bucket rate limiting keyed by bytes32.

All logic lives in RateLimiterLib. This contract is stateless and forwards every call to the library. Inherit this in your Diamond facet to add rate limiting.

Notes:

  • lattice-version: 0.1.0
  • lattice-source: Chainlink CCIP

Functions

getConfig

Returns the capacity and refill rate configured for key.

function getConfig(bytes32 key) public view virtual returns (uint256 capacity, uint256 refillRate);

Parameters

NameTypeDescription
keybytes32The rate limit key to query.

Returns

NameTypeDescription
capacityuint256The maximum token capacity of the bucket.
refillRateuint256The number of tokens refilled per second.

getAvailable

Returns the currently available tokens for key (accounts for elapsed time).

This is a pure read — it does NOT update storage.

function getAvailable(bytes32 key) public view virtual returns (uint256 available);

Parameters

NameTypeDescription
keybytes32The rate limit key to query.

Returns

NameTypeDescription
availableuint256The number of tokens available right now.

configure

Configures or reconfigures a rate limit bucket.

Requires DEFAULT_ADMIN_ROLE. Sets tokens = capacity and lastRefill = now. Reverts with RateLimitInvalidConfig if capacity == 0 OR refillRate == 0. Emits RateLimitConfigured.

function configure(bytes32 key, uint256 capacity, uint256 refillRate) public virtual;

Parameters

NameTypeDescription
keybytes32The rate limit key.
capacityuint256The maximum token capacity (must be > 0).
refillRateuint256The tokens-per-second refill rate (must be > 0).

consume

Consumes amount tokens from the bucket for key.

Refills the bucket first based on elapsed time, then deducts amount. Reverts with RateLimitInvalidAmount if amount == 0. Reverts with RateLimitNotConfigured if the key has no configuration. Reverts with RateLimitExceeded if fewer than amount tokens are available. Emits RateLimitConsumed.

function consume(bytes32 key, uint256 amount) public virtual;

Parameters

NameTypeDescription
keybytes32The rate limit key.
amountuint256The number of tokens to consume (must be > 0).

exportSelectors

ERC-8153 selector export: this facet's cuttable selectors, tightly packed (4 bytes each).

Excludes exportSelectors() itself (0x0ef22643) - it is never cut into a diamond. Order matches forge inspect RateLimiter methodIdentifiers (alphabetical by signature); kept in exact parity by ExportSelectorsParityTest. Chunks: configure(bytes32,uint256,uint256) 0xbe0b31ab consume(bytes32,uint256) 0x98017489 getAvailable(bytes32) 0xd121f72c getConfig(bytes32) 0x6dd5b69d

function exportSelectors() external pure virtual returns (bytes memory selectors);

Returns

NameTypeDescription
selectorsbytes