Skip to main content

Overview

Reserve Folio uses three primary utility libraries that implement core protocol logic. These libraries are used via delegatecall from Folio contracts to reduce deployment size and improve code reusability.

Library Overview

  • FolioLib: Fee calculations and governance operations
  • RebalancingLib: Auction mechanics and rebalancing logic
  • MathLib: Fixed-point math operations

FolioLib

Handles fee calculations and fee recipient management.

Set Fee Recipients

Configure the fee recipient table for a Folio.
FolioLib.sol
Fee recipients must be provided in ascending address order with no duplicates. Portions must sum to exactly 1e18 (100%). An empty table results in all fees going to the DAO.

Compute Fee Shares

Calculate TVL fee shares owed to DAO and fee recipients.
FolioLib.sol
Parameters:
  • currentDaoPending: Existing DAO pending shares
  • currentFeeRecipientsPending: Existing recipient pending shares
  • tvlFee: Per-second TVL fee rate (D18)
  • folioFeeForSelf: Fraction of recipient shares to burn (D18)
  • supply: Current total supply
  • elapsed: Time elapsed since last fee calculation

Set TVL Fee

Convert annual TVL fee to per-second rate.
FolioLib.sol
Converts annual percentage to per-second using formula: 1 - (1 - feeAnnually)^(1/31536000). This ensures accurate compounding over time.

Compute Mint Fees

Calculate fee shares for minting operations.
FolioLib.sol
Parameters:
  • shares: Total shares being minted (before fees)
  • mintFee: Mint fee percentage (D18)
  • folioFeeForSelf: Fraction of recipient fees to burn (D18)
  • minSharesOut: Minimum shares caller must receive

RebalancingLib

Implements auction mechanics and rebalancing operations.

Start Rebalance

Initiate a new rebalancing operation.
RebalancingLib.sol
Validates all token parameters, weights, prices, and limits. Reverts if any are inconsistent or out of bounds.

Open Auction

Open a new auction within an ongoing rebalance.
RebalancingLib.sol
Auctions begin after a 30-second warmup period (AUCTION_WARMUP). Atomic swaps (constant price) start and end at the same timestamp.

Get Bid

Calculate bid parameters for a token pair at current timestamp.
RebalancingLib.sol
Returns:
  • sellAmount: Amount of sell token to transfer (in sellTok)
  • bidAmount: Amount of buy token required (in buyTok)
  • price: Current Dutch auction price (D27 format)

Bid

Execute a bid in an ongoing auction.
RebalancingLib.sol
If withCallback is true, the caller must implement IBidderCallee.bidCallback(). Otherwise, the caller must have pre-approved the buy token.

Price Calculation

Internal function for Dutch auction pricing using exponential decay:
Where:
  • P_0: Starting price (sellPriceHigh / buyPriceLow)
  • P_t: Ending price (sellPriceLow / buyPriceHigh)
  • k: Decay constant = ln(P_0 / P_t) / auctionLength
  • t: Time elapsed since auction start

MathLib

Fixed-point mathematical operations using PRBMath.

Power

Raise a number to a fractional power.
MathLib.sol
uint256
Base (D18 fixed point)
uint256
Exponent (D18 fixed point)
Used for compound interest calculations: (1 - fee)^time

Power (Unsigned)

Raise a number to an integer power.
MathLib.sol
uint256
Base (D18 fixed point)
uint256
Exponent (whole number, not fixed point)

Natural Logarithm

Compute the natural logarithm of a number.
MathLib.sol
uint256
Input (D18 fixed point)
Used in Dutch auction price decay calculations.

Exponential

Compute e raised to a power.
MathLib.sol
int256
Exponent (D18 fixed point, can be negative)
Used for exponential decay in auction pricing: P_0 * e^(-kt)

Constants

Key constants used across libraries:

Fixed Point Scaling

uint256
default:"1e18"
18-decimal fixed point (standard for fees and ratios)
uint256
default:"1e27"
27-decimal fixed point (high precision for weights and prices)

Fee Limits

uint256
default:"0.1e18"
Maximum annual TVL fee: 10%
uint256
default:"0.0003e18"
Minimum mint fee: 3 bps
uint256
default:"10"
Maximum number of fee recipients

Rebalancing Limits

uint256
default:"1e54"
Maximum token weight (D27 * 1e27)
uint256
default:"1e27"
Maximum BU limit per share
uint256
default:"1e45"
Maximum token price (D27 * 1e18)
uint256
default:"1000"
Maximum ratio between high and low price
uint256
default:"1e36"
Maximum single token purchase amount

Auction Settings

uint256
default:"30"
Warmup period before auction bidding opens (seconds)
uint256
default:"30 days"
Maximum rebalance time-to-live

Time Constants

uint256
1/31536000 in D18 format (for annual to per-second conversion)

Usage in Contracts

Libraries are typically used with using directives:
Library functions that modify storage must be called with the correct storage pointers. Ensure you pass storage references, not memory copies.