For the complete documentation index, see llms.txt. This page is also available as Markdown.

PriceAndFeeCalculatorV2

Title: PriceAndFeeCalculator

Calculates and manages anchor/drift price and fees for multiple vaults that share the same numeraire token Acts as a price oracle and fee accrual engine. Vault registration workflow is:

  1. Register a new vault with registerVault()

  2. Set the thresholds for the vault with setThresholds()

  3. Set the initial price state with setInitialPrice() Once registered, a vault can have its price updated by an authorized entity. Vault owners set thresholds for price changes, update intervals, and price age. Anchor-policy violations can either pause or revert depending on vault-level configuration, while drift-policy violations always revert. Paused vaults don’t accrue fees, reject drift updates, and unpause against the current anchor tuple. Accrues fees on each anchor update, based on TVL and performance since last update Supports conversion between vault units, tokens, and numeraire for deposits/withdrawals. All logic and state is per-vault, supporting many vaults in parallel. Only vault owners can set thresholds and pause/unpause their vaults, whereas accountants can also pause their vaults Integrates with an external oracle registry for token price conversions

Constants

ORACLE_REGISTRY

Oracle registry contract for price feeds

IOracleRegistry public immutable ORACLE_REGISTRY

State Variables

_vaultPriceStates

Mapping of vault addresses to their state information

mapping(address vault => VaultPriceStateV2 vaultPriceState) internal _vaultPriceStates

Functions

requiresVaultAuthOrAccountant

constructor

registerVault

Register a new vault with the fee calculator

setInitialPrice

Set the initial anchor price state for the vault

Parameters

Name
Type
Description

vault

address

Address of the vault

price

uint128

New initial anchor price

setThresholds

Set vault thresholds

Parameters

Name
Type
Description

vault

address

Address of the vault

minPriceToleranceRatio

uint16

Minimum ratio (of a price decrease) in basis points

maxPriceToleranceRatio

uint16

Maximum ratio (of a price increase) in basis points

minUpdateIntervalMinutes

uint16

The minimum interval between updates in minutes

maxPriceAge

uint16

Max delay between when a vault was priced and when the price is acceptable

maxUpdateDelayDays

uint8

Max delay between two price updates

setPauseOnBadAnchorUpdate

Set whether out-of-range updates should pause or revert

MUST be configurable by vault owner/authority

Parameters

Name
Type
Description

vault

address

Address of the vault

pauseOnBadAnchorUpdate

bool

True to pause on bad anchor update, false to revert atomically

setAnchorPrice

Set the anchor price for the vault in numeraire terms

Parameters

Name
Type
Description

vault

address

Address of the vault

price

uint128

New anchor price

timestamp

uint32

Timestamp when the anchor price was measured

setDriftPrice

Set the drift price for the vault in numeraire terms

MUST revert when the vault is paused

Parameters

Name
Type
Description

vault

address

Address of the vault

price

uint128

New drift price

timestamp

uint32

Timestamp when the drift price was measured

pauseVault

Pause the vault

Parameters

Name
Type
Description

vault

address

Address of the vault

unpauseVault

Unpause the vault

MUST revert if price or timestamp don’t exactly match the current anchor tuple

Parameters

Name
Type
Description

vault

address

Address of the vault

price

uint128

Expected anchor price at unpause time

timestamp

uint32

Expected anchor timestamp at unpause time

resetHighestPrice

Resets the highest price for a vault to the current anchor price

Parameters

Name
Type
Description

vault

address

Address of the vault

convertUnitsToToken

Convert units to token amount

Parameters

Name
Type
Description

vault

address

Address of the vault

token

IERC20

Address of the token

unitsAmount

uint256

Amount of units

Returns

Name
Type
Description

tokenAmount

uint256

Amount of tokens

convertUnitsToTokenIfActive

Convert units to token amount if vault is not paused

MUST revert if vault is paused

Parameters

Name
Type
Description

vault

address

Address of the vault

token

IERC20

Address of the token

unitsAmount

uint256

Amount of units

rounding

Math.Rounding

The rounding mode

Returns

Name
Type
Description

tokenAmount

uint256

Amount of tokens

convertUnitsToNumeraire

Convert units to numeraire token amount

Parameters

Name
Type
Description

vault

address

Address of the vault

unitsAmount

uint256

Amount of units

Returns

Name
Type
Description

<none>

uint256

numeraireAmount Amount of numeraire

convertUnitsToNumeraire

Convert units to numeraire token amount with rounding control

Parameters

Name
Type
Description

vault

address

Address of the vault

unitsAmount

uint256

Amount of units

rounding

Math.Rounding

The rounding mode

Returns

Name
Type
Description

<none>

uint256

numeraireAmount Amount of numeraire

convertNumeraireToUnits

Convert numeraire amount to vault units

Parameters

Name
Type
Description

vault

address

Address of the vault

numeraireAmount

uint256

Amount of numeraire

rounding

Math.Rounding

The rounding mode

Returns

Name
Type
Description

<none>

uint256

unitsAmount Amount of units

convertNumeraireToToken

Convert numeraire amount to token amount via oracle

Returns numeraireAmount unchanged when token is the numeraire

Parameters

Name
Type
Description

vault

address

Address of the vault

token

IERC20

Address of the token

numeraireAmount

uint256

Amount of numeraire

Returns

Name
Type
Description

<none>

uint256

tokenAmount Amount of tokens

convertTokenToNumeraire

Convert token amount to numeraire via oracle

Returns tokenAmount unchanged when token is the numeraire

Parameters

Name
Type
Description

vault

address

Address of the vault

token

IERC20

Address of the token

tokenAmount

uint256

Amount of tokens

Returns

Name
Type
Description

<none>

uint256

numeraireAmount Amount of numeraire

convertTokenToUnits

Convert token amount to units

Parameters

Name
Type
Description

vault

address

Address of the vault

token

IERC20

Address of the token

tokenAmount

uint256

Amount of tokens

Returns

Name
Type
Description

unitsAmount

uint256

Amount of units

convertTokenToUnitsIfActive

Convert token amount to units if vault is not paused

MUST revert if vault is paused

Parameters

Name
Type
Description

vault

address

Address of the vault

token

IERC20

Address of the token

tokenAmount

uint256

Amount of tokens

rounding

Math.Rounding

The rounding mode

Returns

Name
Type
Description

unitsAmount

uint256

Amount of units

getVaultState

Return the state of the vault

Parameters

Name
Type
Description

vault

address

Address of the vault

Returns

Name
Type
Description

<none>

VaultPriceStateV2

vaultPriceState The price state of the vault

<none>

VaultAccruals

vaultAccruals The accruals state of the vault

getVaultPriceTimestamp

Returns the timestamp of the last submitted price for a vault

Parameters

Name
Type
Description

vault

address

Address of the vault

Returns

Name
Type
Description

timestamp

uint256

The timestamp of the vault’s last price update

getAnchorTimestamp

Returns the timestamp of the last submitted anchor price for a vault

Parameters

Name
Type
Description

vault

address

Address of the vault

Returns

Name
Type
Description

timestamp

uint32

The timestamp of the vault’s current anchor price

getVaultValueAtLastUpdate

Returns the vault value in numeraire at the last price update

MUST revert if the vault is paused

Parameters

Name
Type
Description

vault

address

Address of the vault

Returns

Name
Type
Description

<none>

uint256

vaultValue The vault value in numeraire computed from lastTotalSupply and anchorPrice

isVaultPaused

Check if a vault is paused

Parameters

Name
Type
Description

vault

address

The address of the vault

Returns

Name
Type
Description

<none>

bool

True if the vault is paused, false otherwise

previewFees

version

Returns the semantic version string for this contract surface

Returns

Name
Type
Description

<none>

string

The semantic version string

_accrueFees

Accrues fees for a vault

It is assumed that validation has already been done Tvl is calculated as the product of the minimum of the current and last price and the minimum of the current and last total supply. This is to minimize potential issues with price spikes

Parameters

Name
Type
Description

vault

address

The address of the vault

price

uint256

The price of a single vault unit

timestamp

uint256

The timestamp of the price update

_setVaultPaused

Sets the paused state for a vault

Parameters

Name
Type
Description

vaultPriceState

VaultPriceStateV2

The storage pointer to the vault’s price state

vault

address

The address of the vault

paused

bool

The new paused state

_convertTokenToUnits

Converts a token amount to units

Parameters

Name
Type
Description

vault

address

The address of the vault

token

IERC20

The token to convert

tokenAmount

uint256

The amount of tokens to convert

unitPrice

uint256

The price of a single vault unit

rounding

Math.Rounding

The rounding direction

Returns

Name
Type
Description

unitsAmount

uint256

The amount of units

_convertUnitsToToken

Converts a units amount to tokens

Parameters

Name
Type
Description

vault

address

The address of the vault

token

IERC20

The token to convert

unitsAmount

uint256

The amount of units to convert

unitPrice

uint256

The price of a single vault unit

rounding

Math.Rounding

The rounding direction

Returns

Name
Type
Description

tokenAmount

uint256

The amount of tokens

_getQuoteCeil

Returns the value of baseAmount of baseToken in quoteToken terms for vault

Parameters

Name
Type
Description

vault

address

The address of the vault used for oracle override resolution

baseAmount

uint256

The amount of base token to convert

baseToken

IERC20

The base token being quoted

quoteToken

IERC20

The quote token to convert into

Returns

Name
Type
Description

quoteAmount

uint256

The ceil-rounded quote amount

_validatePriceUpdate

Validates a price update

Price is invalid if it is 0, before the last update, in the future, or if the price age is stale

Parameters

Name
Type
Description

state

VaultPriceStateV2

The storage pointer to the vault’s price state

price

uint256

The price of a single vault unit

timestamp

uint256

The timestamp of the price update

referenceTimestamp

uint256

The timestamp that the candidate update MUST be after

_shouldPauseAnchor

Determines if a price update should pause the vault

Vault should pause if the price increase or decrease is too large, or if the min update interval has not passed

Parameters

Name
Type
Description

state

VaultPriceStateV2

The storage pointer to the vault’s price state

price

uint256

The price of a single vault unit

timestamp

uint32

The timestamp of the price update

anchorTimestamp

uint256

The previous anchor timestamp

Returns

Name
Type
Description

<none>

bool

shouldPause True if the price update should pause the vault, false otherwise

_getCurrentPrice

Returns the current price by latest update between anchor and drift

Parameters

Name
Type
Description

state

VaultPriceStateV2

The storage pointer to the vault’s price state

Returns

Name
Type
Description

price

uint128

Current price

_getLastTimestamp

Returns the latest timestamp between anchor and drift updates

Parameters

Name
Type
Description

state

VaultPriceStateV2

The storage pointer to the vault’s price state

Returns

Name
Type
Description

timestamp

uint256

Active price timestamp

_isPriceWithinAnchorBand

Validates whether a candidate price stays inside the anchor tolerance band

Parameters

Name
Type
Description

state

VaultPriceStateV2

The storage pointer to the vault’s price state

anchorPrice

uint256

The anchor price used for band checks

candidatePrice

uint256

The proposed price to validate against the anchor band

Returns

Name
Type
Description

isWithinBand

bool

True when candidate is inside the anchor tolerance band

_isUpdateDelayExceeded

Checks whether the elapsed time between updates exceeds configured max delay

Parameters

Name
Type
Description

lastTimestamp

uint256

The timestamp of the prior update

timestamp

uint256

The timestamp of the proposed update

maxUpdateDelayDays

uint256

Maximum allowed delay in days

Returns

Name
Type
Description

isExceeded

bool

True when elapsed time exceeds the configured maximum

Last updated