📈Funding Rates
This document outlines the key formulas and calculations that drive our protocol's operations. Understanding these mechanics is crucial for users, liquidity providers, and developers interacting with the system.
Funding Rates
Our system implements a dynamic funding rate system, designed to react to and optimize issues with heavily skewed markets in real-time.
The formula is designed to incentivize an equal balance between long / short positions for each market, to ensure markets are as delta neutral as possible, in order to protect liquidity providers.
Formula for Current Funding Rate
Parameters
fundingRate: The last recorded funding rate
fundingRateVelocity: The rate of change of the funding rate
timeElapsed: Time passed since the last funding rate update
secondsInDay: Constant, equal to 86400
Formula for Funding Rate Velocity
Where: proportionalSkew = skew / skewScale
Parameters
skew: The difference between long and short open interest
skewScale: A configured scale factor for the skew
maxFundingVelocity: The maximum allowed funding rate velocity (used to clamp velocity)
Unrecorded Funding Calculation
The contract also calculates unrecorded funding, which represents the funding that has accrued since the last update:
solidity
solidity
Parameters
storedFundingRate: The funding rate stored from the last update
currentFundingRate: The newly calculated funding rate
timeElapsed: Time passed since the last funding rate update
secondsInDay: Constant, equal to 86400
These formulas work together to continuously adjust the funding rate based on market conditions, helping to balance long and short positions.
Example [Provide a simple example calculation if applicable] Additional Notes [Any important considerations or edge cases]
Borrowing Rates
Borrowing rates determine the fees that traders pay for leveraged positions. These rates are dynamically adjusted based on market conditions.
Formula for Borrowing Rate
solidity
Parameters
borrowScale: The maximum possible borrowing fee per day
openInterest: The current open interest for the given side (long or short)
maxOpenInterest: The maximum allowed open interest in the market
Formula for Pending Fees
Parameters
borrowRate: The current borrowing rate
timeElapsed: Time passed since the last update
secondsPerDay: Constant, equal to 86400
Formula for Average Cumulative Borrowing Fee
Where:
Parameters:
lastCumulative: The previous average cumulative borrowing fee
currentCumulative: The current cumulative borrowing fee
sizeDelta: The change in position size
openInterest: The current open interest
Formula for Total Fees Owed
Parameters
cumulativeBorrowFee: The total accumulated borrowing fee
averageCumulativeBorrowFee: The average cumulative borrowing fee
openInterest: The current open interest
Additional Notes
If the open interest is greater than the maximum open interest, the borrowing rate defaults to the maximum rate per day (borrowScale).
The borrowing rates are calculated separately for long and short positions.
The average cumulative borrowing fee is used to track the average borrowing fee across all positions, weighted by their sizes.
These formulas work together to dynamically adjust borrowing rates based on market utilization, ensuring that rates increase as the market approaches maximum capacity and decrease when there's less demand for leverage.
Price Impact
Price impact represents how a trade affects the asset's price due to changes in market liquidity and skew.
Formula for Price Impact
The core formula for price impact is:
Where:
Parameters
sizeDeltaUsd: The size of the trade in USD
initialSkew: The difference between long and short open interest before the trade
updatedSkew: The difference between long and short open interest after the trade
initialTotalOi: Total open interest before the trade
updatedTotalOi: Total open interest after the trade
liquidityScalar: A configured scalar to adjust impact based on liquidity
availableOi: Available open interest for the trade direction
Formula for Impacted Price
For long positions:
For short positions:
Parameters:
priceImpactUsd: The calculated price impact in USD
sizeDeltaUsd: The size of the trade in USD
indexPrice: The current index price of the asset
Additional Notes
The price impact is calculated differently if the trade causes a skew flip (from positive to negative or vice versa).
Positive impact is capped by the available impact pool for the market.
For decreases in position size, there is no price impact calculated.
The contract includes a slippage check to ensure the impacted price doesn't exceed the user's specified maximum slippage.
The liquidityFactor is only applied for increases in position size, not for decreases.
The contract uses separate scalars for positive and negative liquidity impacts.
These formulas work together to adjust the trade price based on how it affects the market's balance between long and short positions, as well as the overall liquidity. This mechanism helps to maintain market stability and fairness, especially for larger trades that could significantly impact the market. It also helps to prevent price manipulation attacks, and keep markets relatively delta neutral, reducing risk to liquidity providers.
Dynamic Fees
Dynamic fees are applied to deposits and withdrawals to incentivize balanced liquidity between ETH / USDC in the market. These fees adjust based on how the action affects the market's skew between the long and short liquidity.
Formula for Deposit Fee
Where:
Parameters
tokenAmount: The amount of tokens being deposited
BASE_FEE: A constant base fee percentage
FEE_SCALE: A constant to scale the fee factor
negativeSkewAccrued: The amount of negative skew added by the deposit
longValue: The total value of long tokens in the pool
shortValue: The total value of short tokens in the pool
Formula for Withdrawal Fee
Where:
baseFee = tokenAmount * BASE_FEE
additionalFee = tokenAmount * feeFactor
feeFactor = FEE_SCALE * (negativeSkewAccrued / (longValue + shortValue + amountUsd))
Parameters
tokenAmount: The amount of tokens being withdrawn
BASE_FEE: A constant base fee percentage
FEE_SCALE: A constant to scale the fee factor
negativeSkewAccrued: The amount of negative skew added by the withdrawal
longValue: The total value of long tokens in the pool after withdrawal
shortValue: The total value of short tokens in the pool after withdrawal
amountUsd: The USD value of the tokens being withdrawn
Additional Notes
If the deposit or withdrawal improves the skew (reduces its absolute value) and doesn't cause a skew flip, only the base fee is charged.
A skew flip occurs when the action changes the skew from positive to negative or vice versa. In case of a skew flip, the full updated skew is used as the negativeSkewAccrued.
For deposits
If either token balance is zero, only the base fee is charged.
If the value of the token being deposited is zero in the pool, only the base fee is charged.
For withdrawals
If the withdrawal would empty the pool (longValue + shortValue = 0), the maximum possible fee is charged.
The formulas use the concept of "negative skew accrued" to determine the additional fee. This is either the full updated skew (in case of a flip) or the amount of the deposit/withdrawal (if no flip occurs).
These dynamic fee mechanisms help maintain balanced liquidity in the pool by discouraging actions that would significantly imbalance the pool and encouraging actions that improve the balance.
Auto-Deleveraging
Auto-Deleveraging (ADL) is a mechanism used to maintain the health and solvency of the protocol by reducing the impact of large profitable positions on the liquidity pool. This section outlines the key components and calculations involved in the ADL process.
PNL Factor
The PNL factor is used to determine when a pool can be auto-deleveraged. ADL is triggered when positive PNL makes up >= 45% of the pool.
Formula for PNL Factor
Where:
pnl
: The profit and loss for the marketpoolUsd
: The total USD value of the pool
Implementation
The getPnlFactor
function calculates the PNL factor:
solidity
ADL Target Selection
The protocol selects the next position to be auto-deleveraged based on an ADL Target Score.
Formula for ADL Target Score
solidity
Implementation
The getNextAdlTarget
function selects the position with the highest ADL Target Score:
Solidity
ADL Percentage Calculation
The ADL percentage determines how much of a position should be deleveraged.
Formula for ADL Percentage
Where:
Implementation
The calculateAdlPercentage
function calculates the ADL percentage:
Solidity
ADL Price Impact
To maintain market health, the execution price for ADL'd positions is adjusted within specific boundaries.
Formula for Impacted Price
Where:
Implementation
The _executeAdlImpact
function calculates the impacted price for ADL:
Solidity
This Auto-Deleveraging mechanism helps maintain the protocol's solvency by systematically reducing the impact of large profitable positions on the liquidity pool, ensuring a fair and balanced market for all participants.
Liquidation Thresholds
The Liquidation Threshold determines when a position becomes eligible for liquidation. This section outlines the key components and calculations involved in determining whether a position is liquidatable.
Liquidation Condition
A position becomes liquidatable when the total losses plus fees exceed the available collateral. This is determined by comparing the remaining collateral to the maintenance collateral.
Formula for Liquidation Check
Where:
Note: Funding fees are added for long positions and subtracted for short positions.
Implementation
The checkIsLiquidatable
function determines if a position is liquidatable:
Solidity
Maintenance Collateral
The maintenance collateral represents the minimum amount of collateral that must be maintained for a position to avoid liquidation. It is calculated based on the position's leverage.
Formula for Maintenance Collateral
Where:
Implementation
The _getMaintenanceCollateral
function calculates the maintenance collateral:
Solidity
Key Points
The maintenance collateral percentage increases with higher leverage, providing a safety buffer for riskier positions.
The
BASE_MAINTENANCE_MARGIN
ensures a minimum maintenance requirement for all positions.The
MAINTENANCE_MARGIN_SCALE
allows for fine-tuning of the maintenance margin based on leverage.Positions at or above
MAX_LEVERAGE
have the highest maintenance collateral requirement.
This Liquidation Threshold mechanism helps maintain the overall health of the protocol by ensuring that positions are liquidated before they can pose a significant risk to the system's solvency.
Gas Rebates
The Gas Rebates mechanism ensures that users are charged fairly for the actual gas used during transaction execution. This section outlines the process of estimating gas costs, tracking actual gas usage, and refunding excess fees.
Gas Estimation
Before execution, the Gas
smart contract estimates the required gas for a given action.
Implementation
The validateExecutionFee
function in the Gas
library estimates the execution fee:
Solidity
This function:
Checks if the sent value is sufficient for the execution fee.
Estimates the gas cost for the action and price updates.
Ensures the execution fee is sufficient to cover the estimated costs.
Gas Usage Tracking
During execution, the contract tracks the actual gas used.
At the start of execution:
Solidity
At the end of execution:
Solidity
This calculates the actual gas cost of the transaction execution.
Gas Rebate Calculation
After execution, the contract calculates the amount to refund based on the actual gas used.
Implementation
Solidity
This code:
Checks if the actual execution cost exceeds the initial execution fee.
If it does, transfers the full execution cost to the executor.
If it doesn't, refunds the difference to the user.
Key Components
Gas Estimation: The
Gas
contract provides functions to estimate gas costs for various actions, including deposits, withdrawals, and position management.Execution Fee Validation: The contract ensures that the provided execution fee is sufficient to cover the estimated costs.
Actual Gas Usage: The contract tracks the actual gas used during execution using the
gasleft()
function.Refund Mechanism: After execution, any excess fee is refunded to the user, ensuring they only pay for the gas actually used.
Executor Payment: The executor (typically a keeper or automated system) is paid for the actual gas used, even if it exceeds the initial estimate.
Benefits
Fair Pricing: Users only pay for the actual gas used, rather than a fixed fee.
Incentivized Execution: Executors are guaranteed payment for their services, even if gas prices spike.
Flexible: The system adapts to varying gas prices and complex transactions.
This Gas Rebates mechanism ensures that the protocol remains efficient and fair in its gas fee management, benefiting both users and executors.
Last updated