Skip to main content

Caliber

Git Source

Inherits: MakinaContext, AccessManagedUpgradeable, ReentrancyGuardUpgradeable, ERC721HolderUpgradeable, ERC1155HolderUpgradeable, ICaliber

State Variables

MAX_BPS

Full scale value in basis points.

uint256 private constant MAX_BPS = 10_000;

ACCOUNTING_OUTPUT_STATE_END

Flag to indicate end of values in the accounting output state.

bytes32 private constant ACCOUNTING_OUTPUT_STATE_END = bytes32(type(uint256).max);

weirollVm

Address of the Weiroll VM.

address public immutable weirollVm;

CaliberStorageLocation

bytes32 private constant CaliberStorageLocation = 0x32461bf02c7aa4aa351cd04411b6c7b9348073fbccf471c7b347bdaada044b00;

Functions

_getCaliberStorage

function _getCaliberStorage() private pure returns (CaliberStorage storage $);

constructor

constructor(address _registry, address _weirollVm) MakinaContext(_registry);

initialize

Initializer of the contract.

function initialize(CaliberInitParams calldata cParams, address _accountingToken, address _hubMachineEndpoint)
external
override
initializer;

Parameters

NameTypeDescription
cParamsCaliberInitParamsThe caliber initialization parameters.
_accountingTokenaddressThe address of the accounting token.
_hubMachineEndpointaddressThe address of the hub machine endpoints.

onlyOperator

modifier onlyOperator();

onlyRiskManager

modifier onlyRiskManager();

onlyRiskManagerTimelock

modifier onlyRiskManagerTimelock();

authority

Returns the current authority.

function authority() public view override returns (address);

hubMachineEndpoint

Address of the hub machine endpoint.

function hubMachineEndpoint() external view override returns (address);

accountingToken

Address of the accounting token.

function accountingToken() external view override returns (address);

positionStaleThreshold

Maximum duration a position can remain unaccounted for before it is considered stale.

function positionStaleThreshold() external view override returns (uint256);

allowedInstrRoot

Root of the Merkle tree containing allowed instructions.

function allowedInstrRoot() public view override returns (bytes32);

timelockDuration

Duration of the allowedInstrRoot update timelock.

function timelockDuration() external view override returns (uint256);

pendingAllowedInstrRoot

Value of the pending allowedInstrRoot, if any.

function pendingAllowedInstrRoot() public view override returns (bytes32);

pendingTimelockExpiry

Effective time of the last scheduled allowedInstrRoot update.

function pendingTimelockExpiry() public view override returns (uint256);

maxPositionIncreaseLossBps

Max allowed value loss (in basis point) when increasing a position.

function maxPositionIncreaseLossBps() external view override returns (uint256);

maxPositionDecreaseLossBps

Max allowed value loss (in basis point) when decreasing a position.

function maxPositionDecreaseLossBps() external view override returns (uint256);

maxSwapLossBps

Max allowed value loss (in basis point) for base token swaps.

function maxSwapLossBps() external view override returns (uint256);

cooldownDuration

Duration of the cooldown period for swaps and position management.

function cooldownDuration() external view returns (uint256);

getPositionsLength

Length of the position IDs list.

function getPositionsLength() external view override returns (uint256);

getPositionId

Position index => Position ID

There are no guarantees on the ordering of values inside the Position ID list, and it may change when values are added or removed.

function getPositionId(uint256 idx) external view override returns (uint256);

getPosition

Position ID => Position data

function getPosition(uint256 posId) external view override returns (Position memory);

isBaseToken

Token => Registered as base token in this caliber

function isBaseToken(address token) external view override returns (bool);

getBaseTokensLength

Length of the base tokens list.

function getBaseTokensLength() external view override returns (uint256);

getBaseToken

Base token index => Base token address

There are no guarantees on the ordering of values inside the base tokens list, and it may change when values are added or removed.

function getBaseToken(uint256 idx) external view override returns (address);

isInstrRootGuardian

User => Whether the user is a root guardian Guardians have veto power over updates of the Merkle root.

function isInstrRootGuardian(address user) external view override returns (bool);

isAccountingFresh

Checks if the accounting age of each position is below the position staleness threshold.

function isAccountingFresh() external view returns (bool);

getDetailedAum

Returns the caliber's net AUM along with detailed position and base token breakdowns.

function getDetailedAum() external view override returns (uint256, bytes[] memory, bytes[] memory);

Returns

NameTypeDescription
<none>uint256netAum The total value of all base token balances and positive positions, minus total debts.
<none>bytes[]positions The array of encoded tuples of the form (positionId, value, isDebt).
<none>bytes[]baseTokens The array of encoded tuples of the form (token, value).

addBaseToken

Adds a new base token.

function addBaseToken(address token) external override onlyRiskManagerTimelock;

Parameters

NameTypeDescription
tokenaddressThe address of the base token.

removeBaseToken

Removes a base token.

function removeBaseToken(address token) external override onlyRiskManagerTimelock;

Parameters

NameTypeDescription
tokenaddressThe address of the base token.

accountForPosition

Accounts for a position.

If the position value goes to zero, it is closed.

function accountForPosition(Instruction calldata instruction)
external
override
nonReentrant
returns (uint256, int256);

Parameters

NameTypeDescription
instructionInstructionThe accounting instruction.

Returns

NameTypeDescription
<none>uint256value The new position value.
<none>int256change The change in the position value.

accountForPositionBatch

Accounts for a batch of positions.

function accountForPositionBatch(Instruction[] calldata instructions, uint256[] calldata groupIds)
external
override
nonReentrant
returns (uint256[] memory, int256[] memory);

Parameters

NameTypeDescription
instructionsInstruction[]The array of accounting instructions.
groupIdsuint256[]The array of position group IDs. An accounting instruction must be provided for every open position in each specified group. If an instruction's groupId corresponds to a group of open positions of size greater than 1, the group ID must be included in this array.

Returns

NameTypeDescription
<none>uint256[]values The new position values.
<none>int256[]changes The changes in the position values.

managePosition

Manages a position's state through paired management and accounting instructions

*Performs accounting updates and modifies contract storage by:

  • Adding new positions to storage when created.
  • Removing positions from storage when value reaches zero.*
function managePosition(Instruction calldata mgmtInstruction, Instruction calldata acctInstruction)
external
override
nonReentrant
onlyOperator
returns (uint256, int256);

Parameters

NameTypeDescription
mgmtInstructionInstructionThe management instruction.
acctInstructionInstructionThe accounting instruction.

Returns

NameTypeDescription
<none>uint256value The new position value.
<none>int256change The signed position value delta.

managePositionBatch

Manages a batch of positions.

Convenience function to manage multiple positions in a single transaction.

function managePositionBatch(Instruction[] calldata mgmtInstructions, Instruction[] calldata acctInstructions)
external
override
nonReentrant
onlyOperator
returns (uint256[] memory, int256[] memory);

Parameters

NameTypeDescription
mgmtInstructionsInstruction[]The array of management instructions.
acctInstructionsInstruction[]The array of accounting instructions.

Returns

NameTypeDescription
<none>uint256[]values The new position values.
<none>int256[]changes The changes in the position values.

manageFlashLoan

Manages flashLoan funds.

function manageFlashLoan(Instruction calldata instruction, address token, uint256 amount) external override;

Parameters

NameTypeDescription
instructionInstructionThe flashLoan management instruction.
tokenaddressThe loan token.
amountuint256The loan amount.

harvest

Harvests one or multiple positions.

function harvest(Instruction calldata instruction, ISwapModule.SwapOrder[] calldata swapOrders)
external
override
nonReentrant
onlyOperator;

Parameters

NameTypeDescription
instructionInstructionThe harvest instruction.
swapOrdersISwapModule.SwapOrder[]The array of swap orders to be executed after the harvest.

swap

Performs a swap via the swapModule module.

function swap(ISwapModule.SwapOrder calldata order) external override nonReentrant onlyOperator;

Parameters

NameTypeDescription
orderISwapModule.SwapOrderThe swap order parameters.

transferToHubMachine

Initiates a token transfer to the hub machine.

function transferToHubMachine(address token, uint256 amount, bytes calldata data) external override onlyOperator;

Parameters

NameTypeDescription
tokenaddressThe address of the token to transfer.
amountuint256The amount of tokens to transfer.
databytesABI-encoded parameters required for bridge-related transfers. Ignored when called from a hub caliber.

notifyIncomingTransfer

Instructs the Caliber to pull the specified token amount from the calling hub machine endpoint.

function notifyIncomingTransfer(address token, uint256 amount) external override nonReentrant;

Parameters

NameTypeDescription
tokenaddressThe address of the token being transferred.
amountuint256The amount of tokens being transferred.

setPositionStaleThreshold

Sets the position accounting staleness threshold.

function setPositionStaleThreshold(uint256 newPositionStaleThreshold) external override onlyRiskManagerTimelock;

Parameters

NameTypeDescription
newPositionStaleThresholduint256The new threshold in seconds.

setTimelockDuration

Sets the duration of the allowedInstrRoot update timelock.

function setTimelockDuration(uint256 newTimelockDuration) external override onlyRiskManagerTimelock;

Parameters

NameTypeDescription
newTimelockDurationuint256The new duration in seconds.

scheduleAllowedInstrRootUpdate

Schedules an update of the root of the Merkle tree containing allowed instructions.

The update will take effect after the timelock duration stored in the contract at the time of the call.

function scheduleAllowedInstrRootUpdate(bytes32 newAllowedInstrRoot) external override onlyRiskManager;

Parameters

NameTypeDescription
newAllowedInstrRootbytes32

cancelAllowedInstrRootUpdate

Cancels a scheduled update of the root of the Merkle tree containing allowed instructions.

Reverts if no pending update exists or if the timelock has expired.

function cancelAllowedInstrRootUpdate() external override;

setMaxPositionIncreaseLossBps

Sets the max allowed value loss for position increases.

function setMaxPositionIncreaseLossBps(uint256 newMaxPositionIncreaseLossBps)
external
override
onlyRiskManagerTimelock;

Parameters

NameTypeDescription
newMaxPositionIncreaseLossBpsuint256The new max value loss in basis points.

setMaxPositionDecreaseLossBps

Sets the max allowed value loss for position decreases.

function setMaxPositionDecreaseLossBps(uint256 newMaxPositionDecreaseLossBps)
external
override
onlyRiskManagerTimelock;

Parameters

NameTypeDescription
newMaxPositionDecreaseLossBpsuint256The new max value loss in basis points.

setMaxSwapLossBps

Sets the max allowed value loss for base token swaps.

function setMaxSwapLossBps(uint256 newMaxSwapLossBps) external override onlyRiskManagerTimelock;

Parameters

NameTypeDescription
newMaxSwapLossBpsuint256The new max value loss in basis points.

setCooldownDuration

Sets the duration of the cooldown period for swaps and position management.

function setCooldownDuration(uint256 newCooldownDuration) external override onlyRiskManagerTimelock;

Parameters

NameTypeDescription
newCooldownDurationuint256The new duration in seconds.

addInstrRootGuardian

Adds a new guardian for the Merkle tree containing allowed instructions.

function addInstrRootGuardian(address newGuardian) external override restricted;

Parameters

NameTypeDescription
newGuardianaddressThe address of the new guardian.

removeInstrRootGuardian

Removes a guardian for the Merkle tree containing allowed instructions.

function removeInstrRootGuardian(address guardian) external override restricted;

Parameters

NameTypeDescription
guardianaddressThe address of the guardian to remove.

_addBaseToken

Adds a new base token to storage.

function _addBaseToken(address token) internal;

_managePosition

Manages and accounts for a position by executing the provided instructions.

function _managePosition(Instruction calldata mgmtInstruction, Instruction calldata acctInstruction)
internal
returns (uint256, int256);

_accountForPosition

Computes the accounting value of a position. Depending on last and current value, the position is then either created, closed or simply updated in storage.

function _accountForPosition(Instruction calldata instruction, bool checks) internal returns (uint256, int256);

_decodeAccountingOutputState

Decodes the output state of an accounting instruction into an array of amounts.

function _decodeAccountingOutputState(bytes[] memory state) internal pure returns (uint256[] memory);

_invalidateGroupedPositions

Marks all positions in a given group as stale, except for the position currently being managed.

function _invalidateGroupedPositions(uint256 groupId) internal;

_includesGroupId

Checks if a given group ID is included in the provided array of group IDs.

function _includesGroupId(uint256[] calldata groupIds, uint256 groupId) internal pure returns (bool);

_accountingValueOf

Computes the accounting value of a given token amount.

function _accountingValueOf(address token, uint256 amount) internal view returns (uint256);

_checkPositionMinDelta

Checks that absolute position value change is greater than minimum value relative to affected token balance changes and loss tolerance.

function _checkPositionMinDelta(uint256 positionValChange, uint256 affectedTokensValChange, uint256 maxLossBps)
internal
pure;

_checkPositionMaxDelta

Checks that absolute position value change is less than maximum value relative to affected token balance changes and loss tolerance.

function _checkPositionMaxDelta(uint256 positionValChange, uint256 affectedTokensValChange, uint256 maxLossBps)
internal
pure;

_checkInstructionIsAllowed

Checks if the given instruction is allowed by verifying its Merkle proof against the allowed instructions root.

function _checkInstructionIsAllowed(Instruction calldata instruction) internal;

Parameters

NameTypeDescription
instructionInstructionThe instruction to check.

_getStateHash

Computes a hash of the state array, selectively including elements as specified by a bitmap. This enables a weiroll script to have both fixed and variable parameters.

function _getStateHash(bytes[] calldata state, uint128 bitmap) internal pure returns (bytes32);

Parameters

NameTypeDescription
statebytes[]The state array to hash.
bitmapuint128The bitmap where each bit determines whether the corresponding element in state is included or ignored in the hash computation.

Returns

NameTypeDescription
<none>bytes32hash The hash of the state array.

_updateAllowedInstrRoot

Updates the allowed instructions root if a pending update is scheduled and the timelock has expired.

function _updateAllowedInstrRoot() internal returns (bytes32);

Returns

NameTypeDescription
<none>bytes32currentRoot The current allowed instructions root.

_swap

function _swap(ISwapModule.SwapOrder calldata order) internal;

_execute

Executes a set of commands on the Weiroll VM, via a delegatecall.

function _execute(bytes32[] calldata commands, bytes[] memory state) internal returns (bytes[] memory);

Parameters

NameTypeDescription
commandsbytes32[]The commands to execute.
statebytes[]The state to pass to the VM.

Returns

NameTypeDescription
<none>bytes[]outState The new state after executing the commands.

Structs

CaliberStorage

Note: storage-location: erc7201:makina.storage.Caliber

struct CaliberStorage {
address _hubMachineEndpoint;
address _accountingToken;
uint256 _positionStaleThreshold;
bytes32 _allowedInstrRoot;
uint256 _timelockDuration;
bytes32 _pendingAllowedInstrRoot;
uint256 _pendingTimelockExpiry;
uint256 _maxPositionIncreaseLossBps;
uint256 _maxPositionDecreaseLossBps;
uint256 _maxSwapLossBps;
uint256 _managedPositionId;
bool _isManagedPositionDebt;
bool _isManagingFlashloan;
uint256 _cooldownDuration;
uint256 _lastBTSwapTimestamp;
mapping(bytes32 executionHash => uint256 timestamp) _lastExecutionTimestamp;
mapping(uint256 posId => Position pos) _positionById;
mapping(uint256 groupId => EnumerableSet.UintSet positionIds) _positionIdGroups;
EnumerableSet.UintSet _positionIds;
EnumerableSet.AddressSet _baseTokens;
EnumerableSet.AddressSet _instrRootGuardians;
}